《C++ Primer Plus(第六版)》(17)(第十章 对象和类 编程题答案)
10.10编程题
1.
Test.h
#ifndef _Test_H_ #define _Test_H_ #include <iostream> #include <string> using namespace std; class Account { public: Account(const string& name, const string& id, double money); void show(); void add(double m); void decrease(double m); private: string _name; string _id; double _money; }; #endif
Test.cpp
#include "Test.h" #include <iostream> using namespace std; Account::Account(const string& name, const string& id, double money) { _name = name; _id = id; _money = money; } void Account::show() { cout << "User: " << _name << " id:" << _id << " meoney:" << _money << endl; } void Account::add(double m) { _money += m; } void Account::decrease(double m) { _money -= m; }
main.cpp
#include <iostream> #include "Test.h" #include <new> using namespace std; int main(int argc, const char * argv[]) { Account a = Account("FableGame", "100001", 900); a.show(); a.add(123456); a.show(); a.decrease(1234); a.show(); return 0; }
2.
Test.h
#ifndef _Test_H_ #define _Test_H_ #include <iostream> #include <string> using namespace std; class Person { public: Person(); Person(const string& ln, const char* fn = "Heyyou"); void Show()const; void FormalShow()const; private: static const int LIMIT = 25; string lname; char fname[LIMIT]; }; #endifTest.cpp
#include "Test.h" #include <iostream> using namespace std; Person::Person() { lname = ""; fname[0] = '\0'; } Person::Person(const string& ln, const char* fn /*= "Heyyou"*/) { lname = ln; strcpy_s(fname, fn); } void Person::Show() const { cout << fname << " " << lname << endl; } void Person::FormalShow()const { cout << lname << " " << fname << endl; }main.cpp
#include <iostream> #include "Test.h" #include <new> using namespace std; int main(int argc, const char * argv[]) { Person one; Person two("Smythecraft"); Person three("Dimwiddy", "Sam"); one.Show(); one.FormalShow(); two.Show(); two.FormalShow(); three.Show(); three.FormalShow(); return 0; }
Test.h
#ifndef _Test_H_ #define _Test_H_ const int Len = 40; class golf { private: char _fullname[Len]; int _handicap; public: golf(); golf(const char* name, int hc); int setgolf( ); void handicap( int hc); void showgolf( ); }; #endifTest.cpp
#include "Test.h" #include <iostream> using namespace std; int golf::setgolf( ) { cout << "Please enter fullname: "; cin.get(); cin.getline(_fullname, 40); if (strcmp(_fullname, "") == 0) { return 0; } cout << "Please enter handcap: "; cin >> _handicap; return 1; } void golf::handicap( int hc) { _handicap = hc; } void golf::showgolf( ) { cout << _fullname << ": " << _handicap << endl; } golf::golf() { _fullname[0] = '\0'; _handicap = 0; } golf::golf(const char* name, int hc) { strcpy_s(_fullname, name); _handicap = hc; }main.cpp
#include <iostream> #include "Test.h" using namespace std; int main(int argc, const char * argv[]) { golf g[5]; char str[40] = ""; g[0].setgolf(); int num = 1; for (int i = 1; i < 5; i++) { if (g[i].setgolf() == 0) { break; } num++; } cout << "Show Golf" << endl; for (int i = 0; i < num; i++) { g[i].showgolf(); } return 0; }
Test.h
#ifndef _Test_H_ #define _Test_H_ namespace SALES { const int QUARTERS = 4; class Sales { private: double sales[QUARTERS]; double average; double max; double min; public: Sales( const double ar[], int n); Sales( ); void setSales(); void showSales( ); }; } #endifTest.cpp
#include "Test.h" #include <iostream> using namespace std; SALES::Sales::Sales(const double ar[], int n) { double total = 0; for (int i = 0; i < QUARTERS; i++) { if (i >= n) { sales[i] = 0; } else { sales[i] = ar[i]; } if (i == 0) { max = sales[i]; min = sales[i]; } else { if (sales[i] > max) { max = sales[i]; } if (sales[i] < min) { min = sales[i]; } } total += sales[i]; } average = total / QUARTERS; } SALES::Sales::Sales() { min = max = average = 0; } void SALES::Sales::showSales() { cout << "Sales:"; for (int i = 0; i < QUARTERS; i++) { cout << sales[i]; cout << ", "; } cout << "\nMin:" << min << " Max:" << max << " average:" << average << endl; } void SALES::Sales::setSales() { double total = 0; for (int i = 0; i < QUARTERS; i++) { cout << "Enter the sales:"; cin >> sales[i]; if (i == 0) { max = sales[i]; min = sales[i]; } else { if (sales[i] > max) { max = sales[i]; } if (sales[i] < min) { min = sales[i]; } } total += sales[i]; } average = total / QUARTERS; }main.cpp
#include <iostream> #include "Test.h" #include <new> using namespace std; int main(int argc, const char * argv[]) { double d[4] = { 123.3, 323, 342.333, 8933 }; SALES::Sales s1(d, 4), s2; s2.setSales(); s1.showSales(); s2.showSales(); return 0; }
5.
Test.h
#ifndef _Test_H_ #define _Test_H_ struct customer { char fullname[35]; double payment; }; typedef customer Item; class Stack { private: enum { MAX = 10 }; Item items[MAX]; int top; public: Stack(); bool isempty() const; bool isfull() const; bool push(const Item& item); bool pop(Item& item); }; #endifTest.cpp
#include "Test.h" #include <iostream> using namespace std; Stack::Stack() { top = 0; } bool Stack::isempty() const { return top == 0; } bool Stack::isfull() const { return top == MAX; } bool Stack::push(const Item& item) { if (top < MAX) { items[top] = item; top++; return true; } else { return false; } } bool Stack::pop(Item& item) { if (top > 0) { --top; item = items[top]; return true; } else { return false; } }main.cpp
#include <iostream> #include "Test.h" #include <new> using namespace std; int main(int argc, const char * argv[]) { Stack st; char ch; customer po; double total = 0; cout << "Please enter A to add a purchase order,\n" << "P to process a PO, or Q to quit.\n"; while (cin >> ch && toupper(ch) != 'Q') { while (cin.get() != '\n') { continue; } if (!isalpha(ch)) { cout << '\a'; continue; } switch (ch) { case 'A': case 'a': { if (st.isfull()) { cout << "Stack already full\n"; } else { cout << "Enter Name: "; cin.getline(po.fullname, 35); cout << "Enter payment: "; cin >> po.payment; st.push(po); } } break; case 'P': case 'p': { if (st.isempty()) { cout << "stack already empty\n"; } else { st.pop(po); total += po.payment; cout << "PO #" << po.fullname <<" payment:"<< po.payment << " total:" << total << " popped\n"; } break; } break; } cout << "Please enter A to add a purchase order,\n" << "P to process a PO, or Q to quit.\n"; } cout << "Bye\n"; return 0; }
6.
Test.h
#ifndef _Test_H_ #define _Test_H_ class Move { private: double x; double y; public: Move(double a = 0, double b = 0); void showmove()const; Move add(const Move& m) const; void reset(double a = 0, double b = 0); }; #endifTest.cpp
#include "Test.h" #include <iostream> using namespace std; Move::Move(double a /*= 0*/, double b /*= 0*/) { x = a; y = b; } void Move::showmove() const { cout << "x: " << x << " y:" << y << endl; } Move Move::add(const Move& m) const { Move _m; _m.x = x + m.x; _m.y = y + m.y; return _m; } void Move::reset(double a /*= 0*/, double b /*= 0*/) { x = a; y = b; }main.cpp
#include <iostream> #include "Test.h" #include <new> using namespace std; int main(int argc, const char * argv[]) { Move m1(313, 872); Move m2(823, 245); m1.showmove(); m2.showmove(); Move m3 = m1.add(m2); m3.showmove(); m3.reset(1234, 5678); m3.showmove(); return 0; }
7.
Test.h
#ifndef _Test_H_ #define _Test_H_ class Plorg { private: char _name[20]; int _ci; public: Plorg(const char* n = "Plorga", int ci = 50); void show()const; void setCI(int ci); }; #endifTest.cpp
#include "Test.h" #include <iostream> using namespace std; Plorg::Plorg(const char* n /*= "Plorga"*/, int ci /*= 50*/) { strcpy_s(_name, n); _ci = ci; } void Plorg::show() const { cout << "name: " << _name << " ci: " << _ci<< endl; } void Plorg::setCI(int ci) { _ci = ci; }main.cpp
#include <iostream> #include "Test.h" #include <new> using namespace std; int main(int argc, const char * argv[]) { Plorg p1; Plorg p2("FableGame"); Plorg p3("Fable", 1233); p1.setCI(999); p1.show(); p2.show(); p3.show(); return 0; }
8.
Test.h
#ifndef _Test_H_ #define _Test_H_ struct customer { char fullname[35]; double payment; }; void show(customer& cu); typedef customer Item; class List { private: enum { MAX = 10 }; Item items[MAX]; int top; public: List(); bool isempty() const; bool isfull() const; bool add(const Item& item); void visit(void(*pf) (Item &)); }; #endifTest.cpp
#include "Test.h" #include <iostream> using namespace std; List::List() { top = 0; } bool List::isempty() const { return top == 0; } bool List::isfull() const { return top == MAX; } bool List::add(const Item& item) { if (top < MAX) { items[top] = item; top++; return true; } else { return false; } } void List::visit(void(*pf) (Item &)) { for (int i = 0; i < top; i++) { pf(items[i]); } } void show(customer& cu) { cout << "fullname:" << cu.fullname << " payment:" << cu.payment<< endl; }main.cpp
#include <iostream> #include "Test.h" #include <new> using namespace std; int main(int argc, const char * argv[]) { List li; customer po; while (cin ) { if (li.isfull()) { cout << "List already full\n"; } else { cout << "Enter Name: "; cin.getline(po.fullname, 35); cout << "Enter payment: "; cin >> po.payment; cin.get(); li.add(po); } } li.visit(show); cout << "Bye\n"; return 0; }
人生如戏,还是戏如人生?微信公众号:传说之路
csdn博客 http://blog.csdn.net/u012175089/article/list/2