a*b+(c-d/e)*f#
+*ab*-c/def a*b+c-d/e*f ab*cde/-f*+
示例程序
#include#include #include #include #include using namespace std; int bijiao(char ch) { if(ch=='+'||ch=='-') { return 1; } if(ch=='*'||ch=='/') { return 2; } if(ch=='(') { return 3; } } void qian(char str[]) { char a[10100]; char b[10010]; char q[11000]; int pp = 0; int k = 0; int h = 0; int len = strlen(str); for(int i=len-2; i>=0; i--) { a[k++] = str[i]; } a[k] = '#'; //a[k] = '\0'; for(int i=0; a[i]!='#'; i++) { if((a[i]>='a' && a[i]<='z') || (a[i]>='A' && a[i] <='Z')) { b[h++] = a[i]; } else { if(a[i] == ')') { q[++pp] = a[i]; } else if(a[i] == '(') { for(; q[pp]!=')'; pp--) { b[h++] = q[pp]; } pp--; } else { if(bijiao(a[i]) < bijiao(q[pp])) { if(q[pp] == ')') { q[++pp] = a[i]; } else { b[h++] = q[pp]; q[pp] = a[i]; } } else { q[++pp] = a[i]; } } } } while(pp!=0) { b[h++] = q[pp]; pp--; } for(int i=h-1; i>=0; i--) { printf("%c",b[i]); } printf("\n"); } void zhong(char str[]) { for(int i=0; str[i]!='#'; i++) { if(str[i]!='(' && str[i]!=')') { printf("%c",str[i]); } } printf("\n"); } void hou(char str[]) { char qq[100100]; int p = 0; for(int i=0; str[i]!='#'; i++) { if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) { printf("%c",str[i]); } else { if(p == 0) { qq[++p] = str[i]; } else if(str[i] == '(') { qq[++p] = str[i]; } else if(str[i] == ')') { for(; qq[p]!='('; p--) { printf("%c",qq[p]); } p--; } else { if(bijiao(str[i]) <= bijiao(qq[p])) { if(qq[p] == '(') { qq[++p] = str[i]; } else { printf("%c",qq[p]); qq[p] = str[i]; } } else { qq[++p] = str[i]; } } } } while(p!=0) { printf("%c",qq[p]); p--; } printf("\n"); } int main() { char str[10010]; while(scanf("%s",str)!=EOF) { //printf("str = %s\n",str); qian(str); zhong(str); hou(str); } return 0; }