//2-4
int main()
{
const float PI = 3.1416;
float a;
a = PI;
cout << a;
return 0;
}
//2-9
int main()
{
int a = 30;
int b = a++;
int c = ++a;
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
执行完以上语句后,a的值为32,b的值为30,c的值为32。运行截图如下:
//2-11
int main()
{
int n;
for (n = 0; n < 100; n++);
cout << n;
return 0;
}
for循环执行条件为n<100,故满足情况时,最后n为99,之后再执行n++,因此n的最终值为100。
//2-12
//使用while语句
int main()
{
int n = 100;
while (n < 200)
{
n = n + 2;
cout << n << " ";
}
return 0;
}
//2-12
//使用do...while语句
int main()
{
int n = 100;
do {
n = n + 2;
cout << n << " ";
} while (n < 200);
return 0;
}
运行截图如下:
//2-14
int main()
{
int x, y;
cout << "请输入两个不相等的整数x和y:"<<endl;
cin >> x >> y;
if (x < y)
{
y = x;
}
else {
x = y;
}
cout << "x=" << x << endl;
cout << "y=" << y << endl;
}
//2-15
int main()
{
int i;
int j;
i = 10;
j = 20;
cout << "i+j=" << (i + j);
return 0;
}
输出结果为:i+j=30
//2-16
int main()
{
double a;
cout << "请输入一个数:" ;
cin >> a;
cout << "您输入的数为:" << a;
return 0;
}
例如:
//2-17
int main()
{
cout << sizeof(char) << endl;
cout << sizeof(short) << endl;
cout << sizeof(int) << endl;
cout << sizeof(long) << endl;
cout << sizeof(long long) << endl;
cout << sizeof(double) << endl;
cout << sizeof(float) << endl;
}
//2-18
int main()
{
char a;
for (a = 32; a < 127; a++)
{
cout << a << " ";
}
return 0;
}
运行截图如下:
//2-19
int main()
{
unsigned int x;
unsigned int y = 100;
unsigned int z = 50;
x = y - z;
cout << "Difference is:" << x << endl;
x = z - y;
cout << "\nNow difference is:" << x << endl;
return 0;
}