逻辑量、if语句和嵌套结构
4.1 逻辑量
knowledge:逻辑量、关系表达式、逻辑表达式
formula
*待补充*explainment
code
待补充
4.2 if语句
knowledge:选择结构if-else,
①双分支:if-else
- 例1:判断三条边长是否能构成三角形,如果可以,计算S三角形,否则输出错误信息。
- 解:逻辑表达式&&,判断语句:“a+b>c&&a+c>b&&b+c>a”
计算面积公式:海伦公式
LaTex S=\sqrt{l(l-a)(l-b)(l-c)}
LaTex l=\frac{a+b+c}{2}
code
//VS上运行成功
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
double S, l;
printf("请输入三个边长:\n");
scanf("%f%f%f", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a)
{
l = (a + b + c) / 2;
S = sqrt(l * (l - a) * (l - b) * (l - c));
printf("S=%.2f\n", S);
}
else
printf("DATA ERROR!");
}
- 例2:输入两个整数,求最大值。
code
//1
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int a, b, Max;
printf("Input a,b:\n");
scanf("%d%d", &a, &b);
Max = a;
if (Max < b)
Max = b;
else if (Max > b)
return Max;
else if (Max=b)
printf("DATA ERROR!");
printf("The Max is %d\n", Max);
}
//2
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int a, b, Max;
printf("Input a,b:\n");
scanf("%d,%d", &a, &b);
Max = a;
if (Max < b)
Max = b;
printf("The Max is %d\n", Max);
}
②多分支:if语句的嵌套
例:求分段函数的值。
方法一:
code
//VS上运行成功
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
int main()
{
float x, y;
printf("Plz input x\n");
scanf("%f",&x);
if (x > 6)//x:6.28 y:79左右
{
y = 2*x*x + 3 * sin(x);
}
else if (x > 0)
{
y = sqrt(x + 2);//x:2 y:2
}
else if (x >= -3)
{
y = 3 * x * x * x;//x:0 y:0
}
else
y = cos(x)+ 5;//x:-3.14 y:4
printf("y=%.3f", y);
}
方法二:
code
//VS上运行成功
//方法二少了几行。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
int main()
{
float x, y;
printf("Plz input x\n");
scanf("%f", &x);
if (x > 0)
{
if (x>6)
y = 2*x*x + 3 * sin(x);//x:6.28 y:79左右
else
y = sqrt(x + 2);//x:2 y:2
}
else
{
if (x<-3)
y = cos(x) + 5;//x:-3.14 y:4
else
y = 3 * x * x * x;//x:0 y:0
}
printf("y=%.3f", y);
}
4.3 switch语句
knowledge:构造表达式实现switch语句
① 多分支选择结构常用switch语句
- 关键字:switch,case,default
- 形式:
switch(表达式)
{
case 常量1:语句组1
case 常量2:语句组2
···
case 常量n:语句组n
[default:语句组n+1]
}
②构造实例
百分制成绩与等级挂钩,90~ 100,80~ 90,70~ 80,60~ 70, 0~60的分数区间别对应A,B,C,D,E档。现编程,输入分数,输出对应档。
- int(score/10)
code
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
float score;
printf("Plz input your score, lol:\n");
scanf("%f", &score);
switch (int(score / 10))
{
case 10:
case 9: printf("the score %.2f garde is A\n", score); break;
case 8: printf("the score %.2f garde is B\n", score); break;
case 7: printf("the score %.2f garde is C\n", score); break;
case 6: printf("the score %.2f garde is D\n", score); break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: printf("the score %.2f garde is E\n", score); break;
default: printf("the score %.2f is Error!\n", score);
}
}
输入101~ 110也会显示A,需要修补。
- int(score/10)
- if(score>=100||score<0)
code
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
float score;
printf("Plz input your score, lol:\n");
scanf("%f", &score);
if (score >= 100 || score < 0)
{
printf("the score % .2f is Error!\n", score);
}
else
{
switch (int(score / 10))
{
case 10:
case 9: printf("the score %.2f garde is A\n", score); break;
case 8: printf("the score %.2f garde is B\n", score); break;
case 7: printf("the score %.2f garde is C\n", score); break;
case 6: printf("the score %.2f garde is D\n", score); break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: printf("the score %.2f garde is E\n", score); break;
}
}
}
- 添加char语句char grade;精简程序
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
float score;
char grade;
printf("Plz input your score, lol:\n");
scanf("%f", &score);
if (score >= 100 || score < 0)
{
printf("the score % .2f is Error!\n", score);
}
else
{
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;
default: grade = 'E'; break;
}
printf("the score %.2f garde is %c\n", score,grade); //这一句还不懂,应该和char有关,明天再读
}
}