无聊题,中缀表达式。一口气写了290行,就如炎炎烈日下当头一兜凉水般的爽快。
string str;
int cs = 1;
int lev ( char c )
{
if ( c == '+' || c == '-' )
return 1;
if ( c == '*' )
return 2;
if ( c == '(' || c == '@' )
return 0;
return -1;
}
void print ( const vector <int> &v )
{
int i;
for ( i = 0; i < v.size (); i ++ )
{
if ( v[i] == -1000 )
printf ( "x" );
else if ( v[i] >= 0 )
printf ( "%d", v[i] );
else
printf ( "%c", v[i] + 500 );
}
printf ( "/n" );
}
void proc ()
{
int i = str.find_first_of ( '=' );
string left = str.substr ( 0, i );
string right = str.substr ( i + 1, str.size () );
left += '@', right += '@';
stack <int> is;
stack <int> xs;
stack <char> cs;
vector <int> iv;
int sum = 0;
cs.push ( '@' );
for ( i = 0; i < left.size (); i ++ )
{
if ( left[i] == 'x' )
{
iv.push_back ( -1000 );
}
else if ( isdigit ( left[i] ) )
{
sum = sum * 10 + left[i] - '0';
if ( !isdigit ( left[i + 1] ) )
{
iv.push_back ( sum );
sum = 0;
}
}
else if ( left[i] != '@' )
{
if ( left[i] == '(' )
{
cs.push ( '(' );
}
else if ( left[i] == ')' )
{
while ( cs.top () != '(' )
{
iv.push_back ( cs.top () - 500 );
cs.pop ();
}
cs.pop ();
}
else
{
char ch = left[i];
if ( lev ( ch ) > lev ( cs.top () ) )
cs.push ( ch );
else
{
while ( lev ( cs.top () ) >= lev ( ch ) )
{
iv.push_back ( cs.top () - 500 );
cs.pop ();
}
cs.push ( ch );
}
}
}
else
{
while ( cs.top () != '@' )
{
iv.push_back ( cs.top () - 500 );
cs.pop ();
}
}
}
int la = 0, lb = 0; // ax = b
for ( i = 0; i < iv.size (); i ++ )
{
if ( iv[i] >= 0 )
{
is.push ( iv[i] );
xs.push ( 0 );
}
else if ( iv[i] == -1000 )
{
is.push ( 0 );
xs.push ( 1 );
}
else
{
char ch = iv[i] + 500;
int a1 = xs.top ();
xs.pop ();
int a2 = xs.top ();
xs.pop ();
int b1 = is.top ();
is.pop ();
int b2 = is.top ();
is.pop ();
if ( ch == '+' )
{
xs.push ( a1 + a2 );
is.push ( b1 + b2 );
}
if ( ch == '-' )
{
xs.push ( a1 - a2 );
is.push ( b1 - b2 );
}
if ( ch == '*' )
{
if ( a1 == 0 && a2 == 0 )
xs.push ( 0 );
if ( a1 == 0 && a2 != 0 )
xs.push ( a2 * b1 );
if ( a2 == 0 && a1 != 0 )
xs.push ( a1 * b2 );
is.push ( b1 * b2 );
}
}
}
la = xs.top ();
lb = is.top ();
xs.pop ();
is.pop ();
//printf ( "%d %d/n", a, b );
//print ( iv );
iv.clear ();
for ( i = 0; i < right.size (); i ++ )
{
if ( right[i] == 'x' )
{
iv.push_back ( -1000 );
}
else if ( isdigit ( right[i] ) )
{
sum = sum * 10 + right[i] - '0';
if ( !isdigit ( right[i + 1] ) )
{
iv.push_back ( sum );
sum = 0;
}
}
else if ( right[i] != '@' )
{
if ( right[i] == '(' )
{
cs.push ( '(' );
}
else if ( right[i] == ')' )
{
while ( cs.top () != '(' )
{
iv.push_back ( cs.top () - 500 );
cs.pop ();
}
cs.pop ();
}
else
{
char ch = right[i];
if ( lev ( ch ) > lev ( cs.top () ) )
cs.push ( ch );
else
{
while ( lev ( cs.top () ) >= lev ( ch ) )
{
iv.push_back ( cs.top () - 500 );
cs.pop ();
}
cs.push ( ch );
}
}
}
else
{
while ( cs.top () != '@' )
{
iv.push_back ( cs.top () - 500 );
cs.pop ();
}
}
}
int ra = 0, rb = 0; // ax = b
for ( i = 0; i < iv.size (); i ++ )
{
if ( iv[i] >= 0 )
{
is.push ( iv[i] );
xs.push ( 0 );
}
else if ( iv[i] == -1000 )
{
is.push ( 0 );
xs.push ( 1 );
}
else
{
char ch = iv[i] + 500;
int a1 = xs.top ();
xs.pop ();
int a2 = xs.top ();
xs.pop ();
int b1 = is.top ();
is.pop ();
int b2 = is.top ();
is.pop ();
if ( ch == '+' )
{
xs.push ( a1 + a2 );
is.push ( b1 + b2 );
}
if ( ch == '-' )
{
xs.push ( a1 - a2 );
is.push ( b1 - b2 );
}
if ( ch == '*' )
{
if ( a1 == 0 && a2 == 0 )
xs.push ( 0 );
if ( a1 == 0 && a2 != 0 )
xs.push ( a2 * b1 );
if ( a2 == 0 && a1 != 0 )
xs.push ( a1 * b2 );
is.push ( b1 * b2 );
}
}
}
ra = xs.top ();
rb = is.top ();
xs.pop ();
is.pop ();
//printf ( "%d %d/n", la, lb );
//printf ( "%d %d/n", ra, rb );
int a = la - ra, b = rb - lb;
if ( a == 0 && b == 0 )
printf ( "Infinitely many solutions./n" );
else if ( a == 0 && b != 0 )
printf ( "No solution./n" );
else
printf ( "x = %f/n", double ( b ) / double ( a ) );
}
int main ()
{
//freopen ( "in.txt", "r", stdin );
while ( getline ( cin, str ) )
{
printf ( "Equation #%d/n", cs ++ );
proc ();
printf ( "/n" );
}
return 0;
}