#include<stdio.h> #include<string.h> /* another solution: just become array operator by their position and order,when you type two characters ,just find out their i and j,and get i*10 + j,and then compare this value,the bigger one get higher priority */ int AvoidSpe(char *ch); char *operator[16][10] = { {"(",")","[","]","->","."}, {"!","~","++","--","+","-","*","&","(type)","sizeof"}, {"*","/","%%"}, {"+","-"}, {">>","<<"}, {"<","<=",">",">="}, {"==","!="}, {"&"}, {"^"}, {"|"}, {"&&"}, {"||"}, {"?:"}, {"=","+=","-=","*=","/=","%=","&="}, {"^=","|=","<<=",">>="}, {","} }; int order[17] = {0, //for start from 1,not 0 1,0,1,1,1,1,1,1, 1,1,1,1,0,0,1,1}; //1 is left to right,0 is right to left int FindLevel(char * ch) { int tmp; if( strlen( ch ) == 1 ) { if( *ch == '+' || *ch == '-' || *ch == '*' || *ch == '&' ) { tmp = AvoidSpe( ch ); return tmp; } } for( int i = 0 ; i < 16 ; i++ ) { for( int j = 0 ; j < 10 ; j++ ) { if( !operator[i][j] ) { continue; } // for get both i and j if( !strcmp( ch,operator[i][j]) ) { return ( i + 1 )*10 + j; } } } } int AvoidSpe(char *ch) { int tmp = 0; char *p; printf("%s is Unary operators or operator?(1 or 0)",ch); scanf("%d",&tmp); //*p = *ch; if( tmp == 1 ) { switch( *ch ) { case '+': return 2*10+4; break; case '-': return 2*10+5; break; case '*': return 2*10+6; break; case '&': return 2*10+7; break; } } else { switch( *ch ) { case '+': return 4*10+0; break; case '-': return 4*10+1; break; case '*': return 3*10+0; break; case '&': return 8*10+0; break; } } } int check(char *s1,char *s2) { int a = 0; int b = 0; for( int i = 0 ; i < 16 ; i++ ) { for( int j = 0 ; j < 10 ; j++ ) { if( !operator[i][j] ) { continue; } if( !strcmp( s1,operator[i][j]) && !a) { ++a; } if( !strcmp( s1,operator[i][j]) && !b) { ++b; } if( a && b ) return 1; } } printf("wrong input!\n"); return 0; } int main() { int posi_one = 0,posi_two = 0; int posj_one = 0,posj_two = 0; int tmp = 0; char ch_i[3],ch_j[3]; printf("input first para:"); scanf("%s",ch_i); printf("input second para:"); scanf("%s",ch_j); if( !check(ch_i,ch_j) ) return 1; tmp = FindLevel( ch_i ); printf("%d\n",tmp); posi_two = tmp % 10; tmp -= posi_two; tmp /= 10; posi_one = tmp; tmp = FindLevel( ch_j ); printf("%d\n",tmp); posj_two = tmp % 10; tmp -= posj_two; tmp /= 10; posj_one = tmp; //printf("%d %d\n",posi_one,posj_one); //printf("%d %d\n",posi_two,posj_two); if( posi_one > posj_one ) { printf("%s > %s\n",ch_j,ch_i); } else if( posi_one < posj_one ) { printf("%s > %s\n",ch_i,ch_j); } else { if( order[ posi_one ] ) //from left to right { printf("from left to right\n"); } else { printf("from right to left\n"); } } return 0; }