ANYWAYS....
This program is where we will take in a file that has approx. 3 lines in the form of post fix, that contain variables and we have to solve for the multiple variables. I do not know how to solve for multiple variables in a program at once so I'm stumpped!! Here are the basic rules and the sample input/output:
All valid statements in PostfixA are of the form:
= <var> <postfix expr>
and must appear on a single line. (Also, each line of a PostfixA program MUST contain exactly ONE statement.)
The first token of a valid PostfixA statement must be an equal sign and the second must be a variable. Here are the rules for valid variables:
1) Must start with a uppercase letter, but NOT the letter L.
2) Can only contain uppercase letters.
3) Must be in between one and eight characters long, inclusive.
4) At most 5 distinct variables can appear in a PostfixA program.
INPUT:
= X L3 L4 L5 * -
= Y L12 W X + -
= Z Y Z * L3 +
OUTPUT:
X -17
W 0
Y 29
Z 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So I have the basic program that will solve a postfix typed in but,...
1) I can solve the first equation, but somewhere in the last 2 lines I have mistaken my understanding. I find X = -17, then Y = (29-W) but dont understand how to figure that W = 0 or Z = 3.
then 2) I don't know how to solve multiple variables in multiple lines.
If anyone can help with this I'd REALLY appreciate it. I don't know if you need it, but here is my code.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <stdio.h>
#include <ctype.h>
#define MAX 50
#define EMPTY -1
struct stack
{
int data[MAX];
int top;
};
void emptystack(struct stack* s);
void push(struct stack* s,int item);
int pop(struct stack* s);
void display(struct stack s);
int evaluate(char *postfix);
int main()
{
char exp[MAX];
printf("Enter Postfix Expression : ");
gets(exp);
printf("%s EQUALS %d\n",exp,evaluate(&exp[0]));
system("pause");
return 0;
}
void emptystack(struct stack* s)
{
s->top = EMPTY;
}
void push(struct stack* s,int item)
{
if(s->top == (MAX-1))
{
printf("\nSTACK FULL");
}
else
{
++s->top;
s->data[s->top]=item;
}
}
int pop(struct stack* s)
{
int ret=EMPTY;
if(s->top == EMPTY)
printf("\nSTACK EMPTY");
else
{
ret= s->data[s->top];
--s->top;
}
return ret;
}
void display(struct stack s)
{
while(s.top != EMPTY)
{
printf("\n%d",s.data[s.top]);
s.top--;
}
}
int evaluate(char *postfix)
{
char *p;
struct stack stk;
int op1,op2,result;
emptystack(&stk);
p = &postfix[0];
while(*p != '\0')
{
/* removes tabs and spaces */
while(*p == ' ' || *p == '\t' || *p == 'L')
{
p++;
}
/* if is digit */
if(isdigit(*p))
{
push(&stk,*p - 48);
}
else
{
/* it is an operator */
op1 = pop(&stk);
op2 = pop(&stk);
switch(*p)
{
case '+':
result = op2 + op1;
break;
case '-':
result = op2 - op1;
break;
case '/':
result = op2 / op1;
break;
case '*':
result = op2 * op1;
break;
case '%':
result = op2 % op1;
break;
default:
printf("\nInvalid Operator");
return 0;
}
push(&stk,result);
}
p++;
}
result = pop(&stk);
return result;
}

New Topic/Question
Reply




MultiQuote




|