4.1 基础

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qit1314/article/details/90113735

书中页数:P122
代码名称:prec.cc

#include <iostream>
using std::cout; using std::endl;

int main() 
{
	cout << 6 + 3 * 4 / 2 + 2 << endl;

	// parentheses in this expression match default precedence and associativity
	cout << ((6 + ((3 * 4) / 2)) + 2) << endl; // prints 14

	int temp = 3 * 4;         // 12
	int temp2 = temp / 2;     // 6
	int temp3 = temp2 + 6;    // 12
	int result = temp3 + 2;   // 14
	cout << result << endl;

	// parentheses result in alternative groupings
	cout << (6 + 3) * (4 / 2 + 2) << endl;     // prints 36
	cout << ((6 + 3) * 4) / 2 + 2 << endl;     // prints 20
	cout << 6 + 3 * 4 / (2 + 2) << endl;       // prints 9

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qit1314/article/details/90113735