Jump to content

What's up guys,

I'm trying to write a program that converts from prefix to infix and then infix to prefix. I have started at the infix to prefix conversion


string infixToPrefix(string expression)
{   //FINISH ME!!!!!!!
    stack<char> S; //holds operators
    stack<char>output; //display like in class
    string prefix = "";
    char ch;
    i = 0;
    //remember to read backwards the expression

    reverse(expression.begin(), expression.end()); //should be good 
    while (expression.length != '\0')
    {
        ch = expression[i];
        i++;
        s1[0] = ch;
        s1[1] = '\0';
        if (ch >= 'a' && ch <= 'z')
        {
            s.push(s1);
        }
        else
        {
            s.pop(s2);
            s.pop(s3);
            strcpy(temp, "(");
            strcat(temp, s3);
            strcat(temp, s1);
            strcat(temp, s2);
            strcat(temp, ")");
            s.push(temp);
        }

    }
    return prefix;
}

Rest of the code:


/*
 * main.cpp
 *
 *  Created on: Feb 18, 2014
 *      Author: mdcsaenz
 */

#include <iostream>
#include <stack>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

string infixToPrefix(string expression);
string prefixToInfix(string expression);
int isp(char ch);
int icp(char ch);
bool isOperand(char C);
void menuOption(int &choice);

int main()
{
    string expression;
    string output;
    int choice=-1;
    menuOption(choice);
    while(choice>=1){
        cin.ignore();
        switch(choice){
            //FINISH ME!!!!!!!
        case 1 :
            cout << "Enter your infix expression: " << endl;

            break;
            //Add a switch statment that is for case 1
            //case 1 is enter infix expression (for prefix)
            //get the equtation and put it into expression variable
            //call the infixToPrefix function and save the outcome in the output variable
        case 2 :
            cout << "Enter your prefix expression: " << endl;

            break;
            ///Add a switch statement htat is for case 2
            //case 2 is enter prefix expression (for infix)
            // and follow the same steps like case 1 but for prefixToInfix
        case 0: 
            cout << "you have quit application \n";
        default:
            cout << "please rekey..1 for infix to prefix, 2 for prefix to infix, or 0 to quit : ";

            break;
            //Add a default function that is Not a valide choice try again
            //set choice to -1 again
            //and then call menuOption(choice)
        }
        cout<<"Outcome of Conversion:  "<<output<<endl;
        menuOption(choice);
    }
    return 0;
}


string prefixToInfix(string expression){
 //FINISH ME!!!!!!!

 string infix="";
 stack<string> S;
 string op1,op2,ch;
 
    return infix;
}
string infixToPrefix(string expression)
{   //FINISH ME!!!!!!!
    stack<char> S; //holds operators
    stack<char>output; //display like in class
    string prefix = "";
    char ch;
    i = 0;
    //remember to read backwards the expression

    reverse(expression.begin(), expression.end()); //should be good 
    while (expression.length != '\0')
    {
        ch = prefix[i];
        i++;
        s1[0] = ch;
        s1[1] = '\0';
        if (ch >= 'a' && ch <= 'z')
        {
            s.push(s1);
        }
        else
        {
            s.pop(s2);
            s.pop(s3);
            strcpy(temp, "(");
            strcat(temp, s3);
            strcat(temp, s1);
            strcat(temp, s2);
            strcat(temp, ")");
            s.push(temp);
        }

    }
    return prefix;
}

void menuOption(int &choice){

    cout<<"\n\n===Menu=== \n";
    //FINISH ME!!!!!!!
    cout << " 1. Infix to prefix Conversion \n 2. Prefix to Infix Conversion \n 0 to quit \n";
    //Look at the example output to know how to finish up this menu
    cout<<"Choice: ";
    cin >> choice;
}

bool isOperand(char C)
{
    if(C >= '0' && C <= '9') return true;
    if(C >= 'a' && C <= 'z') return true;
    if(C >= 'A' && C <= 'Z') return true;
    return false;
}

int isp(char ch){
    switch(ch)
    {
        case '+':
        case '-': return 1;
        case '*':
        case '/': return 2;
        case '^': return 3;
        case '(': return 0;
    }
    return -1;
}

int icp(char ch){
    switch(ch)
    {
        case '+':
        case '-': return 1;
        case '*':
        case '/': return 2;
        case '^': return 3;
        case '(': return 4;
    }
    return -1;
}
/*
string reverseString(string equation) // test if string is a valid return type
{
    reverse(equation.begin(), equation.end());
    return equation;
} //https://www.quora.com/How-can-I-reverse-a-string-or-a-sentence-in-C++
*/
Link to comment
https://linustechtips.com/topic/670135-c-converter-help/
Share on other sites

Link to post
Share on other sites

Infix to prefix conversion:

Having troubles with get top and is stack empty

string infixToPrefix(string expression)
{   //FINISH ME!!!!!!!
    stack<char> S; //holds operators
    stack<char>output; //display like in class
    string prefix = "";
    char ch , x;
	int i, j;
	i = expression.length() - 1;
	while (i!= -1)
	{
		ch= infix[i];
		i--;
		if (ch > = 'a' && ch <= 'z')
		{
			prefix[j] = ch;
			j++;
		}
		else
		{
			if (ch == '(')
			{
				while (S.getTop()!= ')')
				{
					x = S.pop();
					prefix[j] = x;
					j++;
				}
				x = S.pop();
			}
			else
			{
				while (isp(S.getTop()) > icp(ch))
				{
					x S.pop();
					prefix[j] = x;
					j++;
				}
				s.push(ch);
			}
		}
	}
	while (!S.isempty())
	{
		x = S.pop();
		if (x!= '#')
			prefix[j] = x;
		j++;
	}
	prefix[j] = '\0';
	reverse(prefix.begin(), prefix.end());
    return prefix;
}

 

Link to comment
https://linustechtips.com/topic/670135-c-converter-help/#findComment-8641702
Share on other sites

Link to post
Share on other sites

 

string infixToPrefix(string expression)
{   //FINISH ME!!!!!!!
    stack<char> S; //holds operators
    stack<char>output; //display like in class
    string prefix = "";
    char ch , x;
	int i, j;
	i = expression.length() - 1;
	while (i!= -1)
	{
		ch= infix[i];
		i--;
		if (ch > = 'a' && ch <= 'z')
		{
			prefix[j] = ch;
			j++;
		}
		else
		{
			if (ch == '(')
			{
				while (S.top()!= ')')
				{
					x = S.pop();
					prefix[j] = x;
					j++;
				}
				x = S.pop();
			}
			else
			{
				while (isp(S.top()) > icp(ch))
				{
					x = S.pop();
					prefix[j] = x;
					j++;
				}
				S.push(ch);
			}
		}
	}
	while (!S.empty())
	{
		x = S.pop();
		if (x!= '#')
			prefix[j] = x;
		j++;
	}
	prefix[j] = '\0';
	reverse(prefix.begin(), prefix.end());
    return prefix;
}

errors with this x = S.pop(); error can assign void to char.

Link to comment
https://linustechtips.com/topic/670135-c-converter-help/#findComment-8642770
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×