家庭做业——苗钰婷

1.编写一个程序读取输入,读到#字符中止,而后报告读取的空格数、换行符数和全部其余字符的数量。

/**
	Module Name: 
	Description:记录空格数、换行符数和全部其余字符的数量。 
	Author:miaoyuting
	Created:2019-11-08
	Last Change:
	Functions:
*/
#include <stdio.h>
int main(void)
{
	char ch;
	int i=0,j=0,k=0;
	ch=getchar();
	while(ch!='#'){
		if(ch==' '){
			i++;
		}
		else if(ch=='\n'){
			j++;
		}
		else{
			k++;
		}
		ch=getchar();
	}
	printf("%d,%d,%d",i,j,k);
	return 0;
}

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

/**
	Module Name: 字符转ASCII码 
	Description:
	Author:miaoyuting
	Created:2019-11-08 
	Last Change:
	Functions:
*/
#include <stdio.h>
int main(void)
{
	 char ch;
	 int chCount=0;
	 ch=getchar();
	 while(ch!='#'){
 		printf("ch:%c,Ascii:%d",ch,ch);
 		chCount++;
 		if(chCount%8==0){
		 	printf("\n");
		 }
	 	ch=getchar();
 	}
	return 0;
}

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

/**
	Module Name: 奇数偶数 
	Description:
	Author:miaoyuting
	Created:20191108 
	Last Change:
	Functions:
*/
#include <stdio.h>
int main(void)
{
	int number;
	int os=0,js=0;
	float osave=0,jsave=0;
	printf("please input nimber\n");
	scanf("%d",&number);
	while(number!=0){
		if(number%2==0){
			os++;
			osave+=number;
		}else{
			js++;
			jsave+=number;
		}
		printf("please input number\n");
		scanf("%d",&number);
	}
	osave/=os;
	jsave/=js;
	printf("you have input %d 偶数,average is %f\n",os,osave);
	printf("you have input %d 奇数,average is %f\n",js,jsave);

	return 0;
}

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

/**
	Module Name: 感叹号替换句号,用两个感叹号替换原来的感叹号
	Description:
	Author:miaoyuting
	Created:2019-11-08 
	Last Change:
	Functions:
*/
#include <stdio.h>
int main(void)
{
	char ch;
	int i=0,j=0;
	ch=getchar();
	while(ch!='#'){
		if(ch=='.'){
			putchar('!');
			i++;
		}
		else if(ch=='!'){
			putchar('!');
			putchar('!');
			j++;
		}
		else{
			putchar(ch);
		}
		ch=getchar();
	}
	printf("\n");
	printf(".替换了%d次,!替换了%d次",i,j);
	return 0;
}

5.编写程序读取输入,读到#中止,报告ei出现的次数。

注意 该程序要记录前一个字符和当前字符。用“Receive your eieio award”这样的输入来测试。post

/**
	Module Name: 记录ei的个数 
	Description:
	Author:miaoyuting
	Created:2019-11-08 
	Last Change:
	Functions:
*/
#include <stdio.h>
int main(void)
{
	char ch;
	int j=0;
	char ch1=" ",ch2;
	ch=getchar();
	while(ch!='#'){
		ch2=ch;
		if(ch1=='e' && ch2=='i'){
			j++;
		}
		ch1=ch2;
		ch=getchar();
	}
	printf("出现%d次ei",j);
	return 0;
}

6.编写一个程序,提示用户输入一周工做的小时数,而后打印工资总额、税金和净收入。作以下假设:

a.基本工资 = 1000美圆/小时 b.加班(超过40小时) = 1.5倍的时间 c.税率: 前300美圆为15% 续150美圆为20% 余下的为25% 用#define定义符号常量。不用在乎是否符合当前的税法。测试

/**
	Module Name: 计算税率 
	Description:
	Author:miaoyuting
	Created:2019-11-08 
	Last Change:
	Functions:
*/
#include<stdio.h>
#define BASEPAY 10
#define BASEHRS 40
#define OVERTIME 1.5
#define AMT1 300
#define AMT2 150
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
int main()
{
	int hours,gross,net,taxes;
	printf("Enter the number of hours you worked this week\n");
	scanf("%d",&hours);
	if(hours<=BASEHRS){
		gross=hours*BASEPAY;
	}
	else{
		gross=BASEHRS*BASEPAY+(hours-BASEHRS)*OVERTIME*BASEPAY;
	}
	if(gross<=AMT1){
		taxes=gross*RATE1;
	}
	else if(gross<=AMT1+AMT2){
		taxes=AMT1*RATE1+(gross-AMT1)*RATE2;
	}
	else{
		taxes=AMT1*RATE1+AMT2*RATE2+(gross-AMT1)*RATE3;
	}
	net=gross-taxes;
	printf("工资总额:%d;税金:%d;净收入:%d",gross,taxes,net);
	return 0;
}

7.编写一个程序,只接受正整数输入,而后显示全部小于或等于该数的素数。

/**
	Module Name: 
	Description:程序,只接受正整数输入,显示小于或等于该数的全部素数 
	Author:miaoyuting
	Created:2019-11-08 
	Last Change:
	Functions:
*/
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
int main(void)
{
	int limit,num,div;
	bool numIsPrime;
	printf("please input a postive interger\n");
	while(scanf("%d",&limit)==1 && limit>=0){
		if(limit>1){
			printf("here are the prime number upthrough%d\n",limit);
		}
		else{
			printf("no prime!\n");
		}
		for(num=2;num<=limit;num++){
			for(div=2,numIsPrime=true;div<=sqrt(num);div++){
				if(num%div==0){
					numIsPrime=false;
				}
			}
			if(numIsPrime){
				printf("%d is prime\n",num);
			}
		}
	}
	return 0;
}

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

/**
	Module Name: 税金计算 
	Description:
	Author:miaoyuting
	Created:2019-11-08 
	Last Change:
	Functions:
*/
#include <stdio.h>
#define RATE1 0.15
#define RATE2 0.28
#define BASEHRS1 17850
#define BASEHRS2 23900
#define BASEHRS3 29750
#define BASEHRS4 14875
int main(void)
{
	int gross,net;
	int type;
	printf("1)单身          2)户主\n3)已婚,共有    4)已婚,离异\n");
	scanf("%d",&type);
	printf("请输入你的工资\n");
	scanf("%d",&gross) ;
	while(type!=0){
		switch(type){
		case 1:
			if(gross<=BASEHRS1){
				net=gross*RATE1;
			}
			else{
				net=BASEHRS1*RATE1+(gross-BASEHRS1)*RATE2;
			}
			break;
		case 2:
			if(gross<=BASEHRS2){
				net=gross*RATE1;
			}
			else{
				net=BASEHRS2*RATE1+(gross-BASEHRS2)*RATE2;
			}
			break;
		case 3:
			if(gross<=BASEHRS3){
				net=gross*RATE1;
			}
			else{
				net=BASEHRS3*RATE1+(gross-BASEHRS3)*RATE2;
			}
			break;
		case 4:
			if(gross<=BASEHRS4){
				net=gross*RATE1;
			}
			else{
				net=BASEHRS4*RATE1+(gross-BASEHRS4)*RATE2;
			}
			break;
	} 
	printf("应缴纳税金:%d\n",net);
	printf("1)单身          2)户主\n3)已婚,共有    4)已婚,离异\n");
	scanf("%d",&type);
	printf("请输入你的工资\n");
	scanf("%d",&gross) ;
	}
	return 0;
}

9.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磅的甜菜。而后,该程序要计算货物总价、折扣(若是有的话)、运费和包装费。随后,程序应显示全部的购买信息:物品售价、订购的重量(单位:磅)、订购的蔬菜费用、订单的总费用、折扣(若是有的话)、运费和包装费,以及全部的费用总额。

/**
	Module Name: 菜价计算 
	Description:
	Author:miaoyuting
	Created:2019-11-08 
	Last Change:
	Functions:
*/
#include <stdio.h>
#define PRICE1 2.05
#define PRICE2 1.15
#define PRICE3 1.09
#define FREIGHT1 6.5
#define FREIGHT2 14
#define FREIGHT3 0.5
int main(void)
{
	int net1=0,net2=0,net=0,discount=0,freight=0;
	int weight1=0,weight2=0,weight3=0,weight=0;
	int type;
	printf("1)洋蓟          2)甜菜\n3)胡萝卜    4)退订\n");
	scanf("%d",&type);
	while(type!=4){
		switch(type){
			case 1:
				printf("输入重量:\n");
				scanf("%d",&weight1); 
				break;
			case 2:
				printf("输入重量:\n");
				scanf("%d",&weight2);
				break;
			case 3:
				printf("输入重量:\n");
				scanf("%d",&weight3);  
				break;
		}
		printf("1)洋蓟          2)甜菜\n3)胡萝卜    4)退订\n");
		scanf("%d",&type);
	}
	net1=weight1*PRICE1+weight2*PRICE2+weight2*PRICE3;
	if(net1<=100){
		net2=net2;
	}
	else{
		discount=net1*0.05;
		net2=net1-discount;
	}
	weight=weight1+weight2+weight3;
	if(weight<=5){
		freight=FREIGHT1;
	}
	else if(weight<=20){
		freight=FREIGHT2;
	}
	else{
		freight=FREIGHT2+(weight-20)*FREIGHT3;
	}
	net=net2+freight;
	printf("洋蓟售价为 2.05 美圆/磅,甜菜售价为 1.15美圆/磅\n胡萝卜售价为 1.09美圆/磅");
	printf("洋蓟:%d磅,甜菜:%d磅,胡萝卜:%d磅\n",weight1,weight2,weight3);
	printf("订购的蔬菜费用%d,订单的总费用%d,折扣%d\n",net1,net2,discount);
	printf("运费和包装费%d,全部的费用总额%d\n",freight,net);
	return 0;
}

10.使用switch重写练习4

/**
	Module Name: 同4 
	Description:
	Author:miaoyuting
	Created:2019-11-08 
	Last Change:
	Functions:
*/
#include <stdio.h>
int main(void)
{
	char ch;
	int ct1=0,ct2=0;
	while((ch=getchar())!='#'){
		switch(ch){
			case'.':
				putchar('!');
				ct1++;
				break;
			case'!':
				putchar('!');
				putchar('!');
				ct2++;
				break;
			default:
				putchar(ch); 
		}
	}
	
	printf("\n%d replacement(s) of . with\n",ct1);
	printf("%d replacement(s) of ! with\n",ct2);
	return 0;
}

11.修改练习6的假设a,让程序能够给出一个供选择的工资等级菜单。使 用switch完成工资等级选择。运行程序后,显示的菜单应该相似这样:

Enter the number corresponding to the desired pay rate or action:ui

$8.75/hr              2) $9.33/hrthis

$10.00/hr             4) $11.20/hrcode

quitip

若是选择 1~4 其中的一个数字,程序应该询问用户工做的小时数。程 序要经过循环运行,除非用户输入 5。若是输入 1~5 之外的数字,程序应 提醒用户输入正确的选项,而后再重复显示菜单提示用户输入。使用#define 建立符号常量表示各工资等级和税率。ci

/**
	Module Name: 计算税率 
	Description:
	Author:miaoyuting
	Created:2019-11-08 
	Last Change:
	Functions:
*/
#include<stdio.h>
#define BASEHRS 40
#define OVERTIME 1.5
#define AMT1 300
#define AMT2 150
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
int main()
{
	int hours;
	float gross,net,taxes,BASEPAY;
	int type;
	printf("1) $8.75/hr       2) $9.33/hr\n3) $10.00/hr       4) $11.20/hr\n5) quit\n");
	scanf("%d",&type);
	while(type!=5){
		switch(type){
			case 1:
				BASEPAY=8.75;
				break;
			case 2:
				BASEPAY=9.33;
				break;
			case 3:
				BASEPAY=10.00;
				break;
			case 4:
				BASEPAY=11.20;
				break;
		} 
	printf("Enter the number of hours you worked this week\n");
	scanf("%d",&hours);
	if(hours<=BASEHRS){
		gross=hours*BASEPAY;
	}
	else{
		gross=BASEHRS*BASEPAY+(hours-BASEHRS)*OVERTIME*BASEPAY;
	}
	if(gross<=AMT1){
		taxes=gross*RATE1;
	}
	else if(gross<=AMT1+AMT2){
		taxes=AMT1*RATE1+(gross-AMT1)*RATE2;
	}
	else{
		taxes=AMT1*RATE1+AMT2*RATE2+(gross-AMT1)*RATE3;
	}
	net=gross-taxes;
	printf("工资总额:%.2f;税金:%.2f;净收入:%.2f\n",gross,taxes,net);
	printf("1) $8.75/hr       2) $9.33/hr\n3) $10.00/hr       4) $11.20/hr\n5) quit\n");
	scanf("%d",&type);
	}
	return 0;
}