引例
例4.1
代码#include
#include
int main() {
int a, b, c;
float area;
double s;
printf("请输入三角形的三条边长:");
scanf_s("%d,%d,%d", &a, &b, &c);
s = (a + b + c) / 2.0;
if (a + b <= c || a + c <= a || a + c <= b) {
printf("不构成三角形\n");
}
else {
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("三角形的面积是:%f", area);
}
return 0;
}
流程图
例4.2
代码
#include
int main() {
int x;
float y;
printf("输入x的值:");
scanf_s("%d",&x);
if (x!=0) {
y = 1.0 / x;
}
else {
y = 0;
}
printf("%d,%f",x,y);
return0;
}
流程图
运行结果
关系运算符、逻辑运算符、条件运算符
1.关系运算符
关系运算符用于判断两个操作数的大小关系。
2.关系表达式
用关系表达式将两个表达式连接起来的式子称为关系表达式。
例4.3
代码#include
int main() {
char c = 'm';
int i = 10, j = 20, k = 30;
float x = 13e+5, y = 10.85;
printf("%d,%d,",'a'+5
printf("%d,%d,", 1
return 0;
}#include
int main() {
char c = 'm';
int i = 10, j = 20, k = 30;
float x = 13e+5, y = 10.85;
printf("%d,%d,",'a'+5
printf("%d,%d,", 1
流程图
逻辑运算符和逻辑表达式
1.逻辑运算符
&&和||都是双目运算符!为单目运算符
2.逻辑表达式
将两个表达式连接起来的式子称为逻辑表达式
真或假既为1或0
例4.5
代码#include
int main() {
int a = 3, b = 3, c = 2, d;
printf("%d,",!a&&!b&&c);
printf("%d,",!a||!b||c);
printf("%d\n",a>=b&&b>c);
d = c-- || (b = 6) && a++;
printf("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);
return 0;
}
运行结果
流程图
条件运算符和条件表达式
1.条件运算符
条件运算符由?和:两个符号组成
2.条件表达式
由条件运算符将3个表达式连接起来的式子称为条件表达式
例4.6
代码#include
int main() {
int x, y;
printf("请输入两个整数:");
scanf_s("%d,%d",&x,&y);
printf("两个整数中的最大数为:%d\n",x>y?x:y);
return 0;
}
运行结果
使用if——else实现分支结构
if——else语句
1.双分支if语句
例4.7
代码#include
int main() {
int a, b;
printf("请输入两个整数:");
scanf_s("%d%d",&a,&b);
if (a>b) {
printf("两个整数中的最大值为:%d\n",a);
}
else {
printf("两个整数中的最大值为:%d\n",b);
}
return 0;
}
列4.8
代码#include
#include
int main() {
double x, y;
printf("输入x的值:");
scanf_s("%lf",&x);
if (x<=1) {
y = exp(x);
}
else {
y = pow(x, 2) - 1;
}
printf("f(%f)=%.2f\n",x,y);
return 0;
}
运行结果
2.单分支if语句
例4.9
代码#include
int main() {
float x;
scanf_s("%f",&x);
if (x<0) {
x = -x;
}
printf("%f",x);
return 0;
}
列4.10
代码#include
int main() {
int x, y;
printf("x=");
scanf_s("%d",&x);
if (x) {
y = 1;
}
else {
y = -1;
}
printf("y=%d",y);
return 0;
}
流程图
列4.11
代码#include
int main() {
float a, b, c, t;
scanf_s("%f,%f,%f", &a, &b, &c);
if (a > b) {
t = a;a = b;b = t;
}
if (a > c) {
t = a;a = c;c = t;
}
if (b > c) {
t = b;b = c;c = t;
}
printf("%5.2f,%5.2f,%5.2f\n", a, b, c);
return 0;
}
运行结果
if语句的嵌套
例4.12
代码#include
int main() {
int a, b, c, max;
scanf_s("%d%d%d",&a,&b,&c);
if (a>b) {
if (a>c) {
max = a;
}
else {
max = c;
}
}
else {
if (b>c) {
max = b;
}
else {
max = c;
}
}
printf("max=%d\n",max);
return 0;
}
运行结果
列4.13
代码#include
int main() {
int x, y;
scanf_s("%d",&x);
if (x>0) {
y = 1;
}
else {
if (x==0) {
y = 0;
}
else {
y = -1;
}
}
printf("y=%d\n",y);
return 0;
}
流程图
多分支if语句
例4.14
代码
#include
#include
int main() {
float x, y;
printf("输入x的值:");
scanf_s("%f",&x);
if (x<2) {
y = 2 * x;
}
else if(x<=10){
y = 7 - 3 * x;
}
else {
y = 1 - cos(x);
}
printf("y=%0.6f\n",y);
return 0;
}
多分支if语句
例4.14
代码
#include
#include
int main() {
float x, y;
printf("输入x的值:");
scanf_s("%f",&x);
if (x<2) {
y = 2 * x;
}
else if(x<=10){
y = 7 - 3 * x;
}
else {
y = 1 - cos(x);
}
printf("y=%0.6f\n",y);
return 0;
}
例4.15
代码
#include
int main() {
char c1, c2;
printf("请输入一个字符:");
c1 = getchar();
if (c1 >= 'a' && c1 <= 'z') {
c2 = c1 - 32;
}
else if (c1 >= 'A' && c1 <= 'Z') {
c2 = c1 + 32;
}
else {
c2 = c1 + 1;
}
putchar(c2);
return 0;
}
流程图
使用switch语句实现分支结构
1.在switch语句格式中的每个语句后面都要使用break语句
例4.16
代码
#include
int main() {
int month;
printf("输入月份:");
scanf_s("%d",&month);
switch ((month-1)/3 )
{
case 0:
printf("%d月是第1季度\n",month);
break;
case 1:
printf("%d月是第2季度\n",month);
break;
case 2:
printf("%d月是第3季度\n",month);
break;
case 3:
printf("%d月是第4季度\n",month);
break;
default:
printf("输入错误\n");
break;
}
return 0;
运行结果
流程图
2.在switch语句格式中的部分语句后面使用break语句
例4.17
代码
#include
int main() {
int year, month, days;
printf("请输入年,月:");
scanf_s("%d,%d", &year, &month);
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if (year%400==0||year%4==0&&year%100!=0) {
days = 29;
}
else {
days = 28;
break;
}
}
printf("%d年%d月的天数是%d天\n",year,month,days);
return 0;
}
运行结果
流程图
列4.18
代码#include
#include
int main() {
int expression;
float x, y;
printf("输入x的值:");
scanf_s("%f",&x);
expression = (int)(1 * (x < 2) + 2 * (x >= 2 && x <= 10) + 3 * (x > 10));
switch (expression) {
case 1:
y = 2 * x;
break;
case 2:
y = 7 - 3 * x;
break;
case 3:
y = 1 - cos(x);
break;
}
printf("y=%0.5f\n",y);
return 0;
}
流程图
分支结构程序设计示例
例4.19
#include
#include
int main() {
float x, y;
scanf_s("%f",&x);
if (x>=0) {
y = sqrt(x);
}
else {
y = pow(x, 5) + 2 * x + 1 / x;
}
printf("=%.2f,y=%.2f\n",x,y);
return 0;
}
运行结果
1.标准格式实现
例4.20
代码
#include
int main() {
int year, leap;
printf("输入年份:");
scanf_s("%d",&year);
if ((year%4==0&&year%100!=0)||(year%400==0)) {
leap = 1;
}
else {
leap = 0;
}
if (leap) {
printf("%d是闰年\n",year);
}
else {
printf("%d不是闰年\n",year);
}
return 0;
}
2.嵌套格式实现
代码
#include
int main() {
int year, leap;
printf("输入年份:");
scanf_s("%d",&year);
if (year%4==0) {
if (year%100==0) {
if (year%400==0) {
leap = 1;
}
else {
leap = 0;
}
}
else {
leap = 1;
}
}
else {
leap = 0;
}
if (leap) {
printf("%d是闰年\n",year);
}else {
printf("%d不是闰年\n",year);
}
return 0;
}
3.多分支格式实现
代码
#include
int main() {
int year, leap;
printf("输入年份:");
scanf_s("%d", &year);
if (year%4!=0) {
leap = 0;
}
else if(year%100!=0){
leap = 1;
}
else if(year%400!=0) {
leap = 0;
}else {
leap = 1;
}
if (leap) {
printf("%d是闰年\n",year);
}
else {
printf("%d不是闰年\n", year);
}
return 0;
}
例4.21
代码
#include
int main() {
int n, a, b, c;
printf("请输入一个三位数的整数:");
scanf_s("%d",&n);
if (n<100||n>999) {
printf("输入错误!\n");
}
else {
a = n / 100;
b = n / 10 % 10;
c = n % 10;
if (a printf("%d是序数\n",n);
}
else {
printf("%d不是序数\n",n);
}
return 0;
}
}