發表文章

目前顯示的是 11月, 2013的文章

延遲時間

延遲一秒 寫法一: Sleep(1000); 寫法二: for (int delay=0; delay<=10000; delay++) 寫法三: time_t t1 = time(NULL), t2; do { t2 = time(NULL); } while((t2-t1)<1;

for 巢狀迴圈

#include #include #include using namespace std; int main(int argc, char *argv[]) { int multiplier, faciend; for (faciend=1; faciend<=9; faciend++) { for (multiplier=1; multiplier<=9; multiplier++) { cout << multiplier << '*' << faciend << '=' << setw(2) << multiplier*faciend << '\t'; } cout << endl; } system("PAUSE"); return EXIT_SUCCESS; }

for 迴圈

基本型 int sum=0; for (int count=1; count<=MAX; count++) { sum+=count; } 簡化型 int sum=0; for (int count=1; count<=MAX; count++) sum+=count; 省略型 int sum=0, count=1; for ( ; count=1; count<=MAX; count++) sum+=count; 單行型 int count, sum; for (count=1, sum=0; count<=MAX; sum+=count, count++);

while巢狀迴圈 數字三角形

#include #include #include using namespace std; int main(int argc, char *argv[]) { int outer = 0; while (outer++ <= 4) { int inner = 0; while (inner++ < outer) { cout << setw(3) << inner; } cout << endl; } system("PAUSE"); return EXIT_SUCCESS; }

do-while巢狀迴圈 數字三角形矩陣

#include #include #include using namespace std; int main(int argc, char *argv[]) { int outer = 1; do { int inner = 1; do { cout << setw(3) << inner; } while (inner++ <= 5-outer); cout << endl; } while (outer++ <= 4); system("PAUSE"); return EXIT_SUCCESS; }

兩數除法(判斷大小及除數是否為0)

#include #include using namespace std; int main(void) { int a, b; cout << "請輸入二個整數數值,以空白分隔:"; cin >> a >> b; if(a >= b) { if(b == 0) cout << "除數為 0\n"; else cout << a << " / " << b << " = " << (float)a/(float)b << endl; } else { if(a == 0) cout << "除數為 0\n"; else cout << b << " / " << a << " = " << (float)b/(float)a << endl; } return 0; }

判斷直角、鈍角或銳角三角形(II)

#include #include #include using namespace std; int main(int argc, char *argv[]) { float a, b, c; cout << "請輸入三角形三邊邊長,以空白分隔:"; cin >> a >> b >> c; if((a+b)>c && (b+c)>a && (c+a)>b) { if(pow(a,2) + pow(b,2) == pow(c,2) || pow(b,2) + pow(c,2) == pow(a,2) || pow(c,2) + pow(a,2) == pow(b,2)) cout << a << ' ' << b << ' ' << c << " 三邊構成直角三角形\n"; else if(pow(a,2) + pow(b,2) > pow(c,2) && pow(b,2) + pow(c,2) > pow(a,2) && pow(c,2) + pow(a,2) > pow(b,2)) cout << a << ' ' << b << ' ' << c << " 三邊構成銳角三角形\n"; else if(pow(a,2) + pow(b,2) < pow(c,2) || pow(b,2) + pow(c,2) < pow(a,2) || pow(c,2) + pow(a,2) < pow(b,2)) cout << a << ' ' << b << ' ' << c << " 三邊構成鈍角三角形\n"; } else