6 Replies - 17616 Views - Last Post: 01 April 2011 - 06:12 AM Rate Topic: -----

#1 badbodom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 18-July 09

Infix to Prefix program

Posted 01 April 2011 - 05:37 AM

Hello, I'm having trouble with this code I'm writing. The objective is to write a program to convert a user input equation from infix to prefix.

I've done the entire program but the output is displaying in reverse i.e: when the user inputs for example : a*b+c the outcome should be abc*+ instead its displaying +*abc ! I've been trying to find what I'm doing wrong or why my program is displaying things so weirdly but I couldn't figure it out, what do you guys think? here's the code:

#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include <string.h>
#define MAX 20

using namespace std;

char stack[MAX];
int top = -1;

//Push Function
void push(char item){
 top++;
 stack[top]=item;
}

//Pop Function
char pop(){
 char a;
 a=stack[top];
 top--;
  return a;
}


//Function to analyze the precidence of operators
int prcd(char symbol){
 switch(symbol) {
	case '+':
	case '-':
	 return 2;
	case '*':
	case '/':
	 return 4;
	case '(':
	case ')':
	case '#': 
	 return 1;
 }
}

//Function to sort operators from other data
int isoperator(char symbol){
 switch(symbol) {
	case '+':
	case '-':
	case '*':
	case '/':
	case '(':
	case ')':
	 return 1;
	default:
	 return 0;
 }
}

//Function to invert infix to prefix
void convertip(char infix[],char prefix[]){
int i,symbol,j=0;
char test[MAX]; 

infix=strrev(infix); //Used 'strrev' to reverse string
stack[++top]='#';

for(i=0;i<strlen(infix);i++){
 symbol=infix[i];
  if(isoperator(symbol)==0){
   prefix[j]=symbol;
   j++;
  }
  else{
   if(symbol==')'){
    push(symbol);
   }
   else if(symbol=='('){
    while(stack[top]!=')'){
     prefix[j]=pop();
     j++;
    }
    pop();//pop out (.
   }
   else{
    if(prcd(symbol)>prcd(stack[top])) {
    push(symbol);
   }
   else{
    while(prcd(symbol)<=prcd(stack[top])) {
     prefix[j]=pop();
     j++;
    }
    push(symbol);
   }//end of else.
  }//end of else.
 }//end of else.
}//end of for.

while(stack[top]!='#'){
 prefix[j]=pop();
 j++;
}

prefix[j]='\0';//null terminate string.
prefix=strrev(prefix);
}




int main() {
char infix[20],prefix[20];

cout << "Enter the valid infix string: " << endl;
gets(infix);
convertip(infix,prefix);
cout << "The corresponding prefix string is: " << endl;
puts(prefix);

getch();
return 0;c

}




Is This A Good Question/Topic? 0
  • +

Replies To: Infix to Prefix program

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Infix to Prefix program

Posted 01 April 2011 - 05:47 AM

Well, you would be glad to know that your program shows right output. That is what the right prefix notation is :)

This post has been edited by chinchang: 01 April 2011 - 05:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 badbodom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 18-July 09

Re: Infix to Prefix program

Posted 01 April 2011 - 05:56 AM

View Postchinchang, on 01 April 2011 - 06:47 AM, said:

Well, you would be glad to know that your program shows right output. That is what the right prefix notation is :)



Are you sure?

A*b+c should show abc*+ not +*abc no ?

At least that's what my professor said..
Was This Post Helpful? 0
  • +
  • -

#4 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Infix to Prefix program

Posted 01 April 2011 - 05:59 AM

Prefix notation is when operators come before operands. So +*abc is right.

This post has been edited by chinchang: 01 April 2011 - 06:00 AM

Was This Post Helpful? 0
  • +
  • -

#5 badbodom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 18-July 09

Re: Infix to Prefix program

Posted 01 April 2011 - 06:03 AM

View Postchinchang, on 01 April 2011 - 06:59 AM, said:

Prefix notation is when operators come before operands. So +*abc is right.


Aha, and the postfix notation is when the operators come after? How do I change my prefix function to postfix?

This post has been edited by badbodom: 01 April 2011 - 06:04 AM

Was This Post Helpful? 0
  • +
  • -

#6 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Infix to Prefix program

Posted 01 April 2011 - 06:08 AM

Is the code written by you ? The code has comments saying : Convert infix to prefix. You wrote the complete program without knowing what prefix and postfix is ?
Was This Post Helpful? 0
  • +
  • -

#7 badbodom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 18-July 09

Re: Infix to Prefix program

Posted 01 April 2011 - 06:12 AM

View Postchinchang, on 01 April 2011 - 07:08 AM, said:

Is the code written by you ? The code has comments saying : Convert infix to prefix. You wrote the complete program without knowing what prefix and postfix is ?


Yes mate I did write the entire code and yes I know what prefix and postfix mean, its just that I'm trying to understand what the professor meant by saying (abc+*) is the correct form, and if so I have to change an entire segment of the code,I'm just asking if there's an easy way for it without me having to change half of the code along with it...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1