C++ Primer Plus: 第10章(2)

第10章编程题:
(1)
Account.h:

#ifndef ACCOUNT_H_
#define ACCOUNT_H_#include <string>class Account
{
private:std::string name ;std::string code ;double money ;
public:Account() ;Account(std::string Name, std::string Code, double Money) ;~Account() ;void ShowAccout() ;void InputMoney(double Money) ;void OutputMoney(double Money) ;
} ;#endif

Account.cpp:

#include "Account.h"
#include <iostream>
#include <string>
using namespace std ;Account::Account()
{}Account::Account(string Name, string Code, double Money)
{name = Name ;code = Code ;money = Money ;
}Account::~Account()
{}void Account::ShowAccout()
{using std::ios_base ;ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield) ;std::streamsize prec = cout.precision(0) ;cout << "This customer, who is called " << name << ", and has $" << money << " in " << code << " Account.\n";cout.setf(orig, ios_base::floatfield) ;cout.precision(prec) ;
}void Account::InputMoney(double Money)
{money += Money ;
}void Account::OutputMoney(double Money)
{money -= Money ;
}

main.cpp:

#include "Account.h"
#include <iostream>const int Num = 8 ;int main()
{Account A[Num] = {Account("Jackie Chen", "098425345", 56943),Account("John Rebort", "*&%$#4586", 12366), Account("Bluse Amy", "3425834", 9032455),Account("Killy Joe", "@#%^&*890", 234568690),Account("Beyonce Jim", "*&^%$#678", 999912366),Account("Alan London", "5555555", 222222222222),Account("Jackie Chen", "098425345", 56943),Account("Floyd Dork", "4324049", 247)} ;std::cout << "Let's see everyone: \n" ;for (int i=0; i<Num; i++){A[i].ShowAccout() ;}std::cout << "\nNow everyone increases $100: \n" ;for (int i=0; i<Num; i++){A[i].InputMoney(100) ;}for (int i=0; i<Num; i++){A[i].ShowAccout() ;}std::cout << "\nNow everyone decreases $100: \n" ;for (int i=0; i<Num; i++){A[i].OutputMoney(100) ;}for (int i=0; i<Num; i++){A[i].ShowAccout() ;}
}

(2)
Person.h:

#ifndef PERSON_H_
#define PERSON_H_#include <string>
using namespace std ;class Person
{
private:static const int LIMIT = 25 ;string lname ;char fname[LIMIT] ;
public:Person() {lname = "" ; fname[0] = '\0' ;} ;Person(const string & ln, const char * fn = "Heyyou") ;void Show() const ;void FormalShow() const ; 
} ;#endif

Person.cpp:

#include "Person.h"
#include <iostream>
#include <cstring>
using namespace std ;Person::Person(const string & ln, const char * fn)
{lname = ln ;strcpy(fname, fn) ;
}void Person::Show() const
{cout << fname << " " << lname ;
}void Person::FormalShow() const 
{cout << lname << ", " << fname ;
}

main.cpp:

#include "Person.h"
#include <iostream>int main()
{Person one ;Person two("Smythecraft") ;Person three("Dimwiddy", "Sam") ;std::cout << "The name of one:\n" ;one.Show() ;std::cout << std::endl ;one.FormalShow() ;std::cout << "\nThe name of two:\n" ;two.Show() ;std::cout << std::endl ;two.FormalShow() ;std::cout << "\nThe name of three:\n" ;three.Show() ;std::cout << std::endl ;three.FormalShow() ;std::cout << std::endl ;
}

(3)
golf.h:

#ifndef GOLF_H_
#define GOLF_H_class golf
{
private:static const int Len = 40 ;char fullname[Len] ;int handicap ;
public:golf(const char * name = "", int hc = 0) ;~golf() ;int setgolf() ;void Handicap() ;void showgolf() const ;
} ;#endif

golf.cpp:

#include "golf.h"
#include <cstring>
#include <iostream>golf::golf(const char * name, int hc)
{strncpy(fullname, name, Len) ;handicap = hc ;
}golf::~golf()
{std::cout << "delete!\n" ;
}int golf::setgolf()
{using namespace std ;char name[Len] = {0} ;int hc ;cout << "Fullname: " ;if (cin.get(name, Len).get() == -1){cin.clear() ;        // 务必对输入队列进行重置cout << "Enter Fail!\n" ;return 0 ;}cout << "Handicap: " ;while(!(cin >> hc)){cin.clear() ;while(cin.get() != '\n') ;cout << "Please enter a number: " ;}cin.get() ;golf G(name, hc) ;*this = G ;return 1 ;
}void golf::Handicap()
{int hc ;std::cin >> hc ;(*this).handicap = hc ;
}void golf::showgolf() const
{std::cout << "The " << handicap << " player is "<< fullname << std::endl ;
}

main.cpp:

#include "golf.h"
#include <iostream>const int Num = 5 ;int main()
{using namespace std ;golf G[Num] ;int num = 0 ;cout << "Please input the information of all of players:\n" ;for (int i=0; i<Num; i++){cout << "#" << i+1 << ": \n" ;if(!G[i].setgolf()) break ;else num ++ ;}cout << "\nNow change everyone's handicap:\n" ;for (int i=0; i<num; i++){cout << "#" << i+1 << ": " ;G[i].Handicap() ; }cout << "\nAnd then display all players:\n" ;for (int i=0; i<num; i++){G[i].showgolf() ;}
}

(4)
Sales.h:

#ifndef SALES_H_
#define SALES_H_namespace SALES
{class Sales{private:static const int QUARTERS = 4 ;double sales[QUARTERS] ;double average ;double max ;double min ;public:Sales() ;Sales(const double ar[], int n) ;~Sales() ;void setSales() ;void ShowSales() const ;} ;
} #endif

Sales.cpp:

#include "Sales.h"
#include <iostream>
using namespace SALES ;Sales::Sales()
{min = max = average = 0 ;for (int i=0; i<QUARTERS; i++)sales[i] = 0 ;
}Sales::Sales(const double ar[], int n)
{int i ;for (i=0; i<n && i<QUARTERS; i++)sales[i] = ar[i] ;while (i<QUARTERS){sales[i] = 0 ;i ++ ;} min = max = sales[0] ;double total = 0 ;for (i=0; i<QUARTERS; i++){total += sales[i] ;if (min > sales[i]) min = sales[i] ;if (max < sales[i]) max = sales[i] ;}average = total / QUARTERS ;
}Sales::~Sales()
{std::cout << "delete!\n" ;
}void Sales::setSales()
{using namespace std ;int n ;cout << "Enter the number of double you want to input: " ;cin >> n ;double *ar = new double[n] ;cout << "Enter these numbers you want to input:\n" ;for (int i=0; i<n; i++){cin >> ar[i] ;}Sales S(ar, n) ;*this = S ;delete [] ar ;
}void Sales::ShowSales() const
{std::cout << "The members of Sales:\n" ;int i ;for (i=0; i<QUARTERS; i++){std::cout << sales[i] << " " ;}std::cout << std::endl ;std::cout << "max = " << max << std::endl ;std::cout << "min = " << min << std::endl ;std::cout << "average = " << average << std::endl ;
}

main.cpp:

#include "Sales.h"
#include <iostream>
using namespace SALES ;int main()
{using namespace std ;double ar[7] = {7, 6, 5, 4, 3, 2, 1} ;Sales S1(ar, 7) ;cout << "The content of S1:\n" ;S1.ShowSales() ;Sales S2 ;S2.setSales() ;cout << "The content of S2:\n" ;S2.ShowSales() ;
}

(5)
Stack.h:

#ifndef STACK_H_
#define STACK_H_struct customer {char fullname[35] ;double payment ;
} ;typedef customer Item ;class Stack
{
private:static const int MAX = 10 ;Item items[MAX] ;int top ;double total_payment ;
public:Stack() ;~Stack() ;bool isempty() const ;bool isfull() const ;bool push(const Item & e) ;bool pop(Item & e) ;
} ;#endif

Stack.cpp:

#include "Stack.h"
#include <cstring>
#include <iostream>Stack::Stack()
{top = -1 ;total_payment = 0 ;
}Stack::~Stack()
{std::cout << "Delete!\n" ;
}bool Stack::isempty() const
{if (top == -1) return true ;else return false ;
}bool Stack::isfull() const
{if (top == (MAX-1)) return true ;else return false ;
}bool Stack::push(const Item & e)
{if (top == (MAX-1)) return false ;top++ ;strcpy(items[top].fullname, e.fullname) ;items[top].payment = e.payment ;return true ;
}bool Stack::pop(Item & e)
{if (top == -1) return true ;strcpy(e.fullname, items[top].fullname) ;e.payment = items[top].payment ;top -- ;total_payment += e.payment ;std::cout << "total_payment = " << total_payment << std::endl ;return true ;
}

main.cpp:

#include "Stack.h"
#include <iostream>
#include <cctype>int main()
{using namespace std ;Stack St ;char ch ;Item item ;cout << "Please enter A to add a customer,\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': cout << "Enter a customer:\n" ;cout << "Fullname: " ;cin.get(item.fullname, 35).get() ;cout << "Payment: " ;cin >> item.payment ;if (!St.push(item))cout << "The Stack is full!\n" ;break ;case 'P':case 'p': cout << "Delete a customer:\n" ;if (!St.pop(item))cout << "The Stack is empty!\n" ;cout << "Fullname: " ;cout << item.fullname << endl ;cout << "Payment: " ;cout << item.payment << endl ;break ;}cout << "Please enter A to add a customer,\n"<< "P to process a PO, or Q to quit.\n" ;}cout << "Bye!\n" ;return 0 ;
}

(6)
Move.h:

#ifndef MOVE_H_
#define MOVE_H_class Move
{
private:double x ;double y ;
public:Move(double a = 0, double b = 0) ;~Move() ;void showmove() const ;Move add(const Move & m) const ;void reset(double a = 0, double b = 0) ;
} ;#endif

Move.cpp:

#include "Move.h"
#include <iostream>Move::Move(double a, double b)
{x = a ;y = b ;
}Move::~Move()
{std::cout << "Delete!\n" ;
}void Move::showmove() const
{std::cout << "In this object, x = " << x << ", "<< "y = " << y << std::endl ;
}Move Move::add(const Move & m) const
{Move M(m.x, m.y);return M ;
}void Move::reset(double a, double b) 
{x = a ;y = b ;
}

main.cpp:

#include "Move.h"int main()
{Move Begining ;Move Next(9, 8) ;Begining.showmove() ;Next.showmove() ;Move Destination = Begining.add(Next) ;Destination.showmove() ;
}

(7)
Plorg.h:

#ifndef PLORG_H_
#define PLORG_H_class Plorg
{
private:static const int MAX = 19 ;char fullname[MAX] ;int CI ;
public:Plorg(const char * name = "Plorga") ;~Plorg() ;void ResetCI(int ci) ;void ShowPlorg() const ;
} ;#endif

Plorg.cpp:

#include "Plorg.h"
#include <iostream>
#include <cstring>Plorg::Plorg(const char * name){strncpy(fullname, name, MAX) ;CI = 50 ;
}Plorg::~Plorg(){std::cout << "Delete!\n" ;
}void Plorg::ResetCI(int ci){CI = ci ;
}void Plorg::ShowPlorg() const
{std::cout << "The fullname: " << fullname << ", CI = " << CI << std::endl ;
}

main.cpp:

#include "Plorg.h"int main()
{Plorg P1("Jim Killy") ;Plorg P2("Jackie London") ;Plorg P3("Trump Jim") ;P1.ShowPlorg() ;P2.ShowPlorg() ;P3.ShowPlorg() ;P1.ResetCI(90) ;P2.ResetCI(80) ;P3.ResetCI(70) ;P1.ShowPlorg() ;P2.ShowPlorg() ;P3.ShowPlorg() ;
}

(8)
List.h:

#ifndef LIST_H_
#define LIST_H_typedef int Item ;
struct LinkList {Item ElemType ;LinkList * next ;
} ;class List
{
private:static const int MAX = 10 ;LinkList * head ;LinkList * rail ;LinkList * p ;int num ;
public:List() ;~List() ;bool Add(const Item & i) ;bool isEmpty() ;bool isFull() ;void visit(void (*pf)(Item & i)) ;
} ;#endif

List.cpp:

#include "List.h"
#include <iostream>List::List() {head = new LinkList ;head->next = NULL ;rail = p = head ;num = 0 ;
}List::~List() {while (head != rail) {p = head ;head = head -> next ;delete p ;}delete head ;head = rail = p = NULL ;num = 0 ;std::cout << "This List has been destroyed!\n" ;
}bool List::Add(const Item & i) {if (num == MAX) return false ;p = rail ;p->next = new LinkList ;p->next->ElemType = i ;p->next->next = NULL ;rail = p->next ;num++ ;return true ;
}bool List::isEmpty() {if (num == 0) return true ;else return false ;
}bool List::isFull() {if (num == MAX) return true ;else return false ;
}void List::visit(void (*pf)(Item & i)) {for (p = head->next; p; p=p->next)(*pf)(p->ElemType) ;
}

main.cpp:

#include "List.h"
#include <iostream>void Watch(Item & i) ;
void AddOne(Item & i) ;int main()
{using namespace std ;List L ;cout << "Let's input some numbers:\n" ;Item i ;while (cin>>i) {if (L.isFull()) {cout << "This List is filled!\n" ;break ;}else L.Add(i) ;}cout << "Let's see all of members from this List:\n" ;L.visit(Watch) ;cout << endl ;cout << "Let's add one to every member from this List:\n" ;L.visit(AddOne) ;L.visit(Watch) ;cout << endl ;
}void Watch(Item & i) {std::cout << i << " " ;
}void AddOne(Item & i) {i += 1 ;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/41976.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue history和hash模式

目录 一、简介 一、简介 ~~~~~~~~ 在Vue.js中&#xff0c;路由模式是用来管理应用程序中不同页面之间的导航的机制。Vue Router支持两种常见的路由模式&#xff1a;history模式和hash模式。 History 模式&#xff1a; ~~~~~~~~ History模式使用浏览器的history.pushState API …

红帆OA SQL注入漏洞复现

0x01 产品简介 红帆iOffice.net从最早满足医院行政办公需求&#xff08;传统OA&#xff09;&#xff0c;到目前融合了卫生主管部门的管理规范和众多行业特色应用&#xff0c;是目前唯一定位于解决医院综合业务管理的软件&#xff0c;是最符合医院行业特点的医院综合业务管理平…

Lnton羚通关于如何使用nanoPC-T4 安装OpenCV?

nanoPC-T4 安装 OpenCV Note: OpenCV has been pre-installed in FriendlyCore/FriendlyDesktop (Version after 201905) and does not require manual installation. Please download the latest FriendlyCore/FriendlyDesktop Image file from the following URL: http://do…

springboot 自定义注解

1、引入maven依赖&#xff08;版本太低也会导致不生效&#xff09; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId><version>2.7.10</version></dependency…

深度分析纳斯达克上市公司慧择的竞争优势和投资价值

来源&#xff1a;猛兽财经 作者&#xff1a;猛兽财经 一、保险行业的现状、竞争与机遇 在疫情期间&#xff0c;很多行业的经营理念与经营方式&#xff0c;甚至客户行为、客户需求都发生了变化&#xff0c;进而催生出新的机遇。保险行业亦是如此&#xff0c;受疫情影响&#xf…

用Python编程实现百度自然语言处理接口的对接,助力你开发智能化处理程序

用Python编程实现百度自然语言处理接口的对接&#xff0c;助力你开发智能化处理程序 随着人工智能的不断进步&#xff0c;自然语言处理&#xff08;Natural Language Processing&#xff0c;NLP&#xff09;成为了解决文本处理问题的重要工具。百度自然语言处理接口提供了一系…

腾讯开启2024校招,主要招聘5大类岗位

近日&#xff0c;腾讯的大动作一个接一个&#xff0c;前脚刚公布2023上半年财报&#xff0c;后脚就开启了2024校招&#xff0c;不得不让人感叹腾讯真速度&#xff01; 此次招聘对象为毕业时间在2023年9月至2024年8月期间的2024届应届毕业生&#xff0c;覆盖北上广深等多个城市…

异步编程框架Seastar介绍

使用Seastar进行异步(Asynchronout)编程 介绍 我们在本文中介绍的Seastar&#xff0c;是一个用于在现代多核机器上&#xff0c;编写高效复杂的服务器应用程序的C库。 传统上&#xff0c;用于编写服务器应用程序的编程语言库和框架已经分为两个不同的阵营&#xff1a;那些注重…

环境与能源创新专题:地级市绿色创新、碳排放与环境规制数据

数据简介&#xff1a;推动绿色发展&#xff0c;促进人与自然和谐共生是重大战略举措。绿色发展强调“绿水青山就是金山银山”&#xff0c;人与自然和谐共生重在正确处理生态环境保护与经济发展的关系。在着力于实现绿色发展的过程中&#xff0c;绿色创新是绿色发展的重要驱动因…

关于API数据接口获取商品的数据的说明

获取商品数据已经成为许多应用程序的重要组成部分。为了实现这一目标&#xff0c;许多公司和技术开发者使用API数据接口来获取相关数据。本文将详细介绍如何使用API数据接口获取商品数据&#xff0c;并使用Python作为编程语言示例来展示相关代码。 API数据接口是一种通信协议&…

WPF的CheckBox中的三个状态

WPF的CheckBox中的三个状态 CheckBox控件和RadioButton控件是继承自ToggleButton类&#xff0c;这意味着用户可切换他们的开关状态&#xff0c;其中IsChecked属性是可空的Boolean类型&#xff0c;这意味着该属性可以设置为true&#xff0c;false或null。 null值表示不确定状态…

spring.HttpMessageNotReadableException: JSON parse error

实体类如下&#xff1a; Value public class Search{//搜索内容String value;//是否模糊搜索boolean fuzzy true; //其实这样写并不是“默认”模糊搜索&#xff0c;而是“一定是”模糊搜索 }spring.HttpMessageNotReadableException: JSON parse error: Cannot construct ins…

GPU Microarch 学习笔记 [1]

WARP GPU的线程从thread grid 到thread block&#xff0c;一个thread block在CUDA Core上执行时&#xff0c;会分成warp执行&#xff0c;warp的颗粒度是32个线程。比如一个thread block可能有1024个线程&#xff0c;分成32个warp执行。 上图的CTA&#xff08;cooperative thre…

10条SQL优化技巧

一、一些常见的SQL实践 &#xff08;1&#xff09;负向条件查询不能使用索引 select * from order where status!0 and stauts!1 not in/not exists都不是好习惯 可以优化为in查询&#xff1a; select * from order where status in(2,3) &#xff08;2&#xff09;前导模…

Codeforces Round 893 (Div. 2)B题题解

文章目录 [The Walkway](https://codeforces.com/contest/1858/problem/B)问题建模问题分析1.分析所求2.如何快速计算每个商贩被去除后的饼干数量代码 The Walkway 问题建模 给定n个椅子&#xff0c;其中有m个位置存在商贩&#xff0c;在商贩处必须购买饼干吃&#xff0c;每隔…

Python程序设计——字符串处理的特殊方法

学习目标&#xff1a; 学习如何创建字符串使用len、min和max函数获取一个字符串的长度、串中的最大和最小的字符使用下标运算符([])访问字符串中的元素使用截取运算符str[ start:end]从较长的字符串中得到一个子串使用运算符连接两个字符串&#xff0c;通过*运算符复制一个字符…

【Odroid C4】交叉编译工具链安装以及QT交叉编译环境搭建

【Odroid C4】交叉编译工具链安装以及QT交叉编译环境搭建 虚拟机环境&#xff0c;UBUNTU20.04 文章目录 【Odroid C4】交叉编译工具链安装以及QT交叉编译环境搭建一、Odroid C4交叉编译工具链安装二、QT下载及编译安装1.QT下载2.交叉编译QT 配置QtCreator可以[参考](https://bl…

快速入门vue3新特性和新的状态管理库pinia

(创作不易&#xff0c;感谢有你&#xff0c;你的支持&#xff0c;就是我前行的最大动力&#xff0c;如果看完对你有帮助&#xff0c;请留下您的足迹&#xff09; 目录 Vue3.3新特性 defineOptions defineModel pinia 介绍 与 Vuex 3.x/4.x 的比较 安装 核心概念 定义…

前馈神经网络多分类任务

pytorch深度学习的套路都差不多&#xff0c;多看多想多写多测试&#xff0c;自然就会了。主要的技术还是在于背后的数学思想和数学逻辑。 废话不多说&#xff0c;上代码自己看。 import torch import numpy as np import torch.nn as nn import torchvision import torchvisi…

【腾讯云Cloud Studio实战训练营】使用Cloud Studio社区版快速构建React完成点餐H5页面还原

陈老老老板&#x1f9b8; &#x1f468;‍&#x1f4bb;本文专栏&#xff1a;生活&#xff08;主要讲一下自己生活相关的内容&#xff09; &#x1f468;‍&#x1f4bb;本文简述&#xff1a;生活就像海洋,只有意志坚强的人,才能到达彼岸。 &#x1f468;‍&#x1f4bb;上一篇…