运算符与表达式
-
常用运算符分类
-
算术运算符
-
赋值运算符
-
比较运算符
-
C 语言的比较运算中,“真”用数字“1”来表示“假”用数字“0”来表示。
-
逻辑运算符
- 运算符优先级
自增运算
#include <iostream>
using namespace std;
// 自增运算符
int main() {
// 后置递增
int a = 10;
a++; // 等价于 a = a + 1
cout << "a = " << a << endl;
// 前置递增
int b = 10;
++b; //
cout << "b = " << b << endl;
// 区别
// 前置递增是先对变量进行++, 然后再参与计算表达式
int a2 = 10;
int b2 = ++a2*10;
cout << "b2 = " << b2 << endl;
cout << "a2 = " << a2 << endl;
// 后置递增是先参与计算表达式, 然后再对变量进行++
int a3 = 10;
int b3 = a3++*10;
cout << "b3 = " << b3 << endl;
cout << "a3 = " << a3 << endl;
}
类型转换
数据有不同的类型,不同类型数据之间进行混合运算时必然涉及到类型的转换问题。
转换的方法有两种:
- 自动转换(隐式转换):遵循一定的规则,由编译系统自动完成。
- 强制类型转换:把表达式的运算结果强制转换成所需的数据类型。
类型转换的原则:占用内存字节数少(值域小)的类型,向占用内存字节数多(值域大)的类型转换,以保证精度不降低
隐式转换
#include <stdio.h>
int main()
{
int num = 5;
printf("s1=%d\n", num / 2);
printf("s2=%lf\n", num / 2.0);
return 0;
}
强制转换
强制类型转换指的是使用强制类型转换运算符,将一个变量或表达式转化成所需的类型,其基本语法格式如下所示:
(类型说明符) (表达式)
#include <stdio.h>
int main()
{
float x = 0;
int i = 0;
x = 3.6f;
i = x; //x为实型, i为整型,直接赋值会有警告
i = (int)x; //使用强制类型转换
printf("x=%f, i=%d\n", x, i);
return 0;
}
程序流程结构
C语言支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构。
- 顺序结构:程序按顺序执行,不发生跳转。
- 选择结构:依据是否满足条件,有选择的执行相应功能。
- 循环结构:依据条件是否满足,循环多次执行某段代码。
选择结构
#include <stdio.h>
int main()
{
int age = 18;
if (age >= 18)
{
printf("您的年龄是 %d 岁, 恭喜你成年了可以去网吧了\n", age);
}
return 0;
}
- if…else语句
#include <stdio.h>
int main()
{
int age = 12;
if (age >= 18)
{
printf("您的年龄是 %d 岁, 恭喜你成年了可以去网吧了\n", age);
} else{
printf("您的年龄是 %d 岁, 对不起, 你还是个宝宝\n", age);
}
return 0;
}
- if…else if…else语句
-
#include <iostream>
using namespace std;
// 成绩level显示器
int main() {
int score;
cout<< "please input your score"<< endl;
cin >> score;
cout<< "your score is "<< score<< endl;
if (score >= 90 && score <= 100){
cout<< "your score level is A "<< endl;
}else if (score >= 80 && score < 90){
cout<< "your score level is B "<< endl;
}else if (score >= 60 && score < 80){
cout<< "your score level is C "<< endl;
}else if (score >= 0 && score < 60){
cout<< "your score level is D "<< endl;
}else{
cout<< "your score is error "<< endl;
}
}
作业1:三只小猪称重
有三只⼩猪ABC,请分别输⼊三只⼩猪的体重,并且判断哪只⼩猪最重?
#include <iostream>
using namespace std;
int main(){
int piga;
int pigb;
int pigc;
int res = 0; // 最终的那个
cout << "weights of three pigs" << endl;
cin >> piga;
cin >> pigb;
cin >> pigc;
if (piga >= pigb){
res = piga;
}else{
res = pigb;
}
if (res <= pigc){
res = pigc;
}
cout<< "max weight = "<< res<< endl;
}
作业2:BMI计算器
计算公式为:BMI=体重÷身高^2。(体重单位:千克;身高单位:米。)
#include <iostream>
using namespace std;
int main(){
double weight;
double height;
double BMI;
cout << "input your weight" << endl;
cin>>weight;
cout << "input your height" << endl;
cin>>height;
BMI = weight/(height*height);
if(BMI <= 18.4){
cout << "thin and your BMI is " << BMI << endl;
}else if(BMI > 18.4 && BMI <= 23.9){
cout << "normal and your BMI is " << BMI << endl;
}else if(BMI > 23.9 && BMI <=27.9){
cout << "overweight and your BMI is " << BMI << endl;
}else if(BMI > 27.9 ){
cout << "fat and your BMI is " << BMI << endl;
}else {
cout << "follow the instructions" << endl;
}
}
思考任务(课后练习)
1.从屏幕上输入一个学生的成绩(0-100),对学生成绩进行评定:
<=60为"E"
60~69为"D"
70~79为"C"
80~89为"B"
90以上为"A"
<0或>100提示成绩输入出错
使用:if else if等实现
- 从键盘输入 1~7 的数字,分别提示 Monday、Tuesday、Wednesday、Thursday、Friday、Saturday、Sunday 输入其它,提示出错
使用:switch case break 实现
//int mian(void)
//{
// int num;
// printf(“请输入数据:”);
// scanf("%d", &num);
// switch (num % 10)
// {
// case 1:printf(“星期一”); break;
// case 2:printf(“星期二”); break;
// case 3:printf(“星期三”); break;
// case 4:printf(“星期四”); break;
// case 5:printf(“星期五”); break;
// case 6:printf(“星期六”); break;
// case 7:printf(“星期七”); break;
// default:
// printf(“输入不合法”);
// }
3.输出100以内能被7整除的数,分别用for循环和while循环完成
int main(void)
//{
// for (int i = 0; i < 100; i++)
// {
// if (!(i % 7))
// printf("%d\n", i);
// }
// int i = 100;
// while (i % 7 || printf("%d\n", i),i–);
// system(“pause”);
// return 0;
//}
4.输出100-1000以内的水仙花数
水仙花数算法:一个数等于它各位的立方和,例如:153= 111 + 555 + 333
提示:for循环,求余(%)、取整(/)运算符