-1

I need to implement an infix to postfix calculator using stacks and queues in C++. I know what algorithm to follow but I'm having trouble starting. My program should go like the following:

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

So I need to read input from the command line and that's where I'm having the problem. This is my code so far:

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

My problem here is how do I stop the loop and how can I get the numbers from the string input so I can push them into the queue to print them later on? My code is pushing the entire string into the the first slot in the queue. Hope someone can help.

Thank you

Amanda Slayer
  • 25
  • 2
  • 8
  • You have to parse your input. Read the string, find out what's in it, and go from there. If the elements of your input are always separated by whitespace, as they are in your example, then you can split the string based on that. – Crowman Oct 05 '14 at 04:02
  • I don't know how to do that though..can you give me some more hints? – Amanda Slayer Oct 05 '14 at 18:43
  • Sure, I'd suggest getting a good C++ textbook, this is pretty basic stuff so it would be covered in there for sure. Here's a good place to start: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Crowman Oct 05 '14 at 18:46

1 Answers1

0

use class Tools written by me

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

usage :

 std::cout << "postFix : " << Tools::toPostfix("a+(b*c+t)") << std::endl;
 std::cout << "Answer : " << Tools::calculatePostfix("24*95+-") << std::endl;
Fadakar
  • 509
  • 1
  • 5
  • 21