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];
};
#endif

Test.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;
}

3.
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( );};#endif  

Test.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;
}

4.

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( );};}#endif  

Test.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);
};#endif  

Test.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);
};
#endif  

Test.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);
};
#endif  

Test.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 &));
};#endif  

Test.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;
}

转载于:https://www.cnblogs.com/fablegame/p/6430247.html

《C++ Primer Plus(第六版)》(17)(第十章 对象和类 编程题答案)相关推荐

  1. 深夜里学妹竟然问我会不会C?我直接把这篇文章甩她脸上(C Primer Plus 第六版基础整合)

    C Primer Plus 第六版 前言 第一章 初识C语言 一.C语言的起源 二.C语言的应用 三.C语言的特点 四.编译的过程 五.编码机制 1.简述 2.完成机制 六.在UNIX系统上使用C 七 ...

  2. C Primer Plus第六版(中文版)编程练习答案(完美修订版)汇总

    //本文是博主编写的C Primer Plus第六版(中文版)编程练习答案的所有链接; //使用超链接汇总于此,若是有用请点赞收藏并分享给他人; C Primer Plus 第六版(中文版)第二章(完 ...

  3. C primer plus(第六版)第十一章源代码

    C primer plus(第六版)第十一章源代码 /* 11.1 */ #include<stdio.h> #define MSG "I am a symbolic strin ...

  4. C++ Primer Plus第六版第六章编程练习 第4题, 加入Benevolent Order of Programmer后,在BOP大会上

    /*************************************************************************************************** ...

  5. C++ Primer Plus 第六版 所有章节课后编程练习答案

    我的独立博客地址:www.blog4jimmy.com,欢迎大家关注 下面的是C++ Primer Plus 第六版所有章节的课后编程练习的答案,都是博主自己写的,有不对的地方请大家留言指出讨论讨论 ...

  6. C++ Primer Plus(第六版)第十章课后习题

    C++ Primer Plus(第六版)第十章课后习题 10.10.1 10.10.1.h #ifndef d #define d #include class bank { private: std ...

  7. C++ Primer Plus(第6版)Chapter 4 编程题答案

    C++ Primer Plus(第6版)Chapter 4 编程题答案 第1题: // task 1 #include <iostream> #include <string> ...

  8. C Primer Plus 第7章之菜鸟儿的编程题答案

    本文仅为记录自己手打编程题答案所用,不能保证正确性,欢迎大家学习交流,有问题请随时指出~ 1.原版: #include<stdio.h>int main(void) {int i = 0; ...

  9. **PTA:浙大版《C语言程序设计(第3版)》编程题答案*

    PTA:浙大版<C语言程序设计(第3版)>编程题答案 练习2-3 输出倒三角图案 练习2-4 温度转换 练习2-6 计算物体自由下落的距离 练习2-8 计算摄氏温度 练习2-9 整数四则运 ...

最新文章

  1. linux 怎么删除大文件,如何在Linux中删除超大的(100-200GB)文件
  2. Apache Digester示例–轻松配置
  3. Java——集合(输入5个学生的信息按总分高低排序)
  4. LeetCode算法入门- Multiply Strings -day18
  5. 从省市区多重级联想到的,react和jquery的差别
  6. EasyRecovery14免费并且超好用的数据恢复工具
  7. java font.getfont_FontManager.getFont(方正黑体);这个方法返回值为null
  8. C++实现一个线程池
  9. Oracle里default什么意思,ORACLE中默认值default的使用方法
  10. oracle imp加快速度,加快IMP速度
  11. 20210514:廉价机械键盘学习
  12. 2016年7月17日学习 scratch 小兔子偷胡萝卜的故事
  13. 【Java】interrupt、interrupted和isInterrupted的区别
  14. 2.OrientDB连接操作
  15. Python+Django+Mysql实现在线电影推荐系统 基于用户、项目的协同过滤推荐在线电影系统 代码实现 源代码下载
  16. elementUI Form中 勾选框设置必选不提示问题
  17. 第一周-3.2道路升级
  18. java 中文域名转码_转换java对象
  19. 【AIOT】3.5 物联网传输协议介绍
  20. httpclient4 请一定设置超时时间

热门文章

  1. Android 获取SD卡路径和判断SD卡是否存在.
  2. 二分图的最大带权匹配
  3. linux命令strip
  4. 关于STM32驱动DS1302实时时钟的一点思考
  5. 常考数据结构与算法-manacher算法
  6. java: \uxxxx unicode编码
  7. jvm五:编译期不能确定常量的值
  8. C十六: 两个指针相减
  9. MySql入门知识(一)
  10. Java 中 StringBuilder 在高性能用法总结