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;
}