注册 登录
编程论坛 数据结构与算法

计算器(栈),求大神指点

hunterevil 发布于 2016-03-17 11:05, 3953 次点击
在代码中,应该创建两个栈,请问是创建一个char和一个int还是创建两个char型的
6 回复
#2
azzbcc2016-03-17 11:18
一个数据栈,建议用double吧

一个运算符栈
#3
hunterevil2016-03-17 13:00
在代码中会定义一个char ch;
然后从键盘上输入一个值,把他赋值给ch,进行判定如果为运算符,则入栈存储运算符的那个栈,否则的话则入栈存储数据的那个栈里。
如果一个栈定义为double,一个定义为char,在把ch入栈的时候有没有什么影响
#4
hunterevil2016-03-17 13:03
回复 2楼 azzbcc
能比能把你的代码给我看一下
#5
azzbcc2016-03-17 16:12
否则的话则入栈存储数据的那个栈里

遇到两位数或者小数呢?

代码我没有,以前写的太烂所以不保留。
#6
hunterevil2016-03-17 18:51
那应该怎么办
#7
azzbcc2016-03-18 13:10
刚写了一个,写的着急,凑合凑合。

程序代码:
#include <stack>
#include <string>
#include <cstdlib>
#include <iostream>
class Calculate
{
private:
    double ans;
    std::string str;
    std::stack<char> operStack;
    std::stack<double> dataStack;
public:
    void clear();
    double calculate();
    char precede(const char a, const char b);
    double oper(const double a, const char ch, const double b);
    friend std::istream& operator>>(std::istream &is, Calculate &cal);
    friend std::ostream& operator<<(std::ostream &os, const Calculate &cal);
};
void Calculate::clear()
{
    ans = 0.0L;
    while (!operStack.empty()) operStack.pop();
    while (!dataStack.empty()) dataStack.pop();
    operStack.push('#');
}
double Calculate::calculate()
{
    this->clear();
    std::string data, ss(str + '#');
    for (size_t i = 0;i != ss.size();i++)
    {
        switch (ss[i])
        {
        case '+':
        case '-':
        case '*':
        case '/':
        case '(':
        case ')':
        case '#':
            if (data.size() != 0)
            {
                dataStack.push(atof(data.c_str()));
                data.clear();
            }
            switch (precede(operStack.top(), ss[i]))
            {
            case '<':
                operStack.push(ss[i]);
                break;
            case '=':
                operStack.pop();
                if (operStack.empty())
                {
                    return ans = dataStack.top();
                }
                break;
            case '>':
                double a = dataStack.top();
                dataStack.pop();
                double b = dataStack.top();
                dataStack.pop();
                dataStack.push(oper(b, operStack.top(), a));
                operStack.pop();
                i--;  // 回退
                break;
            }
            break;
        case '.':
        case '0' ... '9':
            data += ss[i];
            break;
        }
    }
    return ans = 0.0L;
}
char Calculate::precede(const char a, const char b)
{
    std::string str = "+-*/()#";
    char pre[][7] = {
        { '>', '>', '<', '<', '<', '>', '>' },
        { '>', '>', '<', '<', '<', '>', '>' },
        { '>', '>', '>', '>', '<', '>', '>' },
        { '>', '>', '>', '>', '<', '>', '>' },
        { '<', '<', '<', '<', '<', '=', '0' },
        { '>', '>', '>', '>', '0', '>', '>' },
        { '<', '<', '<', '<', '<', '0', '=' } };
    return pre[str.find(a)][str.find(b)];
}
double Calculate::oper(const double a, const char ch, const double b)
{
    double sa(0.0L);
    switch (ch)
    {
    case '+':
        sa = a + b;
        break;
    case '-':
        sa = a - b;
        break;
    case '*':
        sa = a * b;
        break;
    case '/':
        sa = a / b;
        break;
    }
    return sa;
}
std::istream& operator>>(std::istream &is, Calculate &cal)
{
    getline(is, cal.str);
    return is;
}
std::ostream& operator<<(std::ostream &os, const Calculate &cal)
{
    os << cal.str << " = " << cal.ans;
    return os;
}
int main()
{
    Calculate cal;
    std::cin >> cal;
    cal.calculate();
    std::cout << cal.calculate();
    return 0;
}



[此贴子已经被作者于2016-3-18 13:21编辑过]

1