《C++ Primer Plus》——编程练习答案(8)
来源:互联网 发布:java迷你图书馆 编辑:程序博客网 时间:2024/06/05 00:31
第十章
10.10.1
#ifndef BAND_H_#define BAND_H_#include<string>using namespace std;class Band{public: Band(string bandName = "NULL", string account="NULL", double deposit = 0.0); ~Band(); void ShowBand(); void PushBand(double Indesposit); void PopBand(double Outdesposit);private: std::string m_bandName; std::string m_account; double m_deposit;};#endif
#include <iostream>#include "Band.h"using namespace std;Band::Band(string bandName, string account, double deposit){ m_bandName = bandName; m_account = account; m_deposit = deposit;}Band::~Band(){}void Band::ShowBand(){ cout << "BandName: " << m_bandName << endl; cout << "Bandaccount: " << m_account << endl; cout << "Desposit: " << m_deposit << endl;}void Band::PushBand(double Indesposit){ m_deposit += Indesposit;}void Band::PopBand(double Outdesposit){ m_deposit += Outdesposit;}
#include <iostream>#include "Band.h"int main(){ Band My("Arthur Yong", "18826451338", 15000.0); My.ShowBand(); My.PushBand(1500.5); My.PopBand(3578.9); My.ShowBand(); cin.get(); return 0;}
10.10.2
#ifndef PERSON_H_#define PERSON_H_#include <string>using namespace std;class Person{public: Person(){ m_lname = ""; m_fname[0] = '\0'; } Person(const string &ln, const char *fn = "Heyyou"); void Show() const; void FormalShow() const;private: static const int LIMIT = 25; string m_lname; char m_fname[LIMIT];};#endif
#include <iostream>#include "Person.h"using namespace std;Person::Person(const string &ln, const char *fn){ m_lname = ln; strcpy_s(m_fname, fn);}void Person::Show() const{ cout << m_fname << " " << m_lname << endl;}void Person::FormalShow() const{ cout << m_lname << " " << m_fname<< endl;}
#include<iostream>#include"Person.h"using namespace std;int main(){ Person one; Person two("Smywiddy"); Person three("Dimwiddy", "Sam"); two.Show(); cout << endl; three.FormalShow(); cin.get(); return 0;}
10.10.3
#ifndef GOLF_H_#define GOLF_H_class golf{public: golf(); golf(const char *fullname,int handcap); ~golf(); golf setgolf(const char *fullname,int handcap); void showgolf() const;private: static const int m_len = 40; char m_fullname[m_len]; int m_handcap;};#endif
#include <iostream>#include "golf.h"golf::golf(){ m_fullname[0] = '\0'; m_handcap = 0;}golf::golf(const char *fullname, int handcap){ strcpy_s(m_fullname, fullname); m_handcap = handcap;}golf::~golf(){}golf golf::setgolf(const char *fullname, int handcap){ golf temp = golf(fullname, handcap); //golf temp(fullname,handcap); strcpy_s(m_fullname, temp.m_fullname); m_handcap = temp.m_handcap; return *this;}void golf::showgolf() const{ using namespace std; cout << "fullname : " << m_fullname << endl; cout << "handcap: " << m_handcap << endl;}
#include <iostream>#include "golf.h"int main(){ using namespace std; golf A; golf B("Arthur Yong", 3); B.showgolf(); A.setgolf("Avalon Y", 5); A.showgolf(); cin.get(); return 0;}
10.10.4
#ifndef CUSTOMER_H_#define CUSTOMER_H_struct customer{ char fullname[35]; double payment;};#endif
#ifndef STACK_H_#define STACK_H_template <typename T>class Stack{public: Stack(int size = Stack_init_size); //构造函数 ~Stack(); Stack(const Stack<T> &s); //拷贝构造函数 const Stack<T>& operator=(const Stack<T> &s); //赋值构造函数 void ClearStack(); //清空栈 bool StackEmpty(); //若栈为空返回TRUE否则返回FALSE int StackLength(); //返回栈的长度,即入栈元素个数 bool GetTop(T &elem); //若栈不空,用elem返回栈顶元素,并返回true,否则返回ERROR void Push(T elem); //插入元素elem为新的栈顶元素 bool Pop(T &elem); //若栈不为空,则删除栈顶元素,用elem返回其值,并返回true,否则返回errorprivate: T *base; T *top; int m_size; enum StackSize { Stack_init_size = 100, //存储空间初始分配量 Stack_incerement = 10 //存储空间分配增加量 };};template <typename T>Stack<T>::Stack(int size){ base = new T[Stack_init_size]; if (base==NULL) { exit(1); } top = base; m_size = size;}template <typename T>Stack<T>::Stack(const Stack<T> &s){ base = new T[s.m_size]; if (base == NULL) { exit(1); } top = base; m_size = s.m_size; T * p1 =s.base ; T * p2 = base; while (p1!=s.top) { *p2 = *p1; p1++; p2++; } top = p2;}template <typename T>const Stack<T>& Stack<T>::operator=(const Stack<T> &s){ delete[] base; base = new T[s.m_size]; if (base == NULL) { exit(1); } top = base; m_size = s.m_size; T * p1 = s.base; T * p2 = base; while (p1 != s.top) { *p2 = *p1; p1++; p2++; } top = p2; return *this;}template <typename T>Stack<T>::~Stack(){ delete[] base;}template <typename T>int Stack<T>::StackLength(){ int n = 0; T * p = top; while (p!= base) { n++; p--; } return n;}/*template <typename T>void Stack<T>::ClearStack(){ while (top != base) { top--; *top = 0; }}*/template <typename T>bool Stack<T>::StackEmpty(){ if (top == base) { return true; } return false;}template <typename T>bool Stack<T>::GetTop(T &elem){ if (StackEmpty()) { return false; } else { top--; elem = *top;// *top = 0; return true; }}template <typename T>void Stack<T>::Push(T elem){ int size_increment; if (top - base>Stack_init_size) { m_size = m_size + Stack_incerement; T *temp = new T[m_size]; T *p = base; T *q = temp; while ((p + 1) != top) { *q = *p; q++; p++; } delete[] base; base = temp; *q = elem; q++; } else { *top = elem; top++; }}template <typename T>bool Stack<T>::Pop(T &elem){ if (top == base) { return false; } top--; elem = *top;// *top = 0; return true;}#endif
#include <iostream>#include "Stack.h"#include"customer.h"#include <cstdlib>int main(){ using namespace std; Stack<customer> T; customer temp[2]= { { "Arthur Yong",1500.5 }, {"Avalon Y",2000.5} }; T.Push(temp[0]); T.Push(temp[1]); customer m; T.Pop(m); cout << m.fullname << " " << m.payment << endl; system("pause"); return 0;}
10.10.6
#ifndef MOVE_H_#define MOVE_H_using namespace std;class Move{public: Move(double x=0.0, double y=0.0){ m_x = x; m_y = y; }; ~Move(){}; void showmove() const; Move add(const Move & s) const; void reset(double a = 0.0, double b = 0.0);private: double m_x; double m_y;};#endif
#include <iostream>#include "Move.h"void Move::showmove() const{ cout << "m_x=: " << m_x << " " << "m_y=: " << m_y << endl;}Move Move::add(const Move & s) const{ double temp_x = m_x + s.m_x; double temp_y = m_y + s.m_y; Move temp=Move(temp_x,temp_y); return temp;}void Move::reset(double a , double b){ m_x = a; m_y = b;}
#include <iostream>#include "Move.h"int main(){ Move a(2.5,3.5); Move b=Move(1.5,2.3); Move c; a.showmove(); b.showmove(); c = a.add(b); c.showmove(); c.reset(5.5, 5.5); c.showmove(); cin.get(); return 0;}
10.10.7
#ifndef PLORG_H_#define PLORG_H_class plorg{public: plorg(); plorg(const char *plorgname, int plorgCI); ~plorg(); void SetCI(int plorgCI); void Showplorg();private: static const int len = 20; char m_plorgname[len]; int m_plorgCI;};#endif
#include <iostream>#include "Plorg.h"plorg::plorg(){ strcpy_s(m_plorgname, "Plorga"); m_plorgCI = 0;}plorg::plorg(const char *plorgname, int plorgCI){ strcpy_s(m_plorgname,plorgname); m_plorgCI = plorgCI;}plorg::~plorg(){}void plorg::SetCI(int plorgCI){ m_plorgCI = plorgCI;}void plorg::Showplorg(){ using namespace std; cout << "Plorg Name: " << m_plorgname << endl; cout << "Plorg CI: " << m_plorgCI << endl;}
#include <iostream>#include"Plorg.h"int main(){ using namespace std; plorg A; A.Showplorg(); plorg B("Arthur Yong", 5); B.Showplorg(); cin.get(); return 0;}
0 0
- 《C++ Primer Plus》——编程练习答案(8)
- 《C++ Primer Plus》——编程练习答案(1)
- 《C++ Primer Plus》——编程练习答案(2)
- 《C++ Primer Plus》——编程练习答案(3)
- 《C++ Primer Plus》——编程练习答案(4)
- 《C++ Primer Plus》——编程练习答案(5)
- 《C++ Primer Plus》——编程练习答案(6)
- 《C++ Primer Plus》——编程练习答案(7)
- 《C++ Primer Plus》——编程练习答案(9)
- 《C++ Primer Plus》——编程练习答案(10)
- 《C++ Primer Plus》——编程练习答案(11)
- C++.Primer.Plus第五版第五章编程练习答案
- c++.primer.plus第五版第六章编程练习答案
- c++.primer.plus第五版第七章编程练习答案
- c++.primer.plus第五版第八章编程练习答案
- c++.primer.plus第五版第九章编程练习答案
- c++.primer.plus第五版第十章编程练习答案
- C++primer plus第六版课后编程练习答案2.2
- MFC-TreeControl
- 【VB.NET视频总结(三)】
- MIT折纸算法
- 获取TrueType字体信息
- 运维sentinel监控配置
- 《C++ Primer Plus》——编程练习答案(8)
- 2016北邮计算机考研复试上机题解
- 配置 Visual Studio Tools for Apache Cordova
- {小结}普及模拟3.26
- IO流缓冲区的本质
- MySQL 索引分析和优化
- 【Machine Learning实验2】 Logistic Regression求解classification问题
- activity主题透明显示系统桌面的问题
- npm使用指南