單棧實現逆波蘭計算器

2021-08-14 13:19:02 字數 2579 閱讀 6109

//標頭檔案
#define _crt_secure_no_warnings

#include #include #include #include #define error 0

#define ok 1

#define yes 1

#define no 0

typedef int status;

typedef struct stacknode

stacknode;

typedef struct linkstack

linkstack;

linkstack* link;

//輸入鍊錶並轉換為字尾表示式

void getinput(char* rpn,char* copy);

//將字尾表示式轉換為結果

double getanswer(char *rpn);

//判斷是否為運算子,並返回運算子等級

int isoperator(char s);

//判斷是什麼運算子,並返回

int whatoperator(char s);

//判斷棧空

status stackempty(linkstack *s);

//鏈棧初始化

void inistack(void);

//出棧字元

status popchar(linkstack *s,char *e);

//進棧字元

status pushchar(linkstack *s, char e);

//出棧數字

status popnum(linkstack *s, double *e);

//進棧數字

status pushnum(linkstack *s, double e);

//實現檔案
#include "rpn.h"

//輸入鍊錶並轉換為字尾表示式

void getinput(char* rpn, char* copy)

else if (isoperator(rpn[i]) == 3)

popchar(link, temp);

button--;

}else if (isoperator(rpn[i]) == 2)}}

pushchar(link, rpn[i]);

}else if (isoperator(rpn[i]) == 1)}}

pushchar(link, rpn[i]);}}

else}}

if (!isoperator(rpn[i]))

while (link->count != 0)

}//將字尾表示式轉換為結果

double getanswer(char *copy)

i++;

}if (!isoperator(copy[i]) && copy[i] != ' ')

pushnum(link, atof(backup));

}if (copy[i] == ' ')

}return link->top->num;

}//判斷是否為運算子,並返回運算子等級

int isoperator(char s)

else if (s == '*' || s == '/')

else if (s == '(')

else if (s == ')')

else

return result;

}//判斷是什麼運算子,並返回

int whatoperator(char s)

else if (s == '-')

else if (s == '*')

else if (s == '/')

else

}//判斷棧空

status stackempty(linkstack *s)

else

}//鏈棧初始化

void inistack(void)

//出棧字元

status popchar(linkstack *s,char *e)

*e = s->top->data;

p = s->top;

s->top = s->top->next;

free(p);

p = null;

s->count--;

return ok;

}//進棧字元

status pushchar(linkstack *s, char e)

//進棧數字

status pushnum(linkstack *s, double e)

//出棧數字

status popnum(linkstack *s, double *e)

*e = s->top->num;

p = s->top;

s->top = s->top->next;

free(p);

p = null;

s->count--;

return ok;

}

棧之逆波蘭計算器

逆波蘭表示式又叫做字尾。在通常的表示式中,二元運算子總是置於與之相關的兩個運算物件之間,這種表示法也稱為中綴表示。波蘭邏輯學家j.lukasiewicz於1929年提出了另一種表示表示式的方法,按此方法,每一運算子都置於其運算物件之後,故稱為字尾表示。逆波蘭表示式,它的語法規定,表示式必須以逆波蘭表...

C語言棧實現逆波蘭計算器

逆波蘭計算器 輸入所要計算的表示式的逆波蘭式,並進行計算。如 1 2 4 5 其逆波蘭式 1 2 4 5 基礎的東西,還是要多敲 如下 中,輸入的格式如 1 2 4 5 include include include define stack init size 20 define stack in...

逆波蘭計算器

include include include define stack init size 20 初始化棧的空間 define stackincrement 10 擴充套件空間 define maxbuffer 10 最大緩衝區 typedef double elemtype 建立乙個棧 type...