Jump to content
  • Advertisement
Sign in to follow this  
tom_mai78101

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

This topic is 2754 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!