urgent help.

souce code and algorithm needed urgently for the conversion of infix t

Page 1 of 1

8 Replies - 969 Views - Last Post: 10 June 2008 - 12:34 AM Rate Topic: -----

#1 saerah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-May 08

urgent help.

Post icon  Posted 19 May 2008 - 06:59 AM

i need a help in the conversion of infix expression to prefix expression in C or C++.
its a urgent help.i need the algorithm and souce code and i dont have any to send you.so plz kindly help out.
Is This A Good Question/Topic? 0
  • +

Replies To: urgent help.

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: urgent help.

Posted 19 May 2008 - 07:26 AM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,060
  • Joined: 04-January 08

Re: urgent help.

Posted 19 May 2008 - 07:44 AM

lol some peoples kids


expecting other people to do their work for them
Was This Post Helpful? 0
  • +
  • -

#4 saerah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-May 08

Re: urgent help.

Posted 19 May 2008 - 08:50 AM

View Postgabehabe, on 19 May, 2008 - 07:26 AM, said:

[rules][/rules]

SIR I DNT HAVE MUCH TIME THATS WHY ASKED FOR URGENT HELP.THIS IS MY CODE BUT THE OUTPUT IS NOT ACCORDING TO THE GIVEN INPUT.

CODE:

#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

enum Boolean { False, True } ;
const int GREATER = 1 ;
const int NOTGREATER = 0 ;
int cnt = 0 , cntcol = 0 , postcnt = 0 ;

// ---------------------------------------------------
struct Stack
{
int info ;
Stack *prev;
} *top;

// ------------------------------------------------------
void Push ( int no )
{
Stack *cur;
cur = new ( Stack ) ;
cur->info = no ;
cur->prev = top ;
top = cur ;
}

// ------------------------------------------------------
int Pop ()
{
int temp ;
Stack *cur = top;
temp = cur->info ;
top = top->prev ;
delete cur ;
return temp ;
}

// ------------------------------------------------------
int StackTop ()
{
return top->info ;
}

// -------------------------------------------------------
Boolean isEmpty()
{
return ( top == NULL ) ? True : False ;
}

// ------------------------------------------------------
Boolean isOperand ( char ch )
{
return ( ch >= '0' && ch <= '9' ) ? True : False ;
}

//-------------------------------------------------------
void Infix ( char infix[][10])
{
char c ;
while ( c != 13 )
{
c = getch() ;
putch©;
if ( c >= '0' && c <= '9' )
{
infix [cnt][cntcol] = c ;
cntcol++ ;
}
else if ( c == '+'|| c == '-'|| c == '*'|| c == '/')
{
infix [cnt][cntcol] = '\0' ;
cnt++ ;
infix [cnt][0] = c ;
infix [cnt][1] = '\0' ;
cnt++ ;
cntcol = 0 ;
}
else if ( c == 13 )
infix[cnt][cntcol] = '\0' ;
}
}


// ------------------------------------------------------
int Calculate (int op1, char opr, int op2)
{
int ans ;
switch ( opr )
{
case '+' : ans = op1 + op2 ; break ;
case '-' : ans = op1 - op2 ; break ;
case '*' : ans = op1 * op2 ; break ;
case '/' : ans = op1 / op2 ; break ;
case '^' : ans = pow(op1, op2) ;
}
return ans ;
}
// ------------------------------------------------------
int Evaluate ( char postfix[][10] )
{
for ( int i = 0 ; i <= cnt ; i ++ )
{
char ch = postfix[i][0] ;
char ch1[10] ;
strcpy ( ch1 ,postfix [i] ) ;
// cout <<endl<< atoi ( ch1) ;
if ( isOperand ( ch ) == True )
Push ( atoi(ch1) );
else
{
int op2 = Pop () ;
int op1 = Pop () ;
int ans = Calculate ( op1, ch, op2 ) ;
// cout << ans << endl ;
Push ( ans ) ;
}
}
int ans = Pop () ;
return ans ;
}

// ------------------------------------------------------
int CheckP (char cur, char st)
{
int a = NOTGREATER ;

if ( cur == '(' )
a = GREATER ;
else if ( cur != '(' && st == '(' )
a = GREATER ;
else if ( cur != '(' && st != '(' )
{
if ( ( cur == '*' || cur == '/' ) && ( st == '+' || st == '-' ) )
a = GREATER ;
}
return a ;

}


// ------------------------------------------------------
void infixToPostfix ( char infix[][10], char postfix[][10] )
{
int postcol = 0 ;
for ( int i = 0 ; i <= cnt ; i ++ )
{
// strcpy(postfix[i], infix [i]) ;
// cout << postfix[i] ;
char ch;
ch = infix[i][0] ;
if ( isOperand ( ch ) == True )
strcpy ( postfix [postcnt++] , infix [i] ) ;
else
{
if ( isEmpty() == True )
Push ( int(ch) ) ;
else
{
while ( CheckP( ch, StackTop() ) != GREATER && isEmpty() != True )
{
char c ;
if ( ch == ')' )
{
do
{
c = char ( Pop() ) ;
if ( c != '(')
{
postfix [postcnt][postcol] = c ;
postfix [postcnt][postcol+1] = '\0' ;
postcnt++ ;
}
}while ( c != '(' && isEmpty() != True);
break ;
}
else
c = char ( Pop() ) ;
if ( c != '(' && c != ')' )
{
postfix [postcnt][postcol] = c ;
postfix [postcnt][postcol+1] = '\0' ;
postcnt++ ;
}
}
if ( ch != ')' )
Push ( int(ch) ) ;
}
}
}

while ( isEmpty () != True )
{
char c = char ( Pop () ) ;
postfix [postcnt][postcol] = c ;
postfix [postcnt][postcol+1] = '\0';
postcnt++ ;
}
// postfix[postcnt][postcol] = '\0';
}

//-------------------------------------------------------
void Postfix ( char postfix[][10] )
{
char ch[10] ;
for ( int i = 0; i <= postcnt; i++ )
{
strcpy(ch , postfix[i] ) ;
cout << ch ;
}
}

// ------------------------------------------------------
void main()
{
clrscr();
top = NULL ;

char infix[50][10] ;
char postfix[50][10] ;

gotoxy ( 5, 8 ) ;
cout << "Enter Arithematic Expression ...................... : ";
Infix ( infix ) ;

infixToPostfix ( infix, postfix ) ;
gotoxy ( 5, 10 ) ;
cout << "Arithematic Expression in Postfix Notation......... : ";
Postfix ( postfix ) ;
int ans = Evaluate ( postfix ) ;
gotoxy (5, 11 ) ;
cout << "The Answer of Expression .......................... : " << ans ;

getch();
}
// ------------------
Was This Post Helpful? 0
  • +
  • -

#5 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,060
  • Joined: 04-January 08

Re: urgent help.

Posted 19 May 2008 - 10:00 AM

what some people don't seem to understand about forums and these types of communities, is that when you say "URGENT HELP" or you title your thread something like that most of the time you are not going to get an immediate response.

Most of the people here have jobs and come here to help people in their spare time. So it is going to take sometime for someone to get back to you (in most cases).

Just for future reference try to take the time to give us a good explanation, post your code and any errors you are encountering.

If you need urgent, absolute immediate help, Skyhawk suggested in another thread trying the Live Help. You have to pay for it but it is live. I am suggesting the same thing to you for future reference.

This post has been edited by jjsaw5: 19 May 2008 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#6 saerah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-May 08

Re: urgent help.

Posted 19 May 2008 - 12:59 PM

[/b]thanks alot jjsaw.but i think you are here from a long time i just joined today and dont know the details.i will be carefull next time.



View Postjjsaw5, on 19 May, 2008 - 10:00 AM, said:

what some people don't seem to understand about forums and these types of communities, is that when you say "URGENT HELP" or you title your thread something like that most of the time you are not going to get an immediate response.

Most of the people here have jobs and come here to help people in their spare time. So it is going to take sometime for someone to get back to you (in most cases).

Just for future reference try to take the time to give us a good explanation, post your code and any errors you are encountering.

If you need urgent, absolute immediate help, Skyhawk suggested in another thread trying the Live Help. You have to pay for it but it is live. I am suggesting the same thing to you for future reference.

Was This Post Helpful? 0
  • +
  • -

#7 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,060
  • Joined: 04-January 08

Re: urgent help.

Posted 19 May 2008 - 01:20 PM

View Postsaerah, on 19 May, 2008 - 02:59 PM, said:

[/b]thanks alot jjsaw.but i think you are here from a long time i just joined today and dont know the details.i will be carefull next time.



View Postjjsaw5, on 19 May, 2008 - 10:00 AM, said:

what some people don't seem to understand about forums and these types of communities, is that when you say "URGENT HELP" or you title your thread something like that most of the time you are not going to get an immediate response.

Most of the people here have jobs and come here to help people in their spare time. So it is going to take sometime for someone to get back to you (in most cases).

Just for future reference try to take the time to give us a good explanation, post your code and any errors you are encountering.

If you need urgent, absolute immediate help, Skyhawk suggested in another thread trying the Live Help. You have to pay for it but it is live. I am suggesting the same thing to you for future reference.




No problem. I see you just joined so its don't worry about it. Just something to keep in mind for future posts
Was This Post Helpful? 0
  • +
  • -

#8 mana2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-June 08

Re: urgent help.

Posted 09 June 2008 - 11:09 PM

i really confuse about my program validation because the program can't give an output on the second validation.

/* PROGRAM TO CONVERT INFIX Expression TO POSTFIX Expression USING STACK */
# include <stdio.h>
# include <string.h>
# include <conio2.h>
# include "menu2.h"

/* MACRO FUNCTION TO CHECK WHETHER GIVEN CHARACTER IS AN OPERAND OR NOT */
# define operand(x) (x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z' || x >= '0' && x <= '9')
char infix[30], postfix[30], stack[30];
int top, i = 0;

/* FUNCTION TO INITIALIZE THE STACK */
void init()
{
	top = -1;
}

/* FUNCTION TO PUSH AN OPERATOR ON TO THE STACK */
void push(char x)
{
	stack[++top] = x;
}

/* FUNCTION TO POP A CHARACTER STORED ONTO THE STACK */
char pop()
{
	return(stack[top--]);
}

/* FUNCTION TO RETURN IN STACK PRIORITY OF A CHARACTER */
int isp(char x)
{
	int y;
	
	y = (x == '(' ? 0 : x == '^' ? 4 : x == '*' ? 2 : x == '/'
		  ? 2 : x == '+' ? 1 : x == '-' ? 1 : x == ')' ? 6 : -1);
	return y;
}

/* FUNCTION TO RETURN INCOMING CHARACTER'S PRIORITY */
int icp(char x)
{
	int y;
	
	y = (x == '(' ?4 : x == '^' ? 4 : x == '*' ? 2 : x == '/' 
		  ? 2 : x == '+' ? 1 : x == '-' ? 1 : x == ')' ? 6 : -1);
	return y;
}

/* FUNCTION TO CONVERT THE GIVEN INFIX TO PREFIX Expression */
void infixtopostfix()
{
	int j, l = 0, space;
	char x, y;
	stack[++top] = '\0';
//	printf("a");
	for (j = 0; (x = infix[i++]) != '\0'; j--)
	{
			if (operand(x))
				postfix[l++] = x;
			  else
				 if (x == ')' )
					while ((y = pop()) != '(')
						postfix[l++] = y;
			   else
					{
						while (isp(stack[top]) >= icp(x))
							postfix[l++] = pop();
						push(x);
					}
	 }
	while (top >= 0)
		postfix[l++] = pop();

}

/* MAIN PROGRAM */
int main()
{
  int pilih;
  Menu menu;
  char plh, x;
  
  init();
  gotoxy(30, 1); printf("Expression CONVERTER\n");
	gotoxy(31, 2); printf("(Infix to Postfix)\n\n");
  
  do {
	  
  menu = menu_create();
  menu_posisi(&menu, 3, 5);
  menu_add_judul(&menu, " MENU UTAMA   ");
  menu_add_item(&menu,  "1. INFIX TO POSTFIX ");
  //menu_add_item(&menu,  "2. Inquiry   ");
  //menu_add_item(&menu,  "3. Laporan   ");
  //menu_add_item(&menu,  "0. Selesai   ");  

  do 
  { 
	plh = menu_pilih(&menu);
  }while (plh < '0' ||  plh > '1');
  
  switch (plh){
		 case '1' : //do {
					   clrscr();
					  do{ 
					   printf("Enter an infix expression : ");
					   fflush(stdin);
					   //infix[0] = '\0';
					   scanf("%s",infix);
					   //if(infix[0] != operand(x));
					   //printf("a");
					   //printf("%s",infix);
					  //if (infix[0] == '\0') break;
					 //printf ("a");
					 infixtopostfix();//printf ("a");
					 printf("The resulting postfix expression is %s\n",postfix);
					 //printf ("a");
					 }while(1);
					 getch();
					 //clrscr();
					 //}while (1);
					 break;
		 //case'2' :
		 }
  
  
  
  
  }while (1);
  fflush(stdin); getchar();
  return 0;
} // End of main



please help me to make the validation of input and postfix evaluation.
i need it ASAP
thx b4
Was This Post Helpful? 0
  • +
  • -

#9 saerah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-May 08

Re: urgent help.

Posted 10 June 2008 - 12:34 AM

View Postmana2, on 9 Jun, 2008 - 11:09 PM, said:

hey mana2 i would surely like to hep u up but due to time im sorry.i have a code for the conversion u can hav a look at it.

SOURCE CODE OF INFIX TO POSTFIX
/*****Infix to postfix convertor and evaluator
****************************************************/
# include <stdio.h>
# include <string.h>
#include<dos.h>

/************Block for in2post conversion*******************/
float stack[50],len;
char post[100],t[10];

int icp(char a)
{
switch(a)
{
case '(':return 3;
case '*':return 2;
case '/':return 2;
case '+':return 1;
case '-':return 1;
default :return 0;
}
//Unreachable portion of code
}
int isp(char a)
{
switch(a)
{
case '(':return 0;
case '*':return 2;
case '/':return 2;
case '+':return 1;
case '-':return 1;
case '$':return -1;
default:return -1;
}
//Unreachable portion of the code
}

void initstack()
{
stack[0]='$';
len=1;
}
void push(float a)
{
stack[len]=a;
len++;
}
void pop()
{
if(stack[len-1]!=')'&& stack[len-1]!='(')
{
sprintf(t,"%c",(char)stack[len-1]);
strcat(post,t);
}
len--;
}
char stackop()
{
return stack[len-1];
}
void emptystack()
{
while(len>1)
pop();
}
int isop(char a)
{
if(a=='*'||a=='+'||a=='/'||a=='-'||a=='(')
return 1;
if(a==')')return 1;
return 0;
}


//Converts infix to postfix and stores the result in post(global variable)


void in2post(char *expr)
{
int i=0;
initstack();
post[0]='\0';
while(i<strlen(expr))
{
while(expr[i]==' ')i++;
if(isop(expr[i])==1)
{
if(expr[i]==')')
{
while(stackop()!='(')pop();
pop();
}
else if(icp(expr[i])>isp(stackop()))
{
push(expr[i]);
}
else
{
pop();
push(expr[i]);
}
}
else
{
sprintf(t,"%c",expr[i]);
strcat(post,t);
}
i++;
}
emptystack();
}

/************Block for evaluation**********************/

float popper()
{
len--;
return stack[len];
}
float calcval(char a)
{
switch(a)
{
case '+':return popper()+popper();
case '*':return popper()*popper();
case '-':return (-1)*(popper()-popper());
case '/':return 1/(popper()/popper());
}
return -1;
}
float evaluate(char *p)
{
int i=0;
float temp;
initstack();
while(i<strlen(p))
{
if(isop(p[i])==1)
{
temp=calcval(p[i]);
push(temp);
}
else
{
printf("\nEnter value for '%c':",post[i]);
scanf("%f",&temp);
push(temp);
}
i++;
}
if(len!=2)
printf("\nError...");
return stack[1];
}
/********End of all functions only main remaining*************/
void intro()
{ printf("\n\n\t\t********************************************\n\n");
printf("\n\t\tInfix to postfix convertor and evaluator ");
printf("\n\n\t\t********************************************\n\n");
printf("\n\n");
}

void main()
{
char in[100],a;
clrscr();
intro();
printf("\nEnter infix expression:");
gets(in);
in2post(in);
printf("\nEquivalent Postfix expression:%s",post);
printf("\n\n");


printf("do you want to evaluate y/n:");
scanf("%s",&a);
if(a=='y')
{
printf("\nThe result is %f",evaluate(post));
}
getch();
}


u can see n take help from here.all the best.



i really confuse about my program validation because the program can't give an output on the second validation.

/* PROGRAM TO CONVERT INFIX Expression TO POSTFIX Expression USING STACK */
# include <stdio.h>
# include <string.h>
# include <conio2.h>
# include "menu2.h"

/* MACRO FUNCTION TO CHECK WHETHER GIVEN CHARACTER IS AN OPERAND OR NOT */
# define operand(x) (x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z' || x >= '0' && x <= '9')
char infix[30], postfix[30], stack[30];
int top, i = 0;

/* FUNCTION TO INITIALIZE THE STACK */
void init()
{
	top = -1;
}

/* FUNCTION TO PUSH AN OPERATOR ON TO THE STACK */
void push(char x)
{
	stack[++top] = x;
}

/* FUNCTION TO POP A CHARACTER STORED ONTO THE STACK */
char pop()
{
	return(stack[top--]);
}

/* FUNCTION TO RETURN IN STACK PRIORITY OF A CHARACTER */
int isp(char x)
{
	int y;
	
	y = (x == '(' ? 0 : x == '^' ? 4 : x == '*' ? 2 : x == '/'
		  ? 2 : x == '+' ? 1 : x == '-' ? 1 : x == ')' ? 6 : -1);
	return y;
}

/* FUNCTION TO RETURN INCOMING CHARACTER'S PRIORITY */
int icp(char x)
{
	int y;
	
	y = (x == '(' ?4 : x == '^' ? 4 : x == '*' ? 2 : x == '/' 
		  ? 2 : x == '+' ? 1 : x == '-' ? 1 : x == ')' ? 6 : -1);
	return y;
}

/* FUNCTION TO CONVERT THE GIVEN INFIX TO PREFIX Expression */
void infixtopostfix()
{
	int j, l = 0, space;
	char x, y;
	stack[++top] = '\0';
//	printf("a");
	for (j = 0; (x = infix[i++]) != '\0'; j--)
	{
			if (operand(x))
				postfix[l++] = x;
			  else
				 if (x == ')' )
					while ((y = pop()) != '(')
						postfix[l++] = y;
			   else
					{
						while (isp(stack[top]) >= icp(x))
							postfix[l++] = pop();
						push(x);
					}
	 }
	while (top >= 0)
		postfix[l++] = pop();

}

/* MAIN PROGRAM */
int main()
{
  int pilih;
  Menu menu;
  char plh, x;
  
  init();
  gotoxy(30, 1); printf("Expression CONVERTER\n");
	gotoxy(31, 2); printf("(Infix to Postfix)\n\n");
  
  do {
	  
  menu = menu_create();
  menu_posisi(&menu, 3, 5);
  menu_add_judul(&menu, " MENU UTAMA   ");
  menu_add_item(&menu,  "1. INFIX TO POSTFIX ");
  //menu_add_item(&menu,  "2. Inquiry   ");
  //menu_add_item(&menu,  "3. Laporan   ");
  //menu_add_item(&menu,  "0. Selesai   ");  

  do 
  { 
	plh = menu_pilih(&menu);
  }while (plh < '0' ||  plh > '1');
  
  switch (plh){
		 case '1' : //do {
					   clrscr();
					  do{ 
					   printf("Enter an infix expression : ");
					   fflush(stdin);
					   //infix[0] = '\0';
					   scanf("%s",infix);
					   //if(infix[0] != operand(x));
					   //printf("a");
					   //printf("%s",infix);
					  //if (infix[0] == '\0') break;
					 //printf ("a");
					 infixtopostfix();//printf ("a");
					 printf("The resulting postfix expression is %s\n",postfix);
					 //printf ("a");
					 }while(1);
					 getch();
					 //clrscr();
					 //}while (1);
					 break;
		 //case'2' :
		 }
  
  
  
  
  }while (1);
  fflush(stdin); getchar();
  return 0;
} // End of main



please help me to make the validation of input and postfix evaluation.
i need it ASAP
thx b4

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1