实验二
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define N 5 6 #define N1 80 7 #define N2 35 8 9 int main(){ 10 int cnt; 11 int random_major, random_no; 12 13 srand(time(NULL)); 14 15 cnt = 0; 16 while(cnt < N) { 17 random_major = rand() % 2; 18 19 if(random_major) { 20 random_no = rand() % N1 + 1; 21 printf("20256343504D\n",random_no); 22 } 23 else{ 24 random_no = rand() % N2 + 1; 25 printf("20256136%04d\n",random_no); 26 } 27 28 cnt++; 29 } 30 system("pause"); 31 return 0; 32 }

srand(time(NULL))作用是使每次运行生成不同随机数序列
任务二
#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_s("%d", &choice); if (choice == 0) break; if (choice < 1 || choice > 4) { printf("无效的饮料编号,请重新输入。\n"); continue; } printf("请输入购买的数量: "); scanf_s("%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_s("%f", &amount_paid); change = amount_paid - total_price; printf("本次购买总价: %.2f 元\n", total_price); printf("找零: %.2f 元\n", change); total_price = 0; } printf("感谢您的购买,欢迎下次光临!\n"); return 0; }
去掉后每次购买后价格不会清零而是一直累加;语义是跳过循环中剩余指令进行下一指令
#include <stdio.h> int main() { char color; while ((color = getchar()) != -1) { if (color == '\n') { continue; } 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"); } while (getchar() != '\n'); } return 0; }
#include <stdio.h> int main() { double expense, total = 0.0; double maxExpense = 0.0, minExpense = 20000.0; printf("输入今日开销,直到输入-1终止:\n"); while (1) { scanf_s("%lf", &expense); if (expense == -1) { break; } if (expense > maxExpense) { maxExpense = expense; } if (expense < minExpense) { minExpense = expense; } total += expense; } printf("今日累计消费总额:%.1f\n", total); printf("今日最高一笔开销:%.1f\n", maxExpense); printf("今日最低一笔开销:%.1f\n", minExpense); return 0; }
#include <stdio.h> int main() { int a, b, c; while (scanf_s("%d %d %d", &a, &b, &c) != -1) { if (a + b > c && a + c > b && b + c > a) { if (a == b && b == c) { printf("等边三角形\n"); } else if (a == b || a == c || b == c) { if ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (b * b + c * c == a * a)) { printf("等腰直角三角形\n"); } else { 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"); } } else { printf("不能构成三角形\n"); } } return 0; }
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand((unsigned int)time(NULL)); int luckyDay = rand() % 30 + 1; int guess, count = 0; printf("猜猜2025年11月哪一天是你的lucky day\n"); printf("开始喽,你有三次机会,猜吧(1~30):"); while (count < 3) { scanf_s("%d", &guess); count++; if (guess == luckyDay) { printf("哇,猜中了:)\n"); return 0; } else if (guess < luckyDay) { printf("你猜的日期早了,你的lucky day还没到呢\n"); } else { printf("你猜的日期晚了,你的lucky day在前面哦\n"); } if (count < 3) { printf("再猜(1~30):"); scanf_s("%d", &guess); count++; if (guess == luckyDay) { printf("哇,猜中了:)\n"); return 0; } else if (guess < luckyDay) { printf("你猜的日期早了,你的lucky day还没到呢\n"); } else { printf("你猜的日期晚了,你的lucky day在前面哦\n"); } } } printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号\n", luckyDay); return 0; }
浙公网安备 33010602011771号