2020 我的C++学习之路 C++PrimerPlus第六章课后习题
生活随笔
收集整理的這篇文章主要介紹了
2020 我的C++学习之路 C++PrimerPlus第六章课后习题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以C++ Primer Plus為參考書籍,自身歸納知識點,加深記憶。僅供參考,DEV C++已通過編譯運行
。
練習1
練習2
#include<iostream> #include<array> const int ArSize = 10; int main() {using namespace std;array<double, ArSize>donation;int count = 0;//實際數組數量計數器int num = 0;//大于均值計數器cout << "Enter double_type number into an array: " << endl;cout << "donation[1] : ";while (count < ArSize && cin >> donation[count])//cin>>將正確讀入目標類型值,因此在輸入非數字的情況下會返回false{++count;if( count < ArSize)cout << "donation[" << count + 1 << "] : ";}double sum, average;sum = 0;for (int cnt = 0; cnt < count; ++cnt)//計算平均值,count實際值可能會小于ArSize。{sum += donation[cnt];}average = sum / count;for (int cnt = 0; cnt < count; ++cnt)//找出大于均值的數{if (donation[cnt] > average)++num;}cout << "There are " << count << " numbers in donation_array" << endl;cout << "The average of donation_array is " << average << endl;cout << "There are " << num << " number over average." << endl;return 0; }練習3
#include<iostream>int main() {using namespace std;cout << "Please enter one of thr following choices: " << endl;cout << "c) carnivore p) pianist" << endl;cout << "t) tree g) game" << endl;char ch;do{cin >> ch;switch (ch){case 'c':cout << "carnivore";break;case 'p':cout << "pianist";break;case 't':cout << "A map is a tree";break;case 'g':cout << "game";break;default: cout << "Please enter a c,p,t or g:";}} while ((ch != 'c') && (ch != 'p') && (ch != 't') && (ch != 'g'));return 0; }練習4
#include<iostream>const int strsize = 30; const int member = 5;struct bop {char fullname[strsize];char title[strsize];char bopname[strsize];int preference; };int main() {using namespace std;cout << "Benevolent Order of Programmers Report" << endl;cout << "a.display by name b.display by title" << endl;cout << "c.display by bopname d.display by preference" << endl;cout << "q.quit" << endl;bop Bop[member] ={{"Wimp Macho","Wm","WMWM",0},{"Raki Rhodes","Junior Programmer","RRRR",1},{"Celia Laiter","Senior Programmer","MIPS",2},{"Hoppy Hipman","Analyst Trainee","HHHH",1},{"Pat hand","Student","LOOPY",2}};char ch;int cnt;cout << "Enter your choice: ";do{cin >> ch;switch (ch){case 'a':for (cnt = 0; cnt < member; ++cnt)cout << Bop[cnt].fullname << endl;break;case 'b':for (cnt = 0; cnt < member; ++cnt)cout << Bop[cnt].title << endl;break;case 'c':for (cnt = 0; cnt < member; ++cnt)cout << Bop[cnt].bopname << endl;break;case 'd':for (cnt = 0; cnt < member; ++cnt){if (Bop[cnt].preference == 0)cout << Bop[cnt].fullname << endl;else if (Bop[cnt].preference == 1)cout << Bop[cnt].title << endl;elsecout << Bop[cnt].bopname << endl;}break;}if (ch != 'q')cout << "Next choice: ";elsecout << "Bye!" << endl;} while (ch != 'q');return 0; }練習5
#include<iostream> const double tax1 = 0.1; const double tax2 = 0.15; const double tax3 = 0.2;int main() {using namespace std;double income, tax;cout << "Please input your income: ";while (cin >> income && income >= 0){if (income > 35000)tax = (income - 35000) * tax3 + 20000 * tax2 + 10000 * tax1;else if (income > 15000)tax = (income - 15000) * tax2 + 10000 * tax1;else if (income > 5000)tax = (income - 5000) * tax1;elsetax = 0;cout << "Your personal tax is : " << tax << endl;cout << "Please input your income: ";}return 0; }練習6
#include<iostream> #include<string> using namespace std; const double max = 10000;struct Donation {string name;double money; };int main() {int num, gpnum, pnum;gpnum = pnum = 0;cout << "Enter the number of donation: ";cin >> num;cin.get();Donation* donation = new Donation[num];for (int cnt = 0; cnt < num; ++cnt){cout << "Enter the name: ";getline(cin, donation[cnt].name);cout << "Enter the money: ";cin >> donation[cnt].money;cin.get();}cout << "Input complete!" << endl;cout << endl;cout << "Grand Patrons:" << endl;for (int cnt = 0; cnt < num; ++cnt){if (donation[cnt].money >= max){cout << donation[cnt].name << " ";cout << donation[cnt].money << endl;++gpnum;}}if (gpnum == 0)cout << "none." << endl;cout << "Patrons: " << endl;;for (int cnt = 0; cnt < num; ++cnt){if (donation[cnt].money < max){cout << donation[cnt].name << " ";cout << donation[cnt].money << endl;++pnum;}}if (pnum == 0)cout << "none." << endl;delete[]donation;return 0; }練習7
#include<iostream> #include<cctype> #include<cstring>//string比較好一些const int ArSize = 30;int main() {using namespace std;cout << "Enter words(q to quit):" << endl;char word[ArSize];int vowels, consonants, others;vowels = consonants = others = 0;cin >> word;while (strcmp(word,"q"))//C語言形式比較,兩個都為字符串,string對象則只需要邏輯運算{if (isalpha(word[0])){switch (word[0]){case 'a':case 'A': ++vowels;break;case 'e':case 'E':++vowels;break;case 'i':case 'I':++vowels;break;case 'o':case 'O':++vowels;break;case 'u':case 'U':++vowels;break;default:++consonants;break;}}else++others;cin >> word;//切勿忘記在沒有接收到q指令循環還在繼續,需要一個繼續鍵入的過程}cout << vowels << " words beginning with vowels" << endl;cout << consonants << " words beginning with consonants" << endl;cout << others << " others" << endl;return 0; }練習8
#include<iostream> #include<fstream> #include<cstdlib>const int SIZE = 30;int main() {using namespace std;char filename[SIZE];ifstream inFile;//TXT文件需與cpp文件在同一個根目錄下cout << "Enter name of file: ";cin.getline(filename, SIZE);inFile.open(filename);if (!inFile.is_open()){cout << "Couldn't open the file " << filename << endl;cout << "Program terminating." << endl;exit(EXIT_FAILURE);}char ch;int count = 0;inFile >> ch;//除去空格//inFile.get(ch);//包括空格while (inFile.good()){++count;cout << ch;inFile >> ch;//inFile.get(ch);}if (inFile.eof())cout << "End of file reached." << endl;else if (inFile.fail())cout << "Input termainated by data mismatch." << endl;elsecout << "Input termainated by unknowed error." << endl;if (count == 0)cout << "No data processed." << endl;elsecout << count << " chars in this txt." << endl;inFile.close();return 0; }練習9
#include<iostream> #include<fstream> #include<cstdlib> #include<string>using namespace std; const int SIZE = 30; const double max = 10000;struct Donation {string name;double money; };int main() {char filename[SIZE];ifstream inFile;//TXT文件需與cpp文件在同一個根目錄下cout << "Enter name of file: ";cin.getline(filename, SIZE);inFile.open(filename);if (!inFile.is_open()){cout << "Couldn't open the file " << filename << endl;cout << "Program terminating." << endl;exit(EXIT_FAILURE);}if (inFile.eof())cout << "End of file reached." << endl;else if (inFile.fail())cout << "Input termainated by data mismatch." << endl;elsecout << "Input termainated by unknowed error." << endl;int num,gpnum, pnum;gpnum = pnum = 0;inFile >> num;Donation* donation = new Donation[num];for (int cnt = 0; cnt < num; ++cnt){//inFile.ignore();inFile.get();getline(inFile, donation[cnt].name);inFile >> donation[cnt].money;}cout << "Input complete!" << endl;cout << endl;cout << "Grand Patrons:" << endl;for (int cnt = 0; cnt < num; ++cnt){if (donation[cnt].money >= max){cout << donation[cnt].name << " ";cout << donation[cnt].money << endl;++gpnum;}}if (gpnum == 0)cout << "none." << endl;cout << "Patrons: " << endl;;for (int cnt = 0; cnt < num; ++cnt){if (donation[cnt].money < max){cout << donation[cnt].name << " ";cout << donation[cnt].money << endl;++pnum;}}if (pnum == 0)cout << "none." << endl;delete[]donation;inFile.close();return 0; }總結
以上是生活随笔為你收集整理的2020 我的C++学习之路 C++PrimerPlus第六章课后习题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020 我的C++学习之路 C++Pr
- 下一篇: 2020 我的C++学习之路 C++Pr