表示式求值之雙陣列

2021-09-05 10:27:16 字數 1365 閱讀 8502

分析:由於課設要求用c語言,我們就用乙個double型別的陣列存運算數,乙個char型別陣列存運算子,乙個char型別陣列存表示式字串。具體**如下:

建立乙個priority.h標頭檔案,**如下:

#ifndef _priority_h_

#define _priority_h_

//求運算子優先順序

int priority(char o)

}//判斷是否為運算子

int isoperator(char c)

//判斷是否為數字

int isnum(char c)

#endif

建立乙個main.c檔案,**如下:

#include #include #include #include #include "priority.h"

#define maxsize 100

double num[maxsize]; //數字陣列

char op[maxsize]; //字元陣列

int inum = -1, iop = -1; //兩個陣列的下標

char s[512], *ps = s; //輸入的字串和遍歷該字串的指標

double x, y; //x,y:兩數字

char o; //o:運算子

int flag = 0; //0:當前運算不在括號內, 1:當前運算在括號內

//字串轉double

void donum()

if(*ps == '.') }

num[++inum] = atof(numtemp);

}//加減乘除運算

void dooperator()

}int main()

if(leftkh != rightkh)

ps = s;

while(*ps) //解析字串併入陣列

else if(isoperator(*ps)) //如果是運算子

iop--;

}else if(priority(*ps) > priority(o)) //如果當前運算子優先順序大於運算子陣列尾部的運算子優先順序

else

op[++iop] = *ps;}}

else op[++iop] = *ps;

} else

ps++; }

while(iop != -1)

dooperator();

printf("結果為:%lf\n", num[inum]);

system("pause");

return 0;

}

表示式求值問題之表示式樹

include include include include define m 1005 define clr arr,now memset arr,now,sizeof arr using namespace std stackshu typedef struct node tire,t cha...

表示式求值

程式的說明見清華大學出版社 資料結構 c語言版 include include define stack init size 40 define stackincrement 20 define ok 1 define false 0 typedef structs stack typedef st...

表示式求值

既然是表示式求值,自然需要在記憶體中儲存計算結果以及中間值。在 用c語言寫直譯器 一 中提過 變數要求是若型別,而 c 語言中的 view plaincopy to clipboardprint?in basic io.h define memery size 26 typedef enum var...