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 ;
}
相关文章:
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 Co…...
c++中的extern关键字
extern关键字主要修饰变量或函数,表示该函数可以跨文件访问,或者表明该变量在其他文件定义,在此处引用。 extern修饰变量 (1)如果某变量int m在a.c中定义声明,则其他b.c文件访问时,需要用exte…...
javaScript:快乐学习计时器
目录 一.前言 二.计时器 1.计时器的分类 2. 创建计时器的方式 创建间隔计时器 创建方式三种 1.匿名函数 2.使用函数直接作为计时器的执行函数 2.使用函数直接作为计时器的执行函数,用字符串的形式写入 3.计时器的返回值 4.清除计时器 5.延迟计时器 相关代码 一.前言 在…...
onnxruntime 支持的所有后端
1 代码导出 import onnxruntime as ort aaa ort.get_all_providers() print(aaa)1. 1 下面是ort支持的所有后端 TensorrtExecutionProvider, CUDAExecutionProvider, MIGraphXExecutionProvider, ROCMExecutionProvider, OpenVINOExecutionProvider, DnnlExecutionProvider…...

k8s 自身原理 5
我们知道容器是通过 pod 来承载的,我们在 k8s 中,服务都是跑在 pod 里面的,pod 里面可以跑 1 个容器,或者跑多个容器,那么咱们 pod 里面跑 1 个服务容器,咱真的就以为里面就只有这样个容器吗? …...

机器视觉应用开发什么最重要?
(QQ群有答疑)零基础小白快速上手海康VisionMaster开发系列课程 高级语言在机器视觉就是工具,机器视觉软件,在机器视觉中也是工具,在机器视觉应用开发中,图像处理是最重要的,一切看图像ÿ…...

React+Typescript使用接口泛型处理props
好 刚讲完组件 那么 这次 我们来看一下 数据传递的 props 还是上文的案例 例如 我们想将 title 传给Hello组件 之前我们可以直接这样 以一个标签属性的形式传过去 而我们在子组件中 这样去使用 但现在 我们从编辑器中都可以看出 这种写法已经不行了 然后 我们将 hello 组件…...
自定义python文件import导入ModuleNotFoundError: No module named ‘***‘ 问题
自定义python文件import导入ModuleNotFoundError: No module named ‘***’ 问题 错误代码: import configparser import os.path import sys from bin import swk_mysql_create, swk_redis_create这里导入自定义python文件,其中swk_mysql_create.py文…...

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

HTTP响应状态码大全:从100到511,全面解析HTTP请求的各种情况
文章目录 前言一、认识响应状态码1. 什么是HTTP响应状态码2. Http响应状态码的作用3. 优化和调试HTTP请求的建议 二、1xx 信息响应1. 认识http信息响应2. 常见的信息响应状态码 三、2xx 成功响应1. 认识HTTP成功响应2. 常见的成功响应状态码 四、3xx 重定向1. 认识http重定向2.…...
Vue-10.集成.env
.env、.env.development 和 .env.preview .env、.env.development 和 .env.preview 文件是用于配置环境变量和应用程序设置的文件,它们在项目开发和部署过程中起到关键作用。这些文件用于在不同的环境中设置不同的变量值,以满足不同环境下的配置需求。 …...

强训第33天
选择 C A ping是TCP/IP协议族的一部分,使用ICMP协议,ICMP底层使用IP协议。如果要ping其他网段,则需要设置网关。 如果是二层交换机故障,则ping同网段的也会不通。 C Dos攻击被称之为“拒绝服务攻击”,其目的是使计算机…...

【CTF-web】buuctf-[极客大挑战 2019]EasySQL 1(sql注入)
题目链接 根据题目判断出可能需要sql注入,看源码可知数据是通过GET的方式传输的,即放在url的username和password两个参数中。 只要将username输入为1 or 11#,password可以为任何值,即可顺利登录。 需要注意的是url中的井号表示…...
脚本语言与编译语言的区别
文章目录 一、语法差异二、执行方式差异三、应用领域差异四、总结 一、语法差异 脚本语言:脚本语言通常使用解释器逐行执行,不需要事先编译。它的语法相对简单,易于学习和使用。常见的脚本语言有Python、JavaScript和Ruby等。 编译语言&…...
大型企业或者组织,组建专属的虚拟局域网,深入理解相关的配置和搭建使用、网络加速和网络优化,可夸地区夸国际使用,深入搞懂每项配置的作用和含义
大型企业或者组织,组建专属的虚拟局域网,深入理解相关的配置和搭建使用、网络加速和网络优化,可夸地区夸国际使用,深入搞懂每项配置的作用和含义。 1、openxxx介绍与图解 1.1 openxxx介绍 openxxx 是一个基于 OpenSSL库的应用层 虚拟局域网 实现。和传统 虚拟局域网 相…...

数据结构:二叉树的递归实现(C实现)
个人主页 : 个人主页 个人专栏 : 《数据结构》 《C语言》 文章目录 前言一、树的概念二、二叉树二叉树的概念二叉树的性质 三、二叉树链式结构实现二叉树节点定义创建二叉树节点遍历二叉树先序遍历二叉树(BinaryTreePrevOrder)中序遍历二叉树(BinaryTree…...

MinGW编译运行报错RTTI symbol not found for class ‘XXX‘
最近在调试程序时莫名的出现图中报错: 还遇到过for class QObject,在此记录一下,排查后发现,原因都是有资源被重复释放导致的。...

table表头颜色 element plus
原图 预期 css :deep(.el-table__header) {background-color: #F5F7FA;} :deep(.el-table tr) {background-color: rgba(0,0,0,0);} :deep(.el-table th.el-table__cell) {background-color: rgba(0,0,0,0);}...

网络安全(自学)
想自学网络安全(黑客技术)首先你得了解什么是网络安全!什么是黑客! 网络安全可以基于攻击和防御视角来分类,我们经常听到的 “红队”、“渗透测试” 等就是研究攻击技术,而“蓝队”、“安全运营”、“安全…...

FPGA芯片IO口上下拉电阻的使用
FPGA芯片IO口上下拉电阻的使用 为什么要设置上下拉电阻一、如何设置下拉电阻二、如何设置上拉电阻为什么要设置上下拉电阻 这里以高云FPGA的GW1N-UV2QN48C6/I5来举例,这个芯片的上电默认初始化阶段,引脚是弱上来模式,且模式固定不能通过软件的配置来改变。如下图所示: 上…...

Unity3D中Gfx.WaitForPresent优化方案
前言 在Unity中,Gfx.WaitForPresent占用CPU过高通常表示主线程在等待GPU完成渲染(即CPU被阻塞),这表明存在GPU瓶颈或垂直同步/帧率设置问题。以下是系统的优化方案: 对惹,这里有一个游戏开发交流小组&…...

【VLNs篇】07:NavRL—在动态环境中学习安全飞行
项目内容论文标题NavRL: 在动态环境中学习安全飞行 (NavRL: Learning Safe Flight in Dynamic Environments)核心问题解决无人机在包含静态和动态障碍物的复杂环境中进行安全、高效自主导航的挑战,克服传统方法和现有强化学习方法的局限性。核心算法基于近端策略优化…...
QT3D学习笔记——圆台、圆锥
类名作用Qt3DWindow3D渲染窗口容器QEntity场景中的实体(对象或容器)QCamera控制观察视角QPointLight点光源QConeMesh圆锥几何网格QTransform控制实体的位置/旋转/缩放QPhongMaterialPhong光照材质(定义颜色、反光等)QFirstPersonC…...
动态 Web 开发技术入门篇
一、HTTP 协议核心 1.1 HTTP 基础 协议全称 :HyperText Transfer Protocol(超文本传输协议) 默认端口 :HTTP 使用 80 端口,HTTPS 使用 443 端口。 请求方法 : GET :用于获取资源,…...

Netty从入门到进阶(二)
二、Netty入门 1. 概述 1.1 Netty是什么 Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. Netty是一个异步的、基于事件驱动的网络应用框架,用于…...
Java求职者面试指南:计算机基础与源码原理深度解析
Java求职者面试指南:计算机基础与源码原理深度解析 第一轮提问:基础概念问题 1. 请解释什么是进程和线程的区别? 面试官:进程是程序的一次执行过程,是系统进行资源分配和调度的基本单位;而线程是进程中的…...

CVPR2025重磅突破:AnomalyAny框架实现单样本生成逼真异常数据,破解视觉检测瓶颈!
本文介绍了一种名为AnomalyAny的创新框架,该方法利用Stable Diffusion的强大生成能力,仅需单个正常样本和文本描述,即可生成逼真且多样化的异常样本,有效解决了视觉异常检测中异常样本稀缺的难题,为工业质检、医疗影像…...

永磁同步电机无速度算法--基于卡尔曼滤波器的滑模观测器
一、原理介绍 传统滑模观测器采用如下结构: 传统SMO中LPF会带来相位延迟和幅值衰减,并且需要额外的相位补偿。 采用扩展卡尔曼滤波器代替常用低通滤波器(LPF),可以去除高次谐波,并且不用相位补偿就可以获得一个误差较小的转子位…...

恶补电源:1.电桥
一、元器件的选择 搜索并选择电桥,再multisim中选择FWB,就有各种型号的电桥: 电桥是用来干嘛的呢? 它是一个由四个二极管搭成的“桥梁”形状的电路,用来把交流电(AC)变成直流电(DC)。…...

【Linux】Linux安装并配置RabbitMQ
目录 1. 安装 Erlang 2. 安装 RabbitMQ 2.1.添加 RabbitMQ 仓库 2.2.安装 RabbitMQ 3.配置 3.1.启动和管理服务 4. 访问管理界面 5.安装问题 6.修改密码 7.修改端口 7.1.找到文件 7.2.修改文件 1. 安装 Erlang 由于 RabbitMQ 是用 Erlang 编写的,需要先安…...