C primer plus课后习题 第七章

1.编写一个程序读取输入, 读到#字符停止, 然后报告读取的空格数、 换行符数和所有其他字符的数量。

#include <stdio.h>
#define STOP '#'

int main (){
	char ch ; 
	int num_spaces = 0 ;
	int num_lines = 0 ;
	int num_other = 0 ;
	 
	while((ch=getchar())!=STOP){
		if(ch==' ')
			num_spaces++ ;
		else if(ch=='\n')
			num_lines++ ;
		else
			num_other++ ;
	};
	
	printf("spaces:%d,linefeed:%d,other chars:%d \n",
			num_spaces,num_lines,num_other) ;
			
	return 0;
}

2.编写一个程序读取输入, 读到#字符停止。 程序要打印每个输入的字 符以及对应的ASCII码(十进制) 。 一行打印8个字符。 建议:使用字符计数 和求模运算符(%) 在每8个循环周期时打印一个换行符。

#include <stdio.h>
#define STOP '#'

int main (){
	char ch ;  
	int char_nums=0 ;  
	printf("Pleast input :\n");
	while((ch=getchar())!=STOP){
		
		printf("%c:%-3d ",ch,ch)  ;
		if(++char_nums%8==0)
			printf("\n") ;
	};
	
	return 0;
}

3.编写一个程序, 读取整数直到用户输入 0。 输入结束后, 程序应报告 用户输入的偶数(不包括 0) 个数、 这些偶数的平均值、 输入的奇数个数及 其奇数的平均值。

int main (){
	int num ; 
	int evennums = 0 ; //偶数个数 
	int oddnums = 0 ; //奇数个数
	int sum_evennums = 0 ; //偶数总和 
	int sum_oddnums = 0 ; //奇数总和
	 
	while( scanf("%d",&num)==1 ){
		if(num==0)
			break ;
			
		if(num%2==0){
			evennums++ ;
			sum_evennums+=num ;
		} 
		
		else {
			oddnums++ ;
			sum_oddnums+=num ;
		}
			 
	};
	
	printf("evennums:%d,avg of evennumber : %.2f ;oddnums:%d,avg of sum_oddnums : %.2f \n",
			evennums,(evennums==0)?0:sum_evennums*1.0/evennums,
			oddnums,(oddnums==0)?0:sum_oddnums*1.0/oddnums) ;
			
	return 0;
}

4.使用if else语句编写一个程序读取输入, 读到#停止。 用感叹号替换句号, 用两个感叹号替换原来的感叹号, 最后报告进行了多少次替换。

#include <stdio.h> 
#define STOP '#'

int main (){
	char ch ;  
	int replace_count=0; 
	 
	printf("Please input :\n");
	
	while((ch=getchar())!=STOP){ 
		
		if(ch=='.'){
			putchar('!') ; 
			replace_count++ ;
		}
		else if(ch=='!'){
			putchar('!') ;
			putchar('!') ;
			replace_count++ ;
		}
	};
	printf("Replace count :%d \n",replace_count);
	return 0;
}

5.使用switch重写练习4。

#include <stdio.h>
#define STOP '#'

int main (){
    char ch ;
    int replace_count=0;

    printf("Please input :\n");

    while((ch=getchar())!=STOP){
        switch(ch){
            case '.':
                putchar('!') ;
                replace_count++ ;
                break ;
            case '!' :
                putchar('!') ;
                putchar('!') ;
                replace_count++ ;
                break ;
        }

    };
    printf("Replace count :%d \n",replace_count);
    return 0;
}

6.编写程序读取输入, 读到#停止, 报告ei出现的次数。 注意 该程序要记录前一个字符和当前字符。 用“Receive your eieio award”这 样的输入来测试。

#include <stdio.h>
#define STOP '#'

int main (){
    char cur_char,pre_ch=' ' ;
    int ei_count=0,all_count=0;

    printf("Please input :\n");
    while((cur_char=getchar())!=STOP){
        if(all_count>0&&pre_ch=='e'){
            if(cur_char=='i'){
                ei_count++ ;
            }
        }
        pre_ch=cur_char ;
        all_count++ ;
    }
    printf("ei count :%d,all count :%d \n",ei_count,all_count);
    return 0;
}

7.编写一个程序, 提示用户输入一周工作的小时数, 然后打印工资总 额、 税金和净收入。 做如下假设: a.基本工资 = 10.00美元/小时 b.加班(超过40小时) = 1.5倍的时间 c.税率: 前300美元为15% 续150美元为20% 494余下的为25% 用#define定义符号常量。 不用在意是否符合当前的税法。

#include <stdio.h>
#define SALARY_PER_HOUR 10.00
#define WORK_HOURS_PER_WEEK 40
#define ORVERTIME 1.5
#define TAXRATE1 0.15
#define TAXRATE2 0.20
#define TAXRATE3 0.25
#define TAXBREAK1 300
#define TAXBREAK2 150
int main (){
    float total_salary=0,taxation=0,net_income=0;
    float work_hours ;
    printf("Please enter the working hours of this week :");

    if(scanf("%f",&work_hours)==1 && work_hours >0){

        //计算总收入
        if(work_hours>WORK_HOURS_PER_WEEK){
            total_salary=((work_hours-WORK_HOURS_PER_WEEK)*ORVERTIME
                    +WORK_HOURS_PER_WEEK)*SALARY_PER_HOUR ;//加班时间按1.5倍计算
        } else{
            total_salary=work_hours*SALARY_PER_HOUR ;//低于40小时工作时长直接计算薪资
        }

        //计算税收
        if(total_salary<=TAXBREAK1){ //前300美元为15%
            taxation=total_salary*TAXRATE1 ;
        }else if(total_salary<=TAXBREAK1+TAXBREAK2){ //续150美元为20%
            taxation=TAXBREAK1*TAXRATE1+(total_salary-TAXBREAK1)*TAXRATE2 ;
        }else{ //余下的为25%
            taxation=TAXBREAK1*TAXRATE1+TAXBREAK2*TAXRATE2+
                    (total_salary-TAXBREAK1-TAXBREAK2)*TAXRATE3 ;
        }

        net_income=total_salary-taxation ; //净收入
    }
    printf("total_salary=%.4f,taxation=%.4f,net_income=%.4f \n",
            total_salary,taxation,net_income);
    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>
#define WORK_HOURS_PER_WEEK 40
#define ORVERTIME 1.5
#define TAXRATE1 0.15
#define TAXRATE2 0.20
#define TAXRATE3 0.25
#define TAXBREAK1 300
#define TAXBREAK2 150
int main (){
    float total_salary=0,taxation=0,net_income=0;
    float work_hours ;
    int salary_id  ;
    float  SALARY_PER_HOUR ;

    while(1){
        printf("Enter the number corresponding to the desired pay rate or action:\n"
               "1) $8.75/hr      2) $9.33/hr\n"
               "3) $10.00/hr     4) $11.20/hr\n"
               "5) quit :\n");

        if (scanf("%d",&salary_id)==1 && salary_id>=1 && salary_id<=5 ){
            switch (salary_id){
                case 1:
                    SALARY_PER_HOUR=8.75 ;
                    break ;
                case 2:
                    SALARY_PER_HOUR=9.33 ;
                    break ;
                case 3:
                    SALARY_PER_HOUR=10.00 ;
                    break ;
                case 4:
                    SALARY_PER_HOUR=11.20 ;
                    break ;
                case 5:
                    printf("Exit!") ;
                    break ;
            }//end switch
        }else{
            printf("Input error,Please input agin. \n") ;
            continue ;
        }

        if(salary_id==5)
            break ;

        printf("Please enter the working hours of this week :");
        if(scanf("%f",&work_hours)==1 && work_hours >0){

            //计算总收入
            if(work_hours>WORK_HOURS_PER_WEEK){
                total_salary=((work_hours-WORK_HOURS_PER_WEEK)*ORVERTIME
                        +WORK_HOURS_PER_WEEK)*SALARY_PER_HOUR ;//加班时间按1.5倍计算
            } else{
                total_salary=work_hours*SALARY_PER_HOUR ;//低于40小时工作时长直接计算薪资
            }

            //计算税收
            if(total_salary<=TAXBREAK1){ //前300美元为15%
                taxation=total_salary*TAXRATE1 ;
            }else if(total_salary<=TAXBREAK1+TAXBREAK2){ //续150美元为20%
                taxation=TAXBREAK1*TAXRATE1+(total_salary-TAXBREAK1)*TAXRATE2 ;
            }else{ //余下的为25%
                taxation=TAXBREAK1*TAXRATE1+TAXBREAK2*TAXRATE2+
                        (total_salary-TAXBREAK1-TAXBREAK2)*TAXRATE3 ;
            }

            net_income=total_salary-taxation ; //净收入
        }
        printf("total_salary=%.4f,taxation=%.4f,net_income=%.4f \n\n",
                total_salary,taxation,net_income);
        }
        total_salary=0,taxation=0,net_income=0 ;
    return 0;
}

9.编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数。

#include <stdio.h> 
#include <ctype.h>

int main(void){  
	int n,i,j ;
	int flag ;
	
	printf("请输入一个正整数:\n") ;
	
	if(scanf("%d",&n)==1 && n>0) {
		printf("小于等于%d的所有质数为:",n) ;
		for (i=2;i<=n;i++){
			for (flag=1,j=2;j<i;j++){
				if(i%j==0){
					flag=0 ;
					break ;
					
				}
			} 
			if (flag==1){
				printf("%d ",i) ;
			}
		}
	}else{
		printf("只能输入正整数!") ;
	}
}

10.1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类 别,每个类别有两个等级。 下面是该税收计划的摘要(美元数为应征税的收入): 例如,一位工资为20000美元的单身纳税人,应缴纳税费
0.15×17850+0.28×(20000−17850)美元。编写一个程序,让用户指定缴纳 税金的种类和应纳税收入,然后计算税金。程序应通过循环让用户可以多次 输入。

#include <stdio.h>  

int main(void){  
	int type,grad ;
	float income,taxes ; 
	
	while(1){
		printf("Please select,\n \
		(1):single \n \ 
		(2):head of household  \n \
		(3):Married sharing  \n \
		(4):divorce \n \
		(5):Exit \n") ;
		
		scanf("%d",&type) ;
		if (type==5){
			printf("Exit! \n\n");
			break ;
		}else if(type>=1 && type <=4){
			switch(type){
			case 1 :
				grad=17850 ;
				break ;
			case 2 :
				grad=23900 ;
				break ;
			case 3 :
				grad=29750 ;
				break ;
			case 4 :
				grad=14875 ;
				break ;}
		}else{
			printf("Input Error,please input agin ! \n\n");
			continue ;
		} 
		
		printf("Input income :\n");
		if(scanf("%f",&income)==1 && income>0){
			if(income<=grad){
				taxes=income*0.15 ;
			}else{
				taxes=grad*0.15+(income-grad)*0.28 ;
			}
			
		}
		
		printf("\ntaxes=%.4f \n\n",taxes) ;
		}
 
}

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>
#include <ctype.h>
int main()
{
	const float price_yj=2.05 ; //洋蓟
	const float price_tc=1.15 ; //甜菜 
	const float price_hlb=1.09 ; //胡萝卜 
	const float discount_grad=100.00 ; //打折阈值 
	const int weight_grad1=5 ;
	const int weight_grad2=20 ; 
	const float extra_charge1=6.5 ;
	const float extra_charge2=14.0 ;
	const float extra_charge3=0.5 ;
	char type ;
	int flag=1 ;
	float w_yj=0,w_tc=0,w_hlb=0,w_total=0,w_input ; //输入的重量,磅数 
	float amt_yj,amt_tc,amt_hlb,amt_total,amt_dis,amt_extra=0,amt_act ;//每项的金额 
	 
	while(flag){  
		printf("请选择品类:a 洋蓟,b 甜菜 ,c 胡萝卜,q 退出订购 \n") ;
	 	type=getchar() ;
		getchar() ;//消除回车符 
		if(islower(type)) {
			switch (type){
			case 'a':
				{
				printf("请输入洋蓟的磅数:\n") ;
				scanf("%f",&w_input) ;
				getchar() ; //消除回车符 
				w_yj+=w_input ;
				}
				break ;
			case 'b':
				{
				printf("请输入甜菜的磅数:\n") ;
				scanf("%f",&w_input) ;
				getchar() ;//消除回车符 
				w_tc+=w_input ;
				}
				break ;
			case 'c': 
				{
				printf("请输入胡萝卜的磅数:\n") ;
				scanf("%f",&w_input) ;
				getchar() ;//消除回车符 
				w_hlb+=w_input ;
				}
				break ;
			case 'q':
				{
				printf("Exit. \n");
				flag=0 ;
				}
				break ;
			default:
				printf("Error,please input agin. \n");
				break ;				
		}
		}  
	}
	
	w_total=w_yj+w_tc+w_hlb ; //总重量 
	amt_yj=w_yj*price_yj; // 洋蓟金额 
	amt_tc=w_tc*price_tc; //甜菜金额 
	amt_hlb=w_hlb*price_hlb; //胡萝卜金额 
	amt_total=amt_yj+amt_tc+amt_tc ; //未计算折扣和包装费的总金额 
	
	if(amt_total<discount_grad){ //计算折扣金额 
		amt_dis=0 ;
	}else{
		amt_dis=amt_total*0.05 ;
	}
	
	if(w_total<weight_grad1 && w_total >0){ //计算额外收费(包装和运费) 
		amt_extra=extra_charge1 ; 
	}else if(w_total<weight_grad2){
		amt_extra=extra_charge2 ;
	}else{
		amt_extra= (w_total-weight_grad2)*extra_charge3 + extra_charge2 ;
	}
	
	amt_act=amt_total+amt_extra-amt_dis ;
	
	//打印所有信息 
	printf("订单明细: 洋蓟 %.4f磅 %.4f美元,甜菜 %.4f磅 %.4f美元,胡萝卜 %.4f磅 %.4f美元 \n",
			w_yj,amt_yj,w_tc,amt_tc,w_hlb,amt_hlb) ;
	printf("订单总费用:%.4f,总重量:%.4f,折扣:%.4f,运费和包装费:%.4f,实付金额:%.4f \n",
			amt_total,w_total,amt_dis,amt_extra,amt_act
	) ;
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值