实验2

实验任务1

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 #define N 5
 6 
 7 int main(){
 8     int number;
 9     int i;
10 
11     srand(time(0));
12     for (i=0;i<N;++i){
13         number=rand()%100+1;
14         printf("20490042%04d\n",number);
15     }
16     system("pause");
17     return 0;
18 }

 运行结果截图

21

问题1:生成5个在1到100之间的随机整数,并放在20490042后,按四位格式输出

问题2:生成1到100之间的随机数并赋值给number

问题3:固定按照四位输出,不足四位在前面补0

问题4:以系统时间作为随机数种子,让每次运行的随机数都不同

实验任务2

源代码

 1 #include <stdio.h>
 2 #include<stdlib.h>   
 3    int main() {
 4    int choice, quantity;
 5    float total_price = 0, amount_paid, change;
 6     
 7     while (1) {
 8     printf("\n自动饮料售卖机菜单:\n");
 9     printf("1. 可乐 - 3 元/瓶\n");
10     printf("2. 雪碧 - 3 元/瓶\n");
11     printf("3. 橙汁 - 5 元/瓶\n");
12     printf("4. 矿泉水 - 2 元/瓶\n");
13     printf("0. 退出购买流程\n");
14     printf("请输入饮料编号: ");
15     scanf("%d",&choice);
16     
17     if(choice==0)
18     break;
19     
20     if(choice<1||choice>4){
21     printf("无效的饮料编号,请重新输入。\n");
22     continue;}
23     
24     printf("请输入购买的数量: ");
25     scanf("%d",&quantity);
26     
27     if(quantity<0){
28     printf("购买数量不能为负数,请重新输入。\n");
29     continue;}
30     if(choice==1||choice==2)
31     total_price+=3*quantity;
32     else if(choice==3)
33     total_price+=5*quantity;
34     else
35     total_price+=2*quantity;
36     
37     printf("请投入金额:");
38     scanf("%f",&amount_paid);
39     change=amount_paid-total_price;
40     printf("本次购买总价: %.2f 元\n",total_price);
41     printf("找零: %.2f 元\n",change);
42     total_price=0;
43 }
44     printf("感谢您的购买,欢迎下次光临!\n");    
45     system("pause");
46     return 0;
47 }
View Code

 运行结果截图

3

问题1:如果去掉会使上一次的金额累加到下一次,导致计算出错

问题2:跳过本次循环的剩余代码,回到循环条件判断出开启下一轮循环

 实验任务3

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main(){
 4     char color;
 5     printf("请输入信号灯颜色(r/g/y)\n");
 6     while(scanf("%c",&color)!=EOF){
 7         if(color=='\n'||color==' '){
 8             continue;
 9         }
10         if(color=='r'){
11             printf("stop!\n");
12         }else if(color=='g'){
13           printf("go go go\n");
14         }else if(color=='y'){
15           printf("wait a minute\n");
16         }else{
17           printf("something must be wrong...\n");
18         }
19         while(getchar()!='\n');
20     }
21     system("pause");
22     return 0;
23 }

 运行结果截图

4

 实验任务4

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main() {
 5     double cost;
 6     double sum = 0.0;
 7     double max = 0.0;
 8     double min = 20000.0;
 9     int count = 0;
10 
11     printf("输入今日开销,直到输入-1终止:\n");
12 
13     while (1) {
14         if (scanf("%lf", &cost) != 1) {
15             break;
16         }
17         if (cost == -1) {
18             break;
19         }
20         if (cost < 0 || cost > 20000) {
21             continue;
22         }
23         if (count == 0) {
24             max = cost;
25             min = cost;
26         }
27         sum += cost;
28         if (cost > max) {
29             max = cost;
30         }
31         if (cost < min) {
32             min = cost;
33         }
34         count++;
35     }
36 
37     printf("今日累计消费总额:%.1f\n", sum);
38     printf("今日最高一笔开销:%.1f\n", max);
39     printf("今日最低一笔开销:%.1f\n", min);
40 
41     system("pause");
42     return 0;
43 }

 运行结果截图

5

 实验任务5

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main() {
 4     int a, b, c;
 5     while (scanf("%d%d%d", &a, &b, &c) != EOF) {
 6        
 7         if (a <= 0 || b <= 0 || c <= 0 ||
 8             (a + b <= c) || (a + c <= b) || (b + c <= a)) {
 9             printf("不能构成三角形\n");
10             continue; 
11         }
12         if (a == b && b == c) {
13             printf("等边三角形\n");
14         } else if (a == b || a == c || b == c) {
15             printf("等腰三角形\n");
16         } else if ((a*a + b*b == c*c) || 
17                    (a*a + c*c == b*b) || 
18                    (b*b + c*c == a*a)) {
19             printf("直角三角形\n");
20         } else {
21             printf("普通三角形\n");
22         }
23     }
24     system("pause");
25     return 0;
26 }

 运行结果截图

6

 实验任务6

 源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 int main()
 6 {
 7     int lucky_day;
 8     int guess;
 9     int n;
10 
11     srand((unsigned)time(NULL));
12     lucky_day = rand() % 30 + 1;
13     n = 0;
14 
15     printf("猜猜2026年4月哪一天是你的lucky day\n");
16     printf("开始喽,你有3次机会,猜吧(1-30):");
17 
18     while(n < 3)
19     {
20         scanf("%d", &guess);
21         n++;
22 
23         if(guess == lucky_day)
24         {
25             printf("哇,猜中了:)\n");
26             system("pause");
27             return 0;
28         }
29         else if(guess > lucky_day)
30             printf("你猜的日期晚了,你的lucky day在前面喔\n");
31         else
32             printf("你猜的日期早了,你的lucky day还没到呢\n");
33 
34         if(n < 3)
35             printf("再猜(1-30):");
36     }
37 
38     printf("次数用光啦,4月你的lucky day是%d号\n", lucky_day);
39     system("pause");
40     return 0;
41 }

运行结果截图

7

 

 

 

 

 
posted @ 2026-04-07 21:21  极法魔君  阅读(2)  评论(0)    收藏  举报