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