实验二

实验二

task1

源代码

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

运行结果截图

task1

回答问题

Q1:为5名同学生成编号1到100的随机学号。
Q2:生成1到100的随机整数。
Q3:控制学号位数相同且最后一位对齐。
Q4:使得每次生成的学号不同,若删去,多次重复运行,随机结果相同。

task2

源代码

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

运行结果截图

task2

问题回答

Q1:会影响下一次循环的总价。
Q2:结束本次循环,进行下一次循环。

task3

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main(){
 4     char light;
 5     while(scanf("%c",&light)!=EOF){
 6         getchar();
 7         switch(light){
 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");
12         }
13     }
14     system("pause");
15     return 0;
16 }

运行结果截图

task3

task4

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main(){
 4     float a,max=0.0,min=20000.0,sum=0.0;
 5     printf("输入今日开销,直到输入-1中止:\n");
 6     while(scanf("%f",&a)!=EOF){
 7         if(a==-1)
 8             break;
 9         if(a>max)
10             max =a;
11         if(a<min)
12             min =a;
13         sum += a;
14     }
15     printf("今日累计消费总额:%.1f\n今日最高一笔开销:%.1f\n今日最低一笔开销:%.1f\n",sum,max,min);
16     system("pause");
17     return 0;
18 }

运行结果截图

task4

task5

源代码

 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         if(a+b<=c||a+c<=b||b+c<=a)
 7             printf("不能构成三角形\n");
 8         else if((a==b&&a!=c)||(a==c&&a!=b)||(b==c&&b!=a))
 9             printf("等腰三角形\n");
10         else if(a==b&&b==c)
11             printf("等边三角形\n");
12         else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a)
13             printf("直角三角形\n");
14         else
15             printf("普通三角形\n");
16     }
17     system("pause");
18     return 0;
19 }

运行结果截图

task5

task6

源代码

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

运行结果截图

task6

 

posted @ 2026-04-01 11:36  B1uerose  阅读(2)  评论(0)    收藏  举报