4.2 算术运算符

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

书中页数:P124
代码名称:unaryOps.cc overflow.cc arith-ex.cc int-div.cc

//unaryOps.cc
#include <iostream>
using std::cout; using std::endl;

int main()
{
	int i = 1024;
	int k = -i; // i is -1024
	
	bool b = true;
	bool b2 = -b; // b2 is true!
	
	cout << b << " " << b2 << " " << endl;
	
	return 0;
}

// overflow.cc 
#include <iostream>
using std::cout; using std::endl;

int main() 
{
	short short_value = 32767; // max value if shorts are 16 bits

	short_value += 1; // this calculation overflows
	cout << "short_value: " << short_value << endl;

    return 0;
}

// arith-ex.cc
#include <iostream>
using std::cout; using std::endl;

int main()
{
	cout << -30 * 3 + 21 / 5 << endl;
	
	cout << -30 + 3 * 21 / 5 << endl;
	
	cout << 30 / 3 * 21 % 5 << endl;
	
	cout << 30 / 3 * 21 % 4 << endl;
	
	cout << -30 / 3 * 21 % 4 << endl;
	
	cout << 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2 << endl;
	
	return 0;
}

//int-div.cc
#include <iostream>
using std::cout; using std::endl;

int main() 
{
	// ival1 is 3; result is truncated; remainder is discarded
	int ival1 = 21/6;

	// ival2 is 3; no remainder; result is an integral value
	int ival2 = 21/7;

	cout << ival1 << " " << ival2 << endl;

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qit1314/article/details/90083544
4.2