实验二
实验2
test1
源代码
#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;
}
截图

问答
1.随机生成5个学员编号,编号格式固定为20490042xxxx,其中后4位xxxx是1~100之间的随机整数。
2.生成一个1到100之间的随机整数,并赋值给变量number。
3.以固定4位宽度输出整数,不足4位时前面补 0。
4.设置随机数种子,让每次运行程序生成的随机数都不一样。若去掉每次生成的数组都相同。
test2
源代码
#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; // line47
}
printf("感谢您的购买,欢迎下次光临!\n");
return 0;
}
截图

问答
1.下一次购买时,总价会累计上一次的金额,程序计算错误。
2.立即结束本次循环的剩余代码,直接回到循环开头,重新执行循环。
test3
源代码
#include <stdio.h>
int main() {
char color;
while (scanf("%c", &color) != EOF) {
if (color == '\n') {
continue;
}
if (color == 'r') {
printf("stop!\n");
} else if (color == 'g') {
printf("go go go\n");
} else if (color == 'y') {
printf("wait a minute\n");
} else {
printf("something must be wrong...\n");
}
}
printf("\mission completed\n");
return 0;
}
截图

test4
源代码
#include <stdio.h>
int main() {
float money;
float total = 0;
float max, min;
int first = 1;
printf("输入今日开销,直到输入-1为止:\n");
while (scanf("%f", &money) == 1 && money != -1) {
if (money <= 0 || money > 20000) {
printf("error\n");
continue;
}
if (first) {
max = money;
min = money;
first = 0;
}
else {
if (money > max) {
max = money;
}
if (money < min) {
min = money;
}
}
total += money;
}
printf("今日累计消费总额:%.1f\n", total);
printf("今日最高消费:%.1f\n", max);
printf("今日最低消费:%.1f\n", min);
return 0;
}
截图

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

test6
源代码
include <stdio.h>
include <stdlib.h>
include <time.h>
int main() {
srand(time(0));
int lucky_day = rand() % 30 + 1;
int guess;
int chances = 3;
printf("游戏规则:幸运日是4月1日-30日之间的一天,你有3次猜测机会!\n\n");
while (chances > 0) {
printf("你还有 %d 次机会,请输入你猜测的日期:", chances);
scanf("%d", &guess);
if (guess < 1 || guess > 30) {
printf("输入无效!请输入1-30之间的数字\n\n");
continue;
}
chances--;
if (guess == lucky_day) {
printf("猜中了!\n", lucky_day);
return 0;
}
else if (guess < lucky_day) {
printf("猜早了!\n\n");
}
else {
printf("猜晚了!\n\n");
}
}
printf("3次机会已用完,4月的lucky day是%d号\n", lucky_day);
return 0;
}
截图


实验总结
1.学习了srand(time(0)),可取随机整数,rand()%n+1表示有范围从1-n的整数。
2.实验六想要猜对真不容易搞一张成功的截图太难了。
3.自己编写代码时还是小错误不断,自己搞不清楚还得ai debug。
4.感觉自己写的不是很简便,判断条件打了很多不知道怎么改会更好。

浙公网安备 33010602011771号