1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数、换行符数和所有其他字符的数量。
#include <stdio.h>
#include <ctype.h>
int main(void ){
char ch = 0 ;
int spaces = 0;
int lines = 0;
int charactors = 0;
while((ch = getchar()) != '#'){
if(ch == ' '){
spaces++;
}
else if(ch == '\n'){
lines++;
}
else if (!isspace(ch)){
charactors++;
}
}
printf("have %d charactors\n",charactors);
printf("have %d lines\n",lines);
printf("have %d spaces\n",spaces);
return 0;
}
2.编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字符以及对应的ASCII码(十进制)。一行打印8个字符。建议:使用字符计数和求模运算符(%)在每8个循环周期时打印一个换行符。
#include <stdio.h>
#include <ctype.h>
int main(void ){
int count = 0;
char ch = 0 ;
int charactors = 0;
while ((ch=getchar()) != '#'){
printf("%d:%c ",ch,ch);
count++;
if(count%8==0){
printf("\n");
}
}
}
3.编写一个程序,读取整数直到用户输入 0。输入结束后,程序应报告用户输入的偶数(不包括 0)个数、这些偶数的平均值、输入的奇数个数及其奇数的平均值。
#include <stdio.h>
#include <ctype.h>
int main(void ){
int doublecount = 0;
int alldbcount = 0;
int singlecount = 0;
int allsigcount = 0;
int ch = 0 ;
int charactors = 0;
printf("Enter your numbers end of 0:");
while ((scanf("%d",&ch))&&ch!=0){
if(ch%2==0){
doublecount++;
alldbcount = alldbcount + ch;
}else{
singlecount++;
allsigcount =allsigcount+ch;
}
}
printf("count even:%d,svgeven: %.2f\n",doublecount,1.0*alldbcount/doublecount);
printf("count odd:%d,svg odd :%.2f",singlecount,1.0*allsigcount/singlecount);
}
4.使用if else语句编写一个程序读取输入,读到#停止。用感叹号替换句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换。
#include <stdio.h>
#include <ctype.h>
int main(void ){
char ch ;
int count = 0;
while((ch=getchar())!='#'){
if(ch=='.'){
ch = '!';
count++;
}
else if (ch=='!'){
putchar(ch);
putchar(ch);
count++;
}
}
printf("%d",count);
}
5.使用switch重写练习4
#include <stdio.h>
#include <ctype.h>
int main(void ){
char ch ;
int count = 0;
while((ch=getchar())!='#'){
switch (ch) {
case '.':
ch = '!';
count++;
break;
case '!':
putchar(ch);
putchar(ch);
count++;
break;
}
}
printf("%d",count);
}
6.编写程序读取输入,读到#停止,报告ei出现的次数
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch;
int count = 0, f = 0;
while((ch = getchar()) != '#')
{
switch(ch)
{
case 'e':
f = 1;
break;
case 'i':
if(f)
{
count++;
f = 0;
}
break;
default:
break;
}
}
printf("\n%d\n", count);
return 0;
}
7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入。做如下假设: a.基本工资 = 10.00美元/小时 b.加班(超过40小时) = 1.5倍的时间 c.税率: 前300美元为15%,续150美元为20%,余下的为25% 用#define定义符号常量。不用在意是否符合当前的税法。
#include <stdio.h>
#include <stdlib.h>
#define BASIC 10.00
#define OVER 1.5
#define rate1 0.15
#define rate2 0.2
#define rate3 0.25
int main(void)
{
int hour,allmoney;
float ratemoney,realmoney;
printf("Enter your work hour a week:");
scanf("%d",&hour);
if(hour>40){
hour = hour+ (hour-40)*OVER;
}
allmoney = hour*BASIC;
if (allmoney<=300) {
ratemoney = allmoney*rate1;
realmoney = allmoney-ratemoney;
}
else if(300<allmoney && allmoney<450){
ratemoney = 300*rate1+(allmoney-300)*rate2;
realmoney = allmoney-ratemoney;
}
else{
ratemoney = 300*rate1 + 150*rate2 + (allmoney-300-15)*rate3;
realmoney = allmoney-ratemoney;
}
printf("%d %.2f %.2f",allmoney,ratemoney,realmoney);
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 创建符号常量表示各工资等级和税率。
#include <stdio.h>
#include <stdlib.h>
#define salary1 8.75
#define salary2 9.33
#define salary3 10.00
#define salary4 11.20
#define OVER 1.5
#define rate1 0.15
#define rate2 0.2
#define rate3 0.25
int main(void)
{
int hour,allmoney,num;
float ratemoney,realmoney;
printf("Enter the number corresponding to the desired pay rate or action::\n");
printf("===================================\n");
printf("1) $8.75/hr\t");
printf("2) $9.33/hr\n");
printf("3) $10.00/hr\t");
printf("4) $11.20/hr\n");
printf("5) quit\n");
printf("===================================\n");
while(scanf("%d",&num)&& num!=5){
if(num>5 || num <1){
printf("your input is not valid!");
}
printf("Enter your work hour:");
scanf("%d",&hour);
if(hour>40){
hour = hour + (hour-40)*OVER;
}
switch (num) {
case 1:
allmoney = hour*salary1;
break;
case 2:
allmoney = hour*salary2;
break;
case 3:
allmoney = hour*salary3;
break;
case 4:
allmoney = hour*salary4;
break;
}
if (allmoney <= 300) {
ratemoney = allmoney * rate1;
}
else if (300 < allmoney && allmoney < 450) {
ratemoney = 300 * rate1 + (allmoney - 300) * rate2;
}
else {
ratemoney = 300 * rate1 + 150 * rate2 + (allmoney - 300 - 15) * rate3;
}
realmoney = allmoney - ratemoney;
printf("%d %.2f %.2f\n",allmoney,ratemoney,realmoney);
printf(" \n");
printf("Enter the number corresponding to the desired pay rate or action::\n");
printf("===================================\n");
printf("1) $8.75/hr\t");
printf("2) $9.33/hr\n");
printf("3) $10.00/hr\t");
printf("4) $11.20/hr\n");
printf("5) quit\n");
printf("===================================\n");
}
return 0;
}
9.编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数。
#include <stdio.h>
int main(void ){
int num;
int sign=0;
printf("Enter a positive integer:");
while(scanf("%d",&num)&&num>0){
for (int i = 2; i <= num; i++) { //因为是遍历1-num所有的素数,所以要对num遍历
for (int j = 2; j <= i; j++) { //j不可以为1,否则sign恒为1
if(i%j==0 && j!=i){
sign =1;
break;
}
}
if(sign==0){
printf("%d\n",i);
}
sign = 0;
}
printf("Enter a positive integer:");
}
return 0;
}
10.1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类别,每个类别有两个等级。 下面是该税收计划的摘要(美元数为应征税的收入):
类别 | 税金 |
---|---|
单身 | 17850美金按15%计,超出部分按28%计 |
户主 | 23900美元按15%计,超出部分按28%计 |
已婚,共有 | 29750美元按15%计,超出部分按28%计 |
已婚,离异 | 14875美元按15%计,超出部分按28%计 |
例如,一位工资为20000美元的单身纳税人,应缴纳税费0.15×17850+0.28×(20000−17850)美元。编写一个程序,让用户指定缴纳税金的种类和应纳税收入,然后计算税金。程序应通过循环让用户可以多次 输入。
#include <stdio.h>
#define A 17850
#define B 23900
#define C 29750
#define D 14875
#define pre 0.15
#define over 0.28
int main(void ){
float kind;
int num;
int money;
float ratemoney;
printf("what kind of your state?\n");
printf("kind\tratemoney\n");
printf("1)danshen\t17850-0.15 over-0,.28\n");
printf("2)huzhu\t23900-0.15 over-0.28\n");
printf("3)yihun,gy\t29750-0.15 over-0.28\n");
printf("4)yihun,ly\t14875-0.15 over-0.28\n");
while((scanf("%d",&num))&&num>0&&num<5){
switch (num) {
case 1:
kind = A;
break;
case 2:
kind = B;
break;
case 3:
kind = C;
break;
case 4:
kind = D;
break;
}
printf("%.2f\n",kind);
printf("Enter your money:\n");
scanf("%d",&money);
if(money>kind){
ratemoney = kind*pre+(money-kind)*over;
printf("%.f",ratemoney);
} else{
ratemoney = money*pre;
}
printf("your money is %d\n",money);
printf("your rate money is %.2f\n",ratemoney);
printf("what kind of your state?\n");
}
return 0;
}
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磅的甜菜。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装费。随后,程序应显示所有的购买信息:物品售价、订购的重量(单位:磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装费,以及所有的费用总额。
#include <stdio.h>
#define Artichoke 2.05
#define Beet 1.15
#define Carrot 1.09
void price(){
printf("welcome to ABC Store\n");
printf("Today vegetable's price\n");
printf("num\t\tkind\t\tprice\n");
printf("1)\t\tArtichoke\t$2.05\n");
printf("2)\t\tBeet\t\t$1.15\n");
printf("3)\t\tCarrot\t\t$1.09\n");
printf("Enter \"q\" to quit the program.\n");
}
int main(void ){
int num;
int x = 1;
float kind;
int pound,allpound;
float allmoney,pre_allmoney;
int kind1=0,kind2=0,kind3=0;
float packet;
int goornot;
while(x){ //订单
price();
printf("Enter which you want num:\n");
while (scanf("%d",&num)&&num!='q'){ //选择菜种类
switch (num) {
case 1:
kind = Artichoke;
printf("How many Artichoke do you want to buy:\n");
scanf("%d",£);
kind1 += pound;
break;
case 2:
kind = Beet;
printf("How many Beef do you want to buy:\n");
scanf("%d",£);
kind2 += pound;
break;
case 3:
kind = Carrot;
printf("How many Carrot do you want to buy:\n");
scanf("%d",£);
kind3 += pound;
break;
}
printf("Do you want to continue shopping?\n");
printf("1)YES\t\t2)NO\n");
scanf("%d",&goornot);
if(goornot==2){
break;
}
printf("Enter which you want num:\n");
price();
}
allpound = kind1 + kind2 + kind3;
if(allpound<=5){
packet = 6.5;
}
else if(allpound>5&&allpound<=20){
packet = 14;
}
else if(allpound>20){
packet = 14 + (allpound-14)*0.5;
}
pre_allmoney = Artichoke*kind1 + Beet*kind2 + Carrot*kind3;
if(pre_allmoney>=100){
pre_allmoney = pre_allmoney*0.95;
}
allmoney = pre_allmoney + packet;
printf("======================");
printf("Shopping List\n");
printf("kind\t\tpound\t\tTotal\n");
printf("Artichoke\t%d\t\t%.2f\n",kind1,kind1*Artichoke);
printf("Beet\t\t%d\t\t%.2f\n",kind2,kind2*Beet);
printf("Carrot\t\t%d\t\t%.2f\n",kind3,kind3*Carrot);
printf("Total order\t%d\t\t%.2f\n",allpound,pre_allmoney);
if(pre_allmoney>=100){
printf("Total dst\t%.2f\n",pre_allmoney*0.05);
}
printf("package\t\t%d\t\t%.2f\n",allpound,packet);
printf("Total\t\t%d\t\t%.2f\n",allpound,allmoney);
printf("======================");
break;
}
printf("looking forward to your next visit!\n");
return 0;
}