#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define STACK_INIT_SIZE 100 #define STACK_INCREAMENT 10 #pragma warning(disable:4996)//我用的vs2015,不加这句用scanf会报错(使用了unsafe的函数) typedef struct { //栈 char *base; char *top; int stackSize; } Stack; void initStack(Stack &stack) { //初始化栈 stack.base = stack.top = (char *)malloc(sizeof(char) * STACK_INIT_SIZE); stack.stackSize = STACK_INIT_SIZE; } void push(Stack &S, char p) { //入栈 if (S.top - S.base >= S.stackSize) { S.base = (char*)realloc(S.base, (S.stackSize + STACK_INCREAMENT)*sizeof(char)); S.top = S.stackSize + S.base; S.stackSize += STACK_INCREAMENT; } *S.top++ = p; } void pop(Stack &stack, char &p) { //出栈 if (stack.base == stack.top) { p = NULL; return; } p = *--stack.top; } char getTop(Stack stack) { //获得栈顶元素 if (stack.base == stack.top) return NULL; return *(stack.top - 1); } char *NIBOLAN(char *e) /* 返回表达式e的逆波兰式 */ {//栈s1用于存放运算符,栈s2用于存放逆波兰式 Stack s1, s2;initStack(s1);initStack(s2); push(s1, '#'); //假设字符'#'是运算级别最低的运算符,并压入栈s1中 //p指针用于遍历传入的字符串,ch用于临时存放字符,length用于计算字符串长度 char *p = e, ch;int length = 0; for (; *p != '