1. 什么是算术运算?什么是关系运算?什么是逻辑运算?
- 【算术运算】:加减乘除等。
- 【关系运算】:比较运算,将两个数值进行比较,判断比较结果是否符合给定的条件。
- 【逻辑运算】:用逻辑运算符将关系表达式或其他逻辑量连接起来做运算,其值是一个逻辑量“真”或“假”。
2. C语言中如何表示"真"和"假"?系统如何判断一个量的"真"和"假"?
- C语言编译系统在表示逻辑运算结果时,用数值1代表“真”,用0代表“假”。
- 在判断时,以0代表“假”,以非0代表“真”。
3. 写出下面各逻辑表达式的值。设a=3,b=4,c=5。
- 【知识点-优先级】:
- 圆括号()>逻辑非(!)>算术运算符>关系运算符>逻辑与(&&)>逻辑或(||)>赋值运算符
1.a+b>c&&b==c
- 3+4>5&&4==5
- 7>5&&4==5
- 1&&0
- 0
2.a||b+c&&b-c
- 3||4+5&&4-5
- 3||9&&(-1)
- 3||1
- 1
3.!(a>b)&&!c||1
- !(3>4)&&!5||1
- !0&&!5||1
- 1&&0||1
- 0||1
- 1
4.!(x=a)&&(y=b)&&0
- !(x=3)&&(y=4)&&0
- !3&&4&&0
- 0&&4&&0
- 0
5.!(a+b)+c-1&&b+c/2
- !(3+4)+5-1&&4+5/2
- !7+5-1&&4+5/2
- 0+5-1&&4+5/2
- 4&&6
- 1
4. 有3个整数a,b,c,由键盘输入,输出其中最大的数。
#include <stdio.h>
int main(){
int max(int x,int y); //声明max函数
int a,b,c,d;
printf("请输入3个整数(a b c):");
scanf_s("%d %d %d",&a,&b,&c);
d=max(max(a,b),c); //先求a,b中最大值,再和c求最大值
printf("max=%d",d);
return 0;
}
int max(int x,int y){ //定义求最大值函数max
int z;
if(x>y){
z=x;
}
else{
z=y;
}
return (z);
}
5. 从键盘输人一个小于1000的正数,要求输出它的平方根(如平方根不是整数,则输出其整数部分)。要求在输入数据后先对其进行检查是否为小于1000的正数。若不是,则要求重新输入。
- 【知识点】:
- 【求x的平方根】:sqrt(x),需要引入math.h头文件。
#include <stdio.h>
#include <math.h>
int main(){
int x,y;
printf("请输入一个小于1000的正数:");
scanf_s("%d",&x);
if(x<1000){
y=sqrt(x);
printf("%d",y);
}
else{
printf("输入有误,请重新输入..."); //x>=1000时不符合
}
return 0;
}
6. 函数输入输出
#include <stdio.h>
int main(){
int x,y;
printf("请输入x:");
scanf_s("%d",&x);
if(x<1){
y=x;
}
else if(x>=10){
y=3*x-11;
}
else{
y=2*x-1;
}
printf("当x=%d时,y=%d",x,y);
return 0;
}
7. 分析程序
- 结果:都不能实现题目要求。
- 原因:else与最近未匹配的if进行匹配。
8. 输出成绩等级
#include <stdio.h>
int main() {
int x;
printf("请输入成绩x(百分制):");
scanf_s("%d", &x);
if (x >= 0 && x <= 100) {
if (x > 90) {
printf("A");
}
else if (x > 80) {
printf("B");
}
else if (x > 70) {
printf("C");
}
else if (x > 60) {
printf("D");
}
else {
printf("E");
}
}
else {
printf("您的输入有误,请重新输入...");
}
return 0;
}
#include <stdio.h>
int main() {
int x;
printf("请输入成绩x(百分制):");
scanf_s("%d", &x);
if (x >= 0 && x <= 100) {
switch (x / 10) {
case 10:
case 9:
printf("A");
break;
case 8:
printf("B");
break;
case 7:
printf("C");
break;
case 6:
printf("D");
break;
default:
printf("E");
break;
}
}
else {
printf("您的输入有误,请重新输入...");
}
return 0;
}
9. 五位正整数-逆序数
#include <stdio.h>
int main(){
int x;
printf("请输入一个不多于5位的正整数:");
scanf_s("%d",&x);
if(x<100000){
int n;
if(x>9999){
n=5;
}
else if(x>999){
n=4;
}
else if(x>99){
n=3;
}
else if(x>9){
n=2;
}
else{
n=1;
}
printf("该数为%d位数\n",n);
int a,b,c,d,e;
a=x/10000;
b=(x-a*10000)/1000;
c=(x-a*10000-b*1000)/100;
d=(x-a*10000-b*1000-c*100)/10;
e=x%10;
printf("%d %d %d %d %d\n",a,b,c,d,e);
int y=e*10000+d*1000+c*100+b*10+a;
printf("该数的逆序数为%d",y);
}
else{
printf("您的输入有误,请重新输入...");
}
return 0;
}
10. 利润提成
#include <stdio.h>
int main(){
long I;
double bonus,b1,b2,b4,b6,b10;
printf("请输入当月利润I:");
scanf_s("%d",&I);
b1=100000*0.1;
b2=b1+100000*0.075;
b4=b2+200000*0.05;
b6=b4+200000*0.03;
b10=b6+400000*0.015;
if(I<=100000){
bonus=I*0.1;
}
else if(I<=200000){
bonus=b1+(I-100000)*0.075;
}
else if(I<=400000){
bonus=b2+(I-200000)*0.05;
}
else if(I<=600000){
bonus=b4+(I-400000)*0.03;
}
else if(I<=1000000){
bonus=b6+(I-600000)*0.015;
}
else{
bonus=b10+(I-600000)*0.01;
}
printf("应发奖金总数是%.2f",bonus);
return 0;
}
#include <stdio.h>
int main(){
long I;
double bonus,b1,b2,b4,b6,b10;
printf("请输入当月利润I:");
scanf_s("%d",&I);
b1=100000*0.1;
b2=b1+100000*0.075;
b4=b2+200000*0.05;
b6=b4+200000*0.03;
b10=b6+400000*0.015;
switch(I/100000){
case 0:
bonus=I*0.1;
break;
case 1:
bonus=b1+(I-100000)*0.075;
break;
case 2:
case 3:
bonus=b2+(I-200000)*0.05;
break;
case 4:
case 5:
bonus=b4+(I-400000)*0.03;
break;
case 6:
case 7:
case 8:
case 9:
bonus=b6+(I-600000)*0.015;
break;
default:
bonus=b10+(I-600000)*0.01;
break;
}
printf("应发奖金总数是%.2f",bonus);
return 0;
}
11. 输入4个整数,要求按由小到大的顺序输出。
#include <stdio.h>
int main() {
int a, b, c, d, t;
printf("请输入4个整数(a b c d):");
scanf_s("%d %d %d %d", &a, &b, &c, &d);
if (a > b) {
t = a;
a = b;
b = t;
}
if (a > c) {
t = a;
a = c;
c = t;
}
if (a > d) {
t = a;
a = d;
d = t;
}
if (b > c) {
t = b;
b = c;
c = t;
}
if (b > d) {
t = b;
b = d;
d = t;
}
if (c > d) {
t = c;
c = d;
d = t;
}
printf("从小到大输出:%d<%d<%d<%d", a, b, c, d);
return 0;
}
12. 圆塔坐标
- 【知识点】:
- 【求平方根】:sqrt(x),需要引入math.h函数
#include <stdio.h>
#include <math.h>
int main(){
double x,y;
int x1=2,x2=-2,y1=2,y2=-2,r=1,r1,r2,r3,r4,h;
printf("请输入坐标(x y):");
scanf_s("%lf %lf",&x,&y);
r1=sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));
r2=sqrt((x-x1)*(x-x1)+(y-y2)*(y-y2));
r3=sqrt((x-x2)*(x-x2)+(y-y1)*(y-y1));
r4=sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2));
if(r1>r&&r2>r&&r3>r&&r4>r){
h=0;
}
else{
h=10;
}
printf("该点的建筑高度为%d",h);
return 0;
}