C primer plus 第六版 第七章 参考答案

本答案仅供参考,尚有不足,欢迎指正交流

//7.12.1

//7.12.1编写一个程序读取输入 
# include <stdio.h>
# include <ctype.h>
# define STOP '#'
int main(void)
{
	int i, space, bh;
	char ch;
	
	i = space = bh = 0;
	
	printf("Please enter the  what you want(# to quit)\n");
	while ((ch = getchar()) != STOP)
	{
		if(ch == ' ')
		   space++;
		if (ch == '\n')
			i++;
		if (isgraph(ch))
		    bh++;			
	}
	printf("%d space, %d \\n, %d graph", space, i, bh);	
	return 0;
}

//7.12..2

//7.12.2读取输入直至# 
# include <stdio.h>
# define  STOP '#'
# include <ctype.h>
int main(void)
{
	char ch;
	int  i = 0;
	
	printf("Please enter what you want\n");
	
	while ((ch = getchar()) != STOP)
	{
		if(isgraph(ch))
			i++;
		if(i % 8 != 0)		
			printf("%c %d ", ch, ch);
		else
			printf("%c %d \n", ch, ch);		
	}
	
	return 0;
}
//我理解题目一行打印8个字符是不包含其ascll码的
//且该程序在enter时会直接进行打印,有了解的求告知 

//7.12.3

//7.12.3按要求计算输入 
# include <stdio.h>
int main(void)
{
	int num, sum1, sum2;
	int i,j;
	float average1,average2;
	
	sum1 = sum2 = 0;
	i = j = 0;
	average1 = average2 = 0;
	
	printf("Please enter the number(0 to quit)\n");
	scanf("%d", &num);
	while(num != 0)  
	{
		if((num % 2) == 0)
		{
			i++;
			sum1 += num;
		}	
		if((num % 2) != 0)
		{
			j++;	
			sum2 += num;		
	    }
	printf("Please enter the next number\n"); 
	scanf("%d", &num);
	}
	average1 = (float)(sum1/i);
	average2 = (float)(sum2/j);
    printf("%d even number,average = %.2f, %d odd number,average  = %.2f", i, average1, j, average2);
	return 0;
}

//7.12.4

//7.12.4替换次数 
# include <stdio.h>
int main(void)
{
	char ch;
	int  i = 0;
	
	printf("Please enter\n"); 
	while((ch = getchar()) != '#')
	{
		if(ch == '.')
			putchar('!') , i++ ; 
		else if(ch == '!')
			printf("!!"), i++; 
		else
			putchar(ch);
	}	
	printf("\n%d shift(s)", i);
	return 0;
 } 

//7.12.5

//7.12.5重复练习4 
# include <stdio.h>
int main(void)
{
	char ch;
	int  i = 0;
	
	printf("Please enter\n");
	while((ch = getchar()) != '#')
	{
		switch(ch)
		{
			case '.':i++,putchar('!');
					break;
			case '!':i++,printf("!!");
					break;
			default:putchar(ch);
		}
	}
	printf("\n%d shift", i);
	
	
	return 0; 
 } 

//7.12.6

//7.12.6报告ei出现的次数 
# include <stdio.h>
int main(void)
{
	char ch;
	int i;
	
	printf("Please enter what you want(# to quit)\n");
	
	while ((ch = getchar()) != '#')
	{
		switch(ch)
		{
		case 'e': i++;	
				break;
		case 'i': i++;
				break;
		}	
	}
	printf("%d times\n", i);
	return 0;
}

//7.12.7

//7.12.7工资计算 
# include <stdio.h>
# define S_H   1000 
# define P_300 0.15
# define P_450 0.20
# define P_OTHER 0.25
int main(void)
{
	float times;
	float salary;
	float tax;
	float net;
	
	printf("Please enter your work times\n");
	scanf("%f", &times);
	
	if (times <= 40)
		{
			salary = times * S_H;
			if( salary >= 0 && salary <= 300)
			{
				tax = salary * P_300;
				net = salary  - tax;
			}
			else if(salary <= 450)
			{
				tax = 300 * P_300 + (salary - 300) * P_450;
				net = salary - tax;
			}
			else if(salary > 450)
			{
				tax = 300 * P_300 + 150 * P_450 + (salary - 450) * P_OTHER;
				net = salary - tax;
			}
			else
				printf("ERROR");
		}
	else if (times > 40)	
		{
		salary = (times - 40) * 1.5 * S_H + 40 * S_H;
		tax = 300 * P_300 + 150 * P_450 + (salary - 450) * P_OTHER;
		net = salary - tax;
		}
	printf("salary = %.2f, tax = %.2f,net = %.2f", salary, tax, net);
	return 0;
}

//7.12.8

//7.12.8工资计算 还有几处有待提高 
# include <stdio.h>

# define S_HFI 8.75
# define S_HSE 9.33
# define S_HTH 10.00
# define S_HFO 11.20 
# define P_300 0.15
# define P_450 0.20
# define P_OTHER 0.25
int main(void)
{
	float S_H;
	float times;
	float salary;
	float tax;
	float net;
	int i = 1;
	int j;
		
	for (i; i < 71; i++)
	{
		printf("*");
	}
	printf("\nEnter the number corresponeding to the desired pay rate or action:\n");
	printf("(1)$%f/hr\t\t\t\t\t\t(2)$%f/hr\n", S_HFI, S_HSE);
	printf("(3)$%f/hr\t\t\t\t\t(4)$%f/hr\n", S_HTH, S_HFO);
	printf("(5)quit\n");
	i = 1;
	for (i; i < 71; i++)
	{
		printf("*");
	}
	printf("\n");
	scanf("%d", &j);
	printf("enter the work time\n");
		
	while (scanf("%f", &times) == 1 && j != 5)
	{
		switch(j)
	{
		case 1 :S_H = S_HFI;
				break;
		case 2 :S_H = S_HSE;
				break;
		case 3 :S_H = S_HTH;
				break;
		case 4 :S_H = S_HFO;
				break;
		case 5 : printf("Done");
				break;
		default:printf("Please enter the correct number");
	}
	if (times <= 40)
		{
			salary = times * S_H;
			if( salary >= 0 && salary <= 300)
			{
				tax = salary * P_300;
				net = salary  - tax;
			}
			else if(salary <= 450)
			{
				tax = 300 * P_300 + (salary - 300) * P_450;
				net = salary - tax;
			}
			else if(salary > 450)
			{
				tax = 300 * P_300 + 150 * P_450 + (salary - 450) * P_OTHER;
				net = salary - tax;
			}
			else
				printf("ERROR");
		}
	else if (times > 40)	
		{
		salary = (times - 40) * 1.5 * S_H + 40 * S_H;
		tax = 300 * P_300 + 150 * P_450 + (salary - 450) * P_OTHER;
		net = salary - tax;
		}
	printf("salary = %.2f, tax = %.2f,net = %.2f\n", salary, tax, net);
	printf("Please enter other select\n");
	scanf("%d", &j);
	printf("Please enter other time\n"); 
	}
	return 0;
}

//7.12.9

//7.12.9只接受正整数输入,然后显示所有小于或等于该数的素数 
# include<stdio.h>
int main(void) 
{
	int fir, sec, thi, i;
	
	printf("please enter an integrate(>1)\n");
	scanf("%d", &fir);
	for(sec = 1; sec <= fir; sec++)
	{
		i = 0;
		for(thi = 1;thi <= sec; thi++)			//使第三个数据逐渐增大并与第二个数据相除计算余数 
			if((sec % thi) == 0)				//从而得到整除次数,次数小于3的即为素数 
				i++;	  		
		if(i < 3 && i != 1)
			printf("%4d", sec);		
	}	
	return 0; 
}
//暴力破解 

//7.12.10

# include <stdio.h>
# define TAX1 17850
# define TAX2 23900
# define TAX3 29750
# define TAX4 14875
# define TAX5 0
# define RATE1 0.15
# define RATE2 0.28

int main(void)
{
	int i;
	float income, TAX;
	
	do
	{
		
		printf("bachelor enter 1\nhost enter 2\nmarried enter 3\ndivorced enter 4\nenter 5 quit\n");
		scanf("%d", &i);
		if(i == 5)
			break;
		printf("Please enter your income\n");
		scanf("%f", &income);
		if(i == 1)
		{
			if(income > TAX5 && income <= TAX1)
				TAX = RATE1 * income;
			else if(income > TAX1)
				TAX = RATE1 * TAX1 + RATE2 * (income - TAX1);
			else
				printf("ERROR!");  
		}
		else if(i == 2)
		{
			if(income > TAX5 && income <= TAX2)
				TAX = RATE1 * income;
			else if(income > TAX2)
				TAX = RATE1 * TAX2 + RATE2 * (income - TAX2);
			else
				printf("ERROR!");  
		}
		else if(i == 3)
		{
			if(income > TAX5 && income <= TAX3)
				TAX = RATE1 * income;
			else if(income > TAX3)
				TAX = RATE1 * TAX3 + RATE2 * (income - TAX3);
			else
				printf("ERROR!");  
		}
		else if(i == 4)
		{
			if(income > TAX5 && income <= TAX1)
				TAX = RATE1 * income;
			else if(income > TAX4)
				TAX = RATE1 * TAX4 + RATE2 * (income - TAX4);
			else
				printf("ERROR!");  
		}
		else
			printf("ERROR");
		printf("TAX = %f\n\n", TAX);
	}while(i != 5);	
	return 0;	
}

//7.12.11

# include <stdio.h>
# define ARTICHOKE 2.05
# define BEETROOT 1.15
# define CARROT 1.09
# define DISCOUNT 0.05
# define PACKAGE1 6.5
# define PACKAGE2 14
# define PACKAGE3 0.5
//printf语句会执行两次,sum质量,sum1洋jian质量,sum2甜菜质量,sum3胡萝卜质量,sum4运费包装费,sum5未折扣钱数,sum6蔬菜钱数,sum7折后钱数 

int main(void)
{
	float i, j, k, sum, sum1, sum2, sum3, sum4, sum5, sum6, sum7;
	char ch;
	printf("please select the vegetable\na is artichoke\nb is beetroot\nc is carrot\nq is quit\n");
	do
	{	
		scanf("%c", &ch);
	if(ch != 'q')
		switch (ch)
	{
		case 'a':
			{
			printf("please enter the pound\n");
			scanf("%f", &i);
			sum1 += i;
			break;
			}
		case 'b':
	 		{
			printf("please enter the pound\n");
			scanf("%f", &j);
			sum2 += j;
			break;
			}
		case 'c':
			{
			printf("please enter the pound\n");
			scanf("%f", &k);
			sum3 += k;
			break;
			}				
	}
	printf("please select the vegetable\na is artichoke\nb is beetroot\nc is carrot\nq is quit\n");
	}while(ch != 'q');
	sum = sum1 + sum2 + sum3;
	if(sum <= 5)
		sum4 = PACKAGE1;
	else if(sum <= 20)
		sum4 = PACKAGE2;
	else if(sum > 20)
		sum4 = PACKAGE2	+ (sum - 20) * PACKAGE3;
	sum6 = sum1 * ARTICHOKE + sum2 * BEETROOT + sum3 * CARROT;
	sum5 = sum6 + sum4;
	
	if(sum >= 100)
		sum7 = sum5 - sum5 * DISCOUNT;
	printf("artichoke is %f dollars/pound\nbeetroot is %f dollars/pound\ncarrot is %f dollars/pounds\n", ARTICHOKE, BEETROOT, CARROT);
	if(sum > 0)
		printf("the total weight is %.2f pound\n", sum);
	if(sum6 > 0)
		printf("the vegetable price is %.2f dollars\n", sum6);
	if(sum7 > 0)	
		printf("the total price is %.2f dollars\n", sum7);
	if(sum5 > 100)
		printf("the discount dollars is %.2f\n", sum5 - sum7);
	if(sum4 > 0)
		printf("the package is %.2f dollars\n", sum4);
	return 0;
}
//现未解决printf重复打印两次问题,敬请指点 
//现已解决printf重复打印问题,详见主页文章

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值