实验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("20251314%04d\n",number);
15     }
16     return 0;
17 }

运行结果:

image

问题回答:

问题1:这个程序的功能是什么?

  以line14的printf中“20251314”为前8位数字,生成5个202513140001-202513140100间的整数。

问题2:解释lin13代码的功能。

  “number=rand()%100+1”表示number是1~100间的随机整数。

问题3:解释line14使用格式符%04d起到什么作用。

  控制数字按4位宽度,不足补0显示。其中 “%”:按十进制整数输出; “4”:占4个字符宽度; “0”:宽度不够时前面补0。

问题4:代码 srand(time(0)); 起到什么作用?(提示:去掉这行代码,多次运行程序,观察结果有什么特点)

   去掉这行代码,多次运行程序,发现结果始终是同样的5个数,不会改变。所以“srand(time(0))”的作用是:用当前系统时间作为种子,初始化随机数生成器,每次得到不一样的随机序列。

实验任务2

源代码:

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

运行结果:

image

问题回答:

问题1:line47代码 total_price = 0; 如果去掉,对程序有什么影响?

  如果把这行去掉,那么程序每次运行都会把之前运行时的总金额加上去,得到的并不是这一次真正的总金额。
问题2:while循环中,有两处使用 continue 语句。解释在循环中使用 continue 语句,语义是什么?

  跳过本次循环剩下的语句,直接回到循环条件判断,决定是否进入下一轮循环。

  对比break和continue:

    break:直接结束整个循环;

    continue:只结束这一圈,继续下一圈。

实验任务3

源代码:

 1 #include<stdio.h>
 2 
 3 int main(){
 4     char color;
 5     while (scanf("%c",&color)==1){
 6         getchar();
 7         switch(color){
 8             case 'r':printf("stop!\n");break;
 9             case 'g':printf("go go go \n");break;
10             case 'y':printf("wait a minute\n");break;
11             default:printf("something must be wrong...\n");break;
12         }
13     }
14     return 0;
15 }
View Code

运行结果:

image

实验任务4

源代码:

 1 #include<stdio.h>
 2 
 3 int main(){
 4     double cost;
 5     double total=0.0;
 6     double max_cost=0.0;
 7     double min_cost=20000.0;
 8     printf("输入今日开销,直到输入-1终止:\n");
 9     while(1){
10         scanf("%lf",&cost);
11         if(cost == -1){
12             break;
13         }
14         total += cost;
15         if(cost > max_cost){
16             max_cost = cost;
17         }
18         if(cost < min_cost){
19             min_cost = cost;
20         }
21     }
22     printf("今日累计消费总额:%.1f\n",total);
23     printf("今日最高一笔开销:%.1f\n",max_cost);
24     printf("今日最低一笔开销:%.1f\n",min_cost);
25     return 0;
26 }
View Code

运行结果:

image

实验任务5

源代码:

 1 #include<stdio.h>
 2 
 3 int main(){
 4     int a,b,c;
 5     while(scanf("%d%d%d",&a,&b,&c)==3){
 6         if(a+b<=c||a+c<=b||b+c<=a||a<=0||b<=0||c<=0){
 7             printf("不能构成三角形\n");
 8             continue;
 9         }
10         int max,mid,min;
11         if(a>=b&&a>=c){
12             max = a;
13             mid = b>=c?b:c;
14             min = b<=c?b:c;
15         }
16         else if(b>=a&&b>=c){
17             max = b;
18             mid = a>=c?a:c;
19             min = a<=c?a:c;
20         }
21         else{
22             max = c;
23             mid = a>=b?a:b;
24             min = a<=b?a:b;
25         }
26         if(a==b&&b==c){
27             printf("等边三角形\n");
28         }
29         else if(a==b||a==c||b==c){
30             printf("等腰三角形\n");
31         }
32         else if(min*min+mid*mid==max*max){
33             printf("直角三角形\n");
34         }
35         else{
36             printf("普通三角形\n");
37         }
38     }
39     return 0;
40 }
View Code

运行结果:

image

实验任务6

源代码:

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

运行结果:

image

image

 

posted @ 2026-04-11 23:24  yailly  阅读(5)  评论(0)    收藏  举报