C ++ Primer Plus Chapter 1 question solution, primer Chapter 2
Chapter C ++ Primer Plus
# Include
Void PrintStr (char * str, int flag = 0); int main () {char str [] = "fuck"; std :: cout <"#1" <std: endl; PrintStr (str); std: cout <"#2" <std: endl; PrintStr (str, 1996); std: cout <"#3" <std: endl; PrintStr (str, 1996); std :: cout <"#4" <std: endl; PrintStr (str); return 0;} void PrintStr (char * str, int flag) {static int count = 0; // The non-connectivity static variable is initialized only once and always exists + + count; if (! Flag) std: cout <str <std: endl; elsefor (int I = 0; I <count; ++ I) std :: cout <str <std: endl ;}
8-2
#include
#include
struct CandyBar{char name[30];double weight;int heat;};void setCandyBar(CandyBar &c, const char *s = "Millennium Munch", const double w = 2.85, const int h = 350);void disp(const CandyBar &c);int main(){CandyBar c;setCandyBar(c);disp(c);char name[30];double weight;int heat;std::cin.getline(name, 30);std::cin >> weight >> heat;setCandyBar(c, name, weight, heat);disp(c);return 0;}void setCandyBar(CandyBar &c, const char *s, const double w, const int h){strcpy(c.name, s);c.weight = w;c.heat = h;}void disp(const CandyBar &c){std::cout << c.name << std::endl << c.weight << std::endl << c.heat << std::endl;}
8-3
#include
#include
void changeUpper(std::string &s); int main() { std::string s; while(getline(std::cin, s) && s[0] != 'q') { changeUpper(s); std::cout << s << std::endl; } return 0; } void changeUpper(std::string &s) { for(int i = 0; i < s.size(); ++i) s[i] = toupper(s[i]); }
8-4
# Include
# Include
Struct stringy {char * str; int ct ;}; void set (stringy & s, const char * str); void show (const char * str, const int count = 1 ); void show (const stringy & s, const int count = 1); int main () {stringy beany; char testing [] = "Reality isn' t what it used to be. "; set (beany, testing); show (beany, 2); testing [0] = 'D'; testing [1] = 'U '; show (testing); show (testing, 3); show ("Done! "); Return 0;} void set (stringy & s, const char * str) {int len = strlen (str); s. ct = len; s. str = new char (len + 1); // You Need To Leave a character to store '\ 0'. Therefore, len + 1 space is allocated to strcpy (s. str, str);} void show (const char * str, const int count) {for (int I = 0; I <count; ++ I) std :: cout <str <std: endl;} void show (const stringy & s, const int count) {for (int I = 0; I <count; ++ I) std: cout <s. str <std: endl ;}
8-5
#include
template
T max5(T *a); int main() { int a[5] = {2, 3, 1, 5, 4}; double b[5] = {1.1, 3.3, 5.5, 4.4, 2.2}; std::cout << max5(a) << std::endl; std::cout << max5(b) << std::endl; return 0; } template
T max5(T *a) { T max = -1; for(int i = 0 ;i < 5 ; ++i) max = max < a[i] ? a[i] : max; return max; }
8-6
#include
#include
template
T maxn(T *a, int len); template <> char *maxn(char *a[], int len); int main() { int a[6] = {1, 2, 3, 4, 5, 6}; double b[4] = {1.1, 2.2, 3.3, 4.4}; char *c[5] = {"fuck", "fuckfuck", "fuckfuckfuck", "fuckfuckfuckfuck", "fuckfuckfuckfuckfuck"}; std::cout << maxn(a, 6) << std::endl; std::cout << maxn(b, 4) << std::endl; std::cout << maxn(c, 5) << std::endl; return 0; } template
T maxn(T *a, int len) { T max = -1; for(int i = 0 ; i < len ; ++i) max = max < a[i] ? a[i] : max; return max; } template <> char *maxn(char *a[], int len) { int max = -1; char *p = NULL; for(int i = 0 ; i < len ; ++i) { if((int)strlen(a[i])> max) { max = strlen(a[i]); p = a[i]; } } return p; }
8-7
#include
struct debts { char name[50]; double amount; }; template
T SumArray(T a[], int n); template
T SumArray(T *a[], int n); int main() { int things[6] = {13, 31, 103, 301, 310, 130}; debts mr_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] = &mr_E[i].amount; std::cout << SumArray(things, 6) << std::endl; std::cout << SumArray(pd, 3) << std::endl; return 0; } template
T SumArray(T a[], int n) { T sum = 0 ; for(int i = 0 ; i < n ; ++i) sum += a[i]; return sum; } template
T SumArray(T *a[], int n) { T sum = 0; for(int i = 0 ; i < n ; ++i) sum += *a[i]; return sum; }