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 November 6th, 2004, 06:46 PM
harith harith is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 33 harith User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 40 m 57 sec
Reputation Power: 5
a algorithm to validate an arithmetic expression

can anyone help me with this problem, any help will be appreciated. Basically I need to write an algorithm to validate an arithmetic expression such as 7 * ( 23 - 105 / 15 ) - 19.

The validate Method
Examination of typical (valid) arithmetic expressions shows that

if there are parentheses then they must appear in pairs, as ( ... ), and ( must be immediately before a number and ) must be immediately after a number,

apart from parentheses a valid arithmetic expression is a sequence of numbers with one operator symbol between successive numbers.

A 2-state FSA (Finite State Automaton) is the obvious device to use.

The process is in state 0 when a number or ( be expected i.e. at the start of an expression or following an operator or following (.

The process is in state 1 when an operator or ) could be accepted e.g. following a number

The FSA should be in state 1 at the end of a valid expression
(see attachment).

If u want more explaination please ask. I'm new to algorithms so I'm having difficulty gettin my head around them.

Thanks
Attached Images
File Type: jpg untitled.jpg (14.8 KB, 280 views)

Reply With Quote
  #2  
Old November 7th, 2004, 01:25 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,475 Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level)Lux Perpetua User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 3 Weeks 6 Days 19 m 53 sec
Reputation Power: 377
An FSA won't cut it because you can nest parentheses. In fact, there's no FSA that accepts all of the following (which are valid):

(1+1)
((1+1))
(((1+1)))
((((1+1))))
...
and only accepts well-formed expressions. (If it accepted all of the above, then since it only has a finite number of states, by the pigeonhole principle, you can construct an expression

(((...(((1+1)))...))) (n left parens, n right parens);

where there are some k,j<n such that k != j but the FSA is in the same state after reading k left parens as after reading j left parens, so since it accepts this expression, it must accept the expression you get by deleting the left parens from k+1 to j, which clearly isn't well-formed.)

You won't get around this problem without some way to keep track of arbitrarily large integers. You could augment your FSA with an assignable integer variable (which can have an infinite number of states, so it's no longer an FSA) to keep track of parentheses; that's just one way to do it.

Reply With Quote
  #3  
Old November 7th, 2004, 11:06 PM
heinrich's Avatar
heinrich heinrich is offline
digital sinner
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: sinner's land
Posts: 68 heinrich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 37 m 30 sec
Reputation Power: 6
Send a message via Yahoo to heinrich
an ideea: you could use trees.
if it can be memorized into a tree it's valid, if not it's not.
or you could try the polish form.
sorry i can't write down an explanation. look them up on google.

Reply With Quote
  #4  
Old December 5th, 2004, 11:45 AM
tinyabs tinyabs is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 93 tinyabs User rank is Private First Class (20 - 50 Reputation Level)tinyabs User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 13 h 24 m 34 sec
Reputation Power: 4
Use stack and postfix evaulation.

4 + 10 * 7 / (5 * 3 + 4 * 6)

+ 4 * 10 7 / ( * 5 3 + 5 * 6)

+4 is last to execute.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > a algorithm to validate an arithmetic expression


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 1 hosted by Hostway
Stay green...Green IT