//inf22.cpp #include #include #include #include char buf[999]; char infix[222], stmp[222], postfix[222]; void toPost(char*, char*); double compute(char*); int main( ) { fprintf(stderr, "Give me an infix expression and I will...\n"); while(38==38) { fprintf(stderr, "type here: "); fgets(buf, sizeof(buf), stdin); strcpy(infix, buf); // infix = buf toPost(infix, postfix); double ans = compute(postfix); /// fprintf(stdout, "infix: %s\n", infix); fprintf(stdout, "postfix: %s\n", postfix); fprintf(stdout, "ans = %d\n", ans); fprintf(stderr, "Again(n, y) ? "); fgets(buf, sizeof(buf), stdin); if(toupper(buf[0]) != 'Y') break; } return 0; } // main void toPost(char*in, char*post) { char tmps[333]; char * p; int k=1; strcpy(tmps, in); p = strtok(tmps, " (+-*/)"); while(p!=0) { printf("%d: %s\n", k, p); p = strtok(0, " (+-*/)"); // ª`·N ++k; } // char*p2; char oper; char tmp; p2 = post; p = in; tmp = *p; while(tmp != 0) { // char by char search if(strchr( "(+-*/)", tmp) ==0) { *p2 = tmp; ++p2; }else{ *p2=' ' ; // blank ++p2; oper = tmp; // process this operator } ++p; tmp=*p; } *p2 = 0; // NULL terminator // //printf("%d\n", strchr("(+-*/)", '3')); // } double compute(char*pfx) { double ans=0.0; /// return ans; }