实验2

实验任务1

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 5
 5 #define N1 80
 6 #define N2 35
 7 int main() {
 8     int cnt;
 9     int random_major, random_no;
10     srand(time(NULL)); // 以当前系统时间作为随机种子
11     
12     cnt = 0;
13     
14     while(cnt < N) {
15         random_major = rand() % 2;
16         if(random_major) {
17             random_no = rand() % N1 + 1;
18             printf("20256343%04d\n", random_no);
19         }
20         else {
21             random_no = rand() % N2 + 1;
22             printf("20256136%04d\n", random_no);
23         }
24         cnt++;
25     }
26     return 0;
27 }

p1(1)

使得每次运行能得到不同的随机数

获得随机的学号

 

实验任务2

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

p2

下一次购买的总价中会加入上一次购买的总价

第一个continue用于输入无效编号后结束本次循环,进入下一次循环的购买过程

第二个用于输入输入购买数量为负数时结束本次循环,进入下一次循环的购买过程

 

实验任务3

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

p3

 

实验任务4

 1 #include<stdio.h>
 2 int main(){
 3     printf("输入今日的开销,直到输入-1终止:\n");
 4         double a,b,c,i,r;
 5         r=0;
 6         b=0;
 7         i=0;
 8     while(1){
 9 
10         scanf("%lf",&a);
11     if(a!=-1){
12 
13         r+=a;
14         if(a>b){
15             b=a;
16         }
17         if(i==0){
18             c=a;
19         }
20         else{
21             if(a<c){
22                 
23                 c=a;
24             }
25         }
26     }
27     else{
28         break;
29     }
30     i++;
31     }
32     printf("今日累计消费总额:%.1lf\n",r);
33     printf("今日最高一笔开销:%.1lf\n",b);
34     printf("今日最低一笔开销:%.1lf\n",c);
35     
36     return 0;
37     
38 }

p4

 

实验任务5

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

p5

 

实验任务6

 1 #include<stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 int main(){
 6     printf("猜猜2025年11月哪一天是你的lucky day\n\n");
 7     printf("开始喽,你有三次机会,猜吧(1~30:"); 
 8     int l,i;
 9     srand(time(NULL)); 
10     l=rand()%(30-1+1)+1;
11     i=0;
12     while(i<3){
13         i++;
14         int day;
15         scanf("%d",&day);
16         
17         if(i<3){
18     
19     
20     if(day==l){
21         printf("\n哇,猜中了:)");
22         break;
23     }
24 
25     else if(day<l){
26         printf("\n你猜的日期早了,你的lucky day还没到呢\n \n再猜(1~30):"); 
27     }
28     else{
29         printf("\n你猜的日期晚了,你的lucky day在前面哦\n \n再猜(1~30):"); 
30     }
31 
32 }
33     if(i==3){
34         
35     if(day==l){
36         printf("\n哇,猜中了:)");
37     }
38 
39     else if(day<l){
40         printf("\n你猜的日期早了,你的lucky day还没到呢\n\n 次数用光啦,偷偷告诉你,11月你的lucky day是%d号",l); 
41     }
42     else{
43         printf("\n你猜的日期晚了,你的lucky day在前面哦\n \n次数用光啦,偷偷告诉你,11月你的lucky day是%d号",l); 
44     }
45     
46     }
47 
48 
49     }
50 
51 
52     return 0;
53     
54 }

p6.3

p6.2

 

posted @ 2025-10-14 16:11  仇昕泽  阅读(2)  评论(0)    收藏  举报