实验2
1.实验任务1
1.task1.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个学号。
**2. 实验任务2 **
task2.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.实验任务3
task3.c
点击查看代码
#include<stdio.h>
int main()
{
int a;
while (1)
{
a = getchar();
getchar();
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("something must be wrong\n");
}
return 0;
}

4.实验任务4
task4.c
点击查看代码
#include<stdio.h>
int main()
{
double sum = 0, n = 0, max=0, min=20000;
printf("输入今日开销,直到输入-1终止:\n");
while (1)
{
scanf_s("%lf", &n);
if (n != -1)
{
if (n > max)
max = n;
if (n < min)
min = n;
sum = sum + n;
}
else if (n == -1)
{
printf("今日累计消费总额:%.1lf\n今日最高一笔开销:%.1lf\n今日最低一笔开销:%.1lf\n", sum, max, min);
break;
}
}
return 0;
}

5.实验任务5
task5.c
点击查看代码
#include<stdio.h>
int main()
{
int a, b, c;
while (1) {
scanf_s("%d%d%d", &a, &b, &c);
if (a + b <= c || a + c <= b || b + c <= a)
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
printf("普通三角形\n");
}
return 0;
}

6.实验任务6
task6.c
点击查看代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(NULL));
int random;
random = rand() % 30 + 1;
int n;
printf("猜猜2025年11月那一天是你的lucky day\n");
printf("开始喽,你有三次机会,猜吧(1~30):");
scanf_s("%d", &n);
if (n == random)
{
printf("哇,猜中了:)\n");
return 0;
}
else if (n < random)
printf("你猜的日期早了,你的lucky day还没到呢\n");
else if (n > random)
printf("你猜的日期晚了,你的lucky day在前面哦\n");
for (int i = 0; i < 2; i++)
{
printf("再猜(1~30):");
scanf_s("%d", &n);
if (n == random)
{
printf("哇,猜中了:)\n");
return 0;
}
else if (n < random)
printf("你猜的日期早了,你的lucky day还没到呢\n");
else if (n > random)
printf("你猜的日期晚了,你的lucky day在前面哦\n");
}
printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号", random);
return 0;
}



浙公网安备 33010602011771号