實驗任務1:
task1.c
原始碼:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define N 5 6 #define N1 397 7 #define N2 476 8 #define N3 21 9 10 int main() { 11 int cnt; 12 int random_major, random_no; 13 14 srand(time(NULL)); // 以當前系統時間作為隨機種子 15 16 cnt = 0; 17 while(cnt < N) { 18 random_major = rand() % 2; 19 20 if(random_major) { 21 random_no = rand() % (N2 - N1 + 1) + N1; 22 printf("20248329%04d\n", random_no); 23 } 24 else { 25 random_no = rand() % N3 + 1; 26 printf("20248395%04d\n", random_no); 27 } 28 29 cnt++; 30 } 31 32 return 0; 33 }
執行結果:
回答問題:
問題1:生成一個範圍在 N1
(397)到 N2
(476)之間的隨機整數,包括這兩個值
問題2:生成一個範圍在 1
到 N3
(21)之間的隨機整數,包括 1
和 21
問題3:隨機抽取學號為202483290397到202483290476或者學號為202483950001到202483950021
實驗任務2:
task.c
原始碼:
1 #include <stdio.h> 2 #include <math.h> 3 4 int main() { 5 double a, b, c; 6 double delta, p1, p2; 7 8 while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) { 9 if(a == 0) { 10 printf("a = 0, invalid input\n"); 11 continue; 12 } 13 14 delta = b*b - 4*a*c; 15 p1 = -b/2/a; 16 p2 = sqrt(fabs(delta))/2/a; 17 18 if(delta == 0) 19 printf("x1 = x2 = %.2g\n", p1); 20 else if(delta > 0) 21 printf("x1 = %.2g, x2 = %.2g\n", p1+p2, p1-p2); 22 else { 23 printf("x1 = %.2g + %.2gi, ", p1, p2); 24 printf("x2 = %.2g - %.2gi\n", p1, p2); 25 } 26 } 27 28 return 0; 29 }
執行結果:
實驗任務3:
task3.c
原始碼:
1 #include <stdio.h> 2 3 int main() { 4 char signal; 5 6 printf("Enter the traffic signal color \n"); 7 8 while (scanf_s(" %c", &signal) != EOF) { 9 if (signal == 'r') { 10 printf("stop!\n"); 11 } 12 else if (signal == 'g') { 13 printf("go go go\n"); 14 } 15 else if (signal == 'y') { 16 printf("wait a minute\n"); 17 } 18 else { 19 printf("something must be wrong...\n"); 20 } 21 } 22 23 return 0; 24 }
執行結果:
或者:用switch語句
1 #include <stdio.h> 2 3 int main() { 4 char signal; 5 6 printf("Enter the traffic signal color \n"); 7 8 while (scanf_s(" %c", &signal) != EOF) 9 switch (signal) { 10 case 'r':printf("stop!\n"); break; 11 12 case 'g':printf("go go go\n");break; 13 14 case 'y':printf("wait a minute\n");break; 15 16 default:printf("something must be wrong...\n");break; 17 } 18 return 0; 19 }
實驗任務4:
task4.c
原始碼:
1 #include <stdio.h> 2 3 int main() { 4 double expense, total = 0, max = 0, min; 5 int first = 1; 6 7 printf("Enter expenses (-1 to finish):\n"); 8 9 while (scanf_s("%lf", &expense), expense != -1) { 10 total += expense; 11 if (first) { 12 max = min = expense; 13 first = 0; 14 } 15 else { 16 if (expense > max) max = expense; 17 if (expense < min) min = expense; 18 } 19 } 20 21 printf("Total: %.1f\nMax: %.1f\nMin: %.1f\n", total, max, min); 22 23 24 return 0; 25 }
執行結果:
實驗任務5
task5.c
原始碼:
1 #include <stdio.h> 2 3 int main() { 4 int a, b, c; 5 6 while (1) { 7 printf("請輸入三角形的三邊長度 a, b, c:"); 8 scanf_s("%d %d %d", &a, &b, &c); 9 10 if (a == 0 && b == 0 && c == 0) { 11 break; 12 } 13 14 if (a + b <= c || a + c <= b || b + c <= a) { 15 printf("不能構成三角形\n"); 16 } 17 else { 18 if (a == b && b == c) { 19 printf("等邊三角形\n"); 20 } 21 else if (a == b || b == c || a == c) { 22 printf("等腰三角形\n"); 23 } 24 else if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) { 25 printf("直角三角形\n"); 26 } 27 else { 28 printf("普通三角形\n"); 29 } 30 } 31 } 32 33 return 0; 34 }
執行結果:
實驗任務6
task6.c
原始碼:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() { 6 int lucky_day, guess; 7 8 srand(time(0)); 9 lucky_day = rand() % 30 + 1; 10 11 printf("猜猜2024年11月哪一天會是你的lucky day\n"); 12 printf("開始嘍,你有三次機會,猜吧(1~30):\n"); 13 14 for (int i = 0; i < 3; i++) { 15 scanf_s("%d", &guess); 16 17 if (guess == lucky_day) { 18 printf("你猜對了!恭喜!\n"); 19 return 0; 20 } 21 else if (guess < lucky_day) { 22 printf("你猜的日期早了,你的lucky day還沒到呢\n"); 23 } 24 else { 25 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 26 } 27 printf("再猜(1~30):\n"); 28 } 29 30 printf("次數用光啦。偷偷告訴你,11月你的lucky day是%d號\n", lucky_day); 31 32 return 0; 33 }
執行結果:
實驗總結:
1.while語句和switch語句存在一定的可替換性(如實驗3)
2.實驗6可採用srand函式,以時間為隨機種子,不然每次執行結果相同