Solving for a Variable in Postfix

Evaluate postfix, solve for a Variable

Page 1 of 1

3 Replies - 4529 Views - Last Post: 30 June 2008 - 11:10 AM Rate Topic: -----

#1 MeKey01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-June 08

Solving for a Variable in Postfix

Posted 29 June 2008 - 07:01 PM

Hi, I'm new to this! As for a lil about my programming exprience I've only taken C Programming and am now enrolled in Comp. Sci I. I cannot beleive the giant gap in what is expected from my previous course to this one, and I did well in C Programming.

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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Solving for a Variable in Postfix

#2 joske  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: Solving for a Variable in Postfix

Posted 30 June 2008 - 06:10 AM

To solve a set of equations with multiple variables, you should write down the problem as a system of linear equations:
http://en.wikipedia....inear_equations

then you can solve the equations for example by using row reduction (Gaussian elimination).

I'm not sure if the equations that you are dealing with are all linear though.

This post has been edited by joske: 30 June 2008 - 06:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 MeKey01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-June 08

Re: Solving for a Variable in Postfix

Posted 30 June 2008 - 10:20 AM

View Postjoske, on 30 Jun, 2008 - 06:10 AM, said:

To solve a set of equations with multiple variables, you should write down the problem as a system of linear equations:
http://en.wikipedia....inear_equations

then you can solve the equations for example by using row reduction (Gaussian elimination).

I'm not sure if the equations that you are dealing with are all linear though.




I know that I can solve with a system of equations in regular math speak, but I have NO IDEA how to attempt to do something like that in a program. That is where the problem is. I can plug in numbers and simplify to an answer, but beyond that.... idk. Not to mention that this is a set of 3 equations and 4 unknowns.
Was This Post Helpful? 0
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Solving for a Variable in Postfix

Posted 30 June 2008 - 11:10 AM

Refer to my tutorial : Converting and Evaluating Infix, Postfix and Prefix Expressions in C and you should have your answer.

Post back if you are still facing difficulties.
:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1