English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

2006-08-19 19:58:35 · 3 answers · asked by Alex Sexy 1 in Computers & Internet Programming & Design

3 answers

What exactly do you need ?

1) Program that converts infix expressions to prefix expressions ?

2) A Driver program that tests an infix prefix converter ?

2006-08-19 20:39:49 · answer #1 · answered by gjmb1960 7 · 0 0

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

2006-08-20 03:37:11 · answer #2 · answered by Shantia 2 · 0 0

try this:

#include
#include
#include
#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<=l;i++)
str[i-1]=out[l-i];
str[i-1]='\0';

infixtoprefix(str,out);

l=strlen(out);
for(i=1;i<=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>47) && (ch<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 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>0);
pos++;
}
while(sp!=0)
{
out[op]=pop();
op++;
}
out[op]='\0';
}

2006-08-20 03:58:00 · answer #3 · answered by IsaacArsenal 3 · 0 0

fedest.com, questions and answers