4.1
#include <iostream>
using namespace std;
int main()
{
cout << 5 + 10 * 20 / 2;//105
return 0;
}
4.2
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec = {1, 3, 5};
cout << *vec.begin() << ' ';
cout << *(vec.begin()) << ' ';
cout << *vec.begin() + 1 << ' ';
cout << (*(vec.begin())) + 1;
return 0;
}
4.3
可以。操作数的求解顺序通常对结果没什么影响,只有当二元操作符的两个操作数涉及同一对象,并改变对象的值时,操作数的求解顺序才会影响计算结果。实现效率的提高能使使用编译器的程序受益。
4.4
#include <iostream>
using namespace std;
int main()
{
cout << 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2;//91
return 0;
}
4.5
#include <iostream>
using namespace std;
int main()
{
cout << -30 * 3 + 21 / 5 << endl;//-86
cout << -30 + 3 * 21 / 5 << endl;//-18
cout << 30 / 3 * 21 % 5 << endl;//0
cout << -30 / 3 * 21 % 4 << endl;//-2
return 0;
}
4.6
a % 2 == 0;
4.7
溢出:计算的结果超出该类型所能表示的范围时就会产生溢出
#include <iostream>
using namespace std;
int main()
{
short i1 = 32767;
++i1;
cout << i1 << endl;
short i2 = -32768;
--i2;
cout << i2 << endl;
short i3 = 200 * 200;
cout << i3 << endl;
return 0;
}
4.8
按优先级从高到低:1)相等性2)逻辑与3)逻辑或
4.9
#include <iostream>
using namespace std;
int main()
{
const char *cp = "Hello World";
//cp不为空且cp指向的字符不为空字符
if(cp && *cp)
cout << 123;
return 0;
}
4.10
#include <iostream>
using namespace std;
int main()
{
int i;
while(cin >> i && i != 42)
cout << "Y" << endl;
cout << "i = 42";
return 0;
}
4.11
#include <iostream>
using namespace std;
int main()
{
int a = 4;
int b = 3;
int c = 2;
int d = 1;
cout << (a > b && b > c && c > d);
return 0;
}
4.12
#include <iostream>
using namespace std;
int main()
{
int i, j, k;
bool a1 = (i != j < k);
bool a2 = (i != (j < k));
if(a1 == a2)
cout << "Y";
return 0;
}
4.13
#include <iostream>
using namespace std;
int main()
{
int i;
double d;
d = i = 3.5;
cout << d << " " << i << endl;
i = d = 3.5;
cout << d << " " << i << endl;
return 0;
}
4.14
#include <iostream>
using namespace std;
int main()
{
int i;
//false
/*
if(42 = i)
cout << "a";
*/
if(i = 42)
cout << "b";
return 0;
}
4.15
#include <iostream>
using namespace std;
int main()
{
double dval;
int ival;
int *pi;
//false,pi is a pointer//dval = ival = pi = 0;
dval = ival = 0;
pi = &ival;
cout << dval << " " << ival << " " << pi;
return 0;
}
4.16
#include <iostream>
using namespace std;
int main()
{
//if(p = getPtr() != 0)
//if((p = getPtr()) != 0)
//if(i = 1024)
//if(i == 1024)
return 0;
}
4.17
前置运算符首先将运算对象加1(或减1),然后将改变后的对象作为求值结果。后置运算符将运算结果加1(或减1),
但求值结果是运算对象改变之前那个值的副本;
前置版本返回左值,后置版本返回右值;
4.18
与原来差一个位置,要修改pbeg != v.end()
4.19
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int i = 1;
int *ptr = &i;
vector<int> vec = {10, 20};
int ival = -1;
cout << (ptr != 0 && *ptr++);//短路求值,对
cout << (ival++ && ival);//同上
cout << (vec[ival++] <= vec[ival]);//未定义
return 0;
}
4.20
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> a = {"123", "1"};
vector<string>::iterator iter = a.begin();
//cout << (*iter++);//123
//false//cout << (*iter)++;
//false//cout << *iter.empty();
//cout << (iter->empty());//1
//false//cout << (++*iter);
cout << (iter++->empty());//0
return 0;
}
4.21
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> a{1, 2, 3};
for(auto &c : a)
{
(c % 2 == 1) ? c *= 2 : c;
}
for(auto c : a)
{
cout << c << endl;
}
return 0;
}
4.22
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int grade;
string finalgrade;
while(cin >> grade)
{
finalgrade = (grade > 90) ? "high pass" :
(grade > 75) ? "pass" :
(grade > 60) ? "low pass" : "fail";
cout << finalgrade << endl;
}
return 0;
}
4.23
#include <iostream>
using namespace std;
int main()
{
string s = "word";
//string p1 = s + s[s.size() - 1] == 's' ? "" : "s";
string p1 = s + ((s[s.size() - 1] == 's') ? "" : "s");
cout << p1;
return 0;
}
4.24
从左往右结合
4.25
#include <iostream>
using namespace std;
int main()
{
char i = 'q';
cout << (~'q' << 6);
}
4.26
int可能为16位,16<27
4.27
#include <iostream>
using namespace std;
int main()
{
unsigned long u11 = 3, u12 = 7;
cout << (u11 & u12) << endl;//3
cout << (u11 | u12) << endl;//7
cout << (u11 && u12) << endl;//1
cout << (u11 || u12) << endl;//1
return 0;
}
4.28
#include <iostream>
using namespace std;
int main()
{
cout << "bool:" << sizeof(bool) << endl;
cout << "char:" << sizeof(char) << endl;
cout << "signed char:" << sizeof(signed char) << endl;
cout << "unsigned char:" << sizeof(unsigned char) << endl;
cout << "short:" << sizeof(short) << endl;
cout << "unsigned short:" << sizeof(unsigned short) << endl;
cout << "int:" << sizeof(int) << endl;
cout << "unsigned int:" << sizeof(unsigned int) << endl;
cout << "long:" << sizeof(long) << endl;
cout << "unsigned long:" << sizeof(unsigned long) << endl;
cout << "float:" << sizeof(float) << endl;
cout << "double:" << sizeof(double) << endl;
cout << "long double:" << sizeof(long double) << endl;
return 0;
}
4.29
#include <iostream>
using namespace std;
int main()
{
int x[10];
int *p = x;
cout << sizeof(x) / sizeof(*x) << endl;
cout << sizeof(p) / sizeof(*p) << endl;
cout << sizeof(p) << endl;
cout << sizeof(*p) << endl;
return 0;
}
4.30
(sizeof x) + y;
sizeof (p->mem[i]);
(sizeof a) < b;
sizeof (f());
4.31
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec(10);
vector<int>::size_type cnt = ivec.size();
//前置后置都可以,不影响
for(vector<int>::size_type ix = 0; ix != ivec.size(); ++ix, cnt--)
ivec[ix] = cnt;
for(auto c : ivec)
cout << c << endl;
return 0;
}
4.32
#include <iostream>
#include <vector>
using namespace std;
int main()
{
constexpr int size = 5;
int ia[size] = {1, 2, 3, 4, 5};
for(int *ptr = ia, ix = 0; ix != size && ptr != ia + size; ++ix, ++ptr)
{
cout << *ptr << " " << ia[ix];
cout << endl;
}
return 0;
}
4.33
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int x = 1, y = 10;
int someValue;
/*someValue非0则加1;为0减1
while(cin >> someValue)
{
cout << (someValue ? (++x, ++y) : (--x, --y)) << endl;
}
*/
while(cin >> someValue)
{
x = 1;
y = 10;
int a = (someValue ? ++x, ++y : --x, --y);
cout << "x = " << x << " y = " << y << endl;
x = 1;
y = 10;
int b = ((someValue ? (++x, ++y) : --x), --y);
cout << "x = " << x << " y = " << y << endl;
cout << "a = " << a << " b = " << b << endl;
if(a == b)
cout << "Y" << endl;
}
return 0;
}
4.34
#include <iostream>
using namespace std;
int main()
{
float fval = 1.0;
int ival = 1;
double dval = 2.0;
char cval = 'a';
if(fval)//float->bool
;
dval = fval + ival;//int->float,float->double
dval + ival * cval;//char->int,int->double
return 0;
}
4.35
#include <iostream>
using namespace std;
int main()
{
char cval;
int ival;
unsigned int ui;
float fval;
double dval;
cval = 'a' + 3;//char->int,int->char;
fval = ui - ival * 1.0;//int->double,unsigned int->double,double->float
dval = ui * fval;//unsigned int->float,float->double
return 0;
}
4.36
#include <iostream>
using namespace std;
int main()
{
int i = 2;
double d = 3;
cout << (i *= d) << endl;
i = 2;
cout << (i *= static_cast<double>(d));
return 0;
}
4.37
#include <iostream>
using namespace std;
int main()
{
int i;
double d;
const string *ps;
char *pc;
void *pv;
//pv = (void*) ps;
pv = static_cast<void*>(const_cast<string*>(ps));
//i = int(*pc);
i = static_cast<int>(*pc);
//pv = &d;
pv = static_cast<void*>(&d);
//pc = (char*) pv;
pc = static_cast<char*>(pv);
return 0;
}
4.38
j/i转化为double类型给slope
来源:https://www.cnblogs.com/buptbfan/p/3405051.html