实验2
实验任务1:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
#define N 5
#define N1 80
#define N2 35
int cut;
int random_major,random_no;
cut = 0;
while(cut<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);
}
cut++;
}
return 0;
}

问题1:当删掉代码时,可以发现生成的学号是固定的,说明代码的作用就是为了生成随机数字

问题2:此程序的作用是生成学号。
实验任务2:
#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;
break;
}
printf("感谢您的购买,欢迎下次光临!\n");
return 0;
}
问题1:去掉total-price=0,可以看见第二次购买时,购买总价出现了与上一次叠加,使得结果出现问题。

问题2:用continue跳出本次循环,直接进行下一次循环。
实验任务3:
#include<stdio.h>
int main(){
char ans;
scanf(" %c",&ans);
while(scanf(" %c", &ans) != EOF){ //ctrl+z
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");
}
}
return 0;
}

实验任务4:
#include <stdio.h>
int main() {
float expense, total = 0, max = 0, min = 20000;
printf("输入今日开销,直到输入-1终止\n");
while(1) {
scanf("%f", &expense);
if(expense == -1) {
break;
}
total += expense;
if(expense > max) {
max = expense;
}
if(expense < min) {
min = expense;
}
}
printf("今日累计消费总额:%.1f\n", total);
printf("今日最高一笔开销:%.1f\n", max);
printf("今日最低一笔开销:%.1f\n", min);
return 0;
}

实验任务5:
#include<stdio.h>
int main(){
int a, b, c;
while(scanf("%d%d%d", &a, &b, &c) != EOF){
if(a + b <= c || a + c <= b || b + c <= a){
printf("不能构成三角形\n");
continue;
}
if(a == b && b == c){
printf("等边三角形\n");
}
else if(a == b || b == c || a == 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");
}
}
return 0;
}
实验任务6;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
int lucky_day = rand() % 30 + 1;
int guess;
int attempts = 0;
int guessed = 0;
printf("猜猜2025年11月哪天是你的lucky day\n");
printf("开始哦,你有三次机会,猜吧(1~30):");
while (attempts < 3) {
scanf("%d", &guess);
attempts++;
if (guess == lucky_day) {
printf("哇,猜中了 :)\n");
guessed = 1;
break;
} else if (guess < lucky_day) {
printf("你猜的日期早了,你的lucky day还没到呢\n");
} else {
printf("你猜的日期晚了,你的lucky day在前面哦\n");
}
if (attempts < 3) {
printf("再猜(1~30):");
}
}
if (!guessed) {
printf("次数用光啦。偷偷告诉你,11月份的lucky day是%d号\n", lucky_day);
}
return 0;
} 

浙公网安备 33010602011771号