#define _CRT_SECURE_NO_WARNINGS
#include"LinkList.c"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//遇到数字压栈
//遇到符号,取出栈的第一个元素为右操作符,第二个元素为左操作符。计算完毕后压栈typedef struct MYNUM {LinkNode node;int val;
}MyNum;int IsNumber(char c)
{return c >= '0' && c <= '9';
}int Caculate(int left,int right,char c)
{int ret = 0;switch (c){case '+':ret = left + right;break;case '-':ret = left - right;break;case '*':ret = left * right;break;case '/':ret = left / right;break;default:break;}return ret;
}int main()
{char* str = "831-5*+";char* p = str;LinkList* stack = Init_linkList();while (*p != '\0'){//如果是数字 直接入栈if (IsNumber(*p)){MyNum* num = (MyNum*)malloc(sizeof(MyNum));num->val = (*p)-'0'; //转换为数字printf("压入元素为:%d", num->val);Push_LinkList(stack, (LinkNode*)num);}else {//先从栈中弹出右操作数MyNum* right = (MyNum*)Top_LinkList(stack);int rightNum =right->val;Pop_LinkList(stack);free(right);MyNum* left = (MyNum*)Top_LinkList(stack);int leftNum = left->val;Pop_LinkList(stack);free(left);//结果入栈int ret=Caculate(leftNum, rightNum, *p);MyNum* num = (MyNum*)malloc(sizeof(MyNum));num->val =ret; //转换为数字Push_LinkList(stack,(LinkNode*)num);}p++;}if (Size_LinkList(stack) == 1){MyNum* num=(MyNum*)Top_LinkList(stack);printf("运算结果为:%d\n", num->val);Pop_LinkList(stack);free(num);}system("pause");return 0;
}