实验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 }

*运行截图

1

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

抽取学号在104900420001~204900420100之间的一名同学

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

随机抽取一个1~100之间的数

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

使抽取到的整数取4位,不到4位前面用0补齐

问题4:代码 srand(time(0)); 起到什么作用?

使抽取到的数根据系统时间决定精确到秒,使每次抽取到的数不一样

++实验任务2

*源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main(){
 5     int choice,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 
18         if (choice==0)
19             break;
20         if (choice<1||choice>4){
21             printf("无效的饮料编号,请重新输入。\n");
22             continue;
23         }
24 
25         printf("请输入购买的数量:");
26         scanf("%d",&quantity);
27 
28         if (quantity<0){
29             printf("购买数量不能为负数,请重新输入。\n");
30             continue;
31         }
32 
33         if(choice==1||choice==2)
34            total_price+=3*quantity;
35         else if(choice==3)
36             total_price+=5*quantity;
37         else
38             total_price+=2*quantity;
39 
40         printf("请输入金额:");
41         scanf("%f",&amount_paid);
42 
43         change=amount_paid-total_price;
44         printf("本次购买总计:%.2f元\n",total_price);
45         printf("找零:%.2f元\n",change);
46 
47         total_price=0;
48     }
49        printf("感谢您的购买,欢迎下次光临:\n");
50        system("pause");
51        return 0;
52 
53 }

 *运行截图

QQ20260318-220807

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

应付金额不会重置,下次购买会加上去

*问题2:while循环中,有两处使用continue语句。解释在循环中使用continue语句,语义是什么?

结束本次购买,重新进行循环

++实验任务3

*源代码

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

*运行截图

QQ20260401-173815

++实验任务4

*源代码

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

*运行截图

QQ20260401-222825

++实验任务5

*源代码

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

*运行截图

QQ20260401-224814

++实验任务6

*源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include<time.h>
 4     int main(){
 5         int num,guess;
 6             srand(time(0));
 7             num=rand()%30+1;
 8             printf("猜猜2026年那一天是你的luckyday\n");
 9             printf("开始喽,你有三次机会,猜吧(1~30)");
10 
11             for(int i=0;i<3;i++){
12             scanf("%d",&guess);
13             if(guess==num){
14                 printf("猜对了\n");
15                 return 0;
16             }
17             else if ( guess > num){
18                 printf("你猜的日期晚了\n");
19             }
20             else {
21                 printf("你猜的日期早了\n");
22             }
23             if (i<2){
24                 printf("再猜\n");
25             }
26     }
27     printf("次数用光了。你的luckyday是%d号\n",num);
28 
29     system("pause");
30     return 0;
31 }

*运行截图

QQ20260401-232432

 

posted @ 2026-04-01 23:25  _Vulcan  阅读(4)  评论(0)    收藏  举报