当前位置:网站首页>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;
}
边栏推荐
- C语言——断言assert的使用
- drf源码分析与全局捕获异常
- verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十四章)
- 【Camera2】由Camera2 特性想到的有关MED(多场景设备互动)的场景Idea
- The specific operation process of cloud GPU (Hengyuan cloud) training
- Unit 10 Continuous Tuning
- verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第九章)
- ftp常用命令详解_iftop命令详解
- drf视图组件
- verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十二章)
猜你喜欢

瑞吉外卖笔记——第10讲Swagger
![[ROS] (06) ROS Communication - Topic Communication](/img/21/d79f2c4e246eb9ea39df9c7435bb36.png)
[ROS] (06) ROS Communication - Topic Communication

The specific operation process of cloud GPU (Hengyuan cloud) training

Visual Studio配置OpenCV之后,提示:#include<opencv2/opencv.hpp>无法打开源文件

函数递归和动态内存初识

What's wrong with running yolov5 (1) p, r, map are all 0
![[ROS]roscd和cd的区别](/img/a8/a1347568170821e8f186091b93e52a.png)
[ROS]roscd和cd的区别

Deep learning framework pytorch rapid development and actual combat chapter4

STM32 (F407) - stack

c语言三子棋详解!!! (电脑智能下棋)(附上完整代码)
随机推荐
Unit 15 Paging, Filtering
第七单元 ORM表关系及操作
跑yolov5又出啥问题了(1)p,r,map全部为0
run yolov5
[ROS](01)创建ROS工作空间
第六单元 初识ORM
Unit 10 Continuous Tuning
鼠标右键菜单栏太长如何减少
chapter7
Introduction and use of Haystack
我的第一篇博客
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十二章)
8576 顺序线性表的基本操作
Implementation of redis distributed lock and watchdog
使用云GPU+pycharm训练模型实现后台跑程序、自动保存训练结果、服务器自动关机
网络剪枝(1)
yolov5改进(一) 添加注意力集中机制
Flask-SQLAlchemy
Hands-on OCR (1)
drf路由组件Routers