7.12编程练习
/*
编写一个程序读取输入,读到#字符停止,
然后报告读取的空格数、换行数和其他所有字符的数量
*/
#include <stdio.h>
int main(void)
{
int sp_c = 0;
int nl_c = 0;
int ot_c = 0;
char ch = 0;
while ((ch=getchar()) != '#')
{
if (ch == ' ')
sp_c++;
else if (ch == '\n')
nl_c++;
else
ot_c++;
}
printf("%d space, %d newline, %d other \n ", sp_c, nl_c, ot_c);
return 0;
}
/*
编写一个程序读取输入,读到#字符停止。
程序要打印每个输入的字符以及对应的ASCII码(十进制),一行打印8个字符
建议:使用字符计数和求模运算符(%)在每8个循环周期时打印一个换行符
*/
#include <stdio.h>
int main(void)
{
char ch;
int count = 0;
while ((ch =getchar()) != '#')
{
count++;
printf(" Character: %c, ASCII code: %d", ch, ch);
if (count % 8 == 0)
printf("\n");
}
return 0;
}
/*
编写一个程序,读取整数直到用户输入0.
输入结束后,程序应该报告用户输入的偶数(不包括0)个数
以及这些偶数的平均值、输入的奇数以及奇数的平均值
*/
#include <stdio.h>
int main(void)
{
int num;
int even_c = 0;
int odd_c = 0;
int even_sum = 0;
int odd_sum = 0;
float even_aver;
float odd_aver;
while ( scanf("%d", num) && num != 0)
{
if (num % 2) //odd number
{
odd_c++;
odd_sum += num;
}
else
{
even_c++;
even_sum += num;
}
}
even_aver = (float)even_sum / even_c;
odd_aver = (float)odd_sum / odd_c;
if (even_c > 0)
printf("%d even numbers and average is %.3f \n", even_c, even_aver);
if (odd_c > 0)
printf("%d odd numbes and averag is %.3f \n", odd_c, odd_aver);
printf("Done! \n");
return 0;
}
这道题的意思是:输入一句话,打印出来的结果是将句子里的’.‘替换为’!’, '!'替换为"!!",不能理解错了= = !, 理解了就很简单了
注意,这里用的是英文字符,测试的时候一定要用英文来测试,否则会怀疑人生的!!
/*
使用if else语句编写一个程序读取输入,读到#停止
使用感叹号替换句号,用两个感叹号替换原来的感叹号
最后报告进行了多少次替换
*/
#include <stdio.h>
int main(void)
{
char ch;
int ct1 = 0;
int ct2 = 0;
while ((ch = getchar()) != '#')
{
if (ch == '.')
{
putchar('!');
ct1++;
}
else if (ch == '!')
{
putchar('!');
putchar('!');
ct2++;
}
else
putchar(ch);
}
printf("replace . %d times, replace ! %d times \n", ct1, ct2);
return 0;
}
注意,这里用的是英文字符,测试的时候一定要用英文来测试,否则会怀疑人生的!!
/*
使用switch重写上面的练习
*/
#include <stdio.h>
int main(void)
{
char ch;
int ct1 = 0;
int ct2 = 0;
while ((ch = getchar()) != '#')
{
switch (ch)
{
case '.' :
putchar('!');
ct1++;
break;
case '!' :
putchar('!');
putchar('!');
ct2++;
break;
default : putchar(ch);
}
}
printf("replace . %d times, replace ! %d times \n", ct1, ct2);
return 0;
}
/*
编写程序读取输入,读到 # 停止,报告出现 e i 的次数。
注意:该程序要记录一个字符和当前字符。
用"Receive your eieio award"这样的输入来测试
*/
#include <stdio.h>
int main(void)
{
char ch;
int count;
while ((ch = getchar()) != '#')
{
if (ch == 'i' || ch == 'e')
count++;
}
printf("Count : %d", count);
return 0;
}
// 吐槽一下,这题这么简单???还没上一题难
/*
编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入
做如下假设:
a.基本工资 = 1000美元 / 小时
b.加班(超过40小时) = 1.5倍时间
c.税率: 前300美元为15%
续150美元为20%
余下的为25%
用#define定义符号常量,不用在意是否符合当前的税法
*/
#include <stdio.h>
#define BASE 1000 //一小时的工资
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define TIME_BREAK 40
#define BREAK1 300
#define BREAK2 450
#define BASE1 BREAK1 * RATE1
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
int main(void)
{
float time;
float sum_money;
float tax;
float get_money;
printf("Please enter your work time: \n");
scanf("%f", &time);
while (time <= 0)
{
printf("invalid value please enter a positive number: \n");
scanf("%f", &time);
}
if (time <= TIME_BREAK)
{
sum_money = time * BASE;
}
else
{
sum_money = 40 * BASE + (time - TIME_BREAK) * 1.5 * BASE;
}
if (sum_money <= BREAK1)
{
tax = sum_money * RATE1;
}
else if (sum_money <= BREAK2)
{
tax = BASE1 + (sum_money - BREAK1) * RATE2;
}
else
tax = BASE2 + (sum_money - BREAK2) *RATE3;
get_money = sum_money - tax;
printf("Your salary is %.3f, and your tax is %.3f, finally you can get %.3f ", sum_money, tax, get_money);
return 0;
}
/*
修改练习7的假设a,让程序可以给出一个供选择的工资等级菜单
使用 switch 完成工资等级的选择。
运行程序后,显示的菜单应该类似这样:
*******************************************************************************
Enter the number corresponding to the desired pay rate of 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创建符号常量表示各工资等级和税率
*/
#include <stdio.h>
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define TIME_BREAK 40
#define BREAK1 300
#define BREAK2 450
#define BASE1 BREAK1 * RATE1
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
int main(void)
{
float time; //工作时间
float sum_money; //薪水
float tax; //税
float get_money; //最终得到的钱
int tag = 2; //初始化为2来控制第一次不打印 ERROR :Please enter a number 1~5
float BASE; //每小时的工资
do
{
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 (tag < 1 || tag > 5)
{
printf("ERROR :Please enter a number 1~5: ");
}
scanf("%d", &tag);
} while (tag < 1 || tag > 5);
while (tag != 5)
{
switch (tag) //根据输入来初始化每小时的工资
{
case 1 : BASE = 8.75;
break;
case 2 : BASE = 9.33;
break;
case 3 : BASE = 10.00;
break;
case 4 : BASE = 11.2;
}
printf("What is your working time? \n");
scanf("%f", &time);
while (time <= 0)
{
printf("invalid value please enter a positive number: \n");
scanf("%f", &time);
}
if (time > 0 && time <= TIME_BREAK)
{
sum_money = time * BASE;
}
else
{
sum_money = 40 * BASE + (time - TIME_BREAK) * 1.5 * BASE;
}
if (sum_money <= BREAK1)
{
tax = sum_money * RATE1;
}
else if (sum_money <= BREAK2)
{
tax = BASE1 + (sum_money - BREAK1) * RATE2;
}
else if (sum_money > 450)
tax = BASE2 + (sum_money - BREAK2) *RATE3;
get_money = sum_money - tax;
printf("Your salary is %.3f, and your tax is %.3f, finally you can get %.3f ", sum_money, tax, get_money);
printf("Choose another par rate tag now. \n");
scanf("%d", &tag);
}
printf("Done! \n");
return 0;
}
/*
编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数。
*/
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
unsigned int num;
unsigned int div;
bool isPrimer = true;
printf("Enter a number (q to quit ): \n");
while (scanf("%u", &num) == 1)
{
if (num <= 1)
printf("No primes.\n");
else
{
for (int i = 2; i <= num; i++)
{
isPrimer = true;
for (div = 2; div * div <= i; div++)
{
if (i % div == 0)
{
isPrimer = false;
}
}
if (isPrimer)
printf("%u is Primer \n", i);
}
}
printf("Enter a number (q to quit ): \n");
}
printf("Done! \n");
return 0;
}
此程序只能选择一次类别,进行此类别的多次计算,思考如何可以让用户再次选择类别进行多次计算?
/*
1988年的美国联邦税收计划是近代最简单的税收方案。
它分为4个类别,每个类别有两个等级。
下面是税收计划的摘要(美元数为应征税的收入):
类别 税金
单身 17850美元按15%计,超出部分按28%计
户主 23900美元按15%计,超出部分按28%计
已婚,共有 29750美元按15%计,超出部分按28%计
已婚,离异 14875美元按15%计,超出部分按28%计
例如:一位工资为20000美元的单身纳税人,应缴纳税费 0.15x17850+0.28x(20000-17850)美元。
编写一个程序,让用户指定缴纳税金的种类和应纳税收入,然后计算税金。
程序应通过循环让用户可以多次输入。
*/
#include <stdio.h>
#define RATE1 0.15
#define RATE2 0.28
#define BREAK1 17850
#define BREAK2 23900
#define BREAK3 29750
#define BREAK4 14875
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BREAK2 * RATE1)
#define BASE3 (BREAK3 * RATE1)
#define BASE4 (BREAK4 * RATE1)
int main(void)
{
float f_money;
float tax;
int tag;
float l_money;
float co_base;
float co_break;
printf(" 类别 税金 \n");
printf("1:单身 17850美元按15%计,超出部分按28%计 \n");
printf("2:户主 23900美元按15%计,超出部分按28%计 \n");
printf("3:已婚,共有 29750美元按15%计,超出部分按28%计 \n");
printf("4:已婚,离异 14875美元按15%计,超出部分按28%计 \n");
printf("请根据上述选项输入你目前的身份 (q to quit):\n");
while ((scanf("%d", &tag)) == 1)
{
if (tag < 1 || tag > 4)
{
printf("请输入一个1-5的数字");
continue;
}
switch (tag)
{
case 1 : co_base = BASE1;
co_break = BREAK1;
break;
case 2 : co_base = BASE2;
co_break = BREAK2;
break;
case 3 : co_base = BASE3;
co_break = BREAK3;
break;
case 4 : co_base = BASE4;
co_break = BREAK4;
break;
}
printf("请输入你的资产:");
while ((scanf("%f", &f_money)) == 1)
{
if (f_money <= co_base )
{
tax = f_money * RATE1;
}
else
tax = co_base + (f_money - co_break) * RATE2;
l_money = f_money - tax;
printf("That is your last money: %.3f \n", l_money);
printf("请输入你的资产:");
}
printf("Error! \n");
}
printf("Done!!! \n");
return 0;
}
/*
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磅的甜菜。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装费。
随后,程序显示所有的购买信息:物品售价、订购的重量(单位:磅)、订购的蔬菜费用、
订单的总费用、折扣(如果有的话)、运费和包装费,以及所有的费用总额。
*/
#include <stdio.h>
#define PRICE_A 2.05 //Artichoke
#define PRICE_B 1.15 //beet
#define PRICE_C 1.09 //carrot
#define OVER_100_DISCOUNT 0.05 //打折率
#define PACKAGE_1 6.5 //打包费
#define PACKAGE_2 14
#define PACKAGE_ADD 0.5
#define P_BREAK1 5
#define P_BREAK2 20
int main(void)
{
float temp_pounds;
float pounds_a = 0.0f;
float pounds_b = 0.0f;
float pounds_c = 0.0f;
float price_a;
float price_b;
float price_c;
float sum_price;
float sum_pounds;
float discount;
float transport;
float package;
char s_tag; //switch判断
char ch; //用来清空输入
printf("Please enter the quantity of ingredients"
"you want(q to quit): \n");
printf("Choose the corrrespond tag \n"
" a)Artichoke b)Beet \n c)Carrot q) quit: \n");
do
{
printf("Which ingredients would you like; ");
scanf("%c", &s_tag);
while ((ch = getchar()) != '\n') //清空缓冲区剩余字符
continue;
while (s_tag < 'a' || s_tag > 'c' && s_tag != 'q') //如果输入的不是a,b,c,q中的一个,提示用户继续输入
{
printf("Invalid choice try again: ");
scanf("%c", &s_tag);
while ((ch = getchar()) != '\n')
continue;
}
if (s_tag == 'q') //输入q退出大循环
break;
switch (s_tag)
{
case 'a' :
printf("Enter pounds of artichokes: ");
scanf("%f", &temp_pounds);
getchar();
pounds_a += temp_pounds;
break;
case 'b' :
printf("Enter pounds of beet: ");
scanf("%f", &temp_pounds);
getchar();
pounds_b += temp_pounds;
break;
case 'c' :
printf("Enter pounds of carrot: ");
scanf("%f", &temp_pounds);
getchar();
pounds_c += temp_pounds;
break;
}
printf("You bought: %.2f pounds of Artichoke, %.2f pounds of Beet, %.2f of Carrot \n",
pounds_a, pounds_b, pounds_c);
} while (s_tag != 'q');
price_a = pounds_a * PRICE_A; //计算artichoke的价格
price_b = pounds_b * PRICE_B;
price_c = pounds_c * PRICE_C;
sum_price = price_a + price_b + price_c;
sum_pounds = pounds_a + pounds_b + pounds_c; //总磅数
if (sum_price > 100)
{
discount = sum_price * OVER_100_DISCOUNT;
}
if (sum_pounds <= P_BREAK1)
{
package = PACKAGE_1;
}
else if (sum_price <= P_BREAK2)
{
package = PACKAGE_2;
}
else
{
package = PACKAGE_2 + (sum_pounds - P_BREAK2) * PACKAGE_ADD;
}
sum_price += package - discount;
printf("***********************************************************************\n");
printf("Artichoke $%.2f/pound, Beet $%.2f/pound, Carrot $%.2f/pound \n",
PRICE_A, PRICE_B, PRICE_C);
printf("You bought:Artichoke : %.2f pounds, Beet : %.2f pounds, Carrot:%.2f pounds \n",
pounds_a, pounds_b, pounds_c);
printf("You cost:Artichoke $%.2f, Beet $%.2f Carrot $%.2f \n",
price_a, price_b, price_c);
printf("Total cost of vegetables is %.2f \n", sum_price);
if (discount)
printf("Discount : $%.2f \n", discount);
printf("Shipping and packaging costs : $%.2f \n", package);
printf("You final cost $%.2f \n", sum_price);
printf("***********************************************************************\n");
return 0;
}