Exp2
Task 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)); 起到什么作用?(提示:去掉这行代码,多次运行程序,观察结果有什么特点)
srand():为rand函数设置时间戳种子(哪一时刻按下运行键确实很随机?)。
去掉的结果:多次结果相同。
问题2:这个程序的功能是什么?
生成随机数,若为奇数生成网络空间安全专业下的随机学号,偶的话则是计科的随机学号。
Task2
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;
}
问题1:line47代码 total_price = 0; 如果去掉,对程序有什么影响?
上次运行的总价会被保留而非更新导致统计错误。
问题2:while循环中,有两处使用 continue 语句。解释在循环中使用 continue 语句,语义是什么?
面对无效输入,跳出当前循环。直接进入下一次询问。使得程序更加安全稳健。
虽然但是C语言是有极限的,若我掏出INT_MAX(在购买数量输入2^31-1=2,147,483,647),阁下又该如何应对?
程序直接崩溃了(fw。建议加上&&quantity<=INT_MAX避免溢出
Task3
include <stdio.h>
int main() {
char c;
while (scanf("%c",&c) != EOF){
if (c == '\n') continue;
if (c == 'y') printf("wait a minute\n");
else if (c == 'g') printf("go go go\n");
else if (c == 'r') printf("stop!\n");
else printf("something must be wrong...\n");
}
return 0;
Task 4
include <stdio.h>
include <limits.h>
int main() {
double x=0,maxcon=-1,mincon=INT_MAX;
double sum=0;
printf("输入今日开销,直到输入-1终止:\n");
while (scanf("%lf",&x) == 1 && x != -1){
if(x<0 || x>20000) {
printf("非法输入\n");
continue;
}
if (x<=mincon) mincon=x;
if (x>=maxcon) maxcon=x;
sum+=x;
}
printf("一天总开销:%.1f\n内最高一笔开销:%.1f\n最低一笔开销:%.1f\n",sum,maxcon,mincon);
return 0;
}
Task 5
include <stdio.h>
void swapf(int* a,int* b) {
int tmp;
tmp=a;
a=b;
b=tmp;
}
int main() {
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c) == 3) {
if (a<b) swapf(&a,&b);
if (a<c) swapf(&a,&c);
//printf("%d%d%d\n",a,b,c);
if (b+c>a) {
if (bb+cc == a*a) printf("直角三角形\n");
else if(ab && bc) printf("等边三角形\n");
else if(ab || ac || b==c) printf("等腰三角形\n");
else printf("普通三角形\n");
}
else printf("不能构成三角形\n");
}
return 0;
}
Task 6
include <stdio.h>
include <time.h>
int main() {
int randnum=time(NULL)%30+1;
printf("猜猜2025年11月哪一天是你的lucky day\n");
printf("开始喽,你有三次机会,猜吧(1~30)😊;
int n=3,x;
while(n--){
scanf("%d",&x);
if (x>randnum) printf("你猜的日期晚了,你的luck yday在前面哦\n");
else if (x<randnum) printf("你猜的日期早了,你的luck yday还没到呢\n");
else {
printf("哇,猜中了:)\n");
break;
}
if (n != 0) printf("再猜(1~30)😊;
}
if (n == -1) printf("次数用光啦。偷偷告诉你,你的lucky day 是%d号\n",randnum);
return 0;
}