【问题标题】:Infix to postfix calculator/ reading input后缀计算器/阅读输入的中缀
【发布时间】:2014-10-05 03:42:22
【问题描述】:

我需要使用 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;
}

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

谢谢

【问题讨论】:

  • 你必须解析你的输入。阅读字符串,找出其中的内容,然后从那里开始。如果您输入的元素总是用空格分隔,就像在您的示例中一样,那么您可以根据它拆分字符串。
  • 我不知道该怎么做..你能给我更多的提示吗?
  • 当然,我建议买一本好的 C++ 教科书,这是非常基本的东西,所以肯定会在其中介绍。这是一个很好的起点:stackoverflow.com/questions/388242/…

标签: c++ stack queue calculator


【解决方案1】:

使用我写的类工具

工具.h

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

工具.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;

【讨论】:

    猜你喜欢
    • 2012-04-05
    • 2012-08-29
    • 2018-03-20
    • 1970-01-01
    • 2012-04-11
    • 2016-07-10
    • 2012-06-26
    • 1970-01-01
    • 2014-01-08
    相关资源
    最近更新 更多