作业2
实验1
1源代码
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
#define CNT 5
int main() {
int number;
int i;
srand(time(0));
for(i = 0; i < CNT; ++i) {
number = rand() % N + 1;
printf("20490042%04d\n", number);
}
return 0;
}

3结论:1程序的主要功能是生成5个指定格式的学号 2lin14的功能是生成一个随机数字 3起到格式化去输出整数 4作用是设置随机数种子
实验2
1源代码
点击查看代码
#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;
}

3结论:1去掉会使总价累加错误 2语义是指跳过本次循环到末尾
实验3
1源代码
点击查看代码
#include<stdio.h>
int main(){
char c;
int i;
for(i=0;i<4;i++){
scanf(" %c",&c);
switch(c){
case 'r':puts("stop!");break;
case 'g':puts("go go go");break;
case 'y':puts("wait a minute");break;
default:puts("smothing must be wrong...");
}
}return 0;
}

实验4
1源代码
点击查看代码
#include<stdio.h>
int main(){
double m,s=0,ma,mi;
int f=1;
puts("输入今日开销:");
while(scanf("%lf",&m),m!=-1){
if(f){ma=mi=m;f=0;}
s+=m;
if(m>ma)ma=m;
if(m<mi)mi=m;
}
printf("今日累计消费总额: %.lf\n",s);
printf("今日最高一笔开销: %.lf\n",ma);
printf("今日最低一笔开销: %.lf\n",mi);
return 0;
}

实验5
1源代码
点击查看代码
#include<stdio.h>
int main(){
int a,b,c,i;
for(i=0;i<5;i++){
scanf("%d%d%d",&a,&b,&c);
if(a+b>c&&a+c>b&&b+c>a){
if(a==b&&b==c)
puts("等边三角形");
else if(a==b||a==c||b==c)
puts("等腰三角形");
else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a)
puts("直角三角形");
else puts("普通三角形");
}else puts("不能构成三角形");
}return 0;
}

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

浙公网安备 33010602011771号