C++ //习题2.5 请写出下列表达式的值。

2023-12-13 03:52:44

C++程序设计 (第三版) 谭浩强 习题2.5

习题2.5 请写出下列表达式的值。

(1) 3.5 * 3 + 2 * 7 - ‘a’
(2) 26 / 3 + 34 % 3 + 2.5
(3) 45 / 2 + (int)3.14159 / 2
(4) a = b = (c = a += 6) 设a的初值为3
(5) a = 3 * 5, a = b = 3 * 2
(6) (int)(a + 6.5) % 2 + (a = b = 5) 设a的初值为3
(7) x + a % 3 * (int)(x + y) % 2 / 4 设x = 2.5,a = 7,y = 4.7
(8) (float)(a + b) / 2 + (int)x % (int)y 设a = 2,b = 3,x = 3.5,y = 2.5
IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。

?

代码块
#include <iostream>
using namespace std;

int main(){
	float p1 = 3.5 * 3 + 2 * 7 - 'a';
	float p2 = 26 / 3 + 34 % 3 + 2.5;
	int p3 = 45 / 2 + (int)3.14159 / 2;
	int a, b, c;
	a = 3;
	int p4 = a = b = (c = a += 6);
	int p5;
	p5 = a = 3 * 5, p5 = a = b = 3 * 2;
	a = 3;
	int p6 = (int)(a + 6.5) % 2 + (a = b = 5);
	float x, y;
	x = 2.5;
	y = 4.7;
	a = 7;
	float p7 = x + a % 3 * (int)(x + y) % 2 / 4;
	a = 2;
	b = 3;
	x = 3.5;
	y = 2.5;
	float p8 = (float)(a + b) / 2 + (int)x % (int)y;
	cout<<"p1 = "<<p1<<endl;
	cout<<"p2 = "<<p2<<endl;
	cout<<"p3 = "<<p3<<endl;
	cout<<"p4 = "<<p4<<endl;
	cout<<"p5 = "<<p5<<endl;
	cout<<"p6 = "<<p6<<endl;
	cout<<"p7 = "<<p7<<endl;
	cout<<"p8 = "<<p8<<endl;

	system("pause");
	return 0;
}
结果显示如下:

在这里插入图片描述

文章来源:https://blog.csdn.net/navicheung/article/details/134920691
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。