0

Can someone please suggest me a psuedo code to extract sub problems from an arithmetic expression:
For example,
How do I remove (8*2) and (3+2) from a given string (5 + 6(3+2) - 2(8*2) +1)

Input string will always start and end with paranthesis and the sub problems must be extracted as strings too.

string[] extract(string s1)
{
string extracted[]; 
if (s1[1]==')' )
cout<<"no expression"
return NULL

else

??????????



Return extracted[]

Edited by Der_sed: n/a

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by WaltP
0

In your example:

(5 + 6(3+2) - 2(8*2) +1)

Moving from left-to-right, in order to find a valid parenthesized pair, you have to find any condition where a ')' is found after traversing the most recent '('

From left-to-right, anytime multiple '(' are detected, the previous '(' position should be discarded.. only keep track of the position of the most current '('

Upon detection of the first ')', you now have the position of a valid parenthesized term. The parenthesis that make this term need to be discarded as candidates from future searches.

Edited by Clinton Portis: n/a

0

As you loop through the equation, remember the position of each ( you see. When you see a ), extract everything between the ) and the last ( you saw. Replace these characters with SPACEs to clear out the extracted section. "Unremember" the ( location since it's no longer there. Continue through the equation until you reach the end.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.