zoukankan      html  css  js  c++  java
  • C++primer plus第十章第5题


    头文件:

    //stack.h
    
    #ifndef _STACK_H
    #define _STACK_H
    
    struct customer
    {
    	char fullname[35];
    	double payment;
    };
    
    typedef customer Item ;
    
    class Stack
    {
    private:
    	enum {MAX=10};
    	Item items[MAX];
    	int top;
    
    public:
    	Stack();
    	virtual ~Stack() {};
    	bool isempty() const;
    	bool isfull() const;
    	bool push(const Item & item);
    	bool pop(Item & item);
    
    };
    
    
    #endif // !_STACK_H
    

    .cpp文件:

    //stack.cpp
    
    #include "stack.h"
    
    Stack::Stack()
    {
    	top = 0;
    }
    
    bool Stack::isempty() const
    {
    	return top == 0;
    }
    
    bool Stack::isfull() const
    {
    	return top == MAX;
    }
    
    bool Stack::push(const Item & item)
    {
    	if (top < MAX)
    	{
    		items[top++] = item;
    		return true;
    	}
    	else
    		return false;
    }
    
    bool Stack::pop(Item & item)
    {
    	if (top > 0)
    	{
    		item = items[--top];
    		return true;
    	}
    	else
    		return false;
    }
    

    main:

    //main.cpp
    
    #include <iostream>
    #include "stack.h"
    
    void get_customer(customer & cu);
    
    using namespace std;
    
    int main()
    {
    	
    	Stack st;
    	char ch;
    	customer temp;
    	double payment = 0;
    	cout << "Enter A or a to push a customer,
    "
    		<< "P or p to pop a customer,and Q or q to quit!" << endl;
    	while ((cin >> ch) && (ch != 'q') && (ch != 'Q'))
    	{
    		while (cin.get() != '
    ')
    			continue;
    		if ((toupper(ch)!= 'A') && (toupper(ch) != 'P' ))
    		{
    			cout << "Please enter A,por Q!" << endl;
    			continue;
    
    		}
    		switch (ch)
    		{
    		case 'A':
    		case 'a':
    			if (st.isfull())
    				cout << "The stack is already full!" << endl;
    			else 
    				get_customer(temp);
    			st.push(temp);
    			break;
    
    		case 'P':
    		case 'p':
    			if (st.isempty())
    				cout << "The stack is empty!" << endl;
    			else
    			{
    				st.pop(temp);
    				payment += temp.payment;
    				cout << temp.payment << "is poped!";
    				cout << "payment now total $" << payment << endl;
    
    			}
    			break;
    		
    		}
    		cout << "Enter A or a to push a customer,
    "
    			<< "P or p to pop a customer,and Q or q to quit!" << endl;
    	
    	}
    	cout << "Done!" << endl;
    
    	return 0;
    }
    
    void get_customer(customer & cu)
    {
    	cout << "Enter customer name :";
    	cin.getline(cu.fullname, 35);
    	cout << "Enter customer payment :";
    	cin >> cu.payment;
    	while (cin.get() != '
    ')
    		continue;
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    NoClassDefFoundError问题
    Spring-Batch处理MySQL数据后存到CSV文件
    jQuery EasyUI + struts2.3 + mongoDB 列表查询翻页JAVA样例
    mongodb exception in initAndListen: 12596 old lock file, terminating 解决方法
    硬盘安装RedHat Enterprise Linux 6(转载)
    jQuery zxxbox弹出框插件(v3.0)
    在html页面中利用ftp访问协议格式载入服务器图片
    eclipse中 com.sun.image.codec.jpeg.JPEGCodec 无法编译通过问题
    java 去掉字符串右侧空格
    去掉eclipse js 错误提示
  • 原文地址:https://www.cnblogs.com/yangquanhui/p/4937513.html
Copyright © 2011-2022 走看看