C语言程序设计(第四版)谭浩强 课后习题答案 第四章

本文详细解答了C语言程序设计第四章的课后习题,涉及算术、关系和逻辑运算,数值比较,条件判断,函数实现以及一系列编程练习,包括找出最大数、平方根计算、成绩等级转换、奖金提成计算等实际问题的解决方案。
摘要由CSDN通过智能技术生成

第四章

1.什么是算术运算?什么是关系运算?什么是逻辑运算?

算数运算包括 + - * /
关系运算包括 > < != == >= <=
逻辑运算包括 && || !

2.C语言中如何表示“真”和“假”?系统如何判断一个量的“真”和“假”?

在语法上如果一个值或一个字符等于“0”则为假,如果非“0”则为真。系统通过一个量和“0”的对比进而判断这个量的真假。

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 (2) 1 (3) 1 (4) 0 (5) 1

4.有3个整数a,b,c由键盘输入,输出其中最大的数

#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
   
	int a,b,c;
	scanf("%d,%d,%d",&a,&b,&c);
	int max;
	max = a>b?a:b;
	max = max>c?max:c;
	printf("max is %d",max);
}

5.从键盘输入一个小于1000的正数,要求输出它的平方根(如果不是整数,则取其整数部分)。

要求输入数据后先对其进行检查是否为小于1000的证书,若不是则要求重新输入

#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
int main(){
   
	int num;
	do{
   
		scanf("%d",&num);
	}while(num > 0 && num < 100);
	int res = sqrt(num);
	printf("the num's sqrt = %d",res);
	return 1;
}

6.有一个函数

.
x (x<1)
y = 2x-1 (1<=x<10)
3x-11 (x>=10)
写程序,输入x的值,输出y相应的值

#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
   
	double x;
	double y;
	scanf("%f",&x);
	if(x < 1)
  		y = x;
  	else if(x >= 1 && x < 10)
  	    y = 2*x - 1;
  	else
  	    y = 3*x - 11;
 	printf("y = %f",y);
 	return 1;
}

7.有一个函数

#include <stdio.h>
using namespace std;
int main(){
   
	int x,y;
	if(x<0)
		y = -1;
	else if(x == 0)
		y = 0;
	else
		y = 1;
	printf("x = %d , y = %d",x,y);
	return 1;
}

8.给出100分制的成绩,要求输出’A’,‘B’,‘C’,‘D’,‘E’.

90分以上为’A’,
80~89分为’B’,
70-79分为’C’,
60-69分为’D’,
60分以下为’E’

#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
   
	int score;
	do{
   
		scanf("%d",&score);
	}while(score > 100);
	if(score >= 90)
		printf("the score is A")
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值