实验2
实验任务1
test1.c源代码
#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:用当前的系统时间随机生成数,确保每次生成的随机数列都不同
问题2:随机生成5个前缀为“20256343”或“20256136”的学号
实验任务2
test2.c源代码
#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:会将上一次的总价与本次相加,导致钱算多了
问题2:跳过当前循环,继续下一次
实验任务3
test3.c源代码
#include <stdio.h>
int main() {
char light;
while (1) {
if (scanf("%c", &light) != 1)
break;
if (light == '\n') {
continue;
}
if (light == 'r') {
printf("stop!\n");
}
else if (light == 'g') {
printf("go go go");
}
else if (light == 'y') {
printf("wait a minute");
}
else {
printf("something must be wrong...");
}
}
return 0;
}
运行结果截图
实验任务4
test4.c源代码
#include<stdio.h>
int main() {
double cost,total=0.0,max=0.0,min=20000.1;
printf("输入今日开销,直到输入-1终止:");
while (1) {
scanf("%lf", &cost);
if (cost == -1)
break;
if (cost <= 0 || cost > 20000)
continue;
total += cost;
if (cost > max)
max = cost;
if (cost < min)
min = cost;
}
printf("今日累计消费总额:%.1f\n", total);
printf("今日最高一笔开销:%.1f\n", max);
printf("今日最低一笔开销:%.1f\n", min);
return 0;
}
运行结果截图
实验任务5
test5.c源代码
#include<stdio.h>
const char* type(int a, int b, int c)
{
if (a + b < c || a + c <= b || b + c <= a)
return("不能构成三角形");
else if (a == b && b== c)
return("等边三角形");
else if (a == b || a == c || b == c)
return("等腰三角形");
else if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a)
return("直角三角形");
else
return("普通三角形");
}
int main()
{
int a, b, c;
while (scanf("%d %d %d",&a,&b,&c) == 3)
printf("%s\n", type(a, b, c));
return 0;
}
运行结果截图
实验任务6
test6.c源代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int lucky, guess;
int left = 3;
srand((unsigned)time(NULL));
lucky = rand() % 30 + 1;
printf("猜猜2025年11月哪一天是你的lucky day\n");
printf("开始喽,你有三次机会,猜吧(1~30):");
while (left--) {
scanf("%d", &guess);
if (guess == lucky) {
printf("哇,猜中了:)\n");
return 0;
}
else if (guess > lucky)
printf("你猜的日期晚了,你的lucky day在前面哦\n");
else
printf("你猜的日期早了,你的lucky day还没到呢\n");
if (left)
printf("再猜(1~30):");
}
printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号\n", lucky);
return 0;
}
运行结果截图
实验总结
对于实验5不太熟悉,用自己的理解写了一个但是不太对
发现自己多组输入的函数还不太熟悉,希望下次能记住