Sign in to follow this  
tom_mai78101

Nested Parentheses and my attempt at solving it. Where can I improve?

Recommended Posts

This is the code below:

std::string CopyStringToStdString (char *str)
{
std::string temp (str);
return temp;
}

bool Do_Parenthese (std::string::iterator left, std::string::iterator right)
{
bool test_L = true;
bool test_R = true;
std::string::iterator L_it = left;
std::string::iterator R_it = right;
std::string::iterator symbol = L_it + 1;
std::string::iterator mark = symbol + 1;
std::string::iterator ptr = L_it + 1;
if (*L_it != '(')
{
while (*L_it != '(')
{
L_it++;
if (L_it == right)
{
L_it = left;
test_L = false;
break;
}
}
}
if (*R_it != ')')
{
while (*right != ')')
{
right--;
if (R_it == left)
{
R_it = right;
test_R = false;
break;
}
}
}
if (test_L == true || test_R == true)
{
Do_Parenthese (L_it + 1, R_it - 1);
}
while ((*symbol >= '0' && *symbol <= '9') || *symbol == '|')
{
symbol++;
mark = symbol + 1;
}
while ((*mark >= '0' && *mark <= '9') || *mark == '|')
mark++;
//DoMath (L_it, symbol, mark). This function doesn't exist yet.
return true;
}

bool Find_Parenthese (char *ptr, std::string &str)
{
str = CopyStringToStdString (ptr);
std::string::iterator left, right;
left = str.begin();
right = str.end()-1; //remove null character.
if (*right != '=')
{
std::cout << "Something is wrong here. Shouldn't reach here." << std::endl;
return false;
}
Do_Parenthese (left, right);
return true;
}

int main()
{
//This is here to prove that this function works as promised...or not.
//It's a test.
const int count = 100;
char str[count];
std::cin >> str;
std::string string;
Find_Parenthese (str, string);

system("pause");
return 0;
}


So, whenever I tried: ("|" is the bar you normally see in fractions.)

(((1|2+3|4)))=

or:

(1+2)=

even:

()=

I can get errors pretty much on all of them. Where should I improve my code part? As the iterator at (string.end() - 1) is on '=', and when R_it minus 1, I still don't get how the iterator R_it can actually skip parentheses... That means, If there's ")))" and R_it is at the very right, after it minus 1 by itself, R_it would be at the very left of the parentheses.

Unless I'm really losing where I am in there...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this