#include<stdio.h>
#include<stdlib.h>
#include <ctype.h>
#include<string.h>
#include<math.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
#define MAXBUFFER 10
#define VOLUMN 100
typedef char ElemType;
typedef double elemtype;
typedef struct
{
ElemType* base;
ElemType* top;
int stackSize;
}sqStack;
typedef struct
{
elemtype* base;
elemtype* top;
int stackSize;
}sqstack;
void InitStack(sqStack* s) //初始化栈
{
s->base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!s->base)
{
printf("ERROR_01");
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void initstack(sqstack* s) //初始化栈
{
s->base = (elemtype*)malloc(STACK_INIT_SIZE * sizeof(elemtype));
if (!s->base)
{
printf("ERROR_01");
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void Push(sqStack* s, ElemType e)//入栈
{
//栈满,追加空间
if (s->top - s->base >= s->stackSize)
{
s->base = (ElemType*)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if (!s->base)
{
printf("ERROR_01");
}
}
*(s->top) = e;
s->top++;
}
void push(sqstack* s, elemtype e)//入栈
{
//栈满,追加空间
if (s->top - s->base >= s->stackSize)
{
s->base = (elemtype*)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(elemtype));
if (!s->base)
{
printf("ERROR_01");
}
}
*(s->top) = e;
s->top++;
}
void Pop(sqStack* s, ElemType* e)//出栈
{
if (s->top == s->base)
{
printf("ERROR_01");
}
*e = *--(s->top);//将栈顶元素弹出并修改栈顶指针
}
void pop(sqstack* s, elemtype* e)//出栈
{
if (s->top == s->base)
{
printf("ERROR_01");
}
*e = *--(s->top);//将栈顶元素弹出并修改栈顶指针
}
int StackLen(sqStack s)//检查栈是否为空
{
return (s.top - s.base);
}
int stackLen(sqstack s)//检查栈是否为空
{
return (s.top - s.base);
}
int main(int argc, char* argv[])
{
sqStack s;
sqstack s1;
InitStack(&s);//s栈在第一过程中用来存放字符
initstack(&s1);//s1栈在第二过程中用来计算逆波兰表达式
char c, e;
int i, j, k, len, len1;
char str1[VOLUMN], str[MAXBUFFER];//str1用来存放逆波兰表达式,str用于计算大于10的数
for (i = 0; i < VOLUMN; i++)
{
str1[i] = 0;
str[i] = 0;
}//初始化数组
len = strlen(argv[1]);
if (argc != 2)
{
printf("ERROR_01");//输入格式有问题
}
for (i = 0,j = 0; i < len; i++)
{
c = argv[1][i];
if (c == '\0')
{
break;
}
if (c >= '0' && c <= '9' || c == '.')
{
str1[j++] = c;
}
if (c == '^')
{
if (!StackLen(s) || StackLen(s) && s.top[-1] == '(') {
Push(&s, c);
}
else {
do {
Pop(&s, &e);
if (e == '(') {
Push(&s, e);
}
else {
printf("%c ", e);
}
} while (StackLen(s) && e != '(' && e != '+' && e != '-');
Push(&s, c);
}
}
if (')' == c)
{
Pop(&s, &e);
while ('(' != e)
{
str1[j++] = e;
Pop(&s, &e);
}
}
if (']' == c)
{
Pop(&s, &e);
while ('[' != e)
{
str1[j++] = e;
Pop(&s, &e);
if (')' == c)
{
Pop(&s, &e);
while ('(' != e)
{
str1[j++] = e;
Pop(&s, &e);
}
}
}
}
else if ('+' == c || '-' == c)
{
if (!StackLen(s))
{
Push(&s, c);
}
else
{
do
{
Pop(&s, &e);
if ('(' == e)
{
Push(&s, e);
}
else
{
str1[j++] = e;
}
} while (StackLen(s) && '(' != e);
Push(&s, c);
}
}
else if ('*' == c || '/' == c || '(' == c)
{
Push(&s, c);
}
else
{
printf("ERROR_02");
}
}
len = strlen(str1) - 1;
while (StackLen(s))
{
Pop(&s, &e);
str1[len++] = e;
}
double d, f;
len = strlen(str1);
for (i = 0, j = 0; i < len; i++)
{
c = str1[i];
if (c == '\0')
{
break;
}
while (isdigit(c) || c == '.')
{
str[j++] = c;
str[j] = '\0';
if (j >= 10)
{
printf("ERROR_02");
return -1;
}
c = str1[++i];
if (c < '0' || c > '9')
{
d = atof(str);
push(&s1, d);
j = 0;
break;
}
}
switch (c)
{
case '+':
pop(&s1, &f);
pop(&s1, &d);
push(&s1, d + f);
break;
case '-':
pop(&s1, &f);
pop(&s1, &d);
push(&s1, d - f);
break;
case '*':
pop(&s1, &f);
pop(&s1, &d);
push(&s1, d * f);
break;
case '/':
pop(&s1, &f);
pop(&s1, &d);
if (f != 0)
{
push(&s1, d / f);
}
else
{
printf("ERROR_03");
return -1;
}
break;
case '^':
pop(&s1, &f);
pop(&s1, &d);
push(&s1, pow(d , f));
break;
}
}
pop(&s1, &d);
printf("%f", d);
return 0;
}