实验2 C语言分支与循环基础应用编程-1

实验任务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函数,以时间为随机种子,不然每次运行结果相同

 

posted @ 2024-10-13 17:32  陈沫*  阅读(4)  评论(0编辑  收藏  举报