求表達式(1 - 2) * (4 + 5)的值
- 人類早就熟悉這種中綴表達式的計算公式,隨便拉一個小朋友過來,他就直到這個結果是等于-9的,因為括號里面的要先進行計算。
- 但計算機不喜歡了,因為我們有小括號中括號大括號,還允許一個嵌套一個,這樣子計算機就要進行很多次if判斷才能決定那里先計算。
逆波蘭表達式(后綴表達式)
波蘭邏輯學家Jan.Lukasiewicz,發明了一種不需要括號的后綴表達式,我們通常把它稱為波蘭表達式(RPN)。
- 上面表達式(1 - 2) * (4 + 5) 的波蘭表達式是這樣的: 1 2 - 4 5 + *
- 這種表達式人類是不太好接受的,不過對計算機來說,利用棧的特點,就可以將這種后綴表達式的性能發揮到極致。
用棧來求解表達式(1 - 2) * (4 + 5)的值
- 數字1和2進棧,遇到減號運算符這彈出兩個元素進行運算并把結果入棧。
圖片.png
- 4和5入棧,遇到加號運算符,4和5彈出棧,相加后將結果9入棧。
圖片.png
- 然后又遇到乘法運算符,將9和-1彈出棧進行乘法計算,此時棧空并無數據壓棧,-9為最終運算結果。
圖片.png
- 正常的表達式 --->逆波蘭表達式
a + b ---> a b +
a + (b - c) ---> a b v - +
a + (b - c) * d ---> a b c - d * +
*代碼實現(c語言)
#include<stdafx.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
#define MAXBUFFER 10
typedef double ElemType;
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)
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
//入棧操作要在棧頂進行,每次向棧中壓入一個數據,top指針就要+1,直到棧滿為止
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)
{
exit(0);
}
}
*(s->top) = e;
s->top++;
}
//出棧操作就是在棧頂取出數據,棧頂指針隨之下移操作,每當從棧內彈出一個數據,棧的當前容量就-1
void Pop(sqStack *s, ElemType *e) {
if (s->top == s->base)
{
return;
}
*e = *--(s->top);
}
//求棧的長度
int StackLen(sqStack s) {
return s.top - s.base;
}
//清空棧, 將棧中的元素全部作廢,但棧本身物理空間并不發生改變(不是銷毀)
void ClearStack(sqStack *s) {
s->top = s->base;
}
//銷毀一個棧, 釋放掉該棧所占據的物理內存空間
void DestoryStack(sqStack *s) {
int i, len;
len = s->stackSize;
for (i = 0; i < len; i++)
{
free(s->base);
s->base;
}
s->base = s->top = NULL;
s->stackSize = 0;
}
int main() {
sqStack s;
char c;
double d, e;
char str[MAXBUFFER];
int i = 0;
InitStack(&s);
printf("請按逆波蘭表達式輸入待計算的數據,數據與運算符之間用空格隔開,以#作為結束標志:");
scanf_s("%c", &c);
while (c != '#')
{
while (isdigit(c) || c == '.')
{
str[i++] = c;
str[i] = '\0';
if (i >= 10)
{
printf("出錯,輸入的單個數據過大!\n");
return -1;
}
scanf_s("%c", &c);
if (c == ' ')
{
d = atof(str);
Push(&s, d);
i = 0;
break;
}
}
switch (c)
{
case '+':
Pop(&s, &e);
Pop(&s, &d);
printf("****%f***%f\n", e, d);
Push(&s, d + e);
break;
case '-':
Pop(&s, &e);
Pop(&s, &d);
Push(&s, d - e);
break;
case '*':
Pop(&s, &e);
Pop(&s, &d);
Push(&s, d * e);
break;
case '/':
Pop(&s, &e);
Pop(&s, &d);
if (e != 0)
{
Push(&s, d / e);
}
else
{
printf("\n出錯:除數為0!\n");
}
break;
default:
break;
}
scanf_s("%c", &c);
}
Pop(&s, &d);
printf("最終的計算結果是:%f", d);
getchar();
getchar();
getchar();
return 0;
}
中綴表達式轉換成逆波蘭表達式
人類喜歡這樣的表達式:(1-2)*(4+5)
而不是這樣的:1 2 - 4 5 + *
- 下面我們來動手寫一個中綴表達式轉換成后綴表達式的計算器:
轉換規則: 從左到右遍歷中綴表達式的每個數字和符號,若是數字則直接輸出,若是符號,則判斷其與棧頂符號的優先級,是右括號或者優先級低于棧頂符號,則棧頂元素依次出棧并輸出,直到遇到左括號或棧空才將其入棧。 - 代碼實現
int main() {
sqStack s;
char c, e;
InitStack(&s);
printf("請輸入中綴表達式,以#作為結束標志:");
scanf_s("%c", &c);
while (c != '#')
{
while (c >= '0' && c < '9')
{
printf("%c", c);
scanf_s("%c", &c);
if (c < '0' || c > '9')
{
printf(" ");
}
}
if(')' == c)
{
Pop(&s, &e);
while ('(' != e)
{
printf("%c", 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
{
printf("%c", e);
}
} while (StackLen(s) && '(' != e);
Push(&s, c);
}
}
else if('*' == c || '/' == c || '(' == c)
{
Push(&s, c);
}
else if('#' == c)
{
break;
}
else
{
printf("\n出錯,輸入格式錯誤!\n");
return -1;
}
scanf_s("%c", &c);
}
while (StackLen(s))
{
Pop(&s, &e);
printf("%c", e);
}
getchar();
getchar();
getchar();
return 0;
}