实验2

task1

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

屏幕截图 2025-10-14 095623

屏幕截图 2025-10-14 095641

代码srand(time(NULL));的作用:保证每次随机生成不同的学号

代码的目的:生成两个不同专业的五个随机学号

task2

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

屏幕截图 2025-10-14 101726

total_price=0;如果去掉,会导致金额不被重置,下次购买时起始金额为本次购买金额

循环中continue语句作用:跳过本次循环中剩下的语句,进行下一次循环

task3

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     char ch;
 6     
 7     while((ch=getchar())!=EOF){
 8         if(ch=='\n'){
 9             continue;
10         }
11         if(ch==103){
12             printf("go go go\n");
13         }
14         else if(ch==121){
15             printf("wait a minute\n");
16             }
17         else if(ch==114){
18             printf("stop!\n");
19             }
20         else{
21             printf("something must be wrong...\n");
22            }
23         while(getchar()!='\n');
24     }
25     return 0;
26 }

屏幕截图 2025-10-14 110219

task4

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

屏幕截图 2025-10-14 114252

task5

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

屏幕截图 2025-10-14 120955

task6

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

屏幕截图 2025-10-14 145843

 

posted @ 2025-10-14 15:03  ZL425  阅读(2)  评论(0)    收藏  举报