Stack类
参考Array类和String类的实现,设计并完成栈类
实现代码
Simple_Stack.h
#ifndef SIMPLE_STACK_H
#define SIMPLE_STACK_H
#include<iostream>
#define MAXSIZE 20;
using namespace std;
class Simple_Stack {
private:
int maxSize;
int* ps;
int top;
bool isEmpty()const;
bool isFull()const;
public:
Simple_Stack();
~Simple_Stack();
int pop();
void push(int);
int top_element();
};
#endif
Simple_Stack.cpp
#include "Simple_Stack.h"
bool Simple_Stack::isEmpty() const
{
return top==0;
}
bool Simple_Stack::isFull() const
{
return top==maxSize-1;
}
Simple_Stack::Simple_Stack()
{
maxSize = MAXSIZE;
ps = new int[20];
top = 0;
}
Simple_Stack::~Simple_Stack()
{
delete[] ps;
ps = NULL;
}
int Simple_Stack::pop()
{
if (isEmpty()) {
cout << "链表为空 输出-1" << endl;
return -1;
}
return ps[top--];
}
void Simple_Stack::push(int a)
{
if (isFull()) {
cout << "链表已满 " << endl;
return;
}
ps[++top] = a;
}
int Simple_Stack::top_element()
{
if (isEmpty()) {
cout << "链表为空 输出-1" << endl;
return -1;
}
return ps[top];
}
MainTest,cpp
#include"Simple_Stack.h"
int main() {
Simple_Stack test;
test.push(1);
test.push(2);
test.push(3);
cout << "the pop():" << test.pop() << endl;
cout << "the top_element():" << test.top_element() << endl;
return 0;
}
package继承层次
( 包裹的继承层次结构) 一些包裹快递商, 如FedEx 、DHL 和UPS , 都提供多样化的服务, 同时也收取不同的费用。请创建一个表示各种不同包裹的继承层次结构。以包裹类Package 作为基类,两日包裹类TwoDayPackage 和隔夜包裹类OvernightPackage 作为派生类。基类Package 应该包括代表寄件人和收件人姓名、地址、所在城市、所在州和邮政编码等的数据成员。此外, 还应包含存储包裹重量( 以盎司计) 和运送包裹的每盎司费用的数据成员。类Package 的构造函数应初始化这些数据成员, 并确保重量和每盎司费用为正值。Package 应该提供public 成员函数calculate-cost ,该函数计算重量和每盎司费用的乘积,得到的是与运输该包裹有关的费用并返回( 返回值类型为double ) 。派生类TwoDayPackage 应该继承基类Package 的功能, 但还应包含一个数据成员, 表示付给两日快递服务的平寄费。TwoDayPackage 构造函数应接受一个值来初始化这个数据成员。类TwoDayPackage 还应该重新定义基类的成员函数calculatecost 来计算运输费用, 具体方法是将平寄费加到由基类Package 的calculateCost 函数计算得到的基于重量的费用中。派生类OvernightPackage 应直接继承基类, 并且应包含一个附加的数据成员, 表示付给隔夜快递服务的每盎司的额外费用。类OvernightPackage 应当重新定义基类的成员函数calculateCost , 从而使它在计算运输费用之前, 先将额外的每盎司费用加到标准的每盎司费用上。编写测试程序, 创建每种Package 的对象并测试成员函数calculateCost 。
实现代码
ConsoleApplication.cpp
#include <iostream>
#include "Package.h"
#include "TwoDayPackage.h"
#include "OvernightPackage.h"
using namespace std;
int main()
{
string tmp = "tmp";
Package package("A", "B", "PuZhuNanLu", "Nanjing", "Jiangsu", 211816, 0.5, -1);
cout << package.CalculateCost() << endl;
TwoDayPackage two_day_package("A", "B", "PuZhuNanLu", "Nanjing", "Jiangsu", 211816, 0.5, 1, 8);
cout << two_day_package.CalculateCost() << endl;
OvernightPackage overnight_package("A", "B", "PuZhuNanLu", "Nanjing", "Jiangsu", 211816, 0.5, 0.9, 0.1);
cout << overnight_package.CalculateCost() << endl;
}
OvernightPackage.h
#pragma once
#include "Package.h"
class OvernightPackage :
public Package
{
public:
OvernightPackage(const string&, const string&, const string&,
const string&, const string&, int, double, double, double);
double CalculateCost() const;
private:
double extra_cost_;
};
OvernightPackage.cpp
#include "OvernightPackage.h"
OvernightPackage::OvernightPackage(const string& sender_name, const string& recipient_name,
const string& address, const string& city, const string& state, int postal_code, double weight,
double cost_per_ounce, double extra_cost)
:Package(sender_name, recipient_name, address, city, state, postal_code,
weight, cost_per_ounce), extra_cost_(extra_cost)
{
}
double OvernightPackage::CalculateCost() const
{
return GetWeight() * (GetCostPerOunce() + extra_cost_);
}
Package.h
#pragma once
#include <string>
using namespace std;
class Package
{
public:
Package(const string&, const string&, const string&, const string&,
const string&, int, double, double);
double CalculateCost() const;
double GetWeight() const;
double GetCostPerOunce() const;
private:
string sender_name_;
string recipient_name_;
string address_;
string city_;
string state_;
int postal_code_{};
double weight_{};
double cost_per_ounce_{};
};
Package.cpp
#include "Package.h"
Package::Package(const string& sender_name, const string& recipient_name,
const string& address, const string& city, const string& state,
int postal_code, double weight, double cost_per_ounce):
sender_name_(sender_name), recipient_name_(recipient_name),
address_(address),
city_(city), state_(state), postal_code_(postal_code)
{
weight_ = weight > 0 ? weight : 1;
cost_per_ounce_ = cost_per_ounce > 0 ? cost_per_ounce : 1;
}
double Package::CalculateCost() const
{
return weight_ * cost_per_ounce_;
}
double Package::GetWeight() const
{
return weight_;
}
double Package::GetCostPerOunce() const
{
return cost_per_ounce_;
}
TwoDayPackage.h
#pragma once
#include "Package.h"
class TwoDayPackage :
public Package
{
public:
TwoDayPackage(const string&, const string&, const string&, const string&,
const string&, int, double, double, double);
double CalculateCost() const;
private:
double normal_shipping_fee_{};
};
TwoDayPackage.cpp
#include "TwoDayPackage.h"
TwoDayPackage::TwoDayPackage(const string& sender_name,
const string& recipient_name, const string& address, const string& city,
const string& state, int postal_code, double weight, double cost_per_ounce,
double normal_shipping_fee)
:Package(sender_name, recipient_name, address, city, state, postal_code,
weight, cost_per_ounce), normal_shipping_fee_(normal_shipping_fee)
{
}
double TwoDayPackage::CalculateCost() const
{
return normal_shipping_fee_ + Package::CalculateCost();
}
account继承层次
( 账户的继承层次结构) 创建一个银行账户的继承层次结构, 表示银行的所有客户账户。所有的客户都能在他们的银行账户存钱( 即记人贷方) 、取钱( 即记人借方) , 但是账户也可以分成更具体的类型。例如, 存款账户依靠存款生利。另一方面, 支票账户对每笔交易( 即存款或取款) 收取费用。
请创建一个类层次结构, 以账户类Account 作为基类, 存款账户类SavingsAccount 和支票账户类CheckingAccount 作为派生类。基类Account 应该包括一个double 类型的数据成员, 表示账户的余额。这个类应该提供一个构造函数, 接收一个初始余额值并用它初始化表示余额的数据成员。而且该构造函数应该确认初始余额的有效性, 保证它大于等于0 。如果小于0 , 则应该将表示余额的数据成员设置为0 , 并显示出错信息, 表明该初始化余额是一个无效的值。类Account 还应当提供三个成员函数。成员函数credit 应该负责向当前表示余额的数据成员加钱; 成员函数debit应该负责从账户中取钱, 并且保证账户不会透支。如果提取金额大于账户余额, 函数将保持表示余额的数据成员的值不变, 并打印信息“ Debit amount exceeded account balance. ” 成员函数getBalance 则应该返回当前表示余额的数据成员的值。
派生类SavingsAccount 不仅继承了基类Account 的功能, 而且还应提供一个附加的double 类型的数据成员, 表示支付给这个账户的利率( 百分比) 。类SavingsAccount 的构造函数应该接收初始余额值和初始利率值, 还应该提供一个public 成员函数calculatelnterest , 返回代表账户所获的利息的一个double 值。成员函数calculatelnterest 应该通过计算账户余额和利率的乘积得到账户所获的利息。注意: 类SavingsAccount 应该继承基类Account 的成员函数credit 和debit ,不需要重新定义。
派生类CheckingAccount 不仅继承了基类Account 的功能, 而且还应提供一个附加的double 类型的数据成员, 表示每笔交易的费用。类CheckingAccount 的构造函数应该接收初始余额值和交易费用值。类CheckingAccount 应该需要重新定义成员函数credit 和debit , 这样当每笔交易顺利完成时, 从账户余额中减去每笔交易的费用。类CheckingAccount 重新定义这些函数时应该调用基类Account 中的这两个函数来执行账户余额的更新。类CheckingAccount 的debit 函数只有当钱被成功提取时( 即提取金额不超过账户余额时) 才应当收取交易费。提示: 定义类Account 的debit 函数使它返回一个类型值, 表示钱是否被成功提取。然后利用这个值决定是否应该扣除交易费。
当这个层次结构中的类定义完毕之后, 请编写一个程序, 要求创建每个类的对象并测试它们的成员函数。将利息加到SavingsAccount 对象的方法是: 先调用它的成员函数calculatelnterest , 然后将返回的利息数传递给该对象的credit 函数
实现代码
Account.h
class Account
{
public:
Account(double);
void Credit(double);
bool Debit(double);
double getBalance() const;
private:
double balance_{};
};
Account.cpp
#include <iostream>
#include "Account.h"
using namespace std;
Account::Account(double balance)
{
if (balance >= 0)
{
balance_ = balance;
}
else
{
balance_ = 0;
cout << "Invalid parameter!" << endl;
}
}
void Account::Credit(double num)
{
balance_ += num;
}
bool Account::Debit(double num)
{
if (num > balance_)
{
cout << "Debit amount exceeded account balance." << endl;
return false;
}
else
{
balance_ -= num;
return true;
}
}
double Account::getBalance() const
{
return balance_;
}
CheckingAccount.h
#pragma once
#include "Account.h"
class CheckingAccount :
public Account
{
public:
CheckingAccount(double, double);
void Credit(double);
bool Debit(double);
private:
double cost_{};
};
CheckingAccount.cpp
#include <iostream>
#include "CheckingAccount.h"
using namespace std;
CheckingAccount::CheckingAccount(double balance, double cost) : Account(balance), cost_(cost)
{
}
void CheckingAccount::Credit(double num)
{
Account::Credit(num - cost_);
}
bool CheckingAccount::Debit(double num)
{
if (Account::Debit(num))
{
Account::Debit(cost_);
return true;
}
else
{
cout << "Debit amount exceeded account balance and cost per transaction." << endl;
return false;
}
}
SavingAccount.h
#pragma once
#include "Account.h"
class SavingsAccount :
public Account
{
public:
SavingsAccount(double, double);
double CalculateInterest() const;
private:
double interest_rate_{};
};
SavingAccount.cpp
#include "SavingsAccount.h"
SavingsAccount::SavingsAccount(double balance, double interest_rate) : Account(balance), interest_rate_(interest_rate)
{
}
double SavingsAccount::CalculateInterest() const
{
return getBalance() * interest_rate_;
}
ConsoleApplication.cpp
#include <iostream>
#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
using namespace std;
int main()
{
Account account(-0.1);
account.Credit(0.1);
account.Debit(0.2);
account.Debit(0.1);
cout << account.getBalance() << endl;
cout << endl;
SavingsAccount saving_account(100, 0.3);
cout << saving_account.CalculateInterest() << endl;
cout << endl;
CheckingAccount checking_account(2, 1);
checking_account.Debit(3);
checking_account.Debit(1);
cout << account.getBalance() << endl;
checking_account.Credit(2);
cout << account.getBalance() << endl;
}