課程內容
C/C++ 程式語言(基本觀念,續)
邏輯值
-
true/false
邏輯值型態
-
bool
bool ok;
ok = true;
關係運算子(邏輯條件)
-
>
-
>=
-
<
-
<=
-
==
-
!=
邏輯運算子(複合邏輯條件)
-
&&
-
||
-
!
bool ok;
ok1 = x >= 3;
ok2 = x <= 5;
ok = ok1 && ok2;
ok = (x >= 3) && (x <= 5);
基本選擇流程控制
完整的兩個選擇分支
if ( 邏輯關係運算 ) {
...... // A 區塊命令列
} else {
...... // B 區塊命令列
}
if ((x >= 3) && (x <= 5)) {
cout << "x 的值介於 3 與 5 之間,包含 3 與 5。" << endl;
} else {
cout << "x 的值不是介於 3 與 5 之間。" << endl;
}
* 如果命令列實際只有一條命令,可以省略 { }。
if (x == 3) {
cout << "x 等於 3" << endl;
} else {
cout << "x 不等於 3" << endl;
}
可以寫成
if (x == 3)
cout << "x 等於 3" << endl;
else
cout << "x 不等於 3" << endl;
簡化的一個選擇分支
if ( 邏輯關係運算 ) {
...... // A 區塊命令列
}
if ((x >= 3) && (x <= 5)) {
cout << "x 的值介於 3 與 5 之間,包含 3 與 5。" << endl;
}
巢狀選擇(Nested-if)流程控制
if 內的區塊包含其他的 if
(C/C++/C#/Java/JavaScript/PHP/...... 現今幾乎所有程式語言幾乎都可以有這種用法,語法也相同)
if ( 邏輯關係運算 ) {
......
[其他的 if 結構]
......
} else {
......
[其他的 if 結構]
......
}
if (x >= 3) {
if (x <= 5) {
cout << "x 的值介於 3 與 5 之間,包含 3 與 5。" << endl;
} else {
cout << "x 的值比 5 大。" << endl;
}
} else {
if (x <= 5) {
cout << "x 的值比 3 小。" << endl;
} else {
cout << "怎麼可能!!!" << endl;
}
}
else if 的特殊巢狀選擇流程控制
if ( 邏輯關係運算狀況1) {
...... // 狀況1的處理命令列
} else if (邏輯關係運算狀況2) {
...... // 狀況2的處理命令列
} else if (邏輯關係運算狀況3) {
...... // 狀況3的處理命令列
} else if ......
......
} else {
...... // 其餘狀況的處理命令列
}
? ... : ...
邏輯關係運算 ? 關係成立時的運算結果 : 關係不成立時的運算結果
cin >> x;
a = (x >= 0) ? x : -x;
switch case default 多分支流程
switch ( 選項索引值 ) {
case 索引值1:
// 狀況 1 的命令列
// ......
break;
case 索引值2:
// 狀況 2 的命令列
// ......
break;
case 索引值3:
// 狀況 3 的命令列
// ......
break;
......
default:
// 狀況外的命令列
// ......
}
選擇流程結構
s001. 設計一個 C++ 程式,輸入一個數字 x,計算 x 的絕對值,並顯示結果在螢幕上。
Key:
-
不使用 cmath 的 fabs
範例程式:
(版本一)
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int x, a;
cin >> x;
if (x >= 0) {
a = x;
} else {
a = -x;
}
cout << x << " 的絕對值是 " << a << endl;
system("pause");
return 0;
}
(版本二)
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
double x;
double a;
// 輸入
cin >> x;
// 計算
if (x < 0) {
a = -x;
} else {
a = x;
}
// 輸出
cout << x << " 的絕對值是 " << a << endl;
system("pause");
return 0;
}
s002. 設計一個 C++ 程式,輸入一個數字 x,計算 x 的平方根,並顯示結果在螢幕上。
Key:
-
需考慮 x 是否大於零
範例程式:
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
double x, s;
cin >> x;
if (x >= 0) {
s = sqrt(x);
printf("%12.4f 的平方根是 %12.4f\n", x, s);
} else {
printf("%12.4f 是負的數字,不可以計算平方根!\n", x);;
}
system("pause");
return 0;
}
s003. 設計一個 C++ 程式,輸入一個正整數 x,判斷 x 是奇數或偶數,並顯示結果在螢幕上。
Key:
-
利用整除運算子(%)
範例程式:
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int x;
cin >> x;
if (x % 2 == 0) {
printf("%d 是偶數\n", x);
} else {
printf("%d 是奇數\n", x);
}
system("pause");
return 0;
}
b004. 設計一個 C++ 程式,輸入兩個數字 a 與 b,判斷這兩個數字的大小,由大而小顯示這兩個數字在螢幕上。
Key:
-
兩個數字的大小次序事先未知
範例程式:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int a, b;
int x, y; // x >= y
cin >> a >> b;
if (a < b) {
x = b;
y = a;
} else {
x = a;
y = b;
}
cout << x << " >= " << y << endl;
system("pause");
return 0;
}
s005. 設計一個 C++ 程式,輸入兩個數字 a 與 b,判斷其中一個數字是否可以被另一個數字整除,並顯示結果在螢幕上。
Key:
-
兩個數字的大小次序事先未知
範例程式:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int a, b;
int M, m;
// 輸入
cin >> a >> b;
// 計算 & 輸出
// 先決定數字大小
if (a >= b) {
M = a;
m = b;
} else {
M = b;
m = a;
}
// 判斷整除關係
if (M % m == 0) {
cout << "整除" << endl;
} else {
cout << "不能整除" << endl;
}
return 0;
}
s006. 設計一個 C++ 程式,輸入一個數字 h,代表色相環中的角度值,依據調和配色理論(Color Harmony)中的三角配色法(Triadic color scheme),計算出以該角度為主色(Base Color)的另外兩個角度,並顯示結果在螢幕上。
Key:
-
色相角度的表示假設為 0 至 359 的整數值
範例程式:
(版本一)
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int d1, d2, d3;
cin >> d1;
d2 = d1 + 120;
d3 = d2 + 120;
if (d2 >= 360) { d2 = d2 - 360; }
if (d3 >= 360) { d3 = d3 - 360; }
printf("主色 %d 度角,輔助色 %d 度角與 %d 度角\n", d1, d2, d3);
system("pause");
return 0;
}
(版本二)
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int d1, d2, d3;
cin >> d1;
if (d1 >= 0) {
d1 = d1 % 360;
} else {
// ?????
}
d2 = d1 + 120;
d3 = d2 + 120;
if (d2 >= 360) { d2 = d2 - 360; }
if (d3 >= 360) { d3 = d3 - 360; }
printf("主色 %d 度角,輔助色 %d 度角與 %d 度角\n", d1, d2, d3);
system("pause");
return 0;
}
(版本三,考慮可以輸入負數的角度)
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int h1, h2, h3;
// 輸入
cin >> h1; // 0-359
// 計算
if (h1 < 0) { // h1 < 0
h1 = 360 - ((-h1) % 360); // h1 >= 0
}
h2 = (h1 + 120) % 360;
h3 = (h2 + 120) % 360;
// 輸出
cout << "h1 = " << h1 << endl;
cout << "h2 = " << h2 << endl;
cout << "h3 = " << h3 << endl;
return 0;
}
s007. 設計一個 C++ 程式,輸入三個數字 a, b, c,找出最大的數字,並顯示結果在螢幕上。
Key:
-
巢狀選擇結構
-
複合邏輯條件
-
特殊排序演算法
範例程式:
(版本一,巢狀選擇結構)
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int a, b, c;
int m;
cin >> a >> b >> c;
if (a >= b) {
// a >= b
if (a >= c) {
// a >= (b, c)
m = a;
} else {
// c > a >= b
m = c;
}
} else {
// b > a
if (b >= c) {
// b >= (a, c)
m = b;
} else {
// c > b > a
m = c;
}
}
cout << "最大數字是 " << m << endl;
system("pause");
return 0;
}
(版本二,複合邏輯條件)
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
int a, b, c;
cin >> a >> b >> c;
int m;
if ((a >= b) && (a >= c)) { m = a; }
if ((b >= a) && (b >= c)) { m = b; }
if ((c >= a) && (c >= b)) { m = c; }
cout << "max = " << m << endl;
system("pause");
return 0;
}
(版本三,特殊設計的最大值演算法)
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
int a, b, c;
int m;
cin >> a >> b >> c;
m = a;
if (b > m) { m = b; }
if (c > m) { m = c; }
cout << "max = " << m << endl;
system("pause");
return 0;
}
s008. 設計一個 C++ 程式,輸入三個數字 a, b, c,找出最小的數字,並顯示結果在螢幕上。
Key:
-
練習用各種設計策略
範例程式:
......
s009. 設計一個 C++ 程式,輸入八個數字 n1, n2, n3, n4, n5, n6, n7, n8,找出最大的數字,並顯示結果在螢幕上。
Key:
-
巢狀選擇或複合邏輯顯然都不切實際
範例程式:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
int n1, n2, n3, n4, n5, n6, n7, n8;
int m;
cin >> n1 >> n2 >> n3 >> n4 >> n5 >> n6 >> n7 >> n8;
m = n1;
if (n2 > m) { m = n2; }
if (n3 > m) { m = n3; }
if (n4 > m) { m = n4; }
if (n5 > m) { m = n5; }
if (n6 > m) { m = n6; }
if (n7 > m) { m = n7; }
if (n8 > m) { m = n8; }
cout << "max = " << m << endl;
system("pause");
return 0;
}
s010. 設計一個 C++ 程式,輸入三個數字 a, b, c,判斷這些數字的大小,由大而小顯示這些數字在螢幕上,例如:輸入 6 2 7,輸出為 7 6 2。
Key:
-
巢狀選擇結構
-
複合邏輯條件
-
特殊排序演算法(氣泡排序法)
範例程式:
(版本一,巢狀選擇結構)
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
int a, b, c;
int m1, m2, m3;
cin >> a >> b >> c;
if (a >= b) {
// a >= b
if (b >= c) {
// a >= b >= c
m1 = a; m2 = b; m3 = c;
} else {
// a, c >= b
if (a >= c) {
// a >= c >= b
m1 = a; m2 = c; m3 = b;
} else {
// c >= a >= b
m1 = c; m2 = a; m3 = b;
}
}
} else {
// b > a
if (a >= c) {
// b > a >= c
m1 = b; m2 = a; m3 = c;
} else {
// b, c >= a
if (b >= c) {
// b >= c >= a
m1 = b; m2 = c; m3 = a;
} else {
// c > b >= a
m1 = c; m2 = b; m3 = a;
}
}
}
cout << m1 << " >= " << m2 << " >= " << m3 << endl;
system("pause");
return 0;
}
(版本二,複合邏輯條件)
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
int a, b, c;
int m1, m2, m3;
cin >> a >> b >> c;
if ((a >= b) && (b >= c)) { m1 = a; m2 = b; m3 = c; }
if ((a >= c) && (c >= b)) { m1 = a; m2 = c; m3 = b; }
if ((b >= a) && (a >= c)) { m1 = b; m2 = a; m3 = c; }
if ((b >= c) && (c >= a)) { m1 = b; m2 = c; m3 = a; }
if ((c >= a) && (a >= b)) { m1 = c; m2 = a; m3 = b; }
if ((c >= b) && (b >= a)) { m1 = c; m2 = b; m3 = a; }
cout << m1 << ", " << m2 << ", " << m3 << endl;
system("pause");
return 0;
}
(版本三,特殊排序演算法,氣泡排序法)
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
int a, b, c;
int t;
cin >> a >> b >> c;
if (c >= b) { t = c; c = b; b = t; }
if (b >= a) { t = b; b = a; a = t; }
if (c >= b) { t = c; c = b; b = t; }
cout << a << ", " << b << ", " << c << endl;
system("pause");
return 0;
}
s011. 設計一個 C++ 程式,輸入三個數字 a, b, c,判斷這些數字的大小,由小而大顯示這些數字在螢幕上,例如:輸入 6 2 7,輸出為 2 6 7。
Key:
......
範例程式:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
int a, b, c;
int t;
cin >> a >> b >> c;
if (b > c) { t = b; b = c; c = t; }
if (a > b) { t = a; a = b; b = t; }
if (b > c) { t = b; b = c; c = t; }
cout << a << " <= " << b << " <= " << c << endl;
system("pause");
return 0;
}
s012. 設計一個 C++ 程式,輸入四個數字 a, b, c, d,判斷這些數字的大小,由小而大顯示這些數字在螢幕上。
Key:
-
巢狀選擇或複合邏輯顯然相對來說都嫌複雜
-
排序演算法
範例程式:
......
s013. 設計一個 C++ 程式,輸入三個數字 a b c,代表一元二次方程式 ax^2+bx+c=0 的係數,計算此方程式的解,並顯示結果在螢幕上。
Key:
-
必須考慮一組相異解、一組相同解、無解的情形
範例程式:
(版本一)
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int argc, char** argv)
{
double a, b, c; // ax^2+bx+c=0
double d;
double x1, x2;
cin >> a >> b >> c;
d = b * b - 4 * a * c;
if (d == 0) {
// 重根
x1 = (-b) / (2 * a);
cout << "重根:" << endl;
cout << "x1 = " << x1 << endl;
} else {
if (d > 0) {
// 兩相異根
x1 = (-b + sqrt(d)) / (2 * a);
x2 = (-b - sqrt(d)) / (2 * a);
cout << "兩相異根:" << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
} else {
cout << "無解!" << endl;
}
}
system("pause");
return 0;
}
(版本二)
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
double a, b, c; // a x^2 + b x + c = 0
double d;
double x1, x2;
cin >> a >> b >> c;
d = b * b - 4.0 * a * c;
if (d >= 0) {
if (d == 0) {
x1 = (-b) / (2 * a);
cout << "唯一解(重根):" << x1 << endl;
} else {
x1 = (-b + sqrt(d)) / (2 * a);
x2 = (-b - sqrt(d)) / (2 * a);
cout << "兩相異根:" << x1 << ", " << x2 << endl;
}
} else {
cout << "無實數解!" << endl;
}
system("pause");
return 0;
}
s014. 設計一個 C++ 程式,輸入一個數字,改利用 ? ... : ...; 的運算式語法,計算這個數字的絕對值,並顯示結果在螢幕上。
Key:
......
範例程式:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int x;
int a;
cin >> x;
// if (x >= 0) a = x; else a = -x;
a = (x >= 0) ? x : -x;
cout << "a = " << a << endl;
system("pause";)
return 0;
}
s015. 設計一個 C++ 程式,輸入三個數字 a b c,代表三角形的三邊長,計算此三角形的面積,並顯示結果在螢幕上。
Key:
-
要考慮可能不能構成三角形的情形
範例程式:
(版本一)
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
double area;
double a, b, c;
double s;
cin >> a >> b >> c;
if ((a + b >= c) && (a + c >= b) && (b + c >= a)) {
s = (a + b + c) / 2;
area = sqrt(s*(s - a)*(s - b)*(s - c));
cout << "area = " << area << endl;
} else {
cout << "Not Triangle!" << endl;
}
system("pause");
return 0;
}
(版本二)
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
double area;
double a, b, c;
double d;
double s;
cin >> a >> b >> c;
s = (a + b + c) / 2;
d = s * (s - a)*(s - b)*(s - c);
if (d >= 0) {
area = sqrt(d);
cout << "area = " << area << endl;
} else {
cout << "Not Triangle!" << endl;
}
system("pause");
return 0;
}
s016. 設計一個 C++ 程式,輸入三個數字 a, b, c,代表圓 C:(x-a)^2 + (y-b)^2 = c^2 軌跡方程式的三個參數,另外,再輸入兩個數字 x0 與 y0 代表點 P:(x0,y0) 的座標值,計算判定該點 P 與圓 C 的相對位置關係,並顯示點 P 是在 C 『圓內』、『圓外』、或『圓上』的結果在螢幕上。
Key:
-
找出 P 在 C 內、外的判定規則
範例程式:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
double a, b, c;
double x0, y0;
double d;
cin >> a >> b >> c;
printf("C: (x-(%f))^2+(y-(%f))^2 = (%f)^2\n", a, b, c);
cin >> x0 >> y0;
d = sqrt((x0 - a)*(x0 - a) + (y0 - b)*(y0 - b));
if (d == c) {
printf("(%f,%f) 在圓上\n", x0, y0);
} else {
if (d > c) {
printf("(%f,%f) 在圓外\n", x0, y0);
} else {
printf("(%f,%f) 在圓內\n", x0, y0);
}
}
system("pause");
return 0;
}
s017. 設計一個 C++ 程式,輸入 x1, y1, r1, x2, y2, r2 代表兩個圓 C1 與 C2 的基本參數,C1 的圓心是 (x1, y1) 半徑是 r1,C2 的圓心是 (x2, y2) 半徑是 r2,判斷這兩個圓的相對關係是『相交』、『相切』、還是『相離』,並顯示結果在螢幕上。
Key:
-
找出兩個圓『相交』、『相切』、『相離』的判定規則
範例程式:
......
s018. 設計一個 C++ 程式,計算手機的通話費用,輸入網內及網外兩項通話時間秒數 s1 與 s2,依照某行動通訊業者『國際大嘴巴(International Big Mouth,IBM)』專案的費率如下:
(1) 網內每秒 0.01 元
(2) 網外,100 分鐘以內,每秒 0.07 元
(3) 網外,100 分鐘至 300 分鐘,每秒 0.05 元
(4) 網外,300 分鐘以上,每秒 0.03 元
計算並顯示網內與網外合計之通話費用在螢幕上。
Key:
-
網內: 6000 秒
-
網外: 12000 秒
-
合計費用: 780 元
範例程式:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
int s1, s2;
double cost;
cin >> s1 >> s2;
cost = s1 * 0.01;
if (s2 <= 100 * 60) {
cost += (s2 * 0.07);
} else if (s2 <= 300 * 60) {
cost += (100 * 60 * 0.07) + (s2 - 100) * 60 * 0.05;
} else {
cost += (100 * 60 * 0.07) + (300 - 100) * 60 * 0.05 + (s2 - 300) * 60 * 0.03;
}
printf("cost = %8.2f\n", cost);
文章標籤
全站熱搜
留言列表