ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1027. D++ Again

Help. I got WA & i have no idea why.
Posted by Alabaster 20 Jul 2002 21:13
// D++ Again
#include <iostream.h>
#include <ctype.h>

short ok=1;
int exp=0, com=0;

void comment()
{
 char c, cpop=' ';
 while (cin.get(c) && !(c==')' && cpop=='*'))
  cpop=c;
 if (c==')' && cpop=='*')
  com--;
}

void expression()
{
 char c, cpop;
 while (cin.get(c) && c!=')' && ok)
 {
  if (!isdigit(c) && c!='=' && c!='+' && c!='-' && c!='*' && c!='/'
&& c!='(' && c!=')' && c!='\n' && c!='\r')
   ok = 0;
  if (c=='*' && ok)
  {
   if (cpop=='(')
   {
    com++;
    comment();
   }
  }
  else if (cpop=='(' && ok)
  {
   exp++;
   expression();
  }
 }
 if (c==')')
  exp--;
}

void text()
{
 char c, cpop=' ';
 while (ok && cin.get(c))
 {
  if (c=='*')
  {
   if (cpop=='(')
   {
    com++;
    comment();
   }
  }
  else if (cpop=='(')
  {
   exp++;
   expression();
  }
  cpop=c;
 }
 if (exp || com)
  ok=0;
}

void check()
{
 text();
 if (ok)
  cout << "YES";
 else
  cout << "NO";
}

int main()
{
 check();
 return 0;
}