日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

2020 我的C++学习之路 C++PrimerPlus第九章课后习题

發(fā)布時(shí)間:2025/3/20 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2020 我的C++学习之路 C++PrimerPlus第九章课后习题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

以C++ Primer Plus為參考書籍,自身歸納知識點(diǎn),加深記憶。僅供參考,DEV C++已通過編譯運(yùn)行

練習(xí)1

//golf.h const int Len = 40; struct golf {char fullname[Len];int handicap; };void setgolf(golf& g, const char* name, int hc); int setgolf(golf& g); void handicap(golf& g, int hc); void showgolf(const golf& g); //function part #include<iostream> #include<cstring> #include"p1.h"void setgolf(golf& g, const char* name, int hc) {strcpy_s(g.fullname,name);g.handicap = hc; } int setgolf(golf& g) {using std::cout;using std::cin;cout << "Enter fullname: ";if (cin.getline(g.fullname, 40) && strlen(g.fullname))//空字符串不等于空格串,空字符串的strlen為0,空格串為1{cout << "Enter the handicap: ";cin >> g.handicap;cin.get();return 1;}else{cout << "error";return 0;} } void handicap(golf& g, int hc) {g.handicap = hc; } void showgolf(const golf& g) {using std::cout;using std::cin;using std::endl;cout << " FULLNAME: " << g.fullname << endl;cout << " HANDICAP: " << g.handicap << endl; } #include<iostream> #include"p1.h"int main() {golf golfar[3];golf golfacc;for (int i = 0; i < 3; ++i){if (!setgolf(golfar[i]))break;}setgolf(golfacc, "Ann Birdfree", 24);showgolf(golfacc);handicap(golfacc, 30);showgolf(golfacc);return 0; }

練習(xí)2

#include<iostream> #include<string> #include<cctype>void strcount(const std::string str);int main() {using namespace std;string input;cout << "Enter a line:\n";getline(cin, input);while (cin){strcount(input);cout << "Enter next line(empty line to quit):\n";getline(cin, input);}}void strcount(const std::string str) {using namespace std;static int total = 0;int count = 0;if (str == "")cout << "BLANK!\n";else{for (int cnt = 0; cnt < str.size(); ++cnt)//str.size()返回所有非‘\0’字符的長度,與題設(shè)不符{if (isalpha(str[cnt]))++count;}total += count;cout << "\"" << str << "\" contains ";cout << count << " characters\n";cout << total << " characters total\n";}}

練習(xí)3
方法一

#include<iostream>struct chaff {char dross[20];int slag; };char buffer[500];int main() {using namespace std;chaff* structsr = new(buffer)chaff[2];for (int i = 0; i < 2; ++i){cout << "Enter dross[20]: ";cin.getline(structsr[i].dross, 20);cout << "Enter slag: ";cin >> structsr[i].slag;cin.get();}for (int i = 0; i < 2; ++i){cout << "DROSS: " << structsr[i].dross << endl;cout << "SLAG: " << structsr[i].slag << endl;}return 0; }

方法二

#include<iostream>struct chaff {char dross[20];int slag; };int main() {using namespace std;chaff* ptr = new chaff[2];for (int i = 0; i < 2; ++i){cout << "Enter dross[20]: ";cin.getline(ptr[i].dross, 20);cout << "Enter slag: ";cin >> ptr[i].slag;cin.get();}for (int i = 0; i < 2; ++i){cout << "DROSS: " << ptr[i].dross << endl;cout << "SLAG: " << ptr[i].slag << endl;}delete[]ptr;return 0; }

練習(xí)4

//p4.h namespace SALES {const int QUARTERS = 4;struct Sales{double sales[QUARTERS];double average;double max;double min;};void setSales(Sales& s, const double ar[], int n);void setSales(Sales& s);void showSales(const Sales& s); } //function part #include<iostream> #include"p4.h"namespace SALES {void setSales(Sales& s, const double ar[], int n){double sum = 0;for (int i = 0; i < n; ++i){s.sales[i] = ar[i];sum += s.sales[i];}s.average = sum / n;double tempmax, tempmin;tempmax = tempmin = ar[0];for (int i = 0; i < n; ++i){if (tempmax < s.sales[i])tempmax = s.sales[i];if (tempmin > s.sales[i])tempmin = s.sales[i];}s.max = tempmax;s.min = tempmin;}void setSales(Sales& s){using std::cout;using std::cin;using std::endl;int cnt = 0;cout << "Enter sales array: ";while (cnt < QUARTERS && cin >> s.sales[cnt] )//順序不可替換,否則會多一次錄入,雖然錄入并沒有什么用++cnt;double sum = 0;for (int i = 0; i < cnt; ++i)sum += s.sales[i];s.average = sum / cnt;double tempmax, tempmin;tempmax = tempmin = s.sales[0];for (int i = 0; i < cnt; ++i){if (tempmax < s.sales[i])tempmax = s.sales[i];if (tempmin > s.sales[i])tempmin = s.sales[i];}s.max = tempmax;s.min = tempmin;}void showSales(const Sales& s){using namespace std;for (int i = 0; i < QURATES ; ++i)cout << "SALES[" << i << "]: " << s.sales[i] << endl;cout << "AVERAGE: " << s.average << endl;cout << "MAX: " << s.max << endl;cout << "MIN: " << s.min << endl;} } #include<iostream> #include"p4.h"int main() {using SALES::Sales;using SALES::setSales;using SALES::showSales;Sales test1;double arr[3] = { 1.3,6.2,2.8 };setSales(test1, arr, 3);//程序存在瑕疵,原因在于arr小于4個時(shí),show也打印出4個,但是把showSales(test1);//后續(xù)部分都處理成0也貌似有點(diǎn)不妥的樣子,所以大概就這樣吧:)Sales test2;setSales(test2);showSales(test2);return 0; }

總結(jié)

以上是生活随笔為你收集整理的2020 我的C++学习之路 C++PrimerPlus第九章课后习题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。