7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金、净收入。做如下假设:
a.基本工资 = 100美元/小时
b.加班(超40小时) = 1.5倍的时间
c.税率:前300美元为15%
续150美元为20%
余下的为25%
用#define定义符号常量。不用在意是否符合当前的税法。
int main()
{
system("chcp 65001");
printf("请输入一周工作的小时数:");
int hours;
scanf_s("%d", &hours);
double salary = 10 * (hours > 40 ? hours * 1.5 - 20 : hours);
printf("工资:%f\n", salary);
double tax = salary <= 300 ? salary * 0.15 : salary <= 450 ? salary * 0.2 - 15 : salary * 0.25 - 37.5;
printf("税金:%f\n", tax);
printf("净收入:%f\n", salary - tax);
return 0;
}
8.修改7的假设a,让程序可以给出一个供选择的工资等级菜单。使用switch完成工资等级选择。运行程序后,现实的菜单应该类似这样:
*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr 2) $9.33/hr
3) $10.00/hr 4) $11.20/hr
5) quit
****************************************************************
如果选择1~4其中一个数字,程序应该询问用户输入工作小时数。程序要通过循环运行,除非用户输入5.如果输入1~5以外的数字,程序应该提醒用户输入正确的选项,然后重新显示菜单提示用户输入。使用#define定义符号常量表示各工资等级和税率。
#define PAY_RATE_1 8.75
#define PAY_RATE_2 9.33
#define PAY_RATE_3 10.00
#define PAY_RATE_4 11.20
int main()
{
system("chcp 65001");
while (1) {
int choice;
double pay_rate;
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr 2) $9.33/hr\n");
printf("3) $10.00/hr 4) $11.20/hr\n");
printf("5) quit\n");
printf("*****************************************************************\n");
if (scanf_s("%d", &choice) == 1)
{
switch (choice) {
case 1:
pay_rate = PAY_RATE_1;
break;
case 2:
pay_rate = PAY_RATE_2;
break;
case 3:
pay_rate = PAY_RATE_3;
break;
case 4:
pay_rate = PAY_RATE_4;
break;
case 5:
return 0;
default:
continue;
}
printf("请输入工作小时数:");
int hours;
scanf_s("%d", &hours);
double salary = pay_rate * (hours > 40 ? hours * 1.5 - 20 : hours);
printf("工资:%f\n", salary);
double tax = salary <= 300 ? salary * 0.15 : salary <= 450 ? salary * 0.2 - 15 : salary * 0.25 - 37.5;
printf("税金:%f\n", tax);
printf("净收入:%f\n", salary - tax);
continue;
}
printf("请输入正确的选项!\n");
}
}
9.编写一个程序,只接受正整数输入,然后现实所有小于或等于该数的素数。
int main()
{
system("chcp 65001");
printf("请输入正整数:");
int n;
while (scanf_s("%d", &n) == 1 && n > 0) {
printf("小于或等于%d的素数有:\n", n);
int count = 0;
if (n == 2)
{
printf("2\n");
}
else if (n > 2)
{
printf("%10d ", 2);
count++;
for (int i = 3; i <= n; i += 2) {
int flag = 1;
for (int j = 3; j < i; j += 2) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag) {
printf("%10d ", i);
count++;
if (count % 5 == 0)
{
printf("\n");
}
}
}
printf("\n");
}
printf("请输入正整数:");
}
}
10.1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类别,每个类别有两个等级。下面是该税收的计划和摘要(美元数为应征收税的收入):
-------------------------------------------------------------------------------------
类别 |税金
-------------------------------------------------------------------------------------
单身 |17850美元按15%计,超出部分按28%计
-------------------------------------------------------------------------------------
户主 |23900美元按15%计,超出部分按28%计
-------------------------------------------------------------------------------------
已婚,共有 |29750美元按15%计,超出部分按28%计
-------------------------------------------------------------------------------------
已婚,离异 |14875美元按15%计,超出部分按28%计
-------------------------------------------------------------------------------------
例如,一位工资为20000美元的单身纳税人,应缴纳税费0.15 X 18750 + 0.28 * (20000 - 18750)美元。
编写一个程序,让用户指定缴纳税金的种类和应纳税收入,然后计算税金。程序通过循环让用户可以多次输入。
int main()
{
system("chcp 65001");
while (1)
{
printf("*****************************************************************\n");
printf("请选择纳税种类:\n");
printf("1) 单身 2) 户主\n");
printf("3) 已婚,共有 4) 已婚,离异\n");
printf("5) 退出\n");
printf("*****************************************************************\n");
int choice;
if (scanf_s("%d", &choice) == 1)
{
int tax_base = 0;
switch (choice)
{
case 1:
tax_base = 17850;
break;
case 2:
tax_base = 23900;
break;
case 3:
tax_base = 29750;
break;
case 4:
tax_base = 14875;
break;
case 5:
return 0;
}
printf("请输入纳税收入:");
unsigned int income;
if (scanf_s("%u", &income) == 1)
{
double tax = income > tax_base ? (income - tax_base) * 0.28 + tax_base * 0.15 : income * 0.15;
printf("税金:%f\n", tax);
}
}
printf("请输入正确的选项!\n");
}
}
11.ABC邮购杂货店出售的洋蓟售价为2.05美元/磅,甜菜售价为1.15美元/磅,胡萝卜售价为1.09美元/磅。在添加运费之前,100美元的订单有5%的打折优惠。少于或等于5磅的订单收取6.5美元的运费和包装费,5磅~20磅的订单收取14美元的远非和包装费,超过20磅的订单在14美元的基础上每续重1磅增加0.5美元。编写一个程序,在循环中用switch语句实现用户输入不同的字母时有不同的响应,即输入a响应的时让用户输入洋蓟的磅数,b是甜菜的磅数,c是胡萝卜的磅数,q是退出订购。程序要记录累计的重量。即,如果用户输入4磅的甜菜,然后输入5磅的甜菜,程序应报告9磅的甜菜。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装费。随后,程序应显示所有的购买信息:物品售价、订购的重量(单位:磅)、订购的蔬菜费、订单的总费用、折扣(如果有的话)、运费和包装费,以及所有的费用总额。11
int main() {
system("chcp 65001");
// 常量定义
const double ARTICHOKE_PRICE = 2.05;
const double BEET_PRICE = 1.15;
const double CARROT_PRICE = 1.09;
const double DISCOUNT_THRESHOLD = 100.0;
const double DISCOUNT_RATE = 0.05;
// 蔬菜重量
double artichoke_weight = 0.0;
double beet_weight = 0.0;
double carrot_weight = 0.0;
char choice;
printf("欢迎来到ABC邮购杂货店订购系统!\n");
printf("可选蔬菜: a-洋蓟($%.2f/磅), b-甜菜($%.2f/磅), c-胡萝卜($%.2f/磅), q-退出\n",
ARTICHOKE_PRICE, BEET_PRICE, CARROT_PRICE);
while (1) {
printf("\n请输入选择 (a/b/c/q): ");
scanf_s(" %c", &choice, 1); // 空格跳过空白字符
switch (choice) {
case 'a':
case 'A': {
double weight;
printf("请输入洋蓟的磅数: ");
scanf_s("%lf", &weight);
artichoke_weight += weight;
printf("已添加 %.2f 磅洋蓟,当前总计: %.2f 磅\n", weight, artichoke_weight);
break;
}
case 'b':
case 'B': {
double weight;
printf("请输入甜菜的磅数: ");
scanf_s("%lf", &weight);
beet_weight += weight;
printf("已添加 %.2f 磅甜菜,当前总计: %.2f 磅\n", weight, beet_weight);
break;
}
case 'c':
case 'C': {
double weight;
printf("请输入胡萝卜的磅数: ");
scanf_s("%lf", &weight);
carrot_weight += weight;
printf("已添加 %.2f 磅胡萝卜,当前总计: %.2f 磅\n", weight, carrot_weight);
break;
}
case 'q':
case 'Q':
goto calculate; // 退出循环
default:
printf("无效输入,请重新选择!\n");
}
}
calculate:
// 计算总重量
double total_weight = artichoke_weight + beet_weight + carrot_weight;
// 计算蔬菜费用
double artichoke_cost = artichoke_weight * ARTICHOKE_PRICE;
double beet_cost = beet_weight * BEET_PRICE;
double carrot_cost = carrot_weight * CARROT_PRICE;
double total_veggie_cost = artichoke_cost + beet_cost + carrot_cost;
// 计算折扣
double discount = 0.0;
if (total_veggie_cost >= DISCOUNT_THRESHOLD) {
discount = total_veggie_cost * DISCOUNT_RATE;
}
double discounted_cost = total_veggie_cost - discount;
// 计算运费
double shipping_cost;
if (total_weight <= 0)
{
shipping_cost = 0.0;
}
else if (total_weight <= 5.0) {
shipping_cost = 6.5;
}
else if (total_weight <= 20.0) {
shipping_cost = 14.0;
}
else {
shipping_cost = 14.0 + (total_weight - 20.0) * 0.5;
}
// 计算总费用
double total_cost = discounted_cost + shipping_cost;
// 打印订购摘要
printf("\n==================================================\n");
printf(" 订购摘要 \n");
printf("==================================================\n");
printf("%16s%14s%15s%14s\n", "蔬菜种类", "单价($)", "重量(磅)", "费用($)");
printf("--------------------------------------------------\n");
printf("%14s%12.2f%12.2f%12.2f\n", "洋蓟", ARTICHOKE_PRICE, artichoke_weight, artichoke_cost);
printf("%14s%12.2f%12.2f%12.2f\n", "甜菜", BEET_PRICE, beet_weight, beet_cost);
printf("%15s%12.2f%12.2f%12.2f\n", "胡萝卜", CARROT_PRICE, carrot_weight, carrot_cost);
printf("--------------------------------------------------\n");
printf("%-32s%21.2f\n", "蔬菜总费用:", total_veggie_cost);
printf("%-29s%21.2f\n", "折扣:", discount);
printf("%-32s%21.2f\n", "折扣后费用:", discounted_cost);
printf("%-33s%21.2f\n", "运费和包装费:", shipping_cost);
printf("==================================================\n");
printf("%-30s%21.2f\n", "总费用:", total_cost);
printf("==================================================\n");
return 0;
}