生成逆波兰表达式
#define MAXSIZE 10 typedef struct OpNode { char data; struct OpNode *next; }OpNode; typedef struct OpStack { OpNode *top; }OpStack; void op_pop(OpStack *s, char *d); void op_push(OpStack *s, char data); int op_len(OpStack *s); int main() { OpStack op = {NULL}; char list[MAXSIZE] = {'0'}; char el, d, e; printf("输入表达式,以=结束:"); scanf("%c", &el); while(el != '=') { while(isdigit(el) || el == '.') { printf("%c", el); scanf("%c", &el); } printf(" "); switch(el) { case ')': if(op_len(&op)) { do { op_pop(&op, &e); printf("%c", e); }while(op_len(&op) && e != '('); }else { printf("出错\n"); exit(-1); } break; case '+': case '-': case '(': if(op_len(&op)) { do { op_pop(&op, &e); if(e == '(') { op_push(&op, e); }else { printf("%c ", e); } }while(op_len(&op) && e != '('); } op_push(&op, el); break; case '*': case '/': op_push(&op, el); break; default: break; } if(el == '=') { break; } scanf("%c", &el); } while(op_len(&op)) { op_pop(&op, &e); printf("%c ", e); } return 0; } void op_push(OpStack *s, char data) { OpNode *p = (OpNode *)malloc(sizeof(OpNode)); p->data = data; p->next = s->top; s->top = p; } void op_pop(OpStack *s, char *d) { OpNode *p = s->top; if(p == NULL) { return ; } *d = p->data; s->top = p->next; free(p); } int op_len(OpStack *s) { int i = 0; OpNode *p = s->top; while(p != NULL) { p = p->next; i++; } return i; }