Tan Haoqiang's "C Language Programming" after-school key exercises and answers Chapters 4 and 5

Chapter Four

3. 写出下面各逻辑表达式的值。设a=3,b=4,c=5。

(1)a + b > c && b == c

(2)a || b + c && b - c

(3)!(a > b) && !c || 1

(4)!(x = a) && (y = b) && 0

(5)!(a + b) + c - 1 && b + c / 2

(1) 0
3+4>5 gives priority to 3+4 to get the result 7, so the result of 7>5 is true; 4==5 is false, one true and one false logic and the final result is false.
(2) 1
priority arithmetic operation 4+5 gets 7, if it is not 0, it is true, 4-5 gets -1, if it is not 0, it is true, then logic and judgment, and finally logic or judgment.
(3) 1
! The highest priority, !(3>4) the final result is true, !5 is false; followed by &&, true && false to get false, and finally ||, 1 is true, false or true is true (4) 0 && has the
lowest
priority It is the last logical operation, so no matter what, in the end && 0, it must be false
(5) 1
will first negate (a+b) in vs to get 0, 0+5-1 and the result is 4, so it is finally true ( Compilers under different platforms may give priority to arithmetic operations, and the result of negation will be false)

8. 给出一百分制成绩,要求输出成绩等级’A’、‘B’、‘C’、‘D’、‘E’。 
90分以上为’A’,8089分为’B’,7079分为’C’ ,60~69分为’D’ ,60分以下为’E’。

switch - case

#include<stdio.h>
#include<stdlib.h>
int main()
{
    
    
	float score;
	char grade;
	printf("请输入学生成绩:");
	scanf("%f", &score);
	while(score > 100 || score < 0)
    {
    
    
        printf("\n输入错误,请重新输入");
        scanf("%f", &score);
    }
    switch((int)(score/10))
    {
    
    
        case 10:
        case 9: grade = 'A';break;
        case 8: grade = 'B';break;
        case 7: grade = 'C';break;
        case 6: grade = 'D';break;
        case 5:
        case 4:
        case 3:
        case 2:
        case 1: grade = 'D';
    }
    printf("成绩是%5.1f,相应的等级为%c\n",score,grade);
	return 0;
}

if - else

	if (score >= 90) {
    
    
		printf("A\n");
	}else if (score >= 80 && score < 90) {
    
    
		printf("B\n");
	}else if (score >= 70 && score < 80) {
    
    
		printf("C\n");
	}else if (score >= 60 && score < 70) {
    
    
		printf("D\n");
	}else {
    
    
		printf("E\n");
	}

insert image description here

chapter Five

insert image description here

#include <stdio.h>
#include <math.h>

int main()
{
    
    
	//n为a的个数
	int n;
	double a, prev_sum = 0.0, total_sum = 0.0;
	printf("请输入a的值以及n的值: ");
	scanf("%lf %d", &a, &n);
	//循环n次求总和
	for (int i = 0; i < n; i++)
	{
    
    
		prev_sum += a * pow(10, i);
		total_sum += prev_sum;
	}
	printf("总和为:%lf\n", total_sum);
	return 0;
}

insert image description here

#include<stdio.h>

int main()
{
    
    
	double total_sum = 0,t = 1;
	for(int i = 1; i <= 20; i++)
	{
    
    
	    t = t * i;
	    total_sum = total_sum + t;
	}
	printf("1~20每个数字阶乘总和为:%lf\n",total_sum);
	return 0;
}

insert image description here

#include <stdio.h>

int main()
{
    
    
	 //a表示百位数字,b表示十位数字,c表示各位数字
	int a, b, c;
	for (int i = 100; i < 1000; i++)
	{
    
    
		a = i / 100;
		b = (i / 10) % 10;
		c = i % 10;
		if (a * a * a + b * b * b + c * c * c == i)
		{
    
    
			printf("%d\n", i);
		}
	}
	return 0;
}

insert image description here
insert image description here

#include<stdio.h>

int main()
{
    
    
	int data, fator, sum;      /* data表示要判断的数,fator表示因子,sum表示因子之和*/

	for (data = 2; data <= 1000; data++)
	{
    
    
		//1是所有整数的因子,所以因子之和从1开始
		sum = 1;
		for (fator = 2; fator <= data / 2; fator++)
		{
    
    
			/* 判断data能否被fator整除,能的话fator即为因子  因子不包括自身 */
			if (data % fator == 0)
			{
    
    
				sum += fator;
			}
		}
		// 判断此数是否等于因子之和 */
		if (sum == data)    
		{
    
    
			printf("%d its factors are 1, ", data);
			for (fator = 2; fator <= data / 2; fator++)
			{
    
    
				if (data % fator == 0)
				{
    
    
					printf("%d, ", fator);
				}
			}
			printf("\n");
		}
	}
	return 0;
}

insert image description here

insert image description here

#include <stdio.h>
int main()
{
    
    
	int i, n = 20;
	double a = 2, b = 1, s = 0, t;
	for (i = 1; i <= n; i++)
	{
    
    
		s = s + a / b;
		//记录前一项分子
		t = a;
		//前一项分子与分母之和为后一项分子
		a = a + b;
		//前一项分子为后一项分母
		b = t;
	}
	printf("sum=%16.10f\n", s);
	return 0;
}

insert image description here
insert image description here

#include <stdio.h>
#include <math.h>

int main()
{
    
    
	float a, x0, x1;
	printf("请输入一个正数: ");
	scanf("%f", &a);
	x0 = a / 2;
	x1 = (x0 + a / x0) / 2;
	do
	{
    
    
		x0 = x1;
		x1 = (x0 + a / x0) / 2;
	} while (fabs(x0 - x1) >= 1e-5);
	printf("[%f] 的平方根为 [%f]\n", a, x1);
	return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/qq_43310387/article/details/124622514