C++ Primer Answer ch05
5.1
空语句是;
在程序的某个地方,语法上需要一条语句但是逻辑上不需要,此时应该使用空语句。
5.2
块(复合语句)是指用花括号括起来的(可能为空的)语句和声明的序列
如果在程序的某个地方,语法上需要一条语句,但是逻辑上需要多条语句,则应该使用复合语句
5.3
#include <iostream> using namespace std; int main() { int sum = 0, val = 1; while(val <= 10) { sum += val; ++val; } cout << "Sum of 1 to 10 inclusive is " << sum << endl; sum = 0; val = 0; while(++val, val <= 10) sum += val; cout << "Sum of 1 to 10 inclusive is " << sum << endl; return 0; }
5.4
#include <iostream> using namespace std; bool find(int) { return true; } int main() { /* string s = "132"; string::iterator iter = s.begin(); while(iter != s.end()) ; */ int word = 2; while(bool status = find(word)) { } //status was not declared//if(!status) //; return 0; }
5.5
#include <iostream> #include <vector> using namespace std; int main() { vector<string> s = {"A", "B", "C"}; int grade; while(cin >> grade) { if(grade >= 100) cout << s[0] << endl; else if(grade >= 60) cout << s[1] << endl; else cout << s[2] << endl; } return 0; }
5.6
#include <iostream> #include <vector> using namespace std; int main() { vector<string> s = {"A", "B", "C"}; int grade; while(cin >> grade) { (grade >= 100) ? (cout << s[0] << endl) : (grade >= 60) ? (cout << s[1] << endl) : (cout << s[2] << endl); } return 0; }
5.7
#include <iostream> using namespace std; int main() { //a if(ival1 != ival2) ival1 = ival2; else ival1= ival2 = 0; //b if(ival < minval) { minval = ival; occurs = 1; } //c int ival; if(ival = get_value()) cout << "ival = " << ival << endl; if(!ival) cout << "ival = 0\n"; //d if(ival == 0) ival = get_value(); return 0; }
5.8
悬垂else:else和if的匹配问题
C++规定else与离它最近的尚未匹配的if匹配
5.9
#include <iostream> using namespace std; int main() { char ch; int vowelCnt = 0; while(cin >> ch) { switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': ++vowelCnt; break; default: ; } } cout << vowelCnt; return 0; }
5.10
#include <iostream> using namespace std; int main() { char ch; int vowelCnt = 0; while(cin >> ch) { switch(ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': ++vowelCnt; break; default: ; } } cout << vowelCnt; return 0; }
5.11
#include <iostream> using namespace std; int main() { char ch; int spaceCnt = 0, tCnt = 0, nCnt = 0; int otherCnt = 0; //cin >> ch遇到空格等结束 while(cin.get(ch)) { switch(ch) { case ' ': ++spaceCnt; break; case '\t': ++tCnt; break; case '\n': ++nCnt; break; default: ++otherCnt; ; } } cout << spaceCnt << endl << tCnt << endl << nCnt << endl << otherCnt << endl; return 0; }
5.12
#include <iostream> using namespace std; int main() { char ch; char prech = '\0'; int ffCnt = 0, flCnt = 0, fiCnt = 0; while(cin.get(ch)) { if(prech == 'f') { switch(ch) { case 'f': ++ffCnt; break; case 'l': ++flCnt; break; case 'i': ++fiCnt; break; default: ; } } prech = ch; } cout << ffCnt << endl << flCnt << endl << fiCnt << endl; return 0; }
5.13
//a /* unsigned cCnt = 0, eCnt = 0, iouCnt = 0; char ch = next_text(); switch(ch) { case 'a': ++aCnt; break; case 'b': eCnt++; break; default: ++iouCnt; } */ //b /* unsigned index = some_value(); int ix; switch(index) { case 1: ix = get_value(); ivec[ix] = index; break; default: ix = ivec.size() - 1; ivec[ix] = index; } */ //c unsigned evenCnt = 0, oddCnt = 0; int digit = get_num() % 10; switch(digit) { case 1: case 3: case 5: case 7: case 9: ++oddCnt; break; case 0: case 2: case 4: case 6: case 8: ++evenCnt; break; } //d const unsigned ival = 512, jval = 1024, kval = 4096; unsigned bufsize; unsigned swt = get_bufCnt(); switch(swt) { case ival: bufsize = ival * sizeof(int); break; case jval: bufsize = jval * sizeof(int); break; case kval: bufsize = kval * sizeof(int); break; }
5.14
#include <iostream> #include <vector> using namespace std; int main() { string word; int cnt = 1; int cntMax = 1; string compareword; string wordMax; while(cin >> word) { if(word == compareword) ++cnt; else cnt = 1; if(cnt > cntMax) { wordMax = compareword; cntMax = cnt; } compareword = word; } if(cntMax > 1) cout << cntMax << ' ' << wordMax; else cout << "There is no repeated word."; return 0; }
5.15
//a int ix; for(ix = 0; ix != sz; ++ix) { } if(ix != sz) ; //b int ix; for(; ix != sz; ++ix) ; //c for(int ix = 0; ix != sz; ++ix) ;
5.16
#include <iostream> using namespace std; int main() { const size_t a_size = 3; int a[a_size] = {1, 2, 3}; size_t i = 0; for(; i != a_size; ++i) cout << a[i] << endl; cout << endl; i = 0; while(i != a_size) { cout << a[i] << endl; ++i; } return 0; }
for更灵活
5.17
#include <iostream> #include <vector> using namespace std; int main() { vector<int> a{0, 1, 1, 2}; vector<int> b{0, 1, 1, 2, 3, 5, 8}; vector<int>::size_type len; (a.size() <= b.size()) ? (len = a.size()) : (len = b.size()); for(decltype(a.size()) i = 0; i != len; ++i) { if(a[i] != b[i]) { return 1; } } cout << "Y"; return 0; }
5.18
#include <iostream> using namespace std; int get_response(void) { return 1; } int main() { //a /* do{ int v1. v2; cout << "Please enter two numbers to sum:"; if(cin >> v1 >> v2) cout << "Sum is:" << v1 + v2 << endl; }while(cin) */ //b /* int ival; do{ }while(ival = get_response()); */ //c int ival = get_response(); do{ }while(ival); return 0; }
5.19
#include <iostream> using namespace std; int main() { string s1, s2; do { cin >> s1 >> s2; if(s1.size() <= s2.size()) cout << s1 << endl; else cout << s2 << endl; }while(cin); return 0; }
5.20
#include <iostream> using namespace std; int main() { string word; string compareWord; int flag = 0; while(cin >> word) { if(word == compareWord) { cout << word << endl; flag = 1; break; } compareWord = word; } if(flag == 0) cout << "no matching"; return 0; }
5.21
#include <iostream> #include <cctype> using namespace std; int main() { string word; string compareWord; int flag = 0; while(cin >> word) { if(!isupper(word[0])) continue; if(word == compareWord) { cout << word << endl; flag = 1; break; } compareWord = word; } if(flag == 0) cout << "no matching"; return 0; }
5.22
/* begin: int sz = get_size(); if(sz <= 0) { goto begin; } */ do{ int sz = get_size(); }while(sz <= 0);
5.23
#include <iostream> #include <stdexcept> using namespace std; int main() { int a, b; while(cin >> a >> b) { try{ if(b == 0) throw runtime_error("b must != 0"); cout << a / b << endl; }catch(runtime_error e){ cout << e.what(); return 1; } } return 0; }
5.24
#include <iostream> #include <stdexcept> using namespace std; int main() { int a, b; while(cin >> a >> b) { if(b == 0) throw runtime_error("b must != 0"); cout << a / b << endl; } return 0; }
5.25
#include <iostream> #include <stdexcept> using namespace std; int main() { int a, b; while(cin >> a >> b) { try{ if(b == 0) throw runtime_error("b must != 0"); cout << a / b << endl; }catch(runtime_error e){ cout << e.what(); cout << "\nTry again?(y/n)"; char ch; cin >> ch; if(!cin || ch == 'n') break; else { cin >> b; if(b == 0) break; cout << a / b << endl; } } } return 0; }
浙公网安备 33010602011771号