实验2
实验二
task1代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<stdlib.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);
}
system("pause");
return 0;
}
运行结果:
问题1:生成1-100随机数
问题2:输出4个数不足补零
问题3:生成12位数字
task2代码:
#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;
}
switch (choice) {
case 1:
case 2:
total_price += 3 * quantity;
break;
case 3:
total_price += 5 * quantity;
break;
case 4:
total_price += 2 * quantity;
break;
}
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:清空总价格防止二次计算时初值不为0
问题2:break直接停止,continue停止本次后继续
问题3:没必要,choice的值只有01234
task3代码:
#include<stdio.h>
int main()
{
char color;
printf("请输入信号灯颜色,r代表red,g代表green,y代表yellow:");
while(scanf("%c",&color) != EOF){
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("请输入信号灯颜色,r代表red,g代表green,y代表yellow:");
getchar();
}
return 0;
}
运行结果:
task4代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{
double x,max,min,sum;
sum=0;
max=0;
printf("输入今日开销,到-1停止\n");
scanf("%lf",&x);
min = x;
while(x != -1){
sum += x;
if(x>max){
max = x;
}
if(x<min){
min = x;
}
scanf("%lf",&x);
}
printf("今日累计消费总额:%.1lf\n今日最高一笔开销:%.1lf\n今日最低一笔开销:%.1lf\n",sum,max,min);
system("pause");
return 0;
}
运行结果:
task5代码:
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 3
int main()
{
int day,i,guess;
printf("猜猜2025年4月哪一天是你的lucky day\n");
srand(time(0));
day = rand() % 30 + 1;
for (i = 0; i < N; i++)
{
if (i == 0)
printf("开始喽,你有三次机会,猜吧(1~30):");
else
printf("再猜(1~30):");
scanf("%d", &guess);
if (guess > day)
printf("你猜的日期晚了,你的lucky day在前面哦\n");
else if (guess < day)
printf("你猜的日期早了,你的lucky day还没到呢\n");
else
{
printf("哇,猜中了:-)\n");
break;
}
}
if (i == 3)
printf("次数用完啦。悄悄告诉你,4月你的lucky day是%d号\n", day);
system("pause");
return 0;
}
运行结果:
task6代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,j;
printf("input n: ");
scanf("%d", &n);
for (i = n; i >0; i--)
{
for (j = 0; j < n-i; j++)
printf(" \t");
for (j = 0; j < 2*i-1; j++)
printf(" O \t");
for (j = 0; j < n - i; j++)
printf(" \t");
printf("\n");
for (j = 0; j < n - i; j++)
printf(" \t");
for (j = 0; j < 2*i-1; j++)
printf("<H>\t");
for (j = 0; j < n - i; j++)
printf(" \t");
printf("\n");
for (j = 0; j < n - i; j++)
printf(" \t");
for (j = 0; j < 2*i-1; j++)
printf("I I\t");
for (j = 0; j < n - i; j++)
printf(" \t");
printf("\n");
printf("\n");
}
system("pause");
return 0;
}
运行结果: