实验二
实验二
试验任务一
代码
点击查看代码
#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;//随机生成一个1~100的数字
printf("20490042%04d\n",number);//%04d的作用:将整数以宽度为4的形式输出,不足4位时在前面补0
}
system("pause");
return 0;
}
ans:1.随机生成一个1~100的数字
2.%04d的作用:将整数以宽度为4的形式输出,不足4位时在前面补0
3.以当前系统时间作为随机种子,利用循环随机生成5个1~100之间的随机数,并将每个随机数与20490042组合输出,生成学生编号。
截图

实验任务二
代码
点击查看代码
#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;
}

ans:
1.用途:将total_price重置为0。如果去掉这行代码,上次输入的数值将会保存到下次输入的数值中,导致total_price不准确。
2.break:立即终止循环,跳转到循环后面的语句继续执行
continue:跳过本次循环中continue后面的语句,直接开始下一次循环的条件判断
3.否,没必要
试验任务三
代码
点击查看代码
#include <stdio.h>
#include<stdlib.h>
int main() {
char ch;
while(scanf("%c",&ch)!=EOF)
{getchar();
switch(ch){
case 'r':printf("stop!\n");break;
case 'g':printf("go go go\n");break;
case 'y':printf("wait a minute\n");break;
default:printf("something must be wrong...\n");
}
}
system("pause");
return 0;
}
截图

试验任务四
代码
点击查看代码
#include <stdio.h>
#include<stdlib.h>
int main() {
double a,total=0,max=0,min=20000;
printf("输入今日开销,直到输入-1终止:\n");
while(1){
scanf("%lf",&a);
if(a==-1){
break;
}
total+=a;
if(a>max){
max=a;
}
if(a<min){
min=a;
}
}
printf("今日累计消费总额:%.1f\n",total);
printf("今日最高一笔开销:%.1f\n",max);
printf("今日最低一笔开销:%.1f\n",min);
system("pause");
return 0;
}
截图

试验任务五
代码
点击查看代码
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 5
int main() {
int luckyday,guess,chance=3;
srand((unsigned int)time(NULL));
luckyday=rand()%30+1;
printf("猜猜2025年4月哪一天是你的luckyday\n");
while(chance>0){
printf("开始喽,你有三次机会,猜吧(1~30):");
scanf("%d",&guess);
if(guess==luckyday){
printf("哇,猜中了!\n");
return 0;
}else if(guess<luckyday)
{
printf("你猜的日期早了,你的luckyday还没到呢\n");}
else{
printf("你猜的日期晚了,你的luckyday在前面哦\n");
}
chance--;
if(chance>0){
printf("再猜(1~30):");
}
}
printf("次数用完啦,偷偷告诉你,4月你的luckyday是%d号\n",luckyday);
system("pause");
return 0;
}
截图

试验任务六
代码
点击查看代码
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,j,t;
int n_line=0;
printf("请输入行数:");
scanf("%d",&n);
n_line=2*n-1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n_line && n_line>=1;j++)
{
printf(" o \t");
}
printf("\n");
for(t=1;t<i;t++)
printf("\t");
for(j=1;j<=n_line && n_line>=1;j++)
{
printf("<H>\t");
}
printf("\n");
for(t=1;t<i;t++)
printf("\t");
for(j=1;j<=n_line && n_line>=1;j++)
{
printf("I I\t");
}
printf("\n");
for(t=0;t<i;t++)
printf("\t");
n_line=n_line-2;
}
system("pause");
return 0;
}
截图


浙公网安备 33010602011771号