中缀到后缀计算器/读取输入

时间:2014-10-05 03:42:23

标签: c++ stack queue calculator

我需要使用C ++中的堆栈和队列来实现一个使用后缀计算器的中缀。 我知道要遵循什么算法,但我在启动时遇到了问题。我的程序应如下所示:

Enter a valid infix expression: 3 + 4 * 5 / 6
The resulting postfi expression is: 3 4 5 * / 6 +
The result is: 6.3333

所以我需要从命令行读取输入,以及我遇到问题的地方。 到目前为止,这是我的代码:

using namespace std;
#include <iostream>
#include <stdlib.h>
#include <stack>
#include <queue>

int main() {

  stack <string> convert;
  stack <string> evaluate;
  queue <string> store;

  string data;
  float num;

  cout << "Enter a valid infix expression: ";
  while (getline(cin, data)) {
    store.push(data);
  }
return 0;
}

我的问题是如何停止循环以及如何从字符串输入中获取数字以便我可以将它们推入队列以便稍后打印?我的代码将整个字符串推入队列中的第一个插槽。 希望有人可以提供帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

使用我写的类工具

的Tools.h

static class Tools
{
   public:
       static char* toPostfix(char* source);
       static double calculatePostfix(char* source);
       static bool contain(char* source,char character);
 };

Tools.cpp

#include "Tools.h"
#include <stack>
#include <iostream>
#include <string>

using namespace std;

char* Tools::toPostfix(char* source)
{
    stack<char> symbol;
    string postfix = "";
    int i = 0;
    char variables[] = { "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" };
    bool find = false;

    while (*source != '\0')
    {
        switch (*source)
        {
        case '+':
        case '-':
        case '*':
        case '/':
            symbol.push(*source); 
            break;
        case ')':
            postfix += symbol.top();
            symbol.pop();
        default:
            find = Tools::contain(variables, *source);
            if (find)
            {
                 postfix += *source;
                 find = false;
             }

        }
        source++;
    }
    // attach other operator in stack(Pop All)
    while (!symbol.empty())
    {
         postfix += symbol.top();
         symbol.pop();
     }

     char* p = new char(postfix.length());
     const char* o = postfix.c_str();
     for (int i = 0; i < postfix.length(); i++)
         p[i] = o[i];
     return p;
}

double Tools::calculatePostfix(char* source)
{
    char numbers[] = { "0123456789" };
    stack<double> number;
    for (int i = 0; i < strlen(source); i++)
    {
        char character = source[i];
        if (Tools::contain(numbers, character))
        {
            number.push(atof(&character));
        }
        else
        {
            double number1, number2;
            switch (character)
            {
            case '+':
                number2 = number.top();
                number.pop();
                number1 = number.top();
                number.pop();
                number.push(number1 + number2);
                break;
            case '-':
                number2 = number.top();
                number.pop();
                number1 = number.top();
                number.pop();
                number.push(number1 - number2);
                break;
            case '*':
                number2 = number.top();
                number.pop();
                number1 = number.top();
                number.pop();
                number.push(number1 * number2);
                break;
            case '/':
                number2 = number.top();
                number.pop();
                number1 = number.top();
                number.pop();
                number.push(number1 / number2);
                break;
            }
        }
    }
    return number.top();
}

bool Tools::contain(char* source, char character)
{
    int size = strlen(source);
    for (int i = 0; i < size ; i++)
    {
        if (source[i] == character)
            return true;
    }
    return false;
}

用法:

 std::cout << "postFix : " << Tools::toPostfix("a+(b*c+t)") << std::endl;
 std::cout << "Answer : " << Tools::calculatePostfix("24*95+-") << std::endl;