C语言程序设计(第四版)谭浩强 课后习题答案 第四章
- 第四章
-
- 1.什么是算术运算?什么是关系运算?什么是逻辑运算?
- 2.C语言中如何表示“真”和“假”?系统如何判断一个量的“真”和“假”?
- 3.写出下面各逻辑表达式的值。
- 4.有3个整数a,b,c由键盘输入,输出其中最大的数
- 5.从键盘输入一个小于1000的正数,要求输出它的平方根(如果不是整数,则取其整数部分)。
- 6.有一个函数
- 7.有一个函数
- 8.给出100分制的成绩,要求输出'A','B','C','D','E'.
- 9.给一个不多于5位的正整数,要求:
- 10.企业发放的奖金根据利润提成
- 11.输入四个整数,要求按由小到大的顺序输出
- 12.有四个圆塔,圆心分别为(2,2),(-2,2),(2,-2),(-2,-2),高度为10,半径为1,输入坐标判断塔高度
第四章
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")