示例1:随机数据生成器
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define STUDENT_COUNT 5
#define MAJOR_ID_1 80
#define MAJOR_ID_2 35
int main() {
int index, majorRandom, studentID;
srand((unsigned)time(NULL));
for(index = 0; index < STUDENT_COUNT; ++index) {
majorRandom = rand() % 2;
if(majorRandom) {
studentID = rand() % MAJOR_ID_1 + 1;
printf("2025634%04d\n", studentID);
} else {
studentID = rand() % MAJOR_ID_2 + 1;
printf("20256136%04d\n", studentID);
}
}
return 0;
}
实验结论1:无图说明。
问题1:srand函数用于初始化随机数发生器。
问题2:此程序旨在生成一系列随机学号。
示例2:自动售卖机模拟器
#include <stdio.h>
int main() {
int selection, quantity;
float totalPrice = 0.0, payment, 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", &selection);
if(selection == 0)
break;
if(selection < 1 || selection > 4) {
printf("无效的选择,请重新输入。\n");
continue;
}
printf("请输入数量:");
scanf("%d", &quantity);
switch(selection) {
case 1:
case 2: totalPrice += 3 * quantity; break;
case 3: totalPrice += 5 * quantity; break;
default: totalPrice += 2 * quantity;
}
printf("请支付金额:");
scanf("%f", &payment);
change = payment - totalPrice;
printf("总价:%.2f元\n", totalPrice);
printf("找零:%.2f元\n", change);
totalPrice = 0.0;
}
printf("感谢您的光临,期待下次再见!\n");
return 0;
}
实验结论2:无图说明。
问题1:需要确保每次购买后重置总价。
问题2:使用break可以立即退出当前循环。
示例3:红绿灯状态机
#include <stdio.h>
int main() {
char command;
while ((command = getchar()) != EOF) {
if (command == '\n') continue;
switch(command) {
case 'r': printf("停止!\n"); break;
case 'g': printf("前进!\n"); break;
case 'y': printf("稍等片刻...\n"); break;
default: printf("未知指令...\n");
}
while(getchar() != '\n');
}
return 0;
}
实验结论3:无图说明。
示例4:日常开销记录器
#include <stdio.h>
int main() {
float expense, totalExpenses = 0.0, maxExpense = 0.0, minExpense = 20000.0;
printf("输入每日开销,输入-1结束录入。\n");
while(1) {
scanf("%f", &expense);
if(expense == -1) break;
totalExpenses += expense;
if(expense > maxExpense) maxExpense = expense;
if(expense < minExpense) minExpense = expense;
}
printf("总开销:%.1f元\n", totalExpenses);
printf("最高单笔开销:%.1f元\n", maxExpense);
printf("最低单笔开销:%.1f元\n", minExpense);
return 0;
}
实验结论4:无图说明。
示例5:三角形类型判断器
#include <stdio.h>
int main() {
int sideA, sideB, sideC;
while(scanf("%d %d %d", &sideA, &sideB, &sideC) != EOF) {
if(sideA + sideB > sideC && sideA + sideC > sideB && sideB + sideC > sideA) {
if(sideA == sideB && sideB == sideC)
printf("等边三角形\n");
else if(sideA == sideB || sideB == sideC || sideA == sideC)
printf("等腰三角形\n");
else if(sideA*sideA + sideB*sideB == sideC*sideC || sideA*sideA + sideC*sideC == sideB*sideB || sideB*sideB + sideC*sideC == sideA*sideA)
printf("直角三角形\n");
else
printf("普通三角形\n");
} else {
printf("无法构成三角形\n");
}
}
return 0;
}
实验结论5:无图说明。
示例6:幸运日猜测游戏
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand((unsigned)time(NULL));
int luckyDay = rand() % 30 + 1, guess, attempts = 0;
printf("猜一猜哪一天(1-30)是你的幸运日吧!\n");
printf("开始吧,你有三次机会。\n");
while(attempts < 3) {
scanf("%d", &guess);
attempts++;
if(guess == luckyDay) {
printf("恭喜你,猜对了!\n");
break;
} else if(guess > luckyDay) {
printf("太大了,试试更小的数字。\n");
} else {
printf("太小了,试试更大的数字。\n");
}
if(attempts < 3) printf("再试一次(1-30):");
}
if(attempts == 3 && guess != luckyDay)
printf("机会用完了。告诉你吧,你的幸运日是%d号。\n", luckyDay);
return 0;
}
实验结论6:无图说明。
实验总结:注意代码块的作用域对循环行为的影响。在处理用户输入时,正确管理状态非常重要。