生活随笔
收集整理的這篇文章主要介紹了
中缀表达式计算、后缀表达式计算、中缀转后缀
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼來源 :
bilibili網站 :https://www.bilibili.com/video/av91996256?from=search&seid=17449723308302029804
注意:中綴表達式 轉 后綴表達式,函數 有誤。
中綴表達式 計算過程 :
后綴表達式 計算過程 :
中綴表達式 轉 后綴表達式 :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>enum boolean
{FALSE
,TRUE
};typedef enum boolean Bool
;
typedef int ElementType
;struct stack
{ElementType
*element
;int MaxSize
; int top
;
};typedef struct stack Stack
;
void InitStack(Stack
*S
, int sz
);
void FreeStack(Stack
*S
);
Bool
Push(Stack
*S
, ElementType X
);
ElementType
Pop(Stack
*S
);
ElementType
GetTop(Stack
*S
);
void MakeStackEmpty(Stack
*S
);
Bool
IsStackEmpty(Stack
*S
);
Bool
IsStackFull(Stack
*S
); void EvaluteMidPostfix();
void EvaluteBackPostfix();
void MidToBack(); int main()
{EvaluteBackPostfix();return 0;
}void InitStack(Stack
*S
, int sz
)
{S
->MaxSize
= sz
;S
->top
= -1; S
->element
= (ElementType
*)malloc(sizeof(ElementType
) * S
->MaxSize
);
}void FreeStack(Stack
*S
)
{free(S
->element
);
}Bool
IsStackFull(Stack
*S
)
{if (S
->top
== S
->MaxSize
- 1)return TRUE
;return FALSE
;
}Bool
IsStackEmpty(Stack
*S
)
{if (S
->top
== -1)return TRUE
;return FALSE
;
}Bool
Push(Stack
*S
, ElementType x
)
{if (IsStackFull(S
))return FALSE
;else{S
->element
[++S
->top
] = x
; return TRUE
;}
}ElementType
Pop(Stack
*S
)
{if (IsStackEmpty(S
)){exit(1); }else{return S
->element
[S
->top
--];}
}ElementType
GetTop(Stack
*S
)
{if (IsStackEmpty(S
)){exit(1); }else{return S
->element
[S
->top
];}
}void MakeStackEmpty(Stack
*S
)
{S
->top
= -1;
}
void compute(Stack
*Sptr
, Stack
*Spnd
)
{int k
;switch (Pop(Sptr
)){case '+':k
= Pop(Spnd
) + Pop(Spnd
);break;case '-':k
= Pop(Spnd
);k
= Pop(Spnd
) - k
;break;case '*':k
= Pop(Spnd
) * Pop(Spnd
);break;case '/':k
= Pop(Spnd
);k
= Pop(Spnd
) / k
;break;}Push(Spnd
, k
);
}void EvaluteMidPostfix()
{char buf
[80];printf("Please input Infix : \n");scanf("%s", buf
);Stack
*Spnd
= (Stack
*)malloc(sizeof(Stack
));Stack
*Sptr
= (Stack
*)malloc(sizeof(Stack
));InitStack(Spnd
, 80);InitStack(Sptr
, 80);int i
= 0;while (buf
[i
] != '\0'){if (buf
[i
] >= '0' && buf
[i
] <= '9')Push(Spnd
, (int)(buf
[i
] - 48)); else if (buf
[i
] == '*' || buf
[i
] == '/') {if (GetTop(Sptr
) == '*' || GetTop(Sptr
) == '/'){compute(Sptr
, Spnd
);}Push(Sptr
, buf
[i
]);}else if (buf
[i
] == '+' || buf
[i
] == '-'){while (Sptr
->top
!= -1 && GetTop(Sptr
) != '(') {compute(Sptr
, Spnd
);}Push(Sptr
, buf
[i
]);}else if (buf
[i
] == ')'){while (GetTop(Sptr
) != '('){compute(Sptr
, Spnd
);}Pop(Sptr
);}else if (buf
[i
] == '('){Push(Sptr
, buf
[i
]);}i
++;}while (Sptr
->top
!= -1) compute(Sptr
, Spnd
);printf("%d", Pop(Spnd
));
}void EvaluteBackPostfix()
{char buf
[80];printf("Please input Suffix : \n");scanf("%s", buf
);Stack
*Spnd
= (Stack
*)malloc(sizeof(Stack
));InitStack(Spnd
, 80);int i
= 0, k
;while (buf
[i
] != '\0'){switch (buf
[i
]){case '+':k
= Pop(Spnd
) + Pop(Spnd
);Push(Spnd
, k
);break;case '-':k
= Pop(Spnd
);k
= Pop(Spnd
) - k
;Push(Spnd
, k
);break;case '*':k
= Pop(Spnd
) * Pop(Spnd
);Push(Spnd
, k
);break;case '/':k
= Pop(Spnd
);k
= Pop(Spnd
) / k
;Push(Spnd
, k
);break;default:Push(Spnd
, (int)(buf
[i
] - 48));}i
++;}printf("%d", Pop(Spnd
));
}void MidToBack()
{char buf
[80];printf("Infix to suffix : \n");scanf("%s", buf
); Stack
*Sptr
= (Stack
*)malloc(sizeof(Stack
));InitStack(Sptr
, 80);int i
= 0;while (buf
[i
] != '\0'){if (buf
[i
] >= '0' && buf
[i
] <= '9')printf("%c", buf
[i
]);else if (buf
[i
] == '*' || buf
[i
] == '/'){if (IsStackEmpty(Sptr
)){Push(Sptr
, buf
[i
]); }else if (GetTop(Sptr
) == '*' || GetTop(Sptr
) == '/'){printf("%c", Pop(Sptr
));Push(Sptr
, buf
[i
]);}else{Push(Sptr
, buf
[i
]);}}else if (buf
[i
] == '+' || buf
[i
] == '-'){if (IsStackEmpty(Sptr
)){Push(Sptr
, buf
[i
]); }}else if (buf
[i
] == ')'){while (GetTop(Sptr
) != '('){printf("%c", Pop(Sptr
));}Pop(Sptr
);}else if (buf
[i
] == '('){Push(Sptr
, buf
[i
]);}i
++;}while (Sptr
->top
!= -1) printf("%c", Pop(Sptr
));
}
運行截圖:
總結
以上是生活随笔為你收集整理的中缀表达式计算、后缀表达式计算、中缀转后缀的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。