C programming Tan Haoqiang fifth edition after class answers Chapter IV exercise answers

Posted by Squallyboy on Thu, 17 Feb 2022 14:07:47 +0100

1. What is arithmetic operation? What is relational operation? What is logical operation?

Analysis: there are too many of these things in C language. Here we only talk about the most common ones.

(1) The most common arithmetic operations are: addition, subtraction, multiplication and division, self increment, self subtraction and modulo

Operators (priority): + +, --; *, /,%; +-

(2) The most common relational operations are relational operations: greater than, less than, equal to, greater than or equal to, less than or equal to, and not equal to

Operators (priority): >, <, > =, < =; ==

(3) Logical operation is basically about the three brothers of "and or not"

Operator (priority):!; & &||

&&: logic and. The output will be true only if the left and right sides are true at the same time;

||: logical or, only one of the left and right sides of the symbol needs to be true, and the output is true;

!: Logical non, the content on the right of the symbol is logically reversed;

Note: here! Very high priority, non priority.

All operators mentioned above are prioritized:

++,-- ;!; The remaining arithmetic operators; Relational operators; Remaining logical operators

2. How to express "true" and "false" in C language? How does the system judge the "true" and "false" of a quantity?

Analysis: pure concept questions. There are original words in the book. Let me give a brief overview.

In C language, the judgment of true and false is very simple: "0 is false, non-0 is true". The specific judgment is divided into two kinds: "logical constant and logical variable"

There are only two logical constants: "0 and 1, 0 is false and 1 is true"

Logical variables, just like his name, will change! Therefore, logical variables can be numbers, letters, symbols or even expressions. The judgment method is that 0 is false and non-0 is true.

 3. Write the values of the following logical expressions. Let a=3,b=4,c=5.

Analysis: this question is the comprehensive practical version of the above two questions.

(1) A + b > C & & B = = C -- > 3 + 4 > 5 & & 4 = = 5 -- > 7 > C & & 4 = = 5 -- > true & & false -- > false

(2) A | B + C & & B-C - > 3 | 4 + 5 & & 4-5 - > 3 | 9 & & - 1 - > true | true & & true - > true

(3)! (a>b) && ! c||1 ——> ! (3>4) && ! 5||1 ——> ! (false) & &! True true - > true & & false true - > true

(4)! (x = a) & & (y = b) & & 0 - > false

(5)! (a+b)+c-1 && b+c/2 ——> ! (3+4)+5-1 && 4+5/2 ——> ! (7) + 4 & & 6.5 -- > 0 + 4 & & 6.5 -- > true

Note: priority of logical operators:! > & & > |. 0 is false, non-0 is true! Non 0 is true! Non 0 is true! Say the important things three times, and the negative number here is also true! The fifth question is related to the compiler. The priority mentioned here is VS2022. The answer with other compilers may be false.

4. There are three integers a, B and C, which are input by the keyboard and output the largest number.

Analysis: this topic is relatively simple, which is a very simple relational operation. We can use if statement to operate and release code!

int main()
{
	int num1, num2, num3;
	int tmp = 0;
	scanf("%d %d %d", &num1, &num2, &num3);
	if (num1 <= num2)
	{
		tmp = num1;
		num1 = num2;
		num2 = tmp;
	}
	if (num1 <= num3)
	{
		tmp = num1;
		num1 = num3;
		num3 = tmp;
	}
	printf("The maximum value is:%d\n", num1);
	return 0;
}

Operation result:

 

5. Input a positive number less than 1000 from the keyboard and output its square root (if the square root is not an integer, output its integer part). It is required to check whether the data is a positive number less than 1000 after entering it. If not, re-enter is required.

Analysis: this question is ambiguous. "Output its integer part". Does it mean to output an integer after rounding or an integer without rounding? Therefore, I have given two answers here, which you can ask for on demand. Note that a < math h> Function sqrt. Put the code!

(1) Rounding version

int main()
{
	while (1)
	{
		int num1;
		printf("Please enter 1-999 Integer of:>");
		scanf("%d", &num1);
		if (num1 >= 1000)
		{
			printf("\n Input error, please re-enter!\n");
		}
		else
		{
			printf("%d Square footage:%.0f", num1, sqrt((double)num1));
			return 0;
		}
		
	}
}

Operation result:

(2) Non rounded version:

int main()
{
	while (1)
	{
		int num1;
		printf("Please enter 1-999 Integer of:>");
		scanf("%d", &num1);
		if (num1 >= 1000)
		{
			printf("\n Input error, please re-enter!\n");
		}
		else
		{
			printf("%d Square footage:%d", num1, (int)sqrt((double)num1));
			return 0;
		}
		
	}

Operation result:

 6. There is a function, write a program, input the value of x and output the corresponding value of y.

Analysis: this question is the same as question 5. It uses the arithmetic operation, logical operation and judgment statements mentioned before. The framework of the program is to use if to divide. The topic is simple and not rigorous. Generally, mathematical operations are not always integers, but I believe Tan Haoqiang wants to use integers to operate. Put the code!

int main()
{
	int x, y;
	printf("Please enter x Value of:");
	scanf_s("%d", &x);
	if (x < 1) 
	{
		y = x;
	}
	else if (x >= 1 && x < 10) 
	{
		y = 2 * x - 1;
	}
	else 
	{
		y = 3 * x - 11;
	}
	printf("y = %d\n", y);
	return 0;
}

When x < 1, operation result:

When 1 < = x < 10. Operation result:

When x > = 10. Operation result:

 

 7. There is a function,Someone has written the following two programs. Please analyze whether they can meet the requirements of the topic. Don't rush to run the program on the computer. First analyze the logic of the two programs, draw their flow chart, and analyze their operation. Then, run the program on the computer to observe and analyze the results.


Parsing: else matches the nearest if, so the programs written by these two people will have an else mismatch. You need to set {} on the if of else.  

 (1)

When x == 0, y = -1; When x > 0, y = 1; y = 0 when x < 0

          

 

(2)

When x < 0, y = 0; When x > 0, y = 1; When x = 0, y = -1

 

 

 8. Give the 100 point system score, and output the score grades' A ',' B ',' C ','D' and 'E'. Above 90 points are 'A', 80-89 points are 'B', 70-79 points are 'C', 60-69 points are'D ', and below 60 points are' E '.

Analysis: the key to this problem is to judge sentences and relational operations, and there are basically no difficulties.

int main()
{
	while (1)
	{
		int num = 0;
		printf("Please enter your grade(0-100):>");
		scanf("%d", &num);
		if (num >= 0 && num <= 100)
		{
			if (num >= 90)
			{
				printf("Grade is'A'\n");
			}
			else if (num >= 80 && num <= 89)
			{
				printf("Grade is'B'\n");
			}
			else if (num >= 70 && num <= 79)
			{
				printf("Grade is'C'\n");
			}
			else if (num >= 60 && num <= 69)
			{
				printf("Grade is'D'\n");
			}
			else
			{
				printf("Grade is'E'\n");
			}
			/*
			If you move return 0 here
			The function of this cycle is only to judge the scope
			*/
		}
		else
		{
			printf("Input error, please re-enter!\n");
		}
	}
	return 0;//return 0 can be recycled here

Operation result:

9. Give a positive integer with no more than 5 digits, which requires:

① Find out how many digits it is;

② Output each digit separately;

③ Output the numbers in reverse order. For example, if the original number is 321, 123 should be output.

Analysis: the difficulty of this problem lies in how to reverse the order. You can use division and modulus to do it. Take the modulus of 10 to obtain a single digit number, divide 10 to obtain a ten digit number, divide 100 to obtain a hundred digit number, divide 1000 to obtain a thousand digit number, and divide 10000 to obtain a ten digit number. You can use the if statement to judge the data range.

void print(int num,int count)
{
	if (num > 9)
	{
		printf("%d ", num % 10);
		print(num/10,++count);	
	}
	else
	{
		++count;
		printf("%d ", num % 10);
		printf("common%d position\n", count);
	}
}
int main()
{
	while (1)
	{
		int num = 0;
		int count = 0;
		printf("Please enter 0-99999 Number of:>");
		scanf("%d", &num);
		printf("\n");
		if (num > 99999 || num < 0)
		{
			printf("Input error, please re-enter!\n");
		}
		else
			print(num,count);
			printf("\n");
	}
	return 0;
}

Operation result:

 

10. The bonus paid by the enterprise shall be deducted according to the profit. If the profit I is less than or equal to 100000 yuan, the bonus can be deducted by 10%; When the profit is higher than 100000 yuan and lower than 200000 yuan (100000 < I ≤ 200000), the part lower than 100000 yuan will be deducted by 10%, and the part higher than 100000 yuan can be deducted by 7.5% 5%; When 200000 < I ≤ 400000, the part less than 200000 yuan shall still be deducted according to the above methods (the same below). 5% commission for the part higher than 200000 yuan; When 400000 < < I ≤ 600000 yuan, the part higher than 400000 yuan shall be deducted by 3%; When 600000 < 1 ≤ 1000000, the part higher than 600000 yuan shall be deducted by 1.5%; 1> If the Commission exceeds 1000000 yuan, 1% will be deducted. Enter the profit I of the current month from the keyboard to calculate the total amount of bonus payable. requirement:

(1) Writing programs using if statements

(2) Writing programs using switch statements

(1) if statement

Analysis: this question is the same as the previous one, that is, the title is long. It's OK to use if statements for classified discussion. Put the code directly!

double bonuses(double I)
{
	double bonus = 0.0;
	double bonus1 = 100000 * 0.1;//100000 bonus
	double bonus2 = (200000 - 100000) * 0.075 + bonus1;//200000 bonus
	double bonus3 = (400000 - 200000) * 0.05 + bonus2;//400000 bonus
	double bonus4 = (600000 - 400000) * 0.03 + bonus3;//600000 bonus
	double bonus5 = (1000000 - 600000) * 0.015 + bonus4;//1 million bonus
	if (I <= 100000 && I >= 0)
	{
		bonus = I * 0.1;//10% commission for less than 100000
	}
	else if (I > 100000 && I <= 200000) {
		bonus = bonus1 + (I - 100000) * 0.075;//The extra 100000 will be calculated in proportion, plus the bonus of 10w
	}
	else if (I > 200000 && I <= 400000) {
		bonus = bonus2 + (I - 200000) * 0.05;//The extra 200000 is calculated proportionally, plus 20w bonus
	}
	else if (I > 400000 && I <= 600000) {
		bonus = bonus3 + (I - 400000) * 0.03;//The extra 400000 is calculated in proportion, plus the bonus of 40w
	}
	else if (I > 600000 && I <= 1000000) {
		bonus = bonus4 + (I - 600000) * 0.015;//The extra 600000 is calculated in proportion, plus the bonus of 60w
	}
	else if (I > 1000000) {
		bonus = bonus5 + (I - 1000000) * 0.01;//The extra one million is calculated in proportion, plus the bonus of 100w
	}
	return bonus;
}
int main()
{
	while (1)
	{
		double I, bonus = 0;
		printf("Please enter profit:>");
		scanf_s("%lf", &I);
		printf("\n");
		if (I < 0)
		{
			printf("Please enter a positive number\n");
		}
		bonus = bonuses(I);
		printf("Bonus is:%.2f\n", bonus);
	}
	return 0;
}

(2) switch statement

Analysis: the switch statement is a little difficult, but! We can see that the title increases by 10w, so here we can use I/10w to grade and put the code!

double bonuses(double I)
{
	double bonus = 0.0;
	double bonus1 = 100000 * 0.1;//100000 bonus
	double bonus2 = (200000 - 100000) * 0.075 + bonus1;//200000 bonus
	double bonus3 = (400000 - 200000) * 0.05 + bonus2;//400000 bonus
	double bonus4 = (600000 - 400000) * 0.03 + bonus3;//600000 bonus
	double bonus5 = (1000000 - 600000) * 0.015 + bonus4;//1 million bonus
	int grade = I / 100000;
	switch (grade)
	{
	case 0:
		bonus = I * 0.1; break;
	case 1:
		bonus = bonus1 + (I - 100000) * 0.075; 
		break;
	case 2://It will be executed to the next break in sequence
	case 3:
		bonus = bonus2 + (I - 200000) * 0.05; 
		break;
	case 4:
	case 5:
		bonus = bonus3 + (I - 400000) * 0.03; 
		break;
	case 6:
	case 7:
	case 8:
	case 9:
		bonus = bonus4 + (I - 600000) * 0.015; 
		break;
	default:	//default is used here for the case where the profit I > 100W
		bonus = bonus5 + (I - 1000000) * 0.01; 
		break;
	}
	return bonus;
}
int main()
{
	while (1)
	{
		double I, bonus = 0;
		printf("Please enter profit:>");
		scanf_s("%lf", &I);
		printf("\n");
		if (I < 0)
		{
			printf("Please enter a positive number\n");
		}
		bonus = bonuses(I);
		printf("Bonus is:%.2f\n", bonus);
	}
	return 0;
}

11. Input 4 integers and output them in descending order.

Analysis: this question is basically the same as the previous one, which is to simply compare the size and re assign the value.

int main()
{
	int num1, num2, num3, num4, temp;
	scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
	/*---------------------------------------------*/
	if (num1 > num2)//Take turns to determine the smallest number
	{
		temp = num1;
		num1 = num2;
		num2 = temp;
	}
	if (num1 > num3)
	{
		temp = num1;
		num1 = num3;
		num3 = temp;
	}
	if (num1 > num4)
	{
		temp = num1;
		num1 = num4;
		num4 = temp;
	}
	/*---------------------------------------------*/
	if (num2 > num3)//The second smallest figure was determined by turns of comparison
	{
		temp = num2;
		num2 = num3;
		num3 = temp;

	}
	if (num2 > num4)
	{
		temp = num2;
		num2 = num4;
		num4 = temp;
	}
	/*---------------------------------------------*/
	if (num3 > num4)//The third smallest figure was determined by turns of comparison
	{
		temp = num3;
		num3 = num4;
		num4 = temp;
	}
	/*---------------------------------------------*/
	printf("%d<%d<%d<%d", num1, num2, num3, num4);
	return 0;
}

Operation result:

12. There are 4 round towers with center of (2,2), (- 2,2), (- 2, - 2), (2, - 2) and radius of 1, as shown in the figure. The height of the four towers is 10m, and there are no buildings outside the towers. Now enter the coordinates of any point to calculate the building height of the point (the height outside the tower is zero).

Analysis: this problem is actually a mathematical problem. The coordinates of the center of the circle are (- 2,2), (2,2), (- 2, - 2), (2, - 2), and the radius of the circle is 1, so | 1 | < x < | 3 |, | 1 | < y < | 3 |. The most important thing is to meet the coordinates within the range of the circle. Here, you can use the point-to-point distance to determine, that is, sqrt (x^2+y^2) < = 1. In order to convert negative numbers to positive numbers, the fabs function is referenced here, #include < math h>. Put the code!

void calculate(double x, double y)
{
	if (1 <= x && x <= 3 && 1 <= y && y <= 3)
	{		
		x = fabs(x) - 2; 
		y = fabs(y) - 2;
		double r = sqrt(x * x + y * y);
		if (r <= 1)
		{
			printf("The coordinate height of this point is 10\n");
		}
		else
			printf("The coordinate height of this point is 0\n");
	}
	else
	{
		printf("The coordinate height of this point is 0\n");
	}

}
int main()
{
	while (1)
	{
		double x, y;
		printf("Please enter x and y Coordinates of points:>");
		scanf("%lf %lf", &x, &y);
		calculate(fabs(x), fabs(y));
	}
	return 0;
}

Operation result:

 

Topics: C Programmer