整數四則混合混算的運算式計算

來源:互聯網
上載者:User

本文實現整數的四則混合混算。比如:輸入(5+8*4+7)*9-3*(13+2*6),返回計算結果為321。

 

思路:正向掃描運算式,使用兩個棧分別儲存整數和符號,有括弧的先計算括弧中的值。遇到乘除法先計算。經過以上計算後得到最後的式子為只有加減法的無括弧式子。再計算最後結果。

 

流程描述:

1.      掃描字元t。

2.      若為’\0’,跳到4;若為’*/+)’,入符號棧;若為’-‘,轉化為(-1)*,分別入數字棧和符號棧;若為數字’0-9’,向前找到不是’0-9’的符號,轉化為整數,入數字棧;若為’)’,符號棧依次出棧計算數字棧頂兩元素後入數字棧,直到遇到’(‘出棧,跳到3。

3.      t++,跳回1。

4.      符號棧依次出棧計算數字棧資料,直到符號棧為空白。跳到5。

5.      數字棧棧頂元素為結果。結束。

輸入:符合四則運算的整數數字運算運算式,可以有括弧,負數。運算式合法性不在此程式驗證範圍之內,如果需要驗證請在外圍加驗證函式。

輸出:運算運算式結果。

限制:運算式使用的數字棧和符號棧的大小會限制計算的運算式範圍;大數加減乘除不在此程式處理;

 

使用例子:

char expession[] = "(5+8*4+7)*9-3*(13+002*6)";int ret = calculate_expression(expession);

程式碼:說明,如果想更清楚的瞭解運算過程和棧的變化,請自行加上棧資料列印函數。

 

程式碼:說明,如果想更清楚的瞭解運算過程和棧的變化,請自行加上棧資料列印函數。

#define ERROR_VALUE 0xffffffff#define MAX_STACK 0x10//數字棧int int_stack[MAX_STACK];int is_top=-1;//數字入棧bool is_push(int i){if (is_top<(MAX_STACK-1)){int_stack[++is_top]=i;return true;}return false;};//數字出棧int is_pop(){if (is_top>=0){int p = int_stack[is_top];is_top--;return p;}return ERROR_VALUE;};//符號棧char char_stack[MAX_STACK];int cs_top = -1;//符號入棧bool cs_push(char ch){if (cs_top<(MAX_STACK-1)){char_stack[++cs_top] = ch;return true;}return false;};//符號出棧char cs_pop(){if (cs_top>=0){char p = char_stack[cs_top];cs_top--;return p;}return -1;};//二元運算式計算int calculate(int left,int right,char exp){if (exp == '*'){return right*left;}if (exp == '/'){return left/right;}if (exp == '+'){return left+right;}if (exp == '-'){return left -right;}return ERROR_VALUE;};//四則混合運算式計算int calculate_expression(char *expession){char *t = expession;char m;int n;while(*t!='\0'){switch (*t){case '+':case '*':case '/':case '(':cs_push(*t);break;case '-'://使用"+(-1)*"來代替"-",可以避免*(-3),這樣的負數帶來的影響cs_push('+');cs_push('*');is_push(-1);break;case ')'://資料棧頂兩個元素用運算子棧頂的元素計算後入資料棧,要直找到'('的位置才結束計算m = cs_pop();do {int r = is_pop();int l = is_pop();n = calculate(l,r,m);is_push(n);m = cs_pop();} while (m!='(');//把括弧左邊的最近的乘除法先計算出來,注意,加減不能算,因為括弧後面還有可能有乘除法。m = cs_pop();if ( m=='*' || m == '/'){int r = is_pop();int l = is_pop();n = calculate(l,r,m);is_push(n);}else if (m>0){cs_push(m);}break;case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':{//如果有連續-9數字則組合為一個數字進棧char str[8];int i = 0;while( *t>='0' && *t <= '9'){str[i] = *t;i++;t++;}str[i] = '\0';t--;n = atoi(str);is_push(n);//把數值左邊的最近的乘除法先計算出來,注意,加減不能算,因為數值後面還有可能有乘除法。m = cs_pop();if ( m=='*' || m == '/'){int r = is_pop();int l = is_pop();n = calculate(l,r,m);is_push(n);}else if (m>0){cs_push(m);}}break;default:printf("expression input error!");break;}t++;}//上面已經做完所有乘法和括弧的運算,若還有加減法沒做完,把剩下的工作做完while ((m = cs_pop())>0){int r = is_pop();int l = is_pop();n = calculate(l,r,m);is_push(n);}printf("the result is %d",n);return n;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.