第10章第5题

myStack.h

#ifndef MYSTACK_H_
#define MYSTACK_H_
#include<iostream>
const int Len = 10;
struct customer
{
char fullname[35];
double payment;
};
class myStack
{
static int sum; //统计历史上所有被压入的结构数量 :)
private:
customer cp[Len];
int Max;
public:
myStack();
myStack(const customer* nc, int lenght);
~myStack();
bool isEmpty()const;
bool isFull()const;
void pushBack(const customer& ct);
void pop(); //删除最后一个元素,方法和书本立体有些不同
void show()const;
};
#endif

myStack.cxx

#include"myStack.h"
int myStack::sum = 0;
myStack::myStack()
{
}
myStack::myStack(const customer* nc, int lenght)
{
Max = lenght;
sum += lenght; //统计总数
for(int i = 0 ; i < lenght ; ++i)
*(cp+i) = *(nc+i);
}
myStack::~myStack()
{
}
bool myStack::isEmpty()const
{
if(Max == 0)
return true;
else return false;
}
bool myStack::isFull()const
{
if(Max == Len)
return true;
else return false;
}
void myStack::pushBack(const customer& ct)
{
cp[Max] = ct;
++Max;
++sum;
}
void myStack::pop()
{
customer A;
--Max;
cp[Max] = A;
}
void myStack::show()const
{
for(int i = 0 ; i < Max ; ++i)
std::cout<<(cp+i)->fullname<<"的酬劳为"<<(cp+i)->payment<<std::endl;
std::cout<<"数量统计:"<<sum<<std::endl;
}

main.cxx

#include"myStack.h"
#include<cstring>
int main()
{
int t = 0;
double pay;
customer* mid = new customer[Len];
while(t != Len)
{
char* names = new char[35];
std::cout<<"请输入姓名.";
std::cin.getline(names,35);
if(!std::cin)
{
delete [] names;
break;
}
strcpy((mid+t)->fullname,names);
delete [] names;

std::cout<<"请输入酬劳:";
std::cin>>pay;
if(!std::cin)
break;
std::cin.get();
(mid+t)->payment = pay; //完成结构内容输入

++t;
}

myStack test(mid,t);
test.show();
customer tail = {"Tail",100};
if(!test.isFull())
{
test.pushBack(tail);
test.show();
}
if(!test.isEmpty())
{
test.pop();
test.show();
}
delete [] mid;

return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值