练习4.21
#include <iostream>
using namespace std;
#include <string>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
vector<int> vec ={1,2,3,4,5,6,7,8,9,10,11,12};
for(auto &i: vec)
i = (i % 2 != 0) ? i * 2 : i;
for(auto p : vec)
cout << p << " ";
cout<<endl;
}
练习4.22
#include <iostream>
using namespace std;
#include <string>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
int grade;
cin >> grade;
string finalgrade = (grade > 90) ? "high pass"
:(grade < 60) ? "fail"
:(grade <75) ? "low pass": "pass";
cout<< finalgrade <<endl;
}
练习5.25
#include <iostream>
using namespace std;
#include <string>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
int i1, i2;
while(cin >> i1 >> i2){
try{
if (i2 == 0)
throw runtime_error("i2 = 0");
cout << i1/i2 << endl;
}catch (runtime_error err) {
cout << err.what()<< " \nTry Again? Enter y or n" <<endl;
char c;
cin >> c;
if (!cin || c == 'n')
break;
}
}
}