1:编写一个程序读取输入,读到#号为止,然后报告读取的空格数,换行符和所有其他字符的数量。
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include <stdio.h>
int main(void)
{
char ch;
int blank_space = 0;
int line_break = 0;
int Other_characters = 0;
printf("请输入字符(输入 '#' 结束):\n");
while ((ch = getchar() )!= '#')
//这行代码的意图是读取字符直到遇到 #。
//但是,由于 != 操作符的优先级比赋值操作符 = 高,
// 实际上的代码行为是先判断 getchar() != '#' 的结果,
// 再将结果赋值给 ch。这会导致 ch 被赋值为 0 或 1,而不是实际读取的字符。
{
if (ch == ' ') {
blank_space++; // 如果是空格,增加空格计数
}
else if (ch == '\n') {
line_break++; // 如果是换行符,增加换行计数
}
else {
Other_characters++; // 否则,增加其他字符计数
}
}
printf("空格数: %d\n", blank_space);
printf("换行符数: %d\n", line_break);
printf("其他字符数: %d\n", Other_characters);
printf("Done!");
return 0;
}
2:编写程序读取输入,读到#为止,报告ei出现的次数。
#include <stdio.h>
int main(void) {
char ch, prev = '\0'; // 初始化当前字符和前一个字符
int count = 0; // 初始化计数器
printf("请输入文本,以 '#' 结束:\n");
while ((ch = getchar()) != '#') { // 读取字符,直到遇到 '#'
if (prev == 'e' && ch == 'i') { // 检查前一个字符和当前字符是否构成 "ei"
count++; // 如果是,计数器加一
}
prev = ch; // 更新前一个字符
}
printf("字符串中出现了 %d 次 \"ei\"。\n", count); // 输出结果
return 0;
}
3:ABC 邮购杂货店出售的洋蓟售价为2.05美元/磅,甜菜售价为1.15/美元磅,胡萝卜的售价为1.09/美元/磅、在添加运费之前,100美元的订单有5%的打折优惠,少于或等于5磅的订单收取6.5元的运费和2元包装费,5磅~20磅的订单收取14美元的运费和2元的包裝费,超过20磅的订单在14美元的基础上每续重1磅增加 0.5美元。编写一个程序,在循环中用 switch 语句实现用户输入不同的字母时有不同的响应,即输入a的响应是让用户输入洋蓟的磅数,b是甜菜的磅数,c是胡萝卜的磅数,q是退出订购,程序要记录累计的重量。即,如果用户输入4磅的甜菜,然后输入5磅的甜菜,程序应报告9磅的甜菜。然后,该程序要计算货物总价,折扣(如果有的话)、运费和包装费。随后,程序应显示所有的购买信息:物品售价、订购的重量(单位:磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装费,以及所有的费用总额。
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include <stdio.h>
#include <stdlib.h> // 用于使用system函数
// 函数声明
void mainMenu();
void option1();
void option1Submenu();
void option2();
void option2Submenu();
void option3();
void option3Submenu();
void option5();
void option5Submenu();
void clearScreen() {
#if defined(_WIN32) || defined(_WIN64)
system("cls"); // Windows 系统清屏
#else
system("clear"); // Linux 和 macOS 系统清屏
#endif
}
int main() {
mainMenu(); // 进入主菜单
return 0;
}
// 主菜单
void mainMenu() {
char choice;
while (1) { // 无限循环,直到用户选择退出
// 显示菜单
printf("*************************************\n");
printf("请选择你的身份类别:\n ");
printf("\na) 洋芋");
printf("\t\t\tb) 甜菜\n");
printf("\nc) 胡萝卜");
printf("\t\tq) 退出订购\n");
printf("*************************************\n");
printf("Enter your choice: ");
scanf(" %c", &choice); // 注意:这里的格式化字符串中有一个空格,以忽略前面的换行符
clearScreen(); // 选择后清屏
// 根据用户选择执行相应的操作
switch (choice) {
case 'a':
option1();
break;
case 'b':
option2();
break;
case 'c':
option3();
break;
case 'q':
option5();
return;
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
}
// 选项1的子菜单
void option1() {
option1Submenu(); // 进入选项1的子菜单
}
// 选项1的子菜单
void option1Submenu() {
int subChoice;
double totalWeight = 0, weight;
const double yang_yu = 2.05;
double totalAmount = 0, shippingCost = 0, packagingCost = 2.00; // 初始化包装费
double discount = 0;
while (1) {
// 显示子菜单
printf("***************************************************************\n");
printf("洋芋的售价为2.05美元/磅\n");
printf("\n1) 输入洋芋的磅数");
printf("\t\t2) 返回主菜单并显示总重量、总额、运费和包装费\n");
printf("*****************************************************************\n");
printf("Enter your choice: ");
scanf("%d", &subChoice);
clearScreen(); // 清屏
switch (subChoice) {
case 1:
printf("请输入洋芋的磅数: ");
if (scanf("%lf", &weight) != 1) {
printf("输入无效。\n");
return;
}
totalWeight += weight;
totalAmount += weight * yang_yu; // 使用正确的货物总额计算公式
printf("当前累计重量为:%.2lf磅\n", totalWeight);
printf("当前货物总额为:%.2lf美元\n", totalAmount);
printf("按 Enter 返回,如需要继续输入磅数,请在返回后继续按'1'\n ");
getchar(); // 暂停以便用户查看结果
getchar();
clearScreen(); // 清屏后显示子菜单
break;
case 2:
totalAmount = yang_yu * totalWeight;
// 应用每100美元有0.05美元的折扣
if (totalAmount >= 100) {
discount = totalAmount * 0.05;
totalAmount -= discount; // 减去折扣
}
// 运费计算:假设运费是每磅0.5美元,超过5磅后,每磅增加0.1美元
if (totalWeight <= 5) {
shippingCost = totalWeight * 6.5;
}
else if (totalWeight > 5 && totalWeight <= 20) {
shippingCost = totalWeight * 14;
}
else {
shippingCost = 20 * 14 + (totalWeight - 20) * 0.5;
}
printf("总重量为:%.2lf磅\n", totalWeight);
printf("当前货物总额为:%.2lf美元\n", totalAmount);
printf("折扣为:%.2lf美元\n", discount);
printf("运费为:%.2lf美元\n", shippingCost);
printf("包装费为:%.2lf美元\n", packagingCost);
printf("总费用为:%.2lf美元\n", totalAmount + shippingCost + packagingCost);
printf("按 Enter 返回主菜单\n");
getchar(); // 暂停以便用户查看结果
getchar();
clearScreen(); // 清屏返回主菜单
return; // 退出子菜单,返回主菜单
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
}
// 选项2的功能 - 进入新的界面(子菜单)
void option2() {
option2Submenu(); // 进入选项2的子菜单
}
// 选项2的子菜单
void option2Submenu() {
int subChoice;
double totalWeight = 0, weight;
const double tian_cai = 1.15;
double totalAmount = 0, shippingCost = 0, packagingCost = 2.00; // 初始化包装费
double discount = 0;
while (1) {
// 显示子菜单
printf("***************************************************************\n");
printf("甜菜的售价为1.15美元/磅\n");
printf("\n1) 输入甜菜的磅数");
printf("\t\t2) 返回主菜单并显示总重量、总额、运费和包装费\n");
printf("*****************************************************************\n");
printf("Enter your choice: ");
scanf("%d", &subChoice);
clearScreen(); // 清屏
switch (subChoice) {
case 1:
printf("请输入甜菜的磅数: ");
if (scanf("%lf", &weight) != 1) {
printf("输入无效。\n");
return;
}
totalWeight += weight;
totalAmount += weight * tian_cai; // 使用正确的货物总额计算公式
printf("当前累计重量为:%.2lf磅\n", totalWeight);
printf("当前货物总额为:%.2lf美元\n", totalAmount);
printf("按 Enter 返回,如需要继续输入磅数,请在返回后继续按'1'\n ");
getchar(); // 暂停以便用户查看结果
getchar();
clearScreen(); // 清屏后显示子菜单
break;
case 2:
totalAmount = tian_cai * totalWeight;
// 应用每100美元有0.05美元的折扣
if (totalAmount >= 100) {
discount = totalAmount * 0.05;
totalAmount -= discount; // 减去折扣
}
// 运费计算:假设运费是每磅0.5美元,超过5磅后,每磅增加0.1美元
if (totalWeight <= 5) {
shippingCost = totalWeight * 6.5;
}
else if (totalWeight > 5 && totalWeight <= 20) {
shippingCost = totalWeight * 14;
}
else {
shippingCost = 20 * 14 + (totalWeight - 20) * 0.5;
}
printf("总重量为:%.2lf磅\n", totalWeight);
printf("当前货物总额为:%.2lf美元\n", totalAmount);
printf("折扣为:%.2lf美元\n", discount);
printf("运费为:%.2lf美元\n", shippingCost);
printf("包装费为:%.2lf美元\n", packagingCost);
printf("总费用为:%.2lf美元\n", totalAmount + shippingCost + packagingCost);
printf("按 Enter 返回主菜单\n");
getchar(); // 暂停以便用户查看结果
getchar();
clearScreen(); // 清屏返回主菜单
return; // 退出子菜单,返回主菜单
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
}
// 选项3的功能 - 进入新的界面(子菜单)
void option3() {
option3Submenu(); // 进入选项3的子菜单
}
// 选项3的子菜单
void option3Submenu() {
int subChoice;
double totalWeight = 0, weight;
const double hu_luo_bo = 1.09;
double totalAmount = 0, shippingCost = 0, packagingCost = 2.00; // 初始化包装费
double discount = 0;
while (1) {
// 显示子菜单
printf("***************************************************************\n");
printf("胡萝卜的售价为1.09美元/磅\n");
printf("\n1) 输入胡萝卜的磅数");
printf("\t\t2) 返回主菜单并显示总重量、总额、运费和包装费\n");
printf("*****************************************************************\n");
printf("Enter your choice: ");
scanf("%d", &subChoice);
clearScreen(); // 清屏
switch (subChoice) {
case 1:
printf("请输入胡萝卜的磅数: ");
if (scanf("%lf", &weight) != 1) {
printf("输入无效。\n");
return;
}
totalWeight += weight;
totalAmount += weight * hu_luo_bo; // 使用正确的货物总额计算公式
printf("当前累计重量为:%.2lf磅\n", totalWeight);
printf("当前货物总额为:%.2lf美元\n", totalAmount);
printf("按 Enter 返回,如需要继续输入磅数,请在返回后继续按'1'\n ");
getchar(); // 暂停以便用户查看结果
getchar();
clearScreen(); // 清屏后显示子菜单
break;
case 2:
totalAmount = hu_luo_bo * totalWeight;
// 应用每100美元有0.05美元的折扣
if (totalAmount >= 100) {
discount = totalAmount * 0.05;
totalAmount -= discount; // 减去折扣
}
// 运费计算:假设运费是每磅0.5美元,超过5磅后,每磅增加0.1美元
if (totalWeight <= 5) {
shippingCost = totalWeight * 6.5;
}
else if (totalWeight > 5 && totalWeight <= 20) {
shippingCost = totalWeight * 14;
}
else {
shippingCost = 20 * 14 + (totalWeight - 20) * 0.5;
}
printf("总重量为:%.2lf磅\n", totalWeight);
printf("当前货物总额为:%.2lf美元\n", totalAmount);
printf("折扣为:%.2lf美元\n", discount);
printf("运费为:%.2lf美元\n", shippingCost);
printf("包装费为:%.2lf美元\n", packagingCost);
printf("总费用为:%.2lf美元\n", totalAmount + shippingCost + packagingCost);
printf("按 Enter 返回主菜单\n");
getchar(); // 暂停以便用户查看结果
getchar();
clearScreen(); // 清屏返回主菜单
return; // 退出子菜单,返回主菜单
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
}
// 选项5的功能 - 进入新的界面(子菜单)
void option5() {
option5Submenu(); // 进入选项5的子菜单
}
// 选项5的子菜单
void option5Submenu() {
int subChoice;
while (1) {
// 显示子菜单
printf("***************************************************************\n");
printf("Are you sure you want to quit?\n");
printf("\n1) 是");
printf("\t\t2) 否,返回主菜单\n");
printf("*****************************************************************\n");
printf("Enter your choice: ");
scanf("%d", &subChoice);
clearScreen(); // 清屏
switch (subChoice) {
case 1:
printf("Exiting... Done!\n");
exit(0); // 退出程序
case 2:
clearScreen(); // 清屏返回主菜单
return; // 退出子菜单,返回主菜单
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
}