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

本文介绍了一个使用C++实现的栈类,该栈用于存储客户信息,并计算累计支付金额。文章详细展示了如何定义栈结构、实现基本操作如push和pop,并通过示例演示了栈的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件
#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();



}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值