水题,前缀表达式求值。 #include <cstdio> #include <cctype> using namespace std; const int MAX = 200; bool value[128][2][2]; char input[MAX]; bool varabile[128]; int inputi; bool findresult() { bool left,right; int cur = input[ inputi ]; if( cur != 'N' ) { inputi++; if( isupper( input[ inputi ] ) ) left = findresult(); else left = varabile[ input[ inputi ++] ]; if( isupper( input[ inputi ] ) ) right = findresult(); else right = varabile[ input[ inputi++ ]]; return value[ cur ][ left ][ right ]; } else { inputi++; if( isupper( input[inputi] ) ) left = findresult(); else left = varabile[ input[ inputi++] ]; return value[ cur ][ left ][ left ]; } } bool cal() { for( int i = 0 ; i != 2 ; ++i ) { varabile['p'] = ( i == 0 ); for( int j = 0 ; j != 2 ; ++j ) { varabile['q'] = ( j == 0 ); for( int k = 0 ; k != 2 ; ++k ) { varabile['r'] = ( k == 0 ); for( int m = 0 ; m != 2 ; ++m ) { varabile['s'] = ( m == 0 ); for( int n = 0 ; n != 2 ; ++n ) { varabile['t'] = ( n == 0 ); inputi = 0 ; if( findresult() != true ) return false; } } } } } return true; } int main() { value['K'][0][0] = 0 ; value['K'][0][1] = 0; value['K'][1][0] = 0 ; value['K'][1][1] = 1; value['A'][0][0] = 0 ; value['A'][0][1] = 1; value['A'][1][0] = 1 ; value['A'][1][1] = 1; value['C'][0][0] = 1 ; value['C'][0][1] = 1; value['C'][1][0] = 0 ; value['C'][1][1] = 1; value['E'][0][0] = 1 ; value['E'][0][1] = 0; value['E'][1][0] = 0 ; value['E'][1][1] = 1; value['N'][0][0] = 1 ; value['N'][1][1] = 0; while( scanf("%s",input) , input[0] != '0' ) { if( cal() ) printf("tautology/n"); else printf("not/n"); } return 0; }