Tuesday, July 14, 2009

I need help in C++ data structure ?

who can give me the code for this program or can give me some hints and help me with the idea to write it ?





Write a driver to test your class templates and algorithms. If the infix arithmetic expression





(66 + 38 - 93) * 5 / 6 - 88 % 6





is given, the postfix version of the preceding infix expression





66 38 + 93 - 5 6 */ 88 6 % -





will be created and then evaluated. Thus the result is = 5

I need help in C++ data structure ?
What exactly do you need ?





1) Program that converts infix expressions to prefix expressions ?





2) A Driver program that tests an infix prefix converter ?
Reply:Oh that programs in so nice.





If you want to Shoot the star you must be specific on these





Be sure that your input will get you the thing you want I mean you must filter your sentences you must use a stack for implementing this program , you must check your sentence character by charachter and put it in the right stack with focusing to the mathematic order of each character (we have table for it).


when you put it right in the stack everything is fine and you can easily change it to prefix or post fix form
Reply:try this:





#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;string.h%26gt;


#define MAX 99


void infixtoprefix(char* st,char* out);


char stack[MAX],str[MAX],out[MAX];


int sp;





void main() {


int i,l;


sp=0;


clrscr();





do {


printf("\n\nEnter a Experssion(enter to exit): ");


gets(out);





l=strlen(out);


for(i=1;i%26lt;=l;i++)


str[i-1]=out[l-i];


str[i-1]='\0';





infixtoprefix(str,out);





l=strlen(out);


for(i=1;i%26lt;=l;i++)


str[i-1]=out[l-i];


str[i-1]='\0';





printf("\n%s",str);


} while(strlen(str)!=0);


}





int Operator(char ch)


{


if (ch=='+' || ch=='-' || ch=='*' || ch=='/')


return 1;


return 0;


}





int Operand(char ch)


{


if ((ch%26gt;47) %26amp;%26amp; (ch%26lt;58))


return 1;


return 0;


}





char pop()


{


char ch;


sp--;


ch=stack[sp];


stack[sp]='\0';


return ch;


}





void push(char ch)


{


stack[sp]=ch;


sp++;


}





int Most(char a, char b)


{


if ( a=='(' || a==')' ) return 1;


if ( a=='*' || a=='/')


if (b=='*' || b=='/' || b=='+' || b=='-') return 1;


if (a=='+' || a=='-')


if (b=='+' || b=='-') return 1;


return 0;


}








void infixtoprefix(char* st,char* out)


{


char ch,temp;


int i,l,pos,op;


l=strlen(st);


pos=0;


op=0;


while (pos%26lt;l) {


ch=st[pos];


if (Operand(ch)) { out[op]=ch; op++; }


if (ch==')') push(ch);


if (Operator(ch))


{


if (sp==0 || stack[0]==')' || Most(ch,stack[0])) push(ch);


else


{


out[op]=pop();


if (out[op]!=')') op++;


push(ch);


}


}


if (ch=='(') do


{


ch=pop();


if (ch==')') break;


out[op]=ch;


op++;


}while(sp%26gt;0);


pos++;


}


while(sp!=0)


{


out[op]=pop();


op++;


}


out[op]='\0';


}

wisteria

No comments:

Post a Comment