void algorithm(STACK* stack, char* num)
{
if (strcmp(num, "+") == 0)
{
int n = 0, m = 0;
n = top(*stack)->index;
pop(stack);
m = top(*stack)->index;
pop(stack);
int i = m+n;
insert(stack, i);
}
else if (strcmp(num, "-") == 0)
{
int n = 0, m = 0;
n = top(*stack)->index;
pop(stack);
m = top(*stack)->index;
pop(stack);
int i = n-m;
insert(stack, i);
}
else if (strcmp(num, "*") == 0)
{
int n = 0, m = 0;
n = top(*stack)->index;
pop(stack);
m = top(*stack)->index;
pop(stack);
int i = m*n;
insert(stack, i);
}
else if (strcmp(num, "/") == 0)
{
int n = 0, m = 0;
n = top(*stack)->index;
pop(stack);
m = top(*stack)->index;
pop(stack);
int i =n/m;
insert(stack, i);
}
}
void find(STACK* stack, char* num)
{
char*p = NULL;
char number[8] = { 0 };
int n = 0;
if (strlen(num)!=0)
{
p = strstr(num, ",");
n = p - num;
memcpy(number, num, n);
if (number[0] >= '!' &&number[0] <= '/')
{
algorithm(stack,number);
}
else
{
int m = atoi(number);
insert(stack, m);
}
strcpy(num, p + 1);
find(stack, num);
}
return;
}
void main()
{
STACK stack = { 0 };
char a[10] = { 0 };
int index = 0;
printf("请输入即将要计算的内容,以逗号间隔,enter结束");
gets(a);
find(&stack, a);
printf("结果是以下内容");
show(stack);
getchar();
}
逆波兰表达式(此篇文章加上上篇栈文章,即可完美实现内容)
最新推荐文章于 2023-06-25 11:04:51 发布