Chapter 10 programming exercise answer 10.1 provides definitions for category 5 of the exercise and demonstrates
// 10.1 provide definitions for category 5 of the exercise review and demonstrate # include
Using namespace std; class BankAccount {stringm_name; stringm_num; unsignedm_balance; public: BankAccount (string name, string num, unsigned balance) {m_name = name; m_num = num; m_balance = balance ;} void show () const {cout <m_name <endl; cout <m_num <endl; cout <m_balance <endl;} void incDeposit (unsigned amount) {m_balance + = amount;} void decDeposit (unsigned amount) {if (m_balance <= amount) m_balance = 0; elsem_balance-= amount ;}; int main () {BankAccount bank ("Tom", "1", 1024); bank. show (); bank. incDeposit (128); bank. show (); bank. decDeposit (2048); bank. show ();}
10.2 define the class and method based on the following class definitions and Examples
// 10.2 define the class method according to the following class definitions and examples # include
# Include
Using namespace std; class Person {static const unsigned LIMIT = 25; string lname; char fname [LIMIT]; public: Person () {lname = ""; fname [0] = '\ 0';} // #1 Person (const string & ln, const char * fn = "Heyyou "); // the following methods display lname and fnamevoid Show () const; // firstname lastname formatvoid FormalShow () const; // lastname, firstname format}; Person :: person (const string & ln, const char * fn) {lname = ln; strcpy (fname, fn);} void Person: Show () const {cout <fname <''<lname <endl;} void Person: FormalShow () const {cout <lname <", "<fname <endl;} int main () {Person one; Person two (" Smythecraft "); Person three (" Dimwiddy "," Sam "); three. show (); three. formalShow ();}
10.3 compile the golf class, including the name and Score data, and call
// 10.3 write the golf class, including the name and Score data, and call # include
# Include
Using namespace std; const int Len = 40; class golf {string fullname; int handicap; public: golf (): fullname ("no"), handicap (0) {} golf (const char * name, int hc) {fullname = name; handicap = hc;} void showgolf () {cout <fullname <'\ t'
10.4 perform programming exercises 9.4 using the Sales class
// 10.4 according to programming practice 9.4, use the Sales class to complete # include
Using namespace std; class Sales {static const int QUARTERS = 4; double sales [QUARTERS]; double size; double average; double max; double min; public: sales (double arr [], int n) {unsignedtimes = n <QUARTERS? (Unsigned) n: QUARTERS; for (unsigned I = 0; I <times; ++ I) sales [I] = arr [I]; for (unsigned I = times; I <QUARTERS; ++ I) sales [I] = 0; size = times; average = calcAverage (); max = calcMax (); min = calcMin ();} void setSales () {cout <"input" <QUARTERS <"sales records:"; size = QUARTERS; for (unsigned I = 0; I <QUARTERS; ++ I) {cin> sales [I]; if (! Cin) {size = I; break ;}for (unsigned I = (unsigned) size; I <QUARTERS; ++ I) sales [I] = 0; cin. clear (); average = calcAverage (); max = calcMax (); min = calcMin ();} double calcAverage () const {doublesum = 0; for (unsigned I = 0; I <size; ++ I) sum + = sales [I]; return (sum/size);} double calcMax () const {unsignedidxMax = 0; for (unsigned I = 0; I <size; ++ I) if (sales [I]> sales [idxMax]) idxMax = I; return (sales [idxMax]);} double calcMin () const {unsignedidxMin = 0; for (unsigned I = 0; I <size; ++ I) if (sales [I] <sales [idxMin]) idxMin = I; return (sales [idxMin]);} void showSales () const {cout <"sales:"; for (const auto & e: sales) cout <e <''; cout <endl; cout <" average: "<average <endl; cout <" max: "<max <endl; cout <" min: "<min <endl ;}}; int main () {doublesalesLst [] = {12.2, 11.16, 10.61, 16.24, 11.53}; Sales salesBook (salesLst, sizeof (salesLst)/sizeof (salesLst [0]); salesBook. showSales ();}
10.5 compile a program to add and delete the customer structure (including the full name and quantity of two members) from the stack. Each time a structure is deleted, the number is counted into the total number.
// 10.5 write a program to add and delete the customer structure from the stack (including the full name and quantity of two members). Each time a structure is deleted, the number is counted into the total number. # Include
Using namespace std; struct customer {char fullname [35]; double payment ;}; typedef customer Item; class Stack {Item items [10]; int top; public: Stack (): top (0) {} bool isEmpty () const {return top = 0;} bool isFull () const {return top = 10;} bool push (const Item & item) {if (isFull () {cout <"Error! Stack is full! "<
// 10.6 write the test # include according to the following classes
# Include
Using namespace std; class Move {doublem_x; doublem_y; public: Move (double a, double B) {m_x = a; m_y = B;} void showMove () const {cout <'(' <m_x <"," <m_y <')';} Move add (const Move & m) const {return (Move (m. m_x + m_x, m. m_y + m_y);} void reset (double a, double B) {m_x = a; m_y = B ;}}; int main () {Move one (12, 4); one. showMove (); cout <endl; Move two (1, 1); one. add (two ). showMove (); cout <endl; one. reset (3, 4); one. showMove (); cout <endl ;}
10.7 compile features like Plorg and function implementation plorg:
Data: The name must not exceed 19 characters; A Satisfaction Index Integer CI
Operation: The New plorg has a name with a CI value of 50. CI can be modified. plorg can report the name and CI. The default name is "Plorga"
// 10.7 write features like Plorg and function implementation plorg: // data: the name cannot exceed 19 characters; satisfied Index Integer CI // operation: the new plorg has a name, CI value 50; CI can be modified; plorg can report name and CI; default name is "Plorga" # include
# Include
Using namespace std; class Plorg {char_name [20]; unsignedCI; public: Plorg (): CI (50) {strcpy (_ name, "Plorga ");} plorg (const char * name) {CI = 50; strcpy (_ name, name); _ name [19] = '\ 0';} void setCI (unsigned ci) {CI = ci;} void showPlorg () const {cout <_ name <''<CI <endl ;}}; int main () {Plorgplorg; plorg. showPlorg (); plorg. setCI (12); plorg. showPlorg ();}
10.8 simple design list class: it can store multiple types; can create an empty list; can add data items to the list; can confirm that the list is full; can access each item and operate
# Include
# Include
Using namespace std; class TList {public: typedefintItem; public: TList (const Item arr [] = NULL, unsigned n = 0); bool isFull (void) const; bool isEmpty (void) const; bool append (const Item & item); void visit (void (* pf) (Item & item); private: static const unsignedmk_capacity = 4; private: Itemm_content [mk_capacity]; unsignedm_size ;}; TList: TList (const Item arr [], unsigned n) {if (NULL = arr) {m_size = 0; re Turn;} m_size = mk_capacity <n? Mk_capacity: n; for (unsigned I = 0; I <m_size; ++ I) {m_content [I] = arr [I] ;}} boolTList: isFull (void) const {return (mk_capacity = m_size);} boolTList: isEmpty (void) const {return (0 = m_size);} bool TList: append (const Item & item) {if (isFull () {return (false);} m_content [m_size ++] = item; return (true);} voidTList: visit (void (* pf) (Item & item) {for (unsigned I = 0; I <m_size; ++ I) {pf (m_content [I]) ;}} static voidshow (TList :: item & item) {cout <item <'';} intmain (void) {TListone; one. visit (show); cout <endl; cout <"empty:" <boolalpha <one. isEmpty (); cout <endl <"=" <endl; TList: Itemarr [] = {1, 2, 3 }; TListtwo (arr, sizeof (arr)/sizeof (arr [0]); two. visit (show); cout <endl; cout <"Full?:" <two. isFull (); cout <endl; cout <"empty:" <two. isEmpty (); cout <endl; cout <"append an item:" <two. append (16); cout <endl; two. visit (show); cout <endl ;}