实验2
task1.c
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 15 cnt = 0; 16 while(cnt < N){ 17 random_major = rand()%2; 18 19 if(random_major){ 20 random_no = rand()%N1 + 1; 21 printf("20256343%04d\n", random_no); 22 } 23 else{ 24 random_no = rand() % N2 + 1; 25 printf("20256136%04d\n", random_no); 26 } 27 28 cnt++; 29 } 30 system("pause"); 31 return 0; 32 }
使学号最后两位数变化,
随机生成五组学号
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 16 scanf("%d", &choice); 17 18 if(choice == 0) 19 break; 20 21 if(choice < 1 || chioce > 4){ 22 printf("无效的饮料编号,请重新输入, \n"); 23 continue; 24 } 25 if(choice == 1 || choice == 2) 26 total_price += 3 * quantity; 27 else if(choice == 3) 28 total_price += 5 * quantity; 29 else 30 total_price += 2 * quantity; 31 printf("请投入金额: "); 32 scanf("%f", &amount_paid); 33 change = amount_paid - total_price; 34 printf("本次购买总价: %.2f 元\n", total_price); 35 printf("找零: %.2f 元\n", change); 36 total_price = 0; 37 } 38 printf("感谢您的购买,欢迎下次光临!\n"); 39 return 0; 40 }
问1:上一次的饮料的价格将会加到这一次上面。
问2:使程序返回到上面if的这一步,继续选择饮料编号或者数量。
task3.c
1 #include<stdio.h> 2 int main() 3 { 4 char ans; 5 printf("交通信号灯的颜色:"); 6 while(scanf(" %c", &ans) != EOF) 7 { 8 9 if(ans == 'r') 10 { 11 printf("stop!\n"); 12 } 13 else if(ans == 'g') 14 { 15 printf("go go go\n"); 16 } 17 else if(ans == 'y') 18 { 19 printf("wait a minute\n"); 20 } 21 else 22 { 23 printf("soemthing must be wrong...\n"); 24 } 25 } 26 return 0; 27 28 }
task4.c
1 #include<stdio.h> 2 int main() 3 { 4 double n, total = 0, max = 0, min = 20000; 5 printf("输入今日开销,直到输入-1时终止:\n"); // 修正:\n 而不是 n 6 7 while(1) 8 { 9 scanf("%lf", &n); 10 11 if(n == -1) 12 break; 13 14 total += n; 15 16 if(n > max) 17 { 18 max = n; 19 } 20 21 if(n < min) 22 { 23 min = n; 24 } 25 } 26 27 printf("今日累计消费:%.1f\n", total); 28 printf("今日最高一笔开销:%.1f\n", max); 29 printf("今日最低一笔开销:%.1f\n", min); 30 31 return 0; 32 }
task5.c
1 #include<stdio.h> 2 int main() 3 { 4 int a, b, c, t; 5 while(scanf("%d%d%d", &a, &b, &c)!=EOF) 6 { 7 if (a > b) { t = a; a = b; b = t; } 8 if (a > c) { t = a; a = c; c = t; } 9 if (b > c) { t = b; b = c; c = t; } 10 if (a + b <= c) 11 { 12 printf("不能构成三角形\n"); 13 } 14 else 15 { 16 if (a == b && b == c) 17 printf("等边三角形\n"); 18 else if (a == b || b == c || a == c) 19 printf("等腰三角形\n"); 20 else if (a * a + b * b == c * c) 21 printf("直角三角形\n"); 22 else 23 printf("普通三角形\n"); 24 } 25 } 26 27 return 0; 28 }
task6.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() 6 { 7 srand((unsigned int)time(NULL)); 8 9 int luckyDay = rand() % 30 + 1; 10 int guess, count = 0; 11 12 printf("猜猜2025年11月哪一天是你的lucky day\n"); 13 while (count < 3) 14 { 15 count++; 16 printf("开始喽,你有三次机会,猜吧(1~30): "); 17 scanf("%d", &guess); 18 19 if (guess == luckyDay) 20 { 21 printf("哇,猜中了:)\n"); 22 break; 23 } 24 else if (guess > luckyDay) 25 { 26 printf("你猜的日期晚了,你的lucky day在前面哦\n"); 27 } 28 else 29 { 30 printf("你猜的日期早了,你的lucky day还没到呢\n"); 31 } 32 if (count < 3) { 33 printf("再猜(1~30): "); 34 } 35 }