c语言逆波兰计算器程序,C语言实现的简单的逆波兰计算器

前后调试了好几天,类型转换问题颇为麻烦,无赖使用两种不同类型的栈解决,但程序目前还有两个问题:

1.目前不能进行float类型的计算;

2.不能进行多位数的运算。

实际上就是目前只能进行个位数的带括号的加减乘除运算,目前只能写到这一步,期待后续改进。

#include

#include

#include

#define CAPACITY 50

typedef struct stack

{

int top;

char arr[CAPACITY];

}*pStack,stack;

pStack initStack1()

{

pStack st = (pStack)malloc(sizeof(stack));

st->top = 0;

return st;

}

int stackEmpty1(pStack st)

{

return(st->top <= 0);

}

int stackFull1(pStack st)

{

return(st->top >= CAPACITY);

}

int pushStack1(pStack st, char a)

{

if(stackFull1(st))

{

printf("栈满!\n");

return 0;

}

else

{

st->arr[st->top++] = a;

return 1;

}

}

char popStack1(pStack st)

{

char a;

if(stackEmpty1(st))

{

printf("栈空!\n");

return 0;

}

else

{

a = st->arr[--st->top];

return a;

}

}

char getTop1(pStack st)

{

if(!stackEmpty1(st))

return st->arr[st->top-1];

else

printf("Empty stack");

return 0;

}

typedef struct

{

int top;

int data[CAPACITY];

}*dataStack;

dataStack initStack2()

{

dataStack st = (dataStack)malloc(sizeof(dataStack));

st->top = 0;

return st;

}

int stackEmpty2(dataStack st)

{

return(st->top <= 0);

}

int stackFull2(dataStack st)

{

return(st->top >= CAPACITY);

}

int pushStack2(dataStack st, int data)

{

if(stackFull2(st))

{

printf("栈满!\n");

return 0;

}

else

{

st->data[st->top++] = data;

return 1;

}

}

int popStack2(dataStack st)

{

int data;

if(stackEmpty2(st))

{

printf("栈空!\n");

return 0;

}

else

{

data = st->data[--st->top];

return data;

}

}

int getTop2(dataStack st)

{

if(!stackEmpty2(st))

return st->data[st->top-1];

else

printf("Empty stack");

return 0;

}

int priority(char op)

{

switch(op)

{

case '*':

case '/':return 2;

case '+':

case '-':return 1;

default: return 0;

}

}

int myAtoi(char a)

{

return a-'0';

}

int main(void)

{

int p = 0;

int i = 0;

int j;

int result, tmp1, tmp2;

char ch, temp;

char infix[CAPACITY] = "\0";

char output[CAPACITY];

pStack st = initStack1();

dataStack st2 = initStack2();

printf("请输入中缀表达式:");

scanf("%s", &infix);

printf("后缀表达式为:");

for(ch = infix[p];ch !='\0';ch = infix[++p])

{

switch(ch)

{

case '(':pushStack1(st,ch);break;

case ')':

{

while(getTop1(st) != '(')

{

output[i++] = getTop1(st);

printf("%c", popStack1(st));

}

popStack1(st);

break;

}

case '+':

case '-':

case '*':

case '/':

{

while(!stackEmpty1(st))

{

temp = getTop1(st);

if(priority(temp) >= priority(ch))

{

output[i++] = temp;

printf("%c", popStack1(st));

}

else

{

break;

}

}

pushStack1(st, ch);

break;

}

default:output[i++] = ch,printf("%c",ch);

}

}

while(!stackEmpty1(st))

{

temp = popStack1(st);

output[i++] = temp;

if(temp == '(')

{

printf("表达式有误!");

exit(EXIT_FAILURE);

}

printf("%c", temp);

printf("\n");

}

output[i] = '\0';

free(st);

for(j=0;j

{

switch(output[j])

{

case'+':tmp1 = popStack2(st2);tmp2 = popStack2(st2);result = tmp1 + tmp2;pushStack2(st2, result);break;

case'-':tmp1 = popStack2(st2);tmp2 = popStack2(st2);result = tmp2 - tmp1;pushStack2(st2, result);break;

case'*':tmp1 = popStack2(st2);tmp2 = popStack2(st2);result = tmp2 * tmp1;pushStack2(st2, result);break;

case'/':tmp1 = popStack2(st2);tmp2 = popStack2(st2);result = tmp2 / tmp1;pushStack2(st2, result);break;

default:pushStack2(st2,myAtoi(output[j]));break;

}

}

printf("输出结果为: %d\n", getTop2(st2));

return 0;

}两种堆栈可以用泛型实现共用函数,目前还不清楚怎么实现。

你可能感兴趣的:(c语言逆波兰计算器程序)