实验任务2
task1.c
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 cnt = 0; 12 while(cnt < N) { 13 random_major = rand() % 2; 14 if(random_major) { 15 random_no = rand() % N1 + 1; 16 printf("20256343%04d\n", random_no); 17 } 18 else { 19 random_no = rand() % N2 + 1; 20 printf("20256136%04d\n", random_no); 21 } 22 cnt++; 23 } 24 return 0; 25 }
A1:起到确保程序每次运行输出数据随机性的作用
A2:功能是随机生成数据(学号)
task2.c
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 }
A1:会导致程序继续运行计算的是多次售卖价格的总和,而不是单次售卖金额
A2:在while循环中快速进入下一个操作过程中
task3
1 #include <stdio.h> 2 3 int main(){ 4 printf("请输入交通信号灯的颜色(r表示red,g表示green,y表示yellow):"); 5 char color; 6 while(scanf("%c",&color) !=EOF){ 7 if(color=='r'){ 8 printf("stop!\n"); 9 } 10 else if(color=='g'){ 11 printf("go go go\n"); 12 } 13 else if(color=='y'){ 14 printf("wait a minute\n"); 15 } 16 else { 17 printf("something must be wrong…\n"); 18 } 19 while(getchar()!='\n'); 20 } 21 return 0; 22 }

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

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

task6
#include<stdio.h> #include <stdlib.h> #include <time.h> int main(){ printf("猜猜2025年11月哪一天是你的lucky day\n\n"); printf("开始喽,你有三次机会,猜吧(1~30):"); int l,i; srand(time(NULL)); l=rand()%(30-1+1)+1; i=0; while(i<3){ i++; int day; scanf("%d",&day); if(i<3){ if(day==l){ printf("\n哇,猜中了"); break; } else if(day<l){ printf("\n你猜的日期早了,你的lucky day还没到呢\n \n再猜(1~30):"); } else{ printf("\n你猜的日期晚了,你的lucky day在前面哦\n \n再猜(1~30):"); } } if(i==3){ if(day==l){ printf("\n哇,猜中了:)"); } else if(day<l){ printf("\n你猜的日期早了,你的lucky day还没到呢\n\n 次数用光啦,偷偷告诉你,11月你的lucky day是%d号",l); } else{ printf("\n你猜的日期晚了,你的lucky day在前面哦\n \n次数用光啦,偷偷告诉你,11月你的lucky day是%d号",l); } } } return 0; }

浙公网安备 33010602011771号