实验2
一、实验结论
1.实验任务1
- 代码task1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int main() {
int number;
int i;
srand(time(0));
for (i = 0; i < N; ++i) {
number = rand() % 100 + 1;
printf("20490042%04d\n", number);
}
return 0;
}
- 运行结果截图
![task1]()
- 回答问题
问题1:这个程序的功能是什么?
答1:生成五个以20490042开头+0+三位生成的随机数构成的学号。
问题2:解释lin13代码的功能。
答2:将系统时间(四位数)对100取余并+1赋值给number这个变量。
问题3:解释line14使用格式符%04d起到什么作用。
答3:使学号后四位对齐:在生成的随机数前面补齐0,使之构成四位对齐的格式。
问题4:代码srand(time(0));起到什么作用?(提示:去掉这行代码,多次运行程序,观察结果有什
么特点)
答4:去掉后无论运行多少次结果都是一样的,所以这行代码起到以时间作为随机数种子的作用
2.实验任务2
- 代码task2
#include <stdio.h>
#include<windows.h>
int main() {
system("chcp 65001");
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_s("%d", &choice);
if (choice == 0)
break;
if (choice < 1 || choice > 4) {
printf("无效的饮料编号,请重新输入。\n");
continue;
}
printf("请输入购买的数量: ");
scanf_s("%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_s("%f", &amount_paid);
change = amount_paid - total_price;
printf("本次购买总价: %.2f 元\n", total_price);
printf("找零: %.2f 元\n", change);
total_price = 0;
}
printf("感谢您的购买,欢迎下次光临!\n");
return 0;
}
-
运行结果截图
![task22]()
-
回答问题
问题1:line47代码total_price = 0;如果去掉,对程序有什么影响?
答1:对第一次运行没有影响,但是第二次就会把第一次的总价叠加,结果出错。
问题2:while循环中,有两处使用continue语句。解释在循环中使用continue语句,语义是什么?
答2:输入出错了,continue可以跳出此次循环,从while开始进行下一次循环。
3.实验任务3
- 代码task3
#include<stdio.h>
int main() {
char a;
while (1) {
printf("Enter color:");
a = getchar();
if (a >= 'A' && a <= 'Z') {
a = a + 32;
}
if (a == 'r') {
printf("stop!\n");
}
else if (a == 'g') {
printf("go go go\n");
}
else if(a=='y'){
printf("wait a minute\n");
}
else {
printf("someting must be wrong...\n");
}
getchar();
}
return 0;
}
- 运行结果截图
![task3]()
4.实验任务4
- 代码task4
#include<stdio.h>
int main() {
double cost = 0, max = 0, min = 0, total = 0;
printf("输入今日开销,直到-1终止:");
scanf_s("%lf", &cost);
while (cost != -1) {
total += cost;
if (max < cost) {
max = cost;
}
if (min > cost) {
min = cost;
}
scanf_s("%lf", &cost);
}
printf("今日总开销:%.1lf\n", total);
printf("今日最大开销:%.1lf\n", max);
printf("今日最小开销:%.1lf\n", min);
return 0;
}
- 运行结果截图
![task4]()
5.实验任务5
- 代码task5
#include<stdio.h>
int main() {
int a, b, c, temp;
while (1) {
printf("输入三角形三边:\n");
scanf_s("%d %d %d", &a, &b, &c);
if (a > b) {
temp = a;
a = b;
b = temp;
}
if (a > c) {
temp = a;
a = c;
c = temp;
}
if (b > c) {
temp = b;
b = c;
c = temp;
}
if (a + b <= c) {
printf("不是三角形\n");
}
else if (a == b && a == c) {
printf("全等三角形\n");
}
else if (a == b && a != c) {
printf("等腰三角形\n");
}
else if (a * a + b * b == c * c) {
printf("直角三角形\n");
}
else {
printf("普通三角形\n");
}
}
return 0;
}
- 运行结果截图
![task5]()
6.实验任务6
- 代码task6
#include<stdio.h>
#include<time.h>
int main() {
int num,gus,i;
srand(time(0));
num = rand() % 30+1;
printf("猜猜2026年4月哪天是你的lucky day\n");
printf("开始喽!猜吧,你有三次机会(1~30)\n");
for (i = 0; i < 3; i++) {
scanf_s("%d", &gus);
if (gus > num) {
printf("你猜的日期晚了,你的lucky day 在前面哦~\n");
printf("再猜(1~30)\n");
}
else if (gus < num) {
printf("你猜的日期早了,你的lucky day 还没到哦~\n");
printf("再猜(1~30)\n");
}
else if (gus ==num) {
printf("哇!猜中了!\n");
break;
}
}
if (i == 3) {
printf("次数用光了。4月你的lucky day是%d号\n", num);
}
return 0;
}
- 运行结果截图
![task6]()
二、实验总结
核心收获与思考:
任务4,算总开销的实验,一开始我把scanf放在了循环的第一句,导致总数少1,算总数的要注意循环执行次数,避免出错。
受任务4影响,我在任务六中也将scanf放在循环最后,结果代码运行四次。 要根据情况需要找位置!!!
任务5中,我觉得把所有判断写在if中有点麻烦,而且不好找出错误。我第一次尝试是把if判断套在while中,然后将a,b,c通过空变量d逐一交换,这样的算法有很大的逻辑bug,程序基本不会执行第二遍,每一次都是错的;后面我认为出现这样的问题是因为输入的a,b,c没有大小排序,所以我第二次先将它们由小到大排序,简化了判断问题。
任意想补充的反馈或分享:
感谢老师帮我解决了一直以来困扰我的中文乱码问题!







浙公网安备 33010602011771号