4.6

#include "stdafx.h"
#include<iostream>
using namespace std;
int main() {
int a=0;
cin >> a;
if (a % 2 == 0) {
cout << "偶数"<< endl;

}else {
cout << "奇数" << endl;

}
}


4.7

1----指针溢出

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main() {
int a[3] = { 0,0,0 };
for (auto q = a; q != a+5; ++q) {
cout << *q << endl;
}


}


2----unsigned溢出

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main() {
int a = 20;
unsigned int b = a / -5;
cout << b << endl;


}


3----short溢出

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main() {
short a = 32768;

}


4.10

#include<iostream>
using namespace std;
int main() {
int a = 0;
while (cin>>a && a!=42) {
cout << "yes" << endl;
}
}


4.11

#include<iostream>
using namespace std;
int main() {
int a=5, b=4, c=3, d=2;
if (a > b && b > c && c > d) {
cout << "符合" << endl;
}
}


4.21

#include<iostream>
using namespace std;
int main() {
int a=5, b=4, c=3, d=2;
if (a > b && b > c && c > d) {
cout << "符合" << endl;
}
}


4.22

1----使用多个if

#include<iostream>
#include<vector>
using std::vector;
using namespace std;
int main() {
int grade;
cin >> grade;
if (grade >= 90) {
cout << "high pass";
}
else {
if (grade >= 75) {
cout << "pass";
}
else {
if (grade >= 60) {
cout << "low pass";
}
else {
cout << "fail";
}
}
}
}


2----只使用条件运算符

#include<iostream>
#include<vector>
using std::vector;
using namespace std;
int main() {
int grade;
cin >> grade;
cout << ((grade >= 90) ? ("high pass") : (grade >= 75) ? ("pass") : (grade >= 60) ? ("low pass") : ("fail"));
}


4.28

#include<iostream>
using namespace std;
int main() {
cout << sizeof(bool) << endl;
cout << sizeof(char) << endl;
cout << sizeof(wchar_t) << endl;
cout << sizeof(char16_t) << endl;
cout << sizeof(float) << endl;
cout << sizeof(double) << endl;
cout << sizeof(long double) << endl;
}


4.29

#include<iostream>
using namespace std;
int main() {
int a[10]; int*p = a;
cout << sizeof(a) / sizeof(*a) << endl;;
cout << sizeof(p) / sizeof(*p);
}


4.31

#include<iostream>
using namespace std;
#include<vector>
int main() {
vector<int>ivec = { 0,1,2 };
vector<int>::size_type cnt = ivec.size();
for (vector<int>::size_type ix = 0; ix != ivec.size();) {
ivec[ix++] = cnt--;
}
for (auto q : ivec) {
cout << q << endl;
}
}