实验2
实验任务1
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 #define N1 80 #define N2 35 int main() { int cnt; int random_major, random_no;
srand(time(NULL)); cnt = 0; while(cnt < N) { random_major = rand() % 2; if(random_major) { random_no = rand() % N1 + 1; printf("20256343%04d\n", random_no); } else { random_no = rand() % N2 + 1; printf("20256136%04d\n", random_no); } cnt++; } return 0; }
运行截图1




问题回答:srand(time(NULL))的作用是根据当前时间,获得不同的随机种子。如果没有它,运行结果是不会变的。
此外这个程序是用来获得固定格式的不同编号。
实验任务2
#include <stdio.h> int main() { int choice, quantity; float total_price = 0, amount_paid, change; while (1) { printf("\n自动饮料售卖机菜单:\n"); printf("1. 可乐 - 3 元/瓶\n"); printf("2. 雪碧 - 3 元/瓶\n"); printf("3. 橙汁 - 5 元/瓶\n"); printf("4. 矿泉水 - 2 元/瓶\n"); printf("0. 退出购买流程\n"); printf("请输入饮料编号: "); scanf("%d", &choice); if (choice == 0) break; if (choice < 1 || choice > 4) { printf("无效的饮料编号,请重新输入。\n"); continue; } printf("请输入购买的数量: "); scanf("%d", &quantity); if (quantity < 0) { printf("购买数量不能为负数,请重新输入。\n"); continue; } if(choice == 1 || choice == 2) total_price += 3 * quantity; else if(choice == 3) total_price += 5 * quantity; else total_price += 2 * quantity; printf("请投入金额: "); scanf("%f", &amount_paid); change = amount_paid - total_price; printf("本次购买总价: %.2f 元\n", total_price); printf("找零: %.2f 元\n", change); total_price = 0; } printf("感谢您的购买,欢迎下次光临!\n"); return 0; }
运行截图2

问题回答::line47代码 total_price = 0的作用是在循环中使 total_price在开始时为0.如果去掉,在循环中,会使购买东西的价格是这次东西的价格和上次东西价格的总和,而不是本次的实际价格。
continue在循环中的作用是跳过这次循环,进入下一次循环。
实验任务3
#include <stdio.h>
int main(){
char M;
while((M=getchar())!=EOF){
if(M=='\n')
continue;
if(M=='r'){
printf("stop!\n");}
else if(M=='g'){
printf("go go go\n");}
else if(M=='y'){
printf("wait a minute\n");}
else{
printf("something must be wrong...\n"); }
}
return 0;
}
运行截图3

实验任务4
#include <stdio.h> int main(){ double expense,total=0.0,min=20000.0,max=0.0; printf("输入今日开销,直到输入-1终止:\n"); while(1){ scanf("%lf",&expense); if(expense==-1){ break;} if(expense<=0||expense>20000){ printf("输入的金额应该大于0元或者小于等于20000元,请重新输入\n"); continue;} total+=expense; if(expense<min){ min=expense; } if(expense>max){ max=expense; } } printf("今日累计消费总额:%.1f\n",total); printf("今日最高一笔开销:%.1f\n",max); printf("今日最低一笔开销:%.1f\n",min); return 0; }
运行截图4

实验任务5
#include <stdio.h>
int main(){
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF){
if(a+b>c&&b+c>a&&a+c>b){
if(a*a+b*b==c*c||b*b+c*c==a*a||a*a+c*c==b*b){
printf("直角三角形\n");
}
else if(a==b&&b==c){
printf("等边三角形\n");
}
else if(a==b||a==c||b==c){
printf("等腰三角形\n");
}
else
printf("普通三角形\n");
}
else
printf("不能构成三角形\n");
}
return 0;
}
运行截图5

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


浙公网安备 33010602011771号