天天看點

C Primer Plus 第七章:分支和跳轉 程式設計答案

7.16 程式設計練習

  1. 編寫一個程式讀取輸入,讀到#字元停止,然後報告讀取的空格數、換行數和其他所有字元的數量。
#include <stdio.h>
int main()
{
	int num_n = 0;
	int num_space = 0;
	int num_else = 0;
	char ch;
	while (scanf("%c", &ch) == 1)
	{
		if (ch == '#')
			break;
		else if (ch == ' ')
			num_space++;
		else if (ch == '\n')
			num_n++;
		else
			num_else++;
	}
	printf("num_n:%3d\num_else:%3d\nnum_space:%3d\n", \
	num_n, num_else, num_space);
	return 0;
}
           
  1. 編寫一個程式讀取輸入,讀到#字元停止。程式要列印每個輸入的字元以及對應的ASCII碼(十進制)。一行列印8個字元。建議:使用字元計數和求模運算符(%)在每8個循環周期時列印一個換行符。
#include <stdio.h>
#define SIZE 8
int main()
{
	char ch;
	int num = 1;
	while (scanf("%c", &ch) == 1)
	{
		if (ch == '#')
		{
			break;
		}
		else
		{
			if (num % SIZE == 0)
				printf("%-5d\n",ch);
			else
				printf("%-5d",ch);
			num++;
		}
	}
	return 0;
}
           
  1. 編寫一個程式,讀取整數直到使用者輸入 0。

    輸入結束後,程式應報告使用者輸入的偶數(不包括 0)個數、

    這些偶數的平均值、輸入的奇數個數及其奇數的平均值

#include <stdio.h>
int main()
{
	int num_even = 0;
	int num_odd = 0;
	int sum_even =0;
	int sum_odd = 0;
	int num;
	while (scanf("%d", &num) == 1)
	{
		if (num == 0)
		{
			break;
		}
		else if (num % 2 == 0)
		{
			num_even++;
			sum_even +=num;
		}
		else
		{
			num_odd++;
			sum_odd +=num;
		}
	}
	printf("num_even:%-5d,num_odd:%-5d\n", num_even, num_odd);
	printf("sum_even:%-5d,sum_odd:%-5d\n", sum_even, sum_odd);
	printf("ave_even:%-5d,ave_odd:%-5d\n",sum_even / num_even, \
	sum_odd / num_odd);
	return 0;
}
           
  1. 使用if else語句編寫一個程式讀取輸入,讀到#停止。用感歎号代替句号,用兩個感歎号代替原來的感歎号,最後報告進行了多少次替代。
#include <stdio.h>
int main()
{
	int i = 0, j = 0;
	char ch;
	while ((ch = getchar()) != '#')
	{
		if (ch == '.')
		{
			putchar('!');
			i++;
		}
		else if (ch == '!')
		{
			putchar('!');
			putchar('!');
			j++;
			
		}
		else
		{
			putchar(ch);
		}
	}
	printf("\nthe times of '.', replaced:\n:%-5d\n", i);
	printf("the times of '!' replaced:\n%-5d\n", j);
	return 0;
}
           
  1. 用switch重寫練習4。
#include <stdio.h>
int main()
{
	int i = 0, j = 0;
	char ch;
	while ((ch = getchar()) != '#')
	{
		switch (ch)
		{
		case '.':
			{
				putchar('!');
				i++;
				break;
			}
		case '!':
			{
				putchar('!');
				putchar('!');
				break;
				j++;
			}
		default:
			{
				putchar(ch);
			}
		}
	}
	printf("\nthe times of '.', replaced:\n:%-5d\n", i);
	printf("the times of '!' replaced:\n%-5d\n", j);
	return 0;
}
           
  1. 編寫程式輸入,讀到#停止,報告ei出現的次數。
#include <stdio.h>
int main()
{
	int count = 0;
	char ch, ch_last;
	while ((ch = getchar()) != '\n')
	{
		if (ch == 'i' && ch_last == 'e')
		{
			count++;
			ch_last = ch;
		}
		else
		{
			ch_last = ch;
		}
	}
	printf("\nthe times of occurrences of 'ei' is:%-2d\n",count);
	return 0;
}
           
  1. 編寫一個程式,提示使用者輸入一周工作的小時數,然後列印工資總額、稅金和淨收入 做如下假設:

    a.基本工資 = 1000美元 / 小時

    b.加班(超過40小時) = 1.5倍時間

    c.稅率: 前300美元為15% 續150美元為20% 餘下的為25%

    用#define定義符号常量,不用在意是否符合目前的稅法

#include<stdio.h>
#define TIME_LIMIT 40
#define PAY_HOUR 10
int main()
{
	float workhours, pre_tax, after_tax;
	printf("please enter your working hours:");
	while(scanf("%f", &workhours) == 1)
	{
		if (workhours <= TIME_LIMIT)
		{
			pre_tax = PAY_HOUR * workhours;
		}
		else
		{
			pre_tax = PAY_HOUR * (TIME_LIMIT + (workhours - TIME_LIMIT) * 1.5);
		}

		if (pre_tax <=300)
		{
			after_tax = pre_tax - pre_tax * 0.15;
		}
		else if (pre_tax > 300 && pre_tax <=450)
		{
			after_tax = pre_tax - 300 * 0.15 - (pre_tax - 300) * 0.2;
		}
		else
		{
			after_tax = pre_tax - 300 * 0.15 - 150 * 0.2 - (pre_tax - 450) * 0.25;
		}
		printf("your salary before tax is:%-5.2f\n", pre_tax);
		printf("your salary after tax is:%-5.2f\n", after_tax);
		printf("please enter your working hours again or enter 'q' to quit:");
	}
	return 0;
}
           
  1. 修改練習7的假設a,讓程式可以給出一個供選擇的工資等級菜單

    使用 switch 完成工資等級的選擇。

    運作程式後,顯示的菜單應該類似這樣:

    *******************************************************************************

    Enter the number corresponding to the desired pay rate of 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 TIME_LIMIT 40
int main()
{
	int i, pay_level;
	float workhours, pre_tax, after_tax, pay_hour;
	for (i = 1; i <= 65; i++)
		putchar('*');
	printf("\nEnter the number corresponding to the desired pay rate or action:\n");
	printf("%d) $8.75/hr%-20d) $9.33/hr\n%d) $10.00/hr%-20d) $11.20/hr\n%d) quit)\n", 1, 2, 3, 4, 5);
	for (i = 1; i <= 65; i++)
		putchar('*');
	printf("\n");
	while(scanf("%d", &pay_level) == 1)
	{
		if (pay_level >=0 && pay_level <=4)
		{
			printf("Enter your working hours:");
			scanf("%f", &workhours);
			switch (pay_level)
			{
			case 1:
				{
					pay_hour = 8.75;
					break;
				}
			case 2:
				{
					pay_hour = 9.33;
					break;
				}
			case 3:
				{
					pay_hour = 10.00;
					break;
				}
			case 4:
				{
					pay_hour = 11.20;
					break;
				}
			default:
				/*do nothing*/;
			}
		}
		else if (pay_level == 5)
		{
			break;
		}
		else
		{
			printf("Enter the correct number, only 1-5 is availble:\n");
		}
		if (workhours <= TIME_LIMIT)
		{
			pre_tax = pay_hour * workhours;
		}
		else
		{
			pre_tax = pay_hour * (TIME_LIMIT + (workhours - TIME_LIMIT) * 1.5);
		}

		if (pre_tax <=300)
		{
			after_tax = pre_tax - pre_tax * 0.15;
		}
		else if (pre_tax > 300 && pre_tax <=450)
		{
			after_tax = pre_tax - 300 * 0.15 - (pre_tax - 300) * 0.2;
		}
		else
		{
			after_tax = pre_tax - 300 * 0.15 - 150 * 0.2 - (pre_tax - 450) * 0.25;
		}
		printf("your salary before tax is:%-5.2f\n", pre_tax);
		printf("your salary after tax is:%-5.2f\n", after_tax);
		printf("please enter the number again or enter 5 to quit:");
	}
	return 0;
}
           
  1. 編寫一個程式,隻接受正整數輸入,然後顯示所有小于等于該數的素數。
#include<stdio.h>
int main()
{
	int i, j, counter, num;
	printf("please enter the integer:");
	scanf("%d", &num);
	while (num > 0)
	{
		for (i = 1; i <= num; i++)
		{
			for (j = 1, counter = 0; j <= i; j++)
			{
				if (i % j == 0)
				{
					counter++;
					if (counter > 2)
					{
						break;
					}
					else
					{
						/*do nothing*/
					}
				}
				else
				{
					/*do nothing*/
				}
			}
			if (counter == 2)
			{
				printf("%-5d\n", i);
			}
			else
			{
				/*do nothing*/
			}
		}
		break;
	}
	return 0;
}
           
  1. 1988年的美國聯邦稅收計劃是近代最簡單的稅收方案。它分為4個類别,每個類别有兩個等級。下面是稅收計劃的摘要(美元數為應征稅的收入):

    類别 稅金

    單身------------------------17850美元按15%計,超出部分按28%計

    戶主------------------------23900美元按15%計,超出部分按28%計

    已婚,共有---------------29750美元按15%計,超出部分按28%計

    已婚,離異---------------14875美元按15%計,超出部分按28%計

    例如:一位工資為20000美元的單身納稅人,應繳納稅費 0.15x17850+0.28x(20000-17850)美元。編寫一個程式,讓使用者指定繳納稅金的種類和應納稅收入,然後計算稅金。程式應通過循環讓使用者可以多次輸入。

#include<stdio.h>
#define RATE1 0.15
#define RATE2 0.28
#define LEVEL1 17850
#define LEVEL2 23900
#define LEVEL3 29750
#define LEVEL4 14875

int main()
{
	int category;
	float pre_tax, after_tax, tax_limit;
	printf("%d)單身%23d)戶主\n%d)已婚、共有%17d已婚、離異\nplease enter the number corresponding your category correctly as above:", 1, 2, 3, 4);
	while (scanf("%d", &category) == 1)
	{
		if (category >= 1 && category <= 4)
		{
			switch (category)
			{
			case 1:
				{
					tax_limit = LEVEL1;
					break;
				}
			case 2:
				{
					tax_limit = LEVEL2;
					break;
				}
			case 3:
				{
					tax_limit = LEVEL3;
					break;
				}
			case 4:
				{
					tax_limit = LEVEL4;
					break;
				}
			}
		}
		else
		{
			printf("the number is error, enter 'q' to quit or enter number again:");
			continue;
		}
		printf("please enter your salary:");
		scanf("%f", &pre_tax);
		if (pre_tax <= tax_limit)
		{
			after_tax = pre_tax * (1 - RATE1);
		}
		else
		{
			after_tax = pre_tax - tax_limit *RATE1 - (pre_tax - tax_limit) * RATE2;
		}
		printf("tax_limit:%-5.2f\nsalary before tax:%-5.2f\nsalary after tax:%-5.2f\n", tax_limit, pre_tax, after_tax);
		printf("please continue to enter the number corresponding your category:");
	}
	printf("your input is not a number, THX!");
	return 0;
}
           
  1. 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退出訂購。程式要記錄累計的重量。然後,該程式要計算貨物總價、折扣(如果有的話)、運費和包裝費。随後,程式要顯示所有的購買資訊:物品售價、訂購的重量(磅)、訂購的蔬菜費用、訂單的總費用、折扣(如果有的話)、運費和包裝費、以及所有的費用總額。
#include<stdio.h>
#define ARTICHOKE 2.05
#define BEET 1.15
#define CARROT 1.09
#define DISCOUNT 0.05
#define LEVEL1 6.5
#define LEVEL2 14
#define RATE 0.5

int main()
{
	char name_veg;
	float weight, price_unit, price_total, pay, freight;
	printf("%7d)a means artichoke\n%7d)b means beet\n%7d)c means carrot\n%7d)q means quit\n", 1, 2, 3, 4);
	printf("Please enter initials of the vegetable name you want to buy:");
	while (scanf("%c", &name_veg) == 1)
	{
		switch (name_veg)
		{
		case 'a':
			{
				price_unit = ARTICHOKE;
				break;
			}
		case 'b':
			{
				price_unit = BEET;
				break;
			}
		case 'c':
			{
				price_unit = CARROT;
				break;
			}
		case 'q':
			{
				break;
			}

		}
		printf("enter how many pounds you need:");
		scanf("%f", &weight);
		price_total = price_unit * weight;
		if (price_total >= 100)
		{
			pay = price_total * (1 - DISCOUNT);
		}
		else
		{
			pay = price_total;
		}
		if (weight <= 5)
		{
			freight = LEVEL1;
		}
		else if (weight >5 && weight <=20)
		{
			freight = LEVEL2;
		}
		else
		{
			freight = LEVEL2 + (weight - 20) * RATE;
		}
		printf("the money you should pay is:%-5.2f", pay + freight);
		break;
	}
	return 0;
}