您的位置:首页 > 编程语言 > C语言/C++

C++primer plus第六版课后编程练习答案10.5

2015-11-30 16:00 465 查看
头文件
#ifndef STACK_H_
#define STACK_H_

#include<iostream>
using namespace std;

struct customer{
char fullname[35];
double payment;
};

typedef customer Item;

class Stack
{
Item item[10];
double payment;
int top;
public:
Stack(){top=0;payment=0;}
bool isEmpty() const;
bool isFull() const;
bool push(const Item &t);
bool pop();
void showpayment(){cout<<"payment="<<payment<<endl;}

};

#endif
<pre name="code" class="cpp">#include<iostream>
#include "stack.h"

using namespace std;

bool Stack::isEmpty()const
{
if(0==top)
return true;
else
return false;
}

bool Stack::isFull()const
{
return top==10;
}

bool Stack::push(const Item &t)
{
if(isFull())
{
cout<<"Error,stack is full!"<<endl;
return false;
}
else
{

item[top]=t;
top++;
return 1;
}
}

bool Stack::pop()
{
if(isEmpty())
{
cout<<"Error,stack is empty!"<<endl;
return 0;
}
else
{
top--;
payment+=item[top].payment;

return 1;
}
}



#include<iostream>
#include "stack.h"

using namespace std;

void main()
{

Stack sc;
customer c[5]={
{"I",10},
{"II",20},
{"III",40},
{"IV",50},
{"V",60}
};
sc.pop();
sc.showpayment();
for(int i=0;i<5;i++)
sc.push(c[i]);
for(;i<10;i++)
sc.push(c[i]);

sc.push(c[0]);
sc.showpayment();
sc.pop();
sc.showpayment();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: