C语言学习《C Primer Plus》编程练习第七章

1

/*
读取空格数、换行符、所有其他符号的数量 
*/
#include 
#include  
int main()
{
	char c;
	int n_space=0;
	int n_changeline=0;
	int n_other=0; 
	printf("请输入一段英文内容,并以#结束输入:"); 
	while((c=getchar())!='#')
	{
		if(c=='\n')
			n_changeline++;
		else if(isspace(c))
			n_space++;
		else
			n_other++;
	}
	printf("该内容有%d个空格,%d个换行符,%d个其他符号",n_space,n_changeline,n_other);
}

2

#include 
int main()
{
	char c;
	int n;
	printf("请输入内容,并以#结束:");
	while((c=getchar())!='#')
	{
		if((n%8==0) && (n!=0))
			printf("\n");
		n++;
		printf("%c:%d  ",c,c);
	} 

}

3

#include  
int main()
{
	int i,e_n=0,o_n=0,e_s=0,o_s=0;
	printf("请输入数字,输入0结束:");
	while(1)
	{
		scanf("%d",&i);
		if(i==0)
			break;
		else if(i%2==0)
		{
			e_s+=i;
			e_n++;
		}
		else
		{
			o_s+=i;
			o_n++;
		}
	}
	printf("偶数有%d个,平均值为%d\n",e_n,e_s/e_n);
	printf("奇数有%d个,平均值为%d\n",o_n,o_s/o_n);
} 

4

#include 
int main()
{
	char c;
	int n=0;
	printf("请输入内容:");
	while((c=getchar())!='#')
	{
		if(c=='.')
		{
			putchar('!');
			n++;
		}
		else if(c=='!')
		{
			putchar('!');
			putchar('!');
			n++;
		}
		else
			putchar(c); 
	}
	printf("转换了%d次\n",n); 
}

5

#include 
int main()
{
	char c;
	int n=0;
	printf("请输入内容:");
	while((c=getchar())!='#')
	{
		switch(c)
		{
			case '.':
				putchar('!');
				n++;
				break;
			case '!':
				putchar('!');
				putchar('!');
				n++;
				break;
			default:
				putchar(c); 
				break;
		}	
	}
	printf("转换了%d次\n",n); 
}

6

#include 
int main()
{
	char cone,ctwo;
	int n=0;
	printf("请输入内容,并以#结束:"); 
	while((ctwo=getchar())!='#')
	{
		if(ctwo=='i')
		{
			if(cone=='e')
			{
				n++;		
			}
		}
		cone=ctwo;
	}	
	printf("ei出现了%d次",n);
} 

7

#include 
#define R1 0.15
#define R2 0.20
#define R3 0.25
int main()
{
	double hour,income,tax,trueincome;
	printf("请输入你的工作时间:");
	scanf("%lf",&hour);
	if(hour>40)
	{
		hour=(hour-40)*1.5+40;
	}
	income=hour*1000;
	if(income<=300)
	{
		tax=income*R1;
		trueincome=income-tax;
	}
	else if(income>300 && income<=450)
	{
		tax=300*R1+(income-300)*R2;
		trueincome=income-tax;
	}
	else
	{
		tax=300*R1+150*R2+(income-450)*R3;
		trueincome=income-tax;
	}
	printf("工资总额:%lf美元\n",income);
	printf("工资税金:%lf美元\n",tax);
	printf("工资净收入:%lf美元\n",trueincome);
} 

8

#include 
#define R1 0.15
#define R2 0.20
#define R3 0.25

int main()
{
	int n=0,m=1;
	double hour_money,hour,income,tax,trueincome;
	while(m)
	{ 
	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");
	printf("请输入你的选择:");
	scanf("%d",&n);
	switch(n)
	{
		case 1:
			hour_money=8.75;
			break;
		case 2:
			hour_money=9.33;
			break;	
		case 3:
			hour_money=10.00;
			break;
		case 4:
			hour_money=11.20;
			break;
		case 5:
			return 0;
		default:
			printf("请输入 1 到 5之间的数\n");
            continue;
            break;
	} 

	printf("请输入你的工作时间:");
	scanf("%lf",&hour);
	if(hour>40)
	{
		hour=(hour-40)*1.5+40;
	}
	income=hour*hour_money;
	if(income<=300)
	{
		tax=income*R1;
		trueincome=income-tax;
	}
	else if(income>300 && income<=450)
	{
		tax=300*R1+(income-300)*R2;
		trueincome=income-tax;
	}
	else
	{
		tax=300*R1+150*R2+(income-450)*R3;
		trueincome=income-tax;
	}
	printf("工资总额:%lf美元\n",income);
	printf("工资税金:%lf美元\n",tax);
	printf("工资净收入:%lf美元\n",trueincome);
	}
} 
	
	

9.

#include 
#include  
int main()
{
	int n,i,j;
	printf("请输入一个正整数:");
	scanf("%d",&n);
	printf("不大于这个数的质数有:"); 
	for(i=2;i<=n;i++)
	{
		for(j=2;jsqrt(i))
			printf("%d ",i);
	}
}

10

#include 
int main()
{
	int n;
	double m;
	while(1)
	{
		printf("请输入你的缴纳税金的种类:\n");
		printf("1)单身  2)户主  3)已婚,共有  4)已婚,离异\n");
		scanf("%d",&n);
		switch(n)
		{
			case 1:
				printf("请输入应缴纳收入:");
				scanf("%lf",&m);
				if(m>17850)
					printf("税金为:%lf\n",17850*0.15+(m-17850)*0.28);
				else
					printf("税金为:%lf\n",m*0.15);
			case 2:
				printf("请输入应缴纳收入:");
				scanf("%lf",&m);
				if(m>23900)
					printf("税金为:%lf\n",23900*0.15+(m-23900)*0.28);
				else
					printf("税金为:%lf\n",m*0.15);
			case 3:
				printf("请输入应缴纳收入:");
				scanf("%lf",&m);
				if(m>29750)
					printf("税金为:%lf\n",29750*0.15+(m-29750)*0.28);
				else
					printf("税金为:%lf\n",m*0.15);
			case 4:
				printf("请输入应缴纳收入:");
				scanf("%lf",&m);
				if(m>14875)
					printf("税金为:%lf\n",14875*0.15+(m-14875)*0.28);
				else
					printf("税金为:%lf\n",m*0.15);
			default:
					printf("请正确输入:\n");
		}
	}
}

11

#include 
int main()
{
	char c;
	double a_w,a_m,b_w,b_m,c_w,c_m,s_w,s_t,s_m;
	
	printf("请选择你要的货物\n");
	printf ("a)洋蓟  b)甜菜  c)胡萝卜  q)退出\n");
	printf("选择:");
	while((c=getchar())!='q')
	{
		switch(c)
		{
			case 'a':
				printf("请输入你想要的磅数:");
				scanf("%lf",&a_w);
				if(a_w<=100)
					a_m=2.05*a_w;
				else
					a_m=2.05*a_w*0.05;
				break;
			case 'b':
				printf("请输入你想要的磅数:");
				scanf("%lf",&b_w);
				if(b_w<=100)
					b_m=2.05*b_w;
				else
					b_m=2.05*b_w*0.05;
				break;
			case 'c':
				printf("请输入你想要的磅数:");
				scanf("%lf",&c_w);
				if(c_w<=100)
					c_m=2.05*c_w;
				else
					c_m=2.05*c_w*0.05;
				break;
				
		}
	}
	printf("洋蓟的售价:2.05美元/磅  甜菜的售价:1.15美元/磅  胡萝卜的售价:1.09美元/磅\n");
	printf("用户订购了%lf磅洋蓟,%lf磅甜菜,%lf磅胡萝卜,总共%lf磅\n",a_w,b_w,c_w,a_w+b_w+c_w);
	printf("洋蓟花费%lf美元,甜菜花费%lf美元,胡萝卜花费%lf美元\n",a_m,b_m,c_m);
	if(a_m+b_m+c_m<=100)
	{
		printf("订单的总费用为:%lf\n",a_m+b_m+c_m);
		s_m=a_m+b_m+c_m;
		printf("没有折扣\n");
	}
	else
	{
		printf("订单的总费用为:%lf\n",(a_m+b_m+c_m)*0.95);
		s_m=a_m+b_m+c_m;
		printf("有九五折扣\n");
	}
	s_w=a_w+b_w+c_w;
	if(s_w<=5)
		s_t=6.5;
	else if(s_w>5 && s_w<=20)
		s_t=14;
	else
		s_t=14+0.5*(s_w-20)*0.5;
	printf("运费和包装费为:%lf\n",s_t);
	printf("总费用为:%lf\n",s_m+s_t);
					
}

 

你可能感兴趣的:(C语言)