p.s. 代碼中的List是自己寫的頭文件,也可以用std的list
#pragma once
#include"List.h"
using std::string;
//字符串分割
class Arithmetic{
private:
static List<string> segement_string(const string& expression) {
List<string> res;
string temp_num;
for (int j = 0; j < expression.size(); j++) {
char temp = expression[j];
if (temp == '(' || temp == ')' ||
temp == '+' || temp == '-' ||
temp == '*' || temp == '/') {//如果是運算或者是括號就檢查前面是否有數字,如果有就先壓入數字,清空,最后壓入當前的符號
if (!temp_num.empty()) {
res.push_back(temp_num);
temp_num.clear();
}
res.push_back(string(1, temp));
}
else {//如果是數字,壓入temp_num
temp_num.push_back(temp);
}
}
if (!temp_num.empty()) {//如果結尾不是符號,就要考慮還有沒有數字沒有壓入res
res.push_back(temp_num);
return res;
}
}
//比較運算符的優先級,a是棧外運算符,b是棧外運算符
static bool isprior(const char& a, const char& b) {
if (b == '(')
if (a != ')') return true;
if (b == '+' || b == '-') {
if (a == '(' || a == '*' || a == '/') return true;
}
if (b == '*' || b == '/') {
if (a == '(') return true;
}
return false;
}
//將中綴表達式改為后綴表達式
static List<string> infix2postfix(const List<string>& infix) {
List<string> postfix;
List<string> temp;
List<string>::const_iterator iter;
for (iter = infix.begin(); iter != infix.end(); iter++) {
if (*iter == "(" || *iter == ")" ||//如果是符號的話,在temp為空或優先級大於temp棧頂的優先級時,對temp進行入棧操作,否則出棧
*iter == "+" || *iter == "-" ||
*iter == "*" || *iter == "/") {
while (!(temp.empty() || isprior((*iter)[0], temp.back()[0]))) {
if (temp.back() == "(") {//"("是不進入post,並且能讓"("出棧的只能是")",一個")"只能讓一個"("出棧,所以需要break
temp.pop_back();
break;
}
else {
postfix.push_back(temp.back());
temp.pop_back();
}
}
if (*iter != ")")//")"是不進入post的
temp.push_back(*iter);
}
else//是數值的話直接壓入post
postfix.push_back(*iter);
}
while (!temp.empty()) {//最后將棧中的符號都壓入post
postfix.push_back(temp.back());
temp.pop_back();
}
return postfix;
}
//將字符串轉為double類型的量
static double string2num(const string& str) {
double res = 0;
double isInt = 0;
for (int i = 0; i < str.size(); i++) {
unsigned int temp;
if (str[i] != '.') {
temp = str[i] - '0';
if (isInt == 0) {
res = res * 10 + temp;
}
else {
res += temp * isInt;
isInt *= 0.1;
}
}
else
isInt = 0.1;
}
return res;
}
//后綴表達式的計算
static double postfixCompute(const List<string>& postfix_expression) {
double temp1;
double temp2;
List<double> temp;
List<string>::const_iterator iter;
for (iter = postfix_expression.begin(); iter != postfix_expression.end(); iter++) {
if (*iter == "+") {
temp2 = temp.back();
temp.pop_back();
temp1 = temp.back();
temp.pop_back();
temp.push_back(temp1 + temp2);
}
else if (*iter == "-") {
temp2 = temp.back();
temp.pop_back();
temp1 = temp.back();
temp.pop_back();
temp.push_back(temp1 - temp2);
}
else if (*iter == "*") {
temp2 = temp.back();
temp.pop_back();
temp1 = temp.back();
temp.pop_back();
temp.push_back(temp1 * temp2);
}
else if (*iter == "/") {
temp2 = temp.back();
temp.pop_back();
temp1 = temp.back();
temp.pop_back();
temp.push_back(temp1 / temp2);
}
else
temp.push_back(string2num(*iter));
}
return temp.back();
}
public:
//計算四則運算
static double arithmetic(const string& expression) {
List<string> segments = segement_string(expression);
List<string> postfix_expression = infix2postfix(segments);
return postfixCompute(postfix_expression);
}
};
