代码比较简陋可能存在一些错误,随便写写,没有考虑free
//
// Created by admin on 2023/12/3.
//
/**
* 四则运算 逆波兰式
*/
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#include <stdio.h>
#include <stdlib.h>
typedef int Status;
typedef struct Stack {
char data[MAXSIZE];
int top;
int count;
} ExpressionStack, OperatorStack;
typedef struct {
double data[MAXSIZE];
int top;
int count;
} CalculateStack;
char* PopAndGererateString(const ExpressionStack* expression_stack) {
char* str = malloc(sizeof((expression_stack->count) * sizeof(char)));
for (int i = 0; i < expression_stack->count; i++) {
str[i] = expression_stack->data[i];
}
str[expression_stack->count] = '\0';
return str;
}
void InitExpressionStack(struct Stack* expression_stack) {
for (int i = 0; i < MAXSIZE; i++) {
expression_stack->data[i] = '\0';
}
expression_stack->top = -1;
expression_stack->count = 0;
}
Status PushOperatorStack(OperatorStack* operator_stack, const char elem) {
if (operator_stack->top == MAXSIZE - 1) {
return ERROR;
}
operator_stack->top++;
operator_stack->count++;
operator_stack->data[operator_stack->top] = elem;
return OK;
}
Status PopOperatorStack(OperatorStack* operator_stack) {
if (operator_stack->top == -1) {
return ERROR;
}
operator_stack->data[operator_stack->top] = '\0';
operator_stack->top--;
operator_stack->count--;
return OK;
}
Status PopExpressionStack(ExpressionStack* expression_stack) {
if (expression_stack->top == -1) {
return ERROR;
}
expression_stack->data[expression_stack->top] = '\0';
expression_stack->top--;
expression_stack->count--;
return OK;
}
Status PushExpressionStackSimple(ExpressionStack* expression_stack, const char elem) {
if (expression_stack->top == MAXSIZE - 1) {
return ERROR;
}
expression_stack->top++;
expression_stack->count++;
expression_stack->data[expression_stack->top] = elem;
return OK;
}
Status PushExpressionStack(ExpressionStack* expression_stack, const char* str) {
if (expression_stack->top == MAXSIZE - 1) {
return ERROR;
}
// 初始化符号栈
OperatorStack operator_stack;
InitExpressionStack(&operator_stack);
char s = *str;
int strcnt = 1;
while (s != '\0') {
if (expression_stack->count == 11) {
printf("");
}
if (s == '+' || s == '-' || s == '*' || s == '/' || s == '(' || s == ')') {
if (s == ')') {
while (operator_stack.data[operator_stack.top] != '(') {
PushExpressionStackSimple(expression_stack, operator_stack.data[operator_stack.top]);
PopOperatorStack(&operator_stack);
}
PopOperatorStack(&operator_stack);
s = *(str + strcnt++);
continue;
}
// 如果当前获取到的字符是+-
if (s == '+' || s == '-') {
// 判断目前符号栈的栈顶是不是优先级更高
// 如果优先级相等也需要弹出
if (operator_stack.data[operator_stack.top] == '*' || operator_stack.data[operator_stack.top] == '/' ||
operator_stack.data[operator_stack.top] == '+' || operator_stack.data[operator_stack.top] == '-') {
// 弹出符号栈的所有符号到表达式栈中
while (operator_stack.top != -1) {
if (operator_stack.data[operator_stack.top] == '(') {
break;
}
PushExpressionStackSimple(expression_stack, operator_stack.data[operator_stack.top]);
PopOperatorStack(&operator_stack);
}
// 结束后压入当前字符到符号栈中
PushOperatorStack(&operator_stack, s);
s = *(str + strcnt++);
continue;
}
// 如果优先级大于栈顶符号,那么继续压入栈中
PushOperatorStack(&operator_stack, s);
s = *(str + strcnt++);
continue;
}
// 如果字符不是+-,正常压入栈中
PushOperatorStack(&operator_stack, s);
} else {
PushExpressionStackSimple(expression_stack, s);
}
s = *(str + strcnt++);
}
while (operator_stack.top != -1) {
PushExpressionStackSimple(expression_stack, operator_stack.data[operator_stack.top]);
PopOperatorStack(&operator_stack);
}
return OK;
}
Status ShowExoressionStackData(const ExpressionStack* expression_stack) {
if (expression_stack->top == -1) {
return ERROR;
}
for (int i = 0; i <= expression_stack->top; i++) {
printf("%c", expression_stack->data[i]);
}
return OK;
}
double CalculateExpression(const ExpressionStack* expression_stack) {
CalculateStack calculate_stack;
// InitExpressionStack(&calculate_stack);
calculate_stack.top = -1;
calculate_stack.count = 0;
for (int i = 0; i <= expression_stack->top; i++) {
const char s = expression_stack->data[i];
if (s == '+' || s == '-' || s == '*' || s == '/' || s == '(' || s == ')') {
const double c1 = calculate_stack.data[calculate_stack.top];
calculate_stack.top--;
const double c2 = calculate_stack.data[calculate_stack.top];
double d = 0.0;
switch (s) {
case '+': {
d = c2 + c1;
}
break;
case '-': {
d = c2 - c1;
}
break;
case '*': {
d = c2 * c1;
}
break;
case '/': {
d = c2 / c1;
}
break;
default: break;
}
calculate_stack.count++;
calculate_stack.data[calculate_stack.top] = d;
} else {
calculate_stack.top++;
calculate_stack.count++;
calculate_stack.data[calculate_stack.top] = (int) (s - '0');
}
}
const double sum = calculate_stack.data[0];
return sum;
}
int main() {
// const char* str = "9+(3-1)*3+2/2";
// const char* str = "1+((4+3))/2-5";
const char* str = "2-(3*4-5*(6-1)+3)+5/5";
// const char* str = "2-(3*4+(1+1))+5";
ExpressionStack expression_stack;
InitExpressionStack(&expression_stack);
PushExpressionStack(&expression_stack, str);
// printf("%d\n", expression_stack.count);
ShowExoressionStackData(&expression_stack);
const double sum = CalculateExpression(&expression_stack);
printf("\n计算的值为%.2lf", sum);
}