2020 我的C++学习之路 C++PrimerPlus第八章课后习题
生活随笔
收集整理的這篇文章主要介紹了
2020 我的C++学习之路 C++PrimerPlus第八章课后习题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以C++ Primer Plus為參考書籍,自身歸納知識點,加深記憶。僅供參考,DEV C++已通過編譯運行
。
練習1:題目我是看了好久才反應過來,不得不說翻譯的不太行
練習2
#include<iostream> #include<cstring> using namespace std; const int maxsize = 40;struct CandyBar {char name[maxsize];double weight;int calories; };void input(CandyBar& candy, char* fb, double Weight=2.85, int Calories=350); //string轉為char *比較麻煩,知曉此處的默認參數設置即可,下方采取char[]直轉char*功能更為方便 void show(const CandyBar& candy);int main() {double wgt;int heat;CandyBar candybara,candybarb;char brand1[maxsize] = "MonkeyHouse";input(candybara, brand1);show(candybara);cout << "Enter a bar brand: ";cin.getline(brand1, maxsize);cout << "Enter its weight: ";cin >> wgt;cout << "Enter its calories: ";cin >> heat;input(candybarb, brand1, wgt, heat);show(candybarb);return 0; }void input(CandyBar& candy, char* fb, double Weight, int Calories) {strcpy_s(candy.name, fb);//vs19報安全警告,strcpy改為strcpy_s,后面參數沒有變化candy.weight = Weight;candy.calories = Calories; }void show(CandyBar& candy) {cout << " BRAND: " << candy.name << endl;cout << "WEIGHT: " << candy.weight << endl;cout << "CALORIES: " << candy.calories << endl; }練習3
#include<iostream> #include<string> #include<cctype>using namespace std; void UP(string&);int main() {string str;cout << "Enter a string (q to quit): ";while ((getline(cin, str)) && str != "q"){UP(str);cout << str << endl;cout << "Enter a string (q to quit): ";}cout << "Done!" << endl;return 0; } void UP(string& stra) {int size = stra.size();for (int i = 0; i < size; ++i){if (isalpha(stra[i]))stra[i] = toupper(stra[i]);} }練習4
#include<iostream> using namespace std; #include<cstring>struct stringy {char* str;int ct; }; void show(const stringy&, int n = 1); void show(const char* str, int n = 1); void set(stringy&, char* str);int main() {stringy beany;char testing[] = "Reality isn't what it used to be.";set(beany, testing);show(beany);show(beany, 2);testing[0] = 'D';testing[1] = 'u';show(testing);show(testing, 3);delete[]beany.str;show("Done!");return 0; } void set(stringy&Te, char* str) {Te.ct = strlen(str);Te.str = new char(Te.ct + 1);strcpy(Te.str, str); } void show(const stringy& Te, int n) {for (int cnt = 0; cnt < n; ++cnt)cout << Te.str << endl; } void show(const char* str, int n) {for (int cnt = 0; cnt < n; ++cnt)cout << str << endl; }練習5
#include<iostream> using namespace std;template <class T> T max5(T* ar);int main() {int intarray[5] = { 3,6,1,4,7 };double douarray[5] = { 2.3,5.7,1.3,5.4,2.1 };cout << max5(intarray) << " is the maximum in intarray" << endl;cout << max5(douarray) << " is the maximum in douarray" << endl;return 0; }template <class T> T max5(T* ar) {T temp;for (int i = 0; i < 5; ++i){if(i ==0)temp = ar[0];if (temp < ar[i])temp = ar[i];}return temp; }練習6
#include<iostream> #include<cstring> using namespace std;template<class T> T maximum(T ar[], int n);template<>char* maximum(char* str[], int n);int main() {int artest1[6] = { 23,3,5,32,40,16 };double artest2[4] = { 3.6,1.8,6.4,2.3 };char* ch[4] ={(char*)"I have a dream.",//string無法直接轉到char*,人工強行轉換即可(char*)"We are the champion.",(char*)"I hate you.",(char*)"I have a dream."};char* max = maximum(ch, 4);cout << maximum(artest1, 6) << " is the maximum.\n";cout << maximum(artest2, 4) << " is the maximum.\n";cout << (void*)max << " is the maximum address.\n";//coutmax會打印字符串,因此在前面用(void*)即可強制轉換為地址return 0; }template<class T> T maximum(T ar[], int n) {int cnt = 0;T temp = ar[cnt];for (; cnt < n ; ++cnt){if (temp < ar[cnt])temp = ar[cnt];}return temp; }template<>char* maximum(char* str[], int n) {char* temp=NULL;int length;int cnt = 0;length = strlen(str[0]);for (; cnt < n; ++cnt){if (length < int(strlen(str[cnt])))//如果不用int強制轉換,會有一個符號不匹配警告,strlen是size_t類型temp = str[cnt];}return temp; }練習7
#include<iostream>template<class T> T SumArray(T* ar, int n);template<typename T> double SumArray(T* ar[], int n);struct debts {char name[50];double amount; };int main() {using namespace std;int things[6] = { 13,31,103,301,310,130 };debts me_E[3] ={{"Ima Wolfe",2400.0},{"Ura Foxe",1300.0},{"Iby Stout",1800.0}};double* pd[3];for (int i = 0; i < 3; ++i)pd[i] = &me_E[i].amount;cout << "Listing sum of things: " << SumArray(things, 6) << endl;cout << "Listing sum of debts: " << SumArray(pd, 3) << endl;return 0; } template<class T> T SumArray(T* ar, int n) {std::cout << "Templat A: \n";T temp = 0;for (int i = 0; i < n; ++i)temp += ar[i];return temp; } template<typename T> double SumArray(T* ar[], int n) {std::cout << "Template B:\n";double temp = 0;for (int i = 0; i < n; ++i)temp += *ar[i];//ar[i]是me_E.amount中每一項的地址,因此在+=時需要添加解引用*return temp; }總結
以上是生活随笔為你收集整理的2020 我的C++学习之路 C++PrimerPlus第八章课后习题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020 我的C++学习之路 C++Pr
- 下一篇: 2020 我的C++的学习之路 第八章函