請實現如下介面
/* 功能:四則運算
* 輸入:strexpression
:字串格式的算術表示式,如
: "3+2*"
* 返回:
算術表示式的計算結果
*/
public
static
intcalculate(string strexpression)
約束:pucexpression
字串中的有效字元包括
[『0』-『9』],『+』,『-』, 『*』,『/』 ,『(』, 『)』,『[』, 『]』,『』。
pucexpression
算術表示式的有效性由呼叫者保證;
輸入描述:
輸入乙個算術表示式輸出描述:
得到計算結果輸入例子:
3+2*輸出例子:
25
思路:中綴轉字尾;負數添0;
#include
#include
#include
#include
using namespace std;
int is_parenthese(char c) //判斷是否為小括號
else return 0;
}int perior(char c) //判斷並返回操作符的優先順序
void check(char c, stack &cnt2, deque &cnt3) //判斷操作符的優先順序
if (is_parenthese(c))
else
cnt2.pop();}}
else
else}}
void allocate(deque &cnt1, stack &cnt2, deque &cnt3) //將中綴轉字尾
else
}while (!cnt2.empty())
}void calculate(deque &cnt3, stack &cnt4)
else}}
}int main()
if (str[i] == ']' || str[i] == '}')
}//將所有負數前面補0,以便後續計算
for (int i = 1; i < str.size(); i++)
if (str[i] == '-' && str[i - 1] == '(')
}for (int i = 0; i < str.size(); i++)
allocate(cnt1, cnt2, cnt3); //中綴轉字尾
cout << "check字尾的輸出結果是否正確:" << endl;
for (int j = 0; j < cnt3.size(); j++)
cout << endl;
calculate(cnt3, cnt4);
cout << "計算結果為:" << endl;
cout << cnt4.top() << endl;
return 0;
}
小學四則運算程式設計
內容 1 引入隨機函式,隨機生成四種運算型別和需要運算的數值 import random def szys sym a random.randint 0,3 n1 random.randint 1,20 n2 random.randint 1,20 result 0 if a 0 result n1...
python四則運算程式 四則運算(Python)
四則運算程式 一 資訊 二.題目要求 寫乙個能自動生成小學四則運算題目的程式,然後在此基礎上擴充套件 除了整數以外,還要支援真分數的四則運算,例如 1 6 1 8 7 24 程式要求能處理使用者的輸入,判斷對錯,累積分數 程式支援可以由使用者自行選擇加 減 乘 除運算 三 import random...
程式設計練習 複數四則運算
題目描述 編寫乙個程式,其功能為 通過鍵盤讀取兩個複數和運算子,完成複數運算的操作,並輸出計算的結果 問題分析 a bi c di a bi c di a bi c di a bi c di 結果的實部 a ca c a c b d a c b d c c d d 結果的虛部 b db d b c ...