Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 20th, 2004, 02:55 AM
Hardeep_chd Hardeep_chd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Pune, India
Posts: 31 Hardeep_chd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 38 sec
Reputation Power: 6
Solving expressions using queue..

Hi,
Can anybody suggest me how to solve a simple expressions using queue instead of stack.

I have found one method.
In this method if we have a expression like (a+b)*(c-d), then we convert it into a tree of form

*
/ \
+ -
/ \ / \
a b c d

then we do a breadth-first traversal of the tree in reverse. The expression becomes dcba-+*.
Now to evaluate the resulting sequence from left to right using a queue in the same way that a postfix expression is evaluated using a stack.

This seems to be a very complex and lengthy method. I need some simple solution.

Thanks in advance..

Reply With Quote
  #2  
Old March 22nd, 2004, 01:26 PM
dog135's Avatar
dog135 dog135 is offline
Doggie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751 dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 7
Here's how I did an RPN interpreter for a language I made a few years back. (called soel)

It's probably not the most elegent way, and the language is very assembly like, but it works. I hope you can see what I'm doing here.

Code:
char *soel::addmath(char *txt){
	long l,a,b,s;
	int c,done,o;
	s=0;
	a=0;
	b=0;
	o=0; // operand
	char *endptr;
	endptr=txt;
	done=0;
	while(!done){
		c=endptr[0];
		if(c == ' '){
			endptr++;
			//continue;
		} else if (c == '('){
			endptr++;
			endptr=addmath(endptr);
			if(o>0){addi(23+o);}
			if(s==0){s=1;}
			else{s=2;}
		} else if (c == ')'){
			endptr++;
			done=1;
		} else if (c == '+'){
			o=1;
			endptr++;
		} else if (c == '-'){
			o=2;
			endptr++;
		} else if (c == '*'){
			o=3;
			endptr++;
		} else if (c == '/'){
			o=4;
			endptr++;
		} else if (c == '%'){
			o=5;
			endptr++;
		} else if (c == '^'){
			o=6;
			endptr++;
		} else if (c == 0){
			done=1;
		} else if(strncmp(endptr,"pi",1)==0){
			addi(11); // "addi" = add instruction
			addf(3.1416);
			if(s==0){s=1;a=31416;}
			else if(s==1){s=2;b=31416;}
			else {s=2;a=b;b=31416;}
			if(s==2 && o>0){addi(23+o);}
			endptr+=2;
		} else if(c>='a' && c<='z'){
			endptr++;
		} else if(c>='A' && c<='Z'){
			endptr++;
		} else {
			l=strtol(endptr,&endptr,10);
			if(s==0){
				addi(11);
				addf(l);
				a=l;
				s=1;
			} else if(s==1){
				addi(11);
				addf(l);
				if(o>0){addi(23+o);}
				b=l;
				s=2;
			} else {
				addi(11);
				addf(l);
				if(o>0){addi(23+o);}
				a=b;
				b=l;
				s=2;
			}
		}
	}
	return endptr;
}
__________________
"Science is constructed of facts as a house is of stones. But a collection of facts is no more a science than a heap of stones is a house." - Henri Poincare

Reply With Quote
  #3  
Old March 23rd, 2004, 04:54 AM
Hardeep_chd Hardeep_chd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Pune, India
Posts: 31 Hardeep_chd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 38 sec
Reputation Power: 6
Thanks for ur reply,
From ur code i got the idea and i have been able to solve the problem using queue and recursive function.

Reply With Quote
  #4  
Old May 6th, 2004, 08:18 AM
bergner bergner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 144 bergner User rank is Private First Class (20 - 50 Reputation Level)bergner User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 48 m 6 sec
Reputation Power: 5
Recursion is equivalent to stack usage

Depends a little on in what way you use recursion, but the most common way to go about this problem using recursion is equivalent to actually using an explicit stack.

Reply With Quote
  #5  
Old May 6th, 2004, 10:58 AM
Hardeep_chd Hardeep_chd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Pune, India
Posts: 31 Hardeep_chd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 38 sec
Reputation Power: 6
Yes you are right, but this stack will be maintained and used by the OS. My requirement was not to use stack in my program. If backend applications supporting my program use the stack then it does not voilate my requirements. For this reason i droped the first idea because to do the breadth first traversal in reverse order we have to use the stack.

If you have some other idea, where we can even do away with recursion then do tell me.

Thanks.

Reply With Quote
  #6  
Old May 26th, 2004, 02:05 AM
Jaspreet Singh Jaspreet Singh is offline
Getting my *facts* right
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Amritsar, India
Posts: 177 Jaspreet Singh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 17 h 40 m
Reputation Power: 5
Thumbs up

Reverse BFS can be obtained directly from the expression without using stack or recursion. just associate a depth variable with each token and initially set depths of all tokens to 0 and scanning the expression from left to right, now if an operator is encountered, scan to the left of it and go on increasing the depths of all the token until a lower priority operator or a ( is encountered, now scan to the right of the operator and go on increasing the depths of all tokens until a ) is encountered or a lower priority operator is encountered. when the algorithm finished, u have the depths of all the tokens as if they were in an expression tree. now just sort the token according to their depths and u have the reverse BFS of the expression.
Eg:
(a+b)*(c-d)
-000-0-000-
when + is read
-101-0-000-
when * is read
-212-0-111-
when - is read
-212-0-212-

sort according to depth, u have
0112222
*+-abcd
reversing
2222011
dcba-+*
__________________
IMHO

Reply With Quote
  #7  
Old May 26th, 2004, 12:27 PM
dog135's Avatar
dog135 dog135 is offline
Doggie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751 dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 7
Quote:
Originally Posted by Jaspreet Singh
sort according to depth, u have
0112222
*+-abcd
reversing
2222011
dcba-+*


Why would you want to reverse it?
*+-abcd
becomes:
Code:
   *
  / \
 +   -
/ \ / \
a b c d

if you just push it drectly into the tree

Last edited by dog135 : May 26th, 2004 at 12:32 PM.

Reply With Quote
  #8  
Old June 4th, 2004, 04:05 AM
Hardeep_chd Hardeep_chd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Pune, India
Posts: 31 Hardeep_chd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 38 sec
Reputation Power: 6
Reversing is necessary.

If we have this expression:
dcba-+*

Then using queue it can be solved.

insert d,c,b,a all in queue.

Next we get -, remove two elements from queue i.e. d and c.
Apply operation -, insert result back to queue i.e. (c-d)

Next we get +, remove two elements from queue i.e. b and a.
Apply operation +, insert result back in quue i.e. (a+b)

Next we get *, remove two elements from queue i.e (a+b) and (c-d), apply operation. You get the final output.

This can not be done with the expression in the form:
*+-abcd

So we have to reverse it..

Reply With Quote
  #9  
Old June 4th, 2004, 04:07 AM
Hardeep_chd Hardeep_chd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Pune, India
Posts: 31 Hardeep_chd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 38 sec
Reputation Power: 6
Jaspreet,
I am looking into the way suggested by u. I will let u know when i implement it..

Thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Solving expressions using queue..


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT