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


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

  • 相关阅读:
    Java并发(十八):阻塞队列BlockingQueue
    web前端
    python学习总结:目录
    Django -- 5.路由层(URLconf)_基于Django1
    python:linux下字符串转换为JSON
    python:一秒中启动一个下载服务器
    Flask【第十二章】:Flask之Websocket,建立单聊群聊
    Flask【第十一章】:Flask中的CBV以及偏函数+线程安全
    Flask【第十章】:特殊装饰器 @app.before_request 和 @app.after_request 以及@app.errorhandler
    Flask【第九章】:Flask之蓝图
  • 原文地址:https://www.cnblogs.com/yangquanhui/p/4937513.html
Copyright © 2011-2022 走看看