7.12 编程练习

1.编写一个程序。改程序读取输入直到遇见#字符,然后宝盖读取的空格数目,读取的换行符数目,以及读取的所有其他字符数目。

#include <stdio.h>
#define STOP '#'
int main(void)
{
	char ch;
	int n_space=0;
	int n_enter=0;
	int n_other=0;
	printf("Please enter any character but # to quit.\n");
	while((ch=getchar())!=STOP)
	{
		if(ch==' ')
		{
			n_space++;
		}
		else if(ch=='\n')
		{
			n_enter++;		
		}
		else
		{
			n_other++;	
		}		
	}
	printf("你输入了%d个空格,%d个换行符,%d个其他字符\n",n_space,n_enter,n_other);
	
	getchar();
	return 0;
}
2.编写一个程序。该程序读取输入直到遇到#字符。使程序打印每个输入的字符以及它的十进制ASCII码。每行打印8个字符/编码对。建议:利用字符计数和模运算符(%)在每8个循环周期时打印一个换行符。
#include <stdio.h>
#define STOP '#'
int main(void)
{
	char ch;
	int count=0;
	printf("Please enter any character but # to quit\n");
	while((ch=getchar())!=STOP)
	{
		if(ch=='\n')
			continue;//吃掉回车换行符,不进行输出 
		count++;
		if(count%8!=0)
		{
			putchar(ch);
			printf(":%d	",ch);
		}
		else
		{
			putchar(ch);
			printf(":%d	",ch);
			printf("\n");
		}
		
	}

3.编写一个程序,该程序读取整数,直到输入0.输入终止后,程序应该报告输入的偶数(不包括0)总个数,偶数的平均值,输入的奇数总个数以及奇数的平均值。

#include <stdio.h>
int main(void)
{
	int num;
	int oddsum=0;
	int oddcount=0;
	int oddaverage;
	int evensum=0;
	int evencount=0;
	int evenaverage;
	
	printf("Please enter a num.enter 0 to quit.\n");
	scanf("%d",&num);
	while(num)
	{
		if(num%2!=0)
		{
			oddsum+=num;
			oddcount++;
		}
		else
		{
			evensum+=num;
			evencount++;
		}
		printf("Please enter a num.enter 0 to quit.\n");
		scanf("%d",&num);
	}
	oddaverage=oddsum/oddcount;
	evenaverage=evensum/evencount;
	printf("奇数总和是%d,平均值是%d\n偶数总和是%d,平均值是%d",oddsum,oddaverage,evensum,evenaverage);
	getchar();
	return 0;
}
4.利用if else语句编写程序读取输入,直到#。用一个感叹号代替每一个句号。将原来的每个感叹号用两个感叹号太低,然后报告进行了多少代替。

#include <stdio.h>
#define EXCAL '!'
#define PERIOD '.'
#define STOP '#'
int main(void)
{
	char ch;
	int instead=0;
	while((ch=getchar())!=STOP)
	{
		if(ch==PERIOD)
		{
			putchar(EXCAL);
			instead++;
		}
		else if(ch==EXCAL)
		{
			putchar(EXCAL);
			putchar(EXCAL);
			instead++;
		}
		else
		{
			putchar(ch);
		}
	}
	printf("\n一共进行了%d次替代\n",instead);
	getchar();
	return 0;
}
5.用switch重做练习3.

6.编写一个程序输入,直到#,并报告序列ei出现的次数。

#include <stdio.h>
#define STOP '#'
int main(void)
{
	char ch;
	char last=0;
	int count=0;
	
	while((ch=getchar())!=STOP)
	{
		if(ch=='i')
		{
			if(last=='e')
			{
				count++;
			}
		}
		last=ch;
		putchar(ch);
	}
	printf("\nei出现了%d次\n",count);
	return 0;
}
7.编写一个程序,要求输入一周中的工作时间数,然后打印工资总额,税金以及净工资。作如下假设:

a.基本工资等级=100.00美元/小时

b.加班(超过40小时)=1.5倍的时间

c.税率 前300美元为15%

下一个150美元为20%

余下的是2%

用define定义常量,不必关心本例是否符合当前税法

#include <stdio.h>
#define HOURLY 100	
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25

int main(void)
{
	float income;
	float after_tax_income;
	float hour;
	float tax;
	printf("enter how many hour did your work:");
	scanf("%f",&hour);
	if(hour>40)
	{
		hour=(hour-40)*1.5+40;
	}
	income=hour*HOURLY;
	if(income<=300)
	{
		tax=income*RATE1;
	}
	else if(income<=450)
	{
		tax=300*RATE1+(income-300)*RATE2;
	}
	else
	{
		tax=300*RATE1+150*RATE2+(income-450)*RATE3;
	}
	after_tax_income=income-tax;
	printf("税前工资%.2f元,需缴税%.2f元,税后工资%.2f元\n",income,tax,after_tax_income);
	getchar();
	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 OVERTIME 1.5
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25
void print();
float tax_amount(float income);
float hourlywage(int choose);
int main(void)
{
	float income;
	float after_tax_income;
	float hour;
	float hourlypay;
	float tax;
	int choose;
	do
	{
		print();
		scanf("%d",&choose);

	switch(choose)
	{
		case 1:
			hourlypay=8.75;break;
		case 2:
			hourlypay=9.33;break;
		case 3:
			hourlypay=10.00;break;
		case 4:
			hourlypay=11.20;break;
		case 5:
			printf("程序结束\n");break;
		default:
			printf("请输入1到5之间的数字\n");continue;
	}

	if(choose==5)
	{
		break;
	}
	
	printf("你的时薪是%.2f\n",hourlypay);
	
	printf("请输入你的工作小时\n");
	scanf("%f",&hour);
	printf("你的实际工作小时是%.2f\n",hour);		
	
	if(hour>40)
	{
		hour=(hour-40)*OVERTIME+40;
		printf("折算的加班时间为%.2f\n",hour);
	}
	
	income=hour*hourlypay;
	
	tax=tax_amount(income);
	
	after_tax_income=income-tax;
	after_tax_income=income-tax;
	printf("税前工资%.2f元,需缴税%.2f元,税后工资%.2f元\n",income,tax,after_tax_income);		
	}while(choose!=5);
	return 0;
}


void print()
{	
	printf("\n");
	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");
}

float tax_amount(float income)
{
	float tax;

	if(income<=300)
	{
		tax=income*RATE1;
	}
	else if(income<=450)
	{
		tax=300*RATE1+(income-300)*RATE2;
	}
	else
	{
		tax=300*RATE1+150*RATE2+(income-450)*RATE3;
	}
	return tax;
}
9.编写一个程序,接受一个整数输入,然后显示所有小于或等于该数的素数。

#include <stdio.h>
#include <stdbool.h>
bool isprime(int num);
int main(void)
{
	int a;
	printf("请输入一个整数\n");
	scanf("%d",&a);
	int b=a;
	for(b=a;b>=2;b--)
	{
		if(isprime(b))
		{
			printf("%d\n",b);
		}
	}
	return 0;
}

bool isprime(int num)
{
	int div;
	bool ret=true;
	for(div=2;div*div<=num;div++)
	{
		if(num%div==0)
		{
			ret=false;
		}
	}
	return ret;
}
10.1988年United States Federal Tax Schedule 是最基本的。它分为四类,每类有两个等级。下面是其摘要;美元数为应征收的收入。

种类税金
单身前17850美元按15%征收,超出部分按照28%征收
户主前23900美元按15%征收,超出部分按28%征收
已婚,共有前29750美元按15%征收,超出部分按28%征收
已婚,离异前14875美元按15%征收,超出部分按28%征收
例如,有20000美元应征收税收入的单身雇佣劳动者应缴纳0.15*17850美元+0.28*(20000-17850)。编写一个程序,让用户指定税金种类和应征收税收入,然后计算税金。使用循环便于用户多次输入。

#include <stdio.h>
void print(void);
float select(int type);
float tax_amount(float income,float bound);
int main(void)
{
	int type;
	float bound;
	float income;
	float tax;
	
	do{
		print();
		scanf("%d",&type);
		bound=select(type);
		if(type<1||type>5)
		{
			continue;
		}
		if(type==5)
		{
			break;
		}
		printf("请输入您的收入\n");
		scanf("%f",&income);
		tax=tax_amount(income,bound);
		printf("您应该纳税总额为%.2f",tax);
	}while(type!=5);
	return 0;
}

void print(void)
{
	printf("\n");
	printf("****************************\n");
	printf("请输入您对应的纳税类型:\n");
	printf("1) 单身\n");
	printf("2) 户主\n");
	printf("3) 已婚,共有\n");
	printf("4) 已婚,离异\n");
	printf("5) 退出\n");
	printf("****************************\n");	
}

float select(int type)
{
	float bound=0.0f;
	switch(type)
	{
		case 1:
			printf("前17850美元按15%%征收,超出部分按照28%%征收\n");
			bound=17850;
			break;
		case 2:
			printf("前23900美元按15%%征收,超出部分按照28%%征收\n");
			bound=23900;
			break;
		case 3:
			printf("前29750美元按15%%征收,超出部分按照28%%征收\n");
			bound=29750;
			break;
		case 4:
			printf("前14875美元按15%%征收,超出部分按照28%%征收\n");
			break;
		case 5:
			printf("程序结束\n");
			break;
		default:
			printf("请正确1到5的数字\n");
	}
	return bound;
}
float tax_amount(float income,float bound)
{
	float tax;
	if(income<=bound)
	{
		tax=income*0.15;
	}
	else
	{
		tax=bound*0.15+(income-bound)*0.28;
	}
	return tax;
}
11.ABC Mail Order Grocery

 朝鲜蓟的售价是1.25美元/磅;

甜菜的售价是0.65美元/磅;

胡萝卜的售价是0.89美元/磅;

在添加运输费用之前,他们为100美元的订单提供5%的打折优惠;

对5磅或以下的订单收取3.50美元的运输和装卸费用;

过5磅而不足20磅的订单收取10.00美元运输和装卸费用;

20磅以上的运输,在8美元基础上每磅加收0.1美元;

编写程序,在循环中使用switch语句,以便对输入a的响应是让用户输入所需的朝鲜蓟磅数,输入b为甜菜的磅数,c为胡萝卜磅数,而q允许用户退出订购过程。

然后程序计算总费用、折扣和运输费用(如果有运输费的话),以及最后总计付款的费用。

随后程序应该显示所有的购买信息:每磅的费用、订购的磅数、该订单每种蔬菜的费用、订单的总费用、折扣,如果有的话加上运输费用,以及所有费用的总和。

#include <stdio.h>
#define a 1.25
#define b 0.65
#define c 0.89

void print(void);

int main(void)
{
	int choice;
	float weighta=0,weightb=0,weightc=0,t_weight=0;
	float t_weighta=0,t_weightb=0,t_weightc=0;
	float billa=0,billb=0,billc=0,t_bill=0,last_bill=0;
	float discount=0,transport=0;
	
	print();	
	while((choice=getchar())!='q')
	{
		switch(choice)
		{
			case 'a':
				printf("您需要购买的是朝鲜蓟,请输入你需要购买的重量\n");
				scanf("%f",&weighta);
				t_weighta=t_weighta+weighta;
				break;
			case 'b':
				printf("您需要购买的是甜菜,请输入你需要购买的重量\n");
				scanf("%f",&weightb);
				t_weightb=t_weightb+weightb;
				break;	
			case 'c':
				printf("您需要购买的是胡萝卜,请输入你需要购买的重量\n");
				scanf("%f",&weightc);
				t_weightc=t_weightc+weightc;
				break;
			case 'q':
				printf("您将退出订购系统,开始计算金额\n");
				break;
			default :
				printf("您的输入不正确,请重现输入\n");
				while(getchar()!='\n')
					continue;				
				continue; 
		}
		while(getchar()!='\n')
			continue;
		printf("请继续选购,退出请按q\n");
	}
	printf("你总计选购%.2f朝鲜蓟。%.2f甜菜。%.2f胡萝卜.\n",t_weighta,t_weightb,t_weightc);
	t_weight=t_weighta+t_weightb+t_weightc;
	billa=t_weighta*a;
	billb=t_weightb*b;
	billc=t_weightc*c;
	t_bill=billa+billb+billc;
	
	if(t_bill>=100)
	{
		discount=t_bill*0.05;
	}
	
	if(t_weight<=5)
	{
		transport=3.50;
	}
	else if(t_weight<20)
	{
		transport=10.00;
	}
	else
	{
		transport=8.0+(t_weight*0.1);
	}
	last_bill=t_bill-discount+transport;
	
	printf("每磅的费用为%.2f\n",last_bill/t_weight);
	printf("订购的所有蔬菜的总磅数为%.2f\n",t_weight);
	printf("订购朝鲜蓟的费用为%.2f\n",billa);
	printf("订购甜菜的费用为%.2f\n",billb);
	printf("订购胡萝卜的费用为%.2f\n",billc);
	printf("订单总费用为%.2f\n",last_bill);
	printf("运输费为%.2f\n",transport);
	return 0;
}


void print(void)
{
	printf("\n");
	printf("****************************\n");
	printf("请输入您需要购买的蔬菜编号和磅数:\n");
	printf("a) 朝鲜蓟\n");
	printf("b) 甜菜\n");
	printf("c) 胡萝卜\n");
	printf("q) 退出订购\n");
	printf("****************************\n");	
}













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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值