实验2
实验1.
#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; printf("20490042%04d\n", number); } return 0; }

1.生成一个随机数
2.使符号宽度为4,如果不够的话就在前面用0补上
3.输出5个特定格式的编号,20490042****
实验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;
}
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,下一次循环total_price为0
2.break终止while(0)这个无限循环,cntinue跳过后续操作回到循环开头
3.有
实验3
#include <stdio.h> int main() { char input; while (scanf_s("%c", &input,1) != EOF) { getchar(); // 读取并丢弃换行符 switch (input) { 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"); break; } } return 0; }

4.实验4
#include <stdio.h> int main() { double expense, total = 0, max = 0, min = 20000; // total 记录总开销,max 记录最高开销,min 记录最低开销,初始设min为20000 printf("输入今日开销,直到输入-1终止:\n"); while (1) { scanf("%lf", &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.实验5
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int luckyDay, guess, attempts = 3; srand(time(0)); luckyDay = rand() % 30 + 1; // 生成1到30之间的随机整数 printf("猜猜2025年4月哪一天是你的lucky day\n"); while (attempts > 0) { printf("开始喽,你有三次机会,猜吧(1~30): "); scanf("%d", &guess); if (guess == luckyDay) { printf("哇,猜中了:)\n"); break; } else if (guess < luckyDay) { printf("你猜的日期早了,你的lucky day还没到呢\n"); } else { printf("你猜的日期晚了,你的lucky day在前面哦\n"); } attempts--; if (attempts > 0) { printf("再猜(1~30): "); } } if (attempts == 0) { printf("次数用完啦,偷偷告诉你,4月你的lucky day是%d号\n", luckyDay); } return 0; }


6.实验6
#include <stdio.h> int main() { int n, i, j, k; printf("input n: "); scanf("%d", &n); for (i = n; i >= 1; i--) { // 打印每行前面的空格 for (j = 0; j < n - i; j++) { printf(" "); } // 打印小人头部和身体上半部分 for (k = 0; k < 2 * i - 1; k++) { printf(" 0 "); if (k != 2 * i - 2) { printf(" "); } } printf("\n"); for (j = 0; j < n - i; j++) { printf(" "); } for (k = 0; k < 2 * i - 1; k++) { printf("<H>"); if (k != 2 * i - 2) { printf(" "); } } printf("\n"); for (j = 0; j < n - i; j++) { printf(" "); } for (k = 0; k < 2 * i - 1; k++) { printf(" I "); if (k != 2 * i - 2) { printf(" "); } } printf("\n"); } return 0; }


浙公网安备 33010602011771号