实验2

task1.c

代码:

 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 
17     system("pause");
18     return 0;
19 }
View Code

问题1:随机生成一个1~100的数字赋值给number。

问题2:将number格式化,占位为4,不足前面补足。

问题3:随机生成五个学号。

运行结果:

task2.c

代码:

 1 #include <stdio.h>
 2 
 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 
25         printf("请输入购买的数量: ");
26         scanf("%d", &quantity);
27 
28         if (quantity < 0) {
29             printf("购买数量不能为负数,请重新输入。\n");
30             continue;
31         }
32 
33         switch (choice) {
34             case 1:
35             case 2:
36                 total_price += 3 * quantity;
37                 break;
38             case 3:
39                 total_price += 5 * quantity;
40                 break;
41             case 4:
42                 total_price += 2 * quantity;
43                 break;
44         }
45 
46         printf("请投入金额: ");
47         scanf("%f", &amount_paid);
48 
49         change = amount_paid - total_price;
50         printf("本次购买总价: %.2f 元\n", total_price);
51         printf("找零: %.2f 元\n", change);
52 
53         total_price = 0;
54     }
55 
56     printf("感谢您的购买,欢迎下次光临!\n");
57     return 0;
58 }
View Code

问题1:总价归零,下一人购买时总价为零。

问题2:break终止当前循环并出循环,continue为跳出当前循环,继续下一循环。

问题3:没必要,前面代码有提示输入有效数值。

运行结果:

task3.c

代码:

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

运行结果:

task4.c

代码:

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

运行结果:

task5.c

代码:

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

运行结果:

task6.c

代码:

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     int a,i,m,n;
 6     printf("input n: ");
 7     scanf("%d",&n);
 8 
 9     for(i=1;i<=n;++i){
10 
11         for(m = n - i + 1;m<n;++m)
12             printf("\t");
13 
14         for(a=0;a<2*(n-i)+1;++a)
15             printf(" O \t");
16         printf("\n");
17         
18         for(m=n-i+1;m<n;++m)
19             printf("\t");
20 
21         for(a=0;a<2*(n-i)+1;++a)
22             printf("<H>\t");
23         printf("\n");
24 
25         for(m=n-i+1;m<n;++m)
26             printf("\t");
27 
28         for(a=0;a<2*(n-i)+1;++a)
29             printf("I I\t");
30         printf("\n");
31     }
32     system("pause");
33     return 0;
34 }
View Code

运行结果:

 

posted @ 2025-03-18 22:19  吉筱嘉  阅读(8)  评论(0)    收藏  举报