实验作业2
任务一
点击查看代码
#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;
}
问题一
获得随机的输出。
问题二
获得五个随机的学号。
任务二
点击查看代码
#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;
}
问题一
该代码用于重置总价,如果去掉那么总价会持续叠加。
问题二
结束本次循环,开始下一次循环。
任务三
点击查看代码
#include<stdio.h>;
int main()
{
char ans;
while (1)
{ ans = getchar();
if (ans == 'r')
{
printf("stop!\n");
}
else if (ans == 'g')
{
printf("go go go\n");
}
else if (ans == 'y')
{
printf("wait a minute\n");
}
else
{
printf("something must be wrong\n");
}
getchar();
}
return 0;
}
任务四
点击查看代码
#include<stdio.h>
int main()
{
double a = 0,sum = 0;
double min, max;
scanf_s("%lf", &a);
if (a != -1)
{
min = a;
max = a;
sum = a;
}
else
{
printf("今日累计消费总额:0.0\n");
printf("今日最高一笔开销: 0.0\n");
printf("今日最低一笔开销: 0.0\n");
return 0;
}
while (1)
{
scanf_s("%lf", &a);
if (a == -1)
break;
else
{
if (min>a)
{
min = a;
}
if (max<a)
{
max = a;
}
sum+=a;
}
}
printf("今日累计消费总额:%.1lf\n", sum);
printf("今日最高一笔开销: %.1lf\n", max);
printf("今日最低一笔开销: %.1lf\n", min);
return 0;
}
任务五
点击查看代码
#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)
{
if (a == b && b == c)
printf("等边三角形\n");
else if ((a == b && b != c) || (b == c && a != b) || (a == c && 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
printf("普通三角形\n");
}
else
printf("不能构成三角形\n");
}
return 0;
}
任务六
点击查看代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(NULL));
printf("猜猜2025年11月那一天使你的lucky day\n开始咯,你有三次机会,猜吧(1-30):");
int x = 1 + rand() % 30, i = 0, a;
while (1)
{
if (i == 2)
{
break;
}
scanf_s("%d", &a);
getchar();
if (a == x)
{
printf("哇,猜中了:)");
break;
}
else if (a < x)
{
printf("你猜的日期早了,你的lucky day还没有到呢\n再猜(1-30):");
}
else if (a > x)
{
printf("你猜的日期晚了,你的lucky day在前面哦\n再猜(1-30):");
}
i++;
}
getchar();
printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号", x);
return 0;
}