当前位置:网站首页>C语言日记 5 运算符和表达式
C语言日记 5 运算符和表达式
2022-08-02 14:03:00 【宇 -Yu】
书P23例题2-3:
#include <iostream>
using namespace std;
void main()
{
cout << 20 / 7 << "," << -20 / 7 << endl;
cout << 20.0 / 7 << "," << -20.0 / 7 << endl; cout << 20 % 7 << endl;
}
算术表达式。例:
#include <iostream>
using namespace std;
void main()
{
int x = 5, r = 8, a= 9, b = 10;
cout << (x + r) * 8 - (a + b) / 7;
}
步骤:括号的优先级最高,所以先算x+r=13和a+b=19,其次优先级最高的是乘除,所以接下来先算乘八和加七,最后在做优先级最低的减法计算,最后得到102。
书P4例题2-4:
#include <iostream>
using namespace std;
void main()
{
int i = 8;
cout << ++i << endl;
cout << --i << endl;
cout << i++ << endl;
cout << i-- << endl;
cout << -i++ << endl;
cout << -i-- << endl;//输出9//输出 8//输出 8//输出9//输出-8//输出-9
}
notes:
例2-5:
#include <iostream>
using namespace std;
void main()
{
int i = 5, j = 5, p, q;
p = (i++) + (i++) + (i++);
q = (++j) + (++j) + (++j);
cout << p << "," << q << "," << i << "," << j << endl;
}
对于p我们没什么好说的。
对于q,q = (++j) + (++j) + (++j);遵循原则“先自增,再运算”;
由于++运算符的优先级高于+,所以我们先进行自增操作:
步骤1:j等于j+1等于6,然后我们带着j=6的结果继续参加下一步运算;
步骤2:j等于j+1等于7,然后我们带着j=7的结果继续参加下一步运算;
步骤3:j等于j+1等于8,然后我们带着j=8的结果继续参加下一步运算;
自增完成,下面我们继续进行加法运算即可:q=8+8+8=24;
例2-6:
using namespace std;
void main()
{
int a, b = 322;//整型
float x, y = 8.88;//浮点型,即实(数)型
char cl = 'k', c2;//字符型
a = y; //实型转整型,a=8
x = b;//整型转实型,x=322.0
a = cl;//字符型转整型,把字符k对应的ASCII码数值ASC(k)=107赋给a,a=107
c2 = b;//整型转字符型
//把数字322对应二进制数(101000010)低八位(01000010)作为ASCII码数值(66)
//把ASCII码数值(66)对应的字符Char(66)=B赋给c2
cout << a << "," << x << "," << a << "," << c2 << endl;
}
复合赋值运算符
例如:
#include <iostream>
using namespace std;
void main()
{
int a = 5;
a += 5;
cout << a;
}
等价于
#include <iostream>
using namespace std;
void main()
{
int a = 5;
a = a + 5;
cout << a;
}
书P28例2-7:
#include <iostream>
using namespace std;
int main()
{
int a = 0, b = 1;
bool m = (a++) && (b++);
//表达式 a++的值为a原本的值0
//所以&&前的表达式确定为假,b++不会被执行
cout << a << "," << b << endl;
m = a++ && b++;
cout << a << "," << b << endl;
m = a++ || b++;
//表达式a++确定为真,b++不会被执行
cout << a << "," << b << endl;
return 0;
}
在这里,其实因为自增的算数运算符优先级始终大于逻辑运算符。
所以不管是先自增后运算还是先运算后自增,不管有没有加括号,都是先进行自增操作,然后再进行国际运算符的与操作,或操作。
注意:这里括号加跟不加都一样,比如:
#include <iostream>
using namespace std;
int main()
{
int a = 0, b = 1;
bool m = a++ && b++;
//表达式 a++的值为a原本的值0
//所以&&前的表达式确定为假,b++不会被执行
cout << a << "," << b << "," << m << endl;
m = a++ && b++;
cout << a << "," << b << "," << m << endl;
m = (a++) || (b++);
//表达式a++确定为真,b++不会被执行
cout << a << "," << b << "," << m << endl;
return 0;
}
书P29例2-8:
#include <iostream>
using namespace std;
int main()
{
int a = 2, b = 4, c = 6, x, y;
y = ((x = a + b), (b + c));
cout << "y=" << y << ",x=" << x << endl;
}
强制类型转换两种方式:
一:
#include <iostream>
using namespace std;
int main()
{
float f = 1.2;
int i = (int)f;
cout << i;
}
二:
#include <iostream>
using namespace std;
int main()
{
float f = 1.2;
int i = int(f);
cout << i;
}
边栏推荐
- drf路由组件Routers
- 8583 顺序栈的基本操作
- 【Camera2】由Camera2 特性想到的有关MED(多场景设备互动)的场景Idea
- Haystack的介绍和使用
- Creating seven NiuYun Flask project complete and let cloud
- Flask framework in-depth
- [ROS](03)CMakeLists.txt详解
- Deep learning framework pytorch rapid development and actual combat chapter3
- 8580 合并链表
- Linux: CentOS 7 install MySQL5.7
猜你喜欢
随机推荐
[ROS]ROS常用工具介绍(待续)
重新学习编程day1 【初始c语言】【c语言编写出计算两个数之和的代码】
The specific operation process of cloud GPU (Hengyuan cloud) training
Flask-SQLAlchemy
drf source code analysis and global catch exception
Creating seven NiuYun Flask project complete and let cloud
(ROS) (03) CMakeLists. TXT, rounding
St. Regis Takeaway Notes - Lecture 10 Swagger
Chapter6 visualization (don't want to see the version)
yolov5,yolov4,yolov3 mess
网页设计(新手入门)[通俗易懂]
【ROS】工控机的软件包不编译
瑞吉外卖笔记——第10讲Swagger
Unit 7 ORM table relationships and operations
函数递归和动态内存初识
第八单元 中间件
What's wrong with running yolov5 (1) p, r, map are all 0
字符串的小知识
c语言用scanf出错不安全的解决办法
瑞吉外卖笔记——第08讲读写分离