2020 我的C++学习之路 C++PrimerPlus第五章课后习题
生活随笔
收集整理的這篇文章主要介紹了
2020 我的C++学习之路 C++PrimerPlus第五章课后习题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以C++ Primer Plus為參考書籍,自身歸納知識點,加深記憶。僅供參考,DEV C++已通過編譯運行
。
練習1
練習2
#include<iostream> #include<array> const int ArSize = 101;int main() {using namespace std;//cout.setf(ios_base::fixed, ios_base::floatfield);//去除科學計數法顯示array<long double, ArSize>factorials;factorials[0] = factorials[1] = 1.0; for (int i = 2; i < ArSize; i++)factorials[i] = i * factorials[i - 1];for (int i = 0; i < ArSize; i++)cout << i << "!= " << factorials[i] << endl;return 0; }練習3
#include<iostream>int main() {using namespace std;int integer, sum;sum = 0;cout << "Enter an integer(0 to quit): ";cin >> integer;while (integer)//integer != 0即為integer = true {sum += integer;cout << "Enter an integer(0 to quit): ";cin >> integer;}cout << "Sum of integer is: " << sum << endl;return 0; }練習4
#include<iostream> const double beneda = 0.1; const double benec = 0.05; int main() {using std::cout;using std::endl;double daphne, cleo, temp;daphne = cleo = 100.0;temp = daphne * beneda;int count = 1;while (cleo <= daphne){daphne += temp;cleo += cleo * benec;cout << count << " " << daphne << " " << cleo << endl;count++;}cout << "Atleat " << count << " years";cout << "Cleo will beat Daphne." << endl;cout << count << " years latter,Daphne has " << daphne;cout << " dallors,Cleo has " << cleo << " dallors" << endl;return 0; }練習5
#include<iostream> const int month = 12;int main() {using namespace std;const char* Month[month] ={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};int* salenum = new int[month];int count = 1;int sum = 0;while (count <= month){cout << "Enter salenum in month " << Month[count-1] << " : ";//Month[]這里需要注意數組的序號問題cin >> salenum[count];sum += salenum[count];count++;}cout << "This Year sales " << sum << endl;delete []salenum;return 0; }練習6
#include<iostream> const int month = 12; const int year = 3;int main() {using namespace std;int salenum[year][month];int cnty, cntm;//行列計數器int sumyear, sum;//每年總結以及總數總結sum = 0;for (cnty = 0; cnty < year; ++cnty){cout << "Enter year " << cnty + 1 << "'s salenum per month" << endl;for (cntm = sumyear = 0; cntm < month; ++cntm)//注意每年sum需要重置0{cout << "No." << cntm + 1 << " month's salenum is:";cin >> salenum[cnty][cntm];sumyear += salenum[cnty][cntm];}cout << cnty + 1 << "year's salenu sum = " << sumyear << endl;sum += sumyear;}cout << cnty + 1 << " years later, salesnum is : " << sum << endl;return 0; }練習7
#include<iostream> #include<string> using namespace std;struct Car {string name;int year; };int main() {int num, cnt;cout << "How many car do you wish to catalog: ";cin >> num;cin.get();//此處有個換行符Car* car = new Car[num];for (cnt = 0; cnt < num;++cnt){cout << "Car #" << cnt+1 << ":" << endl;cout << "Please enter the make:";getline(cin, car[cnt].name);cout << "Please enter the year made:";cin >> car[cnt].year;cin.get();//此處也有個換行符}cout << "Here is your collection:" << endl;for (cnt = 0; cnt < num; ++cnt){cout << car[cnt].year << " " << car[cnt].name << endl;}delete[]car;return 0; }練習8
#include<iostream> #include<cstring>int main() {using namespace std;char word[15];int count = 0;cout << "Enter words(to stop ,type the word done):" << endl;cin >> word;while (strcmp(word, "done")){++count;cin >> word;}cout << "You entered a total of " << count << " words." << endl;return 0; }練習9
#include<iostream> #include<string>int main() {using namespace std;string word;int count = 0;cout << "Enter words(to stop ,type the word done):" << endl;cin >> word;while (word != "done"){++count;cin >> word;}cout << "You entered a total of " << count << " words." << endl;return 0; }練習10
#include<iostream>int main() {using namespace std;int row,col,cntc,star;//row表示行,col表示列,cntc計數器,star星星個數cout << "Enter number of rows: ";cin >> row;col = row;;for (star = 1; star <= row; ++star)//構造單行{for (cntc = 1; cntc < col; ++cntc)cout << "." ;//單行循環輸出“。”for (; cntc <= row; ++cntc)cout << "*";//接“。”后的星星--col;//每一行遞減一個“。”cout << endl;}return 0; }總結
以上是生活随笔為你收集整理的2020 我的C++学习之路 C++PrimerPlus第五章课后习题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020 我的C++学习之路 C++Pr
- 下一篇: 2020 我的C++学习之路 C++Pr