一个多功能计算器

//stack.h

#ifndef CXX_CALCULATOR_H
#define CXX_CALCULATOR_H


template <class T>  
class Stack
{ 
public:

    Stack(int MAXSIZE = 100);
    virtual ~Stack();

    bool IsEmpty();
    bool IsFull();
    T Top() const;
    Stack<T>& Add(const T&x);  
    Stack<T>& Del(T&x);  
    void Clear();
    void Print();

private:

    int m_top;
    int m_MaxSize;
    T *m_stack;
};  

#endif

stack.cpp

#include "stdafx.h"
#include "Stack.h"

template<class T>  
Stack<T>::Stack(int MaxStackSize){  
    m_MaxSize = MaxStackSize - 1;  
    m_stack = new T[MaxStackSize];  
    m_top = -1;  
}  

template<class T>  
Stack<T>::~Stack(){  
    delete []m_stack;  
}  

template<class T>  
Stack<T>& Stack<T>::Add(const T& x){  
    if(IsFull()) {
        printf("No memory\n");
        return *this;      
    }  
    m_top = m_top + 1;  
    stack[m_top] = x;  
    return *this;      
} 

template<class T>  
Stack<T>& Stack<T>::Del(T& x){  
    if(IsEmpty()) {
        printf("no element\n");
        return *this;  
    }
    x = stack[m_top];  
    m_top = m_top - 1;  
    return *this;  
}  


template<class T> 
bool Stack<T>::IsEmpty()
{
    return m_top == -1;
}

template<class T>
bool Stack<T>::IsFull()
{
    return m_top == m_MaxSize;
}

//calucator.h

//第一步利用数字栈,符号栈,计算一个表达式的值
//第二步支持大数的计算,重载加减乘除
//第三步支持二进制,八进制,十进制,十六进制模式
//第四步多线程计算表达式的值
//第五步    MFC
//套用类模板
#ifndef CXX_CALCULATOR_H
#define CXX_CALCULATOR_H

#include <string>
using namespace std;
const int MAX = 100;


template <class T>  
class Stack;
class Calculator
{ 
public:
    Calculator(string s);

    virtual ~ Calculator();

    void Calculat();

private:  

    Stack<char> *s_sym;  

    Stack<int> *s_num;  
    string m_string;


};  

#endif

//calculator.cpp

#include "stdafx.h"
#include "Callcutor.h"
#include <algorithm> 



Calculator::Calculator(string s)
{

    //s_sym = new Stack<char>(MAX);  
    //s_num = new Stack<int>(MAX);  
}

Calculator::~Calculator()
{
    if (NULL != s_num) {
        delete s_num;
        s_num = NULL;
    }

    if (NULL != s_sym) {
        delete s_sym;
        s_sym = NULL;
    }
}

bool isNum(char c){  
    if((c >= '0') && (c <= '9')) {
        return true;  
    }
    else {
        return false;  
    }
} 

void deleteBlank(string &s){  
    string::iterator it = s.begin();  
    while ((it = find(it, s.end(), ' '))!=s.end()) {
        s.erase(it); 
    } 
}   

int Pow(int Base , int index)
{
    int res = 1;
    for (int i = 0; i < index; ++i) {
        res = res * Base;
    }
    return res;
}

还没写完,暂时保存

猜你喜欢

转载自blog.csdn.net/breakpoints_/article/details/80541134
今日推荐