程式設計期末考
程式設計期末考
程式設計期末考
#include <iostream>
using namespace std;
int main()
{
double x, y;
cout << "please input x : ";
cin >> x;
cout << "please input y : ";
cin >> y;
if (x > 0 && y > 0)
cout << "第一象限";
else if (x < 0 && y > 0)
cout << "第二象限";
else if (x < 0 && y < 0)
cout << "第三象限";
else if (x > 0 && y < 0)
cout << "第四象限";
else if (x != 0 && y == 0)
cout << "x軸";
else if (x == 0 && y != 0)
cout << "y軸";
else
cout << "原點";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a, b, c, z=0 ; //宣告三邊長
for (c = 1; c < 600; c++) //確認斜邊且限制在小於600
for (a = 1; a < c; a++) //確認第一邊
for (b = a + 1; b < c; b++) //確認第二邊
if (a * a + b * b == c * c) //確認是否符合畢氏定理
{
cout << a << " " << b << " " << c << endl; //把結果秀出來
z+=1;
}
cout << z;
}
#include <iostream>
using namespace std;
int main()
{
int i = 0, z = 0, min = 0;
double sum = 0;
cout << "Please input your number : " ;
cin >> i;
min = i;
while (i != 9999)
{
sum += i; //加總
cin >> i; //繼續輸入
z += 1;
if (i <= min && i != 9999) { //判斷最小值
min = i;
}
}
cout << "The average is " << sum / z << endl;
cout << "The minimum number is " << min << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i, fact = 1;
for (i = 1; i <= 10; i++) {
fact *= i;
cout << "The factorial of " << i << " is " << fact << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int num ; //宣告整數num
cout << "Please input the number (1-99) :" << endl;
cin >> num;
switch (num) { //判別1-19
case 1: cout << "one" << endl;
break;
case 2: cout << "two" << endl;
break;
case 3: cout << "three" << endl;
break;
case 4: cout << "four" << endl;
break;
case 5: cout << "five" << endl;
break;
case 6: cout << "six" << endl;
break;
case 7: cout << "seven" << endl;
break;
case 8: cout << "eight" << endl;
break;
case 9: cout << "nine" << endl;
break;
case 10: cout << "ten" << endl;
break;
case 11: cout << "eleven" << endl;
break;
case 12: cout << "twelve" << endl;
break;
case 13: cout << "thirdteen" << endl;
break;
case 14: cout << "fourteen" << endl;
break;
case 15: cout << "fifteen" << endl;
break;
case 16: cout << "sixteen" << endl;
break;
case 17: cout << "seventeen" << endl;
break;
case 18: cout << "eighteen" << endl;
break;
case 19: cout << "nineteen" << endl;
break;
}
if (num > 19) { //判別19-99
switch (num / 10) { //判別十位數
case 2: cout << "twenty";
break;
case 3: cout << "thirty";
break;
case 4: cout << "fourty";
break;
case 5: cout << "fifty";
break;
case 6: cout << "sixty";
break;
case 7: cout << "seventy";
break;
case 8: cout << "eighty";
break;
case 9: cout << "ninety";
break;
}
switch (num % 10) { //判別個位數
case 1: cout << "-one" << endl;
break;
case 2: cout << "-two" << endl;
break;
case 3: cout << "-three" << endl;
break;
case 4: cout << "-four" << endl;
break;
case 5: cout << "-five" << endl;
break;
case 6: cout << "-six" << endl;
break;
case 7: cout << "-seven" << endl;
break;
case 8: cout << "-eight" << endl;
break;
case 9: cout << "-nine" << endl;
break;
}
}
system("pause");
return 0;
}
#include <iostream>
using namespace std;
#include <iomanip>
int main()
{
int i, j; //宣告變數
for (int i = 1;i <=9;i++)
{
for (int j = 2;j <= 9;j++) {
cout << j << "*" << i << "=" << setw(2) << j*i << " " ; //印出i*j值
}cout << endl;
}
}
#include <cstdio>
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double pi = 0, i = 0; //宣告浮點變數
int n = 1; //宣告變數
do
{
pi = pi + (4.0 * pow(-1.0, n - 1)) / (2 * n - 1); //計算pi值
n++; //增加項數
i = ((int)(pi * 100) / 100.0); //把pi設為整數以方便辨識
} while (i != 3.14); //設條件i不為3.14
cout << "First shown 3.14 at n=" << n - 1 << endl; //印出結果
do
{
pi = pi + (4.0 * pow(-1.0, n - 1)) / (2 * n - 1); //計算pi值
n++; //增加項數
i = ((int)(pi * 1000) / 1000.0); //把pi設為整數以方便辨識
} while (i != 3.141); //設條件i不為3.141
cout << "First shown 3.141 at n=" << n - 1 << endl; //印出結果
do
{
pi = pi + (4.0 * pow(-1.0, n - 1)) / (2 * n - 1); //計算pi值
n++; //增加項數
i = ((int)(pi * 10000) / 10000.0); //把pi設為整數以方便辨識
} while (i != 3.1415); //設條件i不為3.1415
cout << "First shown 3.1415 at n=" << n - 1 << endl; //印出結果
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n; //宣告變數
cout << "Please input an odd number (1-27) :" << endl;
cin >> n;
if (n % 2 == 1 && n <= 27) { //判斷n值為題目所需
寫兩個函數CircleX與CircleY給予圓心座標、半徑與角度(度)即可以分別計算出圓之X及Y座標
double CircleX(double CenX, double Angle)
double CircleY(double CenY, double Angle)
其中圓之半徑以全域變數設定,PI以全域常數設定,其餘以區域變數設定
主程式負責讓使用者輸入圓心座標、半徑、角度值及顯示最後之二維座標
#include <iostream>
#include <cmath>
using namespace std;
double Radius = 0; // 全域變數,用來儲存圓的半徑
double Pi = 3.14159; // 全域變數,用來儲存圓的圓周率
double CircleX(double cenX, double angle); // 函數宣告,用來計算圓上一點的 x 座標
double CircleY(double cenY, double angle); // 函數宣告,用來計算圓上一點的 y 座標
int main()
{
double CenX = 0, CenY = 0, Angle = 0; // 宣告變數,用來儲存圓心座標和角度
cout << "Please input CenX" << endl; // 使用者輸入圓心座標和角度
cin >> CenX;
cout << "Please input CenY" << endl;
cin >> CenY;
cout << "Please input Angle" << endl;
cin >> Angle;
cout << "Please input the Radius" << endl; // 使用者輸入圓的半徑
cin >> Radius;
cout << " (CircleX, CircleY ) = " << "(" << CircleX(CenX, Angle) << ", " << CircleY(CenY, Angle)
<< ")" << endl; // 呼叫 CircleX 和 CircleY 函數計算並輸出圓上一點的座標
system("pause");
return 0;
}
double CircleX(double cenX, double angle)
{
double circleX = 0;
circleX = cenX + Radius * cos((angle / 180) * Pi);
return circleX;
}
double CircleY(double cenY, double angle)
{
double circleY = 0;
circleY = cenY + Radius*sin((angle / 180) * Pi);
return circleY;
}
寫一個函數給予圓心座標、半徑(度),與角度即可以計算圓之二維座標
Void Circle(doubleCenX,doubleCenY,doubleRadius,doubleAngle,double&TargetX,double&TargetY)
全部變數以區域變數設定,主程式只負責輸入跟輸出即讓使用者輸入圓心座標、半徑、角度
值及顯示最後之二維座標設定數值的輸出格式
欄位內靠右對齊
將浮點精度設為3位數
欄位寬度設定為6
如果為正數,整數之前加上+號
使用一般的浮點數表示法
#include <iostream>
#include <cmath>
#include<iomanip>
using namespace std;
void Circle(double CenX, double CenY, double Radius, double Angle, double& TargetX, double& TargetY);
int main()
{
double CenX = 0, CenY = 0, Angle = 0, Radius = 0, TargetX = 0, TargetY = 0; // 宣告變數,
用來儲存圓心座標、角度、半徑
const double Pi = 3.14159;
cout << "Please input CenX" << endl; // 使用者輸入圓心座標和角度
cin >> CenX;
cout << "Please input CenY" << endl;
cin >> CenY;
cout << "Please input Angle" << endl;
cin >> Angle;
cout << "Please input the Radius" << endl; // 使用者輸入圓的半徑
cin >> Radius;
Circle(CenX, CenY, Radius, Angle, TargetX, TargetY);
cout << "(TargetX,TargetY)=" << "(" << setw(6) << fixed << showpos << setprecision(3) << TargetX
<< "," << TargetY << ")" << endl; /*印出答案*/
system("pause");
return 0;
}
void Circle(double CenX, double CenY, double Radius, double Angle, double& TargetX, double& TargetY)
{
const double Pi = 3.14159;
TargetX = CenX + Radius * cos(Angle * Pi / 180);
TargetY = CenY + Radius * sin(Angle * Pi / 180);
}
試寫一個函數可以計算BMI,使用者於主程式輸入自己體重、身高後,透過BMI函數得到數值,然後在主程式判
斷體重分級
#include <iostream>
using namespace std;
void func(double bmi);
int main()
{
double height, weight, BMI; /*宣告變數*/
cout << "Please input your height(cm)" << endl; /*請使用者輸入身高(公分)*/
cin >> height;
cout << "Please input your weight(KG)" << endl; /*請使用者輸入體重*/
cin >> weight;
BMI = weight / (height/100 * height/100); /*BMI公式*/
cout << "BMI=" << BMI << endl;
func(BMI);
system("pause");
return 0;
}
void func(double bmi)
{
if (bmi < 18.5)
cout << "體重過輕" << endl; /*判定bmi範圍*/
else if (bmi < 24 && bmi >= 18.5) /*輸出bmi分級*/
cout << "正常範圍" << endl;
else if (bmi < 27 && bmi >=24)
cout << "過重" << endl;
else if (bmi < 30 && bmi >=27)
cout << "輕度肥胖" << endl;
else if (bmi < 35 && bmi >=30)
cout << "中度肥胖" << endl;
else
cout << "重度肥胖" << endl;
}
日常生活中,四捨五入是很常用的算術法則,但C/C++卻無相關函數
請寫一個double roundToTenths (double number) 函數,可將輸入的浮點數四捨五入至小數
點第二位
Hint:可用<math.h> 裡的floor函數
double floor(double x) :不大於 x 的最大整數(但其型別為 double)
例如: x=floor(5.3) ,則x=5.0
x=floor(5.8) ,則x=5.0
x=floor(-5.3) ,則x=-6.0
x=floor(-5.8) ,則x=-6.0
在主程式裡,對每一個輸入進來的(正/負)浮點數值,於螢幕顯示其原來的值與四捨五入至
小數點第二位的值。
#include "stdafx.h"
#include <iostream>;
#include<iomanip>;
using namespace std;
cout << setprecision(15) << number << "," << n / 100 << endl; /*印出結果*/
system("pause");
return 0;
}
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
const int Size = 25; /*宣告Size為常數變數,共25個數字*/
srand(int(time(0))); /*初始化準亂數產生器*/
int Num[Size]; /*宣告一維陣列*/
int Const = 0;
do
{
int Value = rand() % (100 - 10 + 1) + 10; /*取10到100的亂數*/
bool AlreadyInTheArrayFlag = false; /*檢查Value是否已經在Num*/
/*如果沒有,則*/
if (!AlreadyInTheArrayFlag)
Num[Const++] = Value;
else
cout << "重複數值: " << Value << endl;
system("pause");
return 0;
}
讓使用者自行輸入一個位於0~999的整數
讓電腦以亂數方式來猜測此一數字
於螢幕上顯示電腦總共花多少次猜中
// 112303577_1128_作業10-2.cpp : 定義主控台應用程式的進入點。
//
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
int num = 0, x = 0, Count = 0; /*宣告變數*/
srand(int(time(0))); /*初始化準亂數產生器*/
cout << "Please input a number between 0 and 999" << endl;
cin >> num; /*輸入數值*/
if (num >= 0 && num < 1000) /*判斷輸入數字的範圍*/
{
do
{
x = rand() % (1000); /*在0-999間取亂數*/
Count++;
} while (x != num); //確保當 x = num 時迴圈會繼續執行
cout << "猜中的次數:" << Count << endl; /*輸出猜測的次數*/
}
else
cout << "請重新輸入0-999的數值" << endl; /*超出範圍則請使用者重新輸入數字*/
system("pause");
return 0;
}
// 112303577_1205_作業11-1.cpp : 定義主控台應用程式的進入點。
//
#include <cstdlib>
#include <iostream>
#include<ctime>
using namespace std;
const int Size = 49; /*宣告Size為常數變數,共49個數字*/
void Bubble(double V[], int N)
{
for (int i = 0; i < N; i++)
{
for (int j = N - 1; j > i; j--)
{
if (V[j] < V[j - 1])
{
double num = V[j]; /*交換Data的值*/
V[j] = V[j - 1];
V[j - 1] = num;
}
}
}
}
int main()
{
srand(int(time(0))); //*初始化準亂數產生器*/
double V[Size]; /*宣告一維陣列*/
int Const = 0;
do
{
double Value = rand() % 1081 / 100.0 - 5.4;
V[Const++] = Value;
} while (Const < Size);
Bubble(V, Size); /*氣泡排序法排列*/
for (int i = 0; i < Size; i++)
{
#include <cstdlib>
#include <iostream>
#include<ctime>
using namespace std;
double Average(int Num[], int Size);
int Min(int Num[], int Size);
int main()
{
const int Size = 15; /*宣告Size為常數變數,共15個數字*/
int Num[Size]; /*宣告一維陣列*/
srand(int(time(0))); //*初始化準亂數產生器*/
int Const = 0;
cout << "產生的數列:" << endl;
for (int i = 0; i < Size; i++)
{
Num[i] = rand() % 5000;
cout << Num[i] << " "; /*印出陣列中的數值*/
}
double average = Average(Num, Size); /*計算平均值*/
int minVal = Min(Num, Size); /*計算最小值*/
cout << "Average = " << average << endl << "Min = " << minVal << endl; /*將平均值與最
小值印出*/
system("pause");
return 0;
}
}
return double(sum) / Size;
}
int Min(int Num[], int Size)
{
int minVal = Num[0];
for (int i = 1; i < Size; i++)
{
if (Num[i] < minVal)
{
minVal = Num[i];
}
}
return minVal;
}
#include <iostream>
using namespace std;
}
int main()
{
int A[2][2] = { {1,2}, {3,4} }; // 定義2x2矩陣並初始化
int B[2][2] = { {5,6}, {7,8} };
int C[2][2];
MatrixAddition(A, B, C); // 調用函數執行矩陣相加
cout << "A+B=C ,C= " << endl; // 印出矩陣C
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << C[i][j] << " ";
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}
// 112303577_1212_作業12-2.cpp : 定義主控台應用程式的進入點。
#include <iostream>
using namespace std;
void MatrixMultiplier(double A[3][3], double B[3][3], double C[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
C[i][j] = 0; //將C矩陣初始化
for (int k = 0; k < 3; k++) //新增一迴圈,負責增加子項
{
C[i][j] += A[i][k] * B[k][j]; // 將對應位置的元素相乘後加總
}
cout << C[i][j] << " "; // 印出C矩陣
}
cout << endl;
}
}
int main()
{
double A[3][3] = { { 3.2, 0, 4.4 },{ 2.1, 1.0, 2.1 },{ 0, 5.9, 9.2 } }; // 宣告3x3矩陣並初
始化
double B[3][3] = { { 1.2, 1.0, 3.2 },{ 0, 7.3, 1.0 },{ 0.3, 2.3, 5.6 } };
double C[3][3];
// 112303577_1212_作業12-2.cpp : 定義主控台應用程式的進入點。
//
#include <iostream>
using namespace std;
int main()
{
double A[3][3] = { { 3.2, 0, 4.4 },{ 2.1, 1.0, 2.1 },{ 0, 5.9, 9.2 } }; // 宣告3x3矩陣並初
始化
double B[3][3] = { { 1.2, 1.0, 3.2 },{ 0, 7.3, 1.0 },{ 0.3, 2.3, 5.6 } };
double C[3][3];
system("pause");
return 0;
}
// 112303577_1219_作業13-2.cpp : 此檔案包含 'main' 函式。程式會於該處開始執行及結束執行。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const int SLen = 255; // 定義字串長度上限
char S[SLen]; // 宣告儲存字串的陣列
cout << "Please input S" << endl << endl;
cin.getline(S, SLen); // 使用cin.getline()輸入字串,避免換行字元留在輸入緩衝區
cout << endl << endl << "Input S = " << S << endl << endl << "反轉後 = ";
// 反轉字串並顯示
for (int i = 0; i < strlen(S); i++)
{
cout << S[strlen(S) - i - 1];
}
cout << endl << endl;
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1; //宣告字串1
string str2; //宣告字串2,用於存放反轉後的字串
co ut << "Please input str" << endl << endl;
getline(cin, str1); // 使用getline(cin, str1)輸入字串
cout << endl << endl << "Input Str1 = " << str1 << endl << endl << "反轉後 = "; // 反轉字
串並顯示
int length = str1.length(); // 取得字串長度
for (int i = length - 1; i >= 0; i--) //反轉字串
{
str2.append(str1.substr(i, 1));
}
cout << str2 << endl << endl; //顯示反轉後的字串
system("pause");
return 0;
}
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int main() {
const int size = 1000; // 範圍包括 0 ~ 1000
vector<int> Num(size + 1, 1); // 將所有元素初始化 = 1(true)
for (int i = 2; i * i <= size; ++i)
{
if (Num[i])
{
for (int j = i * i; j <= size; j += i)
{
Num[j] = 0; // 設 i 的倍數 = 0 (非質數)
}
}
}
cout << "1 ~ 999 間的質數如下:" << endl ; //將1~999間的質數印出
for (int i = 2; i <= 999; ++i)
{
if (Num[i])
cout << setw(3) << i << ", ";
}
return 0;
}