C Primer Plus sixth edition programming exercise Chapter 7 answers

Keywords: C stm32 ARM

1. Write a program to read the input, stop reading the # characters, and then report the number of spaces, line breaks and all other characters.

/*7.12*/
#include<stdio.h>
#define STOP '#'
#define BLACK ' '
#define NEWLINE '\n'
int main()
{
	char ch;
	int n_black = 0;
	int n_NEWLINE = 0;
	int n_others = 0;
	printf("please enter the char:");
	while ((ch = getchar()) != STOP)
	{
		printf("%c", ch);         //**Print one character at a time
		if (ch == BLACK)
		{
			n_black++;
		}
		else if (ch == NEWLINE)
		{
			n_NEWLINE++;
		}
		else
			n_others++;
		
	}
	printf("%d%d%d", n_black, n_NEWLINE, n_others);
	return 0;
}

2. Write a program to read the input and stop reading the # character. The program prints each input character and the corresponding ASCII code (decimal). Print 8 characters per line. Recommendation: use the character count and modulus operator (%) to print a line break every 8 cycles.

/*2*/
#include<stdio.h>
#define STOP '#'
int main()
{
	char ch;
	int i = 0;
	printf("please enter:\n");
	while ((ch = getchar()) != STOP)
	{
		i++;
	 printf("%c,%d\t", ch, ch);
	 if (i % 8 == 0)
		 printf("\n");
	}
}

3. Write a program to read integers until the user enters 0. After input, the program should report the number of even numbers (excluding 0), the average value of these even numbers, the number of odd numbers and the average value of odd numbers.

/*3*/
#include<stdio.h>
int main()
{
	int int_num;
	int opp_num = 0, pair_num = 0;
	float average_opp, average_pair;
	int opp_sum = 0, pair_sum = 0;
	printf("please enter a int num(0 to stop)\n");
	while ((scanf_s("%d", &int_num) == 1) && int_num !=0)
	{
		if (int_num % 2 == 1)                     
	//How to find the average? To understand the principle of while, although I input a lot of numbers at the beginning, while is tested one by one.
		{
			opp_num += 1;
			opp_sum += int_num;
			average_opp = opp_sum / opp_num;
		}
		else
		{
			pair_num += 1;
			pair_sum += int_num;
			average_pair = pair_sum / pair_num;
		}
	}
	printf("opp:%d pair:%d\n", opp_num, pair_num);
	printf("the average of opp is:%f\n,the average of pair is:%f", average_opp, average_pair);
	return 0;
}

4. Use the if else statement to write a program to read the input until it # stops. Use exclamation point instead of full stop, use two exclamation points instead of the original exclamation point, and finally report how many times it has been replaced.
 

/*4*/
#include<stdio.h>
#define STOP '#'
#define GANTAN '!'
#define JUHAO '.'
int main()
{
	char ch;
	int count = 0;
	while ((ch = getchar()) != STOP)
	{
		if (ch == '.')
		{
			putchar(GANTAN);
			count++;
		}
		else if (ch == '!')
		{
			putchar(GANTAN);
			putchar(GANTAN);
			count++;
		}
		else
			putchar(ch);
	}
	printf("exchange time:%d", count);
	return 0;
}

5. Rewrite exercise 4 with switch.

/*4*/
#include<stdio.h>
#define STOP '#'
#define GANTAN '!'
#define JUHAO '.'
int main()
{
	char ch;
	int count = 0;
	while ((ch = getchar()) != STOP)
	{
		switch(ch)
		{
			case '.':
			{
				putchar(GANTAN);
				count++;
				break;
			}
			case  '!':
			{
				putchar(GANTAN);
				putchar(GANTAN);
				count++;
				break;
			}
			default:
			putchar(ch);
		}
	}
	printf("exchange time:%d", count);
	return 0;
}

6. Write a program to read the input, read to # stop, and report the number of ei occurrences.

/*6*/
#include<stdio.h>
#define STOP '#'
int main()
{
	char ch;
	int first;
	int count = 0;
	printf("please enter the char\n");
	while ((ch = getchar()) != STOP)
	{
		if (ch == 'e')
			first = 1;
		else 
		{
			if(ch == 'i' && first == 1)
					count++;
			first = 0;
		}
	}
	printf("%d", count);
	return 0;
}

7. Write a program to prompt the user to enter the number of working hours a week, and then print the total salary, tax and net income. Make the following assumptions: A. basic salary = 10 dollars / hour b. overtime (more than 40 hours) = 1.5 times the time c. tax rate: 15% for the first 300 dollars, 20% for the second 150 dollars, and 25% for the rest. Use #define to define symbolic constants. Don't worry about compliance with the current tax law.

/*7*/
#include <stdio.h>
#define BASE_AVERAGE 10
#define OVER_TIME 1.5
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25
#define FIRST_SUM 300 * 0.85
#define NEXT_SUM 150*0.8
int main()
{
	float work_t;
	float sum_average = 0;
	printf("plesse enter your work time");
	while (scanf_s("%f", &work_t) == 1)
	{
		if (work_t < 300 / 10)
			printf("%lf", (double)10 * work_t * (1 - 0.15));
		else if (work_t < 400 / 10)
			printf("%lf", FIRST_SUM + (double)(work_t * 10 - 300) * 0.8);
		else
			printf("%f", FIRST_SUM + NEXT_SUM + (work_t - 40) * 1.5 * 10 * 0.75);
	}
	return 0;
}

8. Modify hypothesis a of exercise 7 so that the program can give an optional salary scale menu. Use switch to complete salary grade selection. After running the program, the displayed menu should be similar to this: Enter the number corresponding to the desired pay rate or action:1) .75/hr 2) .33/hr 3).00/hr 4) .20/hr 5) quit if you select one of the numbers 1 to 4, the program should ask the user for the number of working hours. The program will run through a loop unless the user enters 5. If a number other than 1 ~ 5 is entered, the program shall remind the user to enter the correct option, and then repeatedly display the menu to prompt the user for input. Use #define to create symbolic constants to represent each wage grade and tax rate

/*8*/
#include<stdio.h>
#include<stdio.h>
#define NON_OVERTIME 40.00
#define BASIC 10.00
#define FIRST_HOUR 300.00
#define NEXT_HOUR 450.00
#define FIRST_RATE 0.15
#define NEXT_RATE 0.20
#define LAST_RATE 0.25

int main()
{
	float per_hour;
	char ch;
	int x = 1;
	int num;
	float hour, sum, tax, gain;
	printf("pelase 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) $12.20/hr\n");
	printf("5) quit\n");
	printf("Which is your wages per hour.\n");
	scanf_s("%d", &num);
	while (x)  //**
	{
		switch (num)
		{
		case (1):
			per_hour = 8.73;
			x = 0;
			break;
		case (2):
			per_hour = 9.33;
			x = 0;
			break;
		case (3):
			per_hour = 10.00;
			x = 0;
			break;
		case (4):
			per_hour = 12.20;
			x = 0;
			break;
		case (5):
			return 0;
			break;
		default:
			printf("please enter the num from 1 to 5:");
			scanf_s("%d", &num);
		}
	}

	printf("Please enter you work hour a week.\n");
	scanf_s("%f", &hour);
	if (hour > NON_OVERTIME)
		hour = NON_OVERTIME + (hour - NON_OVERTIME) * 1.5;

	sum = per_hour * hour;
	if (sum <= FIRST_HOUR)
	{
		tax = sum * 0.15;
		gain = sum - tax;
	}
	else if (sum > FIRST_HOUR && sum <= NEXT_HOUR)
	{
		tax = FIRST_HOUR * 0.15 + (sum - FIRST_HOUR) * 0.20;
		gain = sum - tax;
	}
	else
	{
		tax = FIRST_HOUR * 0.15 + 150 * 0.2 + (hour - 450) * 0.25;
		gain = sum - tax;
	}
	printf("real hour:%.f  perhour:%.2f  tax:%.2f  gain:%.2f", hour, per_hour, tax, gain);

	return 0;
}

9. Write a program to accept only positive integer input, and then display all prime numbers less than or equal to the number.

/*9*/
#include<stdio.h>
void shu_num(int n);
int main()
{
	int x = 1;
	int num;
	printf("please enter a num:\n");
	while (x)
	{
		while ((scanf_s("%d", &num) == 1) && num > 0)
		{
			shu_num(num);
		}
		printf("please enter a num(>0):\n");
		continue;
	}
	return 0;
}
void shu_num(int n)
{
	int p = 1;
	for (int j = n; j > 2; j--)
	{
		int q = 1;
		for (int i = 2; i < j; i++)
		{
			if (j % i != 0)
			{
				p = 1;
			}
			else
				p = 0;
			q *= p;
		}
		if (q == 1)
		{
			printf("all the shu_num %d\n", j);
		}
	}
}

10. The United States federal tax plan in 1988 is the simplest tax plan in modern times. It is divided into four categories, each with two levels. The following is a summary of the tax plan (dollars are taxable income): for example, a single taxpayer with a salary of $20000 should pay tax of 0.15 × 17850+0.28 × (20000 − 17850) USD. Write a program to let users specify the type of tax payment and taxable income, and then calculate the tax. The program shall allow the user to enter multiple times through a loop.

/*10*/
#include<stdio.h>
#define ODD_SIDE 17850
#define OWNER 23900
#define MARRY 29750
#define UN_MARRY 14875
#define FIREST_PER 0.15
#define OVER_PER 0.28
int main()
{
	int num, money;
	float tax_num;
	printf("choose the type of taxer:\n");
	printf("1.ODD_SIDE			2.OWNER\n");
	printf("3.MARRY				4.UN_MARRY\n");
	scanf_s("%d", &num);
	printf("enter your money:\n");
	scanf_s("%d", &money);
	while (1)
	{
		switch (num)
		{
		case(1):
			if (money <= ODD_SIDE)
				tax_num = FIREST_PER * money;
			else
				tax_num = FIREST_PER * ODD_SIDE + (money - ODD_SIDE) * OVER_PER;
			break;
		case(2):
			if (money <= OWNER)
				tax_num = FIREST_PER * money;
			else
				tax_num = FIREST_PER * money + (money - OWNER) * OVER_PER;
			break;
		case(3):
			if (money <= MARRY)
				tax_num = FIREST_PER * money;
			else
				tax_num = FIREST_PER * money + (money - MARRY) * OVER_PER;
			break;
		case(4):
			if (money <= UN_MARRY)
				tax_num = FIREST_PER * money;
			else
				tax_num = FIREST_PER * money + (money - UN_MARRY) * OVER_PER;
			break;
		}
		printf("you need to tax :%f\n\n", tax_num);
		printf("choose the type of taxer:\n");
		printf("1.ODD_SIDE			2.OWNER\n");
		printf("3.MARRY				4.UN_MARRY\n");
		scanf_s("%d", &num);
		printf("enter your money:\n");
		scanf_s("%d", &money);
	}
	return 0;
}

11.ABC mail order grocery stores sell artichokes at $2.05/lb, beets at $1.15/lb and carrots at $1.09/lb. There is a 5% discount on $100 orders before shipping is added. For orders less than or equal to 5 pounds, the freight and packaging fee of US $6.5 will be charged. For orders from 5 pounds to 20 pounds, the freight and packaging fee of US $14 will be charged. For orders over 20 pounds, the freight and packaging fee of US $0.5 will be increased for each additional pound on the basis of US $14. Write a program. In the loop, use the switch statement to realize that the user has different responses when inputting different letters, that is, the response of inputting a is to let the user input the pounds of artichokes, b is the pounds of beets, c is the pounds of carrots, and q is to exit the order. The program records the accumulated weight. That is, if the user enters 4 pounds of beet and then 5 pounds of beet, the program should report 9 pounds of beet. The program then calculates the total price of the goods, discounts (if any), freight and packaging costs. Then, the program should display all purchase information: the selling price of the item, the weight ordered (in pounds), the cost of vegetables ordered, the total cost of the order, discounts (if any), freight and packaging costs, and the total amount of all costs.

/*11*/
#include<stdio.h>
#define YANGCHONG 2.05
#define HUCAI 1.15
#define HULUOBO 1.09
#define OVER_HUNDERD 0.05
#define LOW_FIVE 6.5
#define FIBV_TWENTY 14
#define OVER_TWENTY 0.5
int main()
{
	int x = 1;
	int pounds;
	char ch;
	float sum_money_1 , sum_money_2 ,sum_money_3, sale, move_and_pack_money_1, move_and_pack_money_2, move_and_pack_money_3;
	float sum_money, move_and_pack_money;
	sum_money_1 = sum_money_2 = sum_money_3 = sale = move_and_pack_money_1 = move_and_pack_money_2=0;
	move_and_pack_money_3 = sum_money = move_and_pack_money = 0;
	printf("enter the kind you want to buy:\n");
	printf("a.the pounds of onion			b.the pounds of beet\n");
	printf("c.the pounds of Carrot			q.quit\n");
	scanf_s("%c", &ch);
	while (x)
	{
		switch (ch)
		{
		case('a'):
			printf("enter the pounds:");
			scanf_s("%d", &pounds);
			if (pounds <= 5)
			{
				move_and_pack_money_1 = LOW_FIVE;
				sum_money_1 = YANGCHONG * pounds + move_and_pack_money_1;
			}
			else if (pounds <= 20)
			{
				move_and_pack_money_1 = FIBV_TWENTY;
				sum_money_1 = YANGCHONG * pounds + move_and_pack_money_1;
			}
			else if (pounds <= 100/2.05)
			{
				move_and_pack_money_1 = FIBV_TWENTY + OVER_TWENTY * (pounds - 20);
				sum_money_1 = YANGCHONG * pounds + move_and_pack_money_1;
			}
			else
			{
				move_and_pack_money_1 = FIBV_TWENTY + OVER_TWENTY * (pounds - 20);
				sum_money_1 = YANGCHONG * pounds * OVER_TWENTY + move_and_pack_money_1;
			}
		case ('b'):
			printf("enter the pounds:");
			scanf_s("%d", &pounds);
			if (pounds <= 5)
			{
				move_and_pack_money_2 = LOW_FIVE;
				sum_money_2 = HUCAI * pounds + move_and_pack_money_2;
			}
			else if (pounds <= 20)
			{
				move_and_pack_money_2 = FIBV_TWENTY;
				sum_money_2 = HUCAI * pounds + move_and_pack_money_2;
			}
			else if (pounds <= 100 / 2.05)
			{
				move_and_pack_money_2 = FIBV_TWENTY + OVER_TWENTY * (pounds - 20);
				sum_money_2 = HUCAI * pounds + move_and_pack_money_2;
			}
			else
			{
				move_and_pack_money_3 = FIBV_TWENTY + OVER_TWENTY * (pounds - 20);
				sum_money_2 = HUCAI * pounds * OVER_TWENTY + move_and_pack_money_3;
			}
		case ('c'):
			printf("enter the pounds:");
			scanf_s("%d", &pounds);
			if (pounds <= 5)
			{
				move_and_pack_money_3 = LOW_FIVE;
				sum_money_3 = HULUOBO * pounds + move_and_pack_money_3;
			}
			else if (pounds <= 20)
			{
				move_and_pack_money_3 = FIBV_TWENTY;
				sum_money_3 = HULUOBO * pounds + move_and_pack_money_3;
			}
			else if (pounds <= 100 / 2.05)
			{
				move_and_pack_money_3 = FIBV_TWENTY + OVER_TWENTY * (pounds - 20);
				sum_money_3 = HULUOBO * pounds + move_and_pack_money_3;
			}
			else
			{
				move_and_pack_money_3 = FIBV_TWENTY + OVER_TWENTY * (pounds - 20);
				sum_money_3 = HULUOBO * pounds * OVER_TWENTY + move_and_pack_money_3;
			}
			x = 0;
			break;
		case ('q'):
			x = 0;
			break;
		default:
			break;
		}
	}
	sum_money = sum_money_1 + sum_money_2 + sum_money_3;
	move_and_pack_money = move_and_pack_money_1 + move_and_pack_money_2 + move_and_pack_money_3;
	printf("sum money is :%f \t move_pack_mony is:%f", sum_money, move_and_pack_money);
	return 0;
}

Posted by allexx_d on Thu, 02 Dec 2021 17:49:53 -0800