家庭做業——苗鈺婷

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;
}
相關文章
相關標籤/搜索