聲明:
一個正確的計算串是格式良好的,如:{ln(10)+5*[cos(2π+1/4π)+sin(1+2+3π)]}。
計算是將一系列不相關的數據聯系或整合為一個數字,這就是計算的意義。
正文:
正如事例所示,一個正確的計算串包括以下幾種格式: 數字(0~9)、字母(a~z)、希臘字符(π Pai)、括號({}、[]、())、四則運算符(+、-、*、/)。
那麼按照正常人類的思考方式,去吧字符串進行優先級區分,將優先級最高的一段串計算為數值,循環如此知道沒有優先級最高的操作符的時候(僅剩下一個數),計算完成,返回結果。
不完整功能代碼如下
//聲明:目前所有操作符都是單字符型
//根據合理的字符串,返回值
private decimal getValue(string str)
{
decimal result = 0;//存儲要返回的結果
int operatorIndex = -1;//存儲當前操作符在字符串中的索引
string operatorType = "";//存儲當前操作符的類型
operatorIndex = getOperatorIndex(str);
operatorType = getOperatorType(operatorIndex, str);
int operator0Index = -1;//存儲當前操作符前一個操作符0在字符串中的索引
//string operator0Type = "";//存儲當前操作符的前一個操作符0的類型
operator0Index = getOperator0Index(str, operatorIndex);
//operator0Type = getOperatorType(operator0Index, str);
int operator1Index = -1;//存儲當前操作符後一個操作符1在字符串中的索引
//string operator1Type = "";//存儲當前操作符的後一個操作符1的類型
operator1Index = getOperator1Index(str, operatorIndex);
//operator1Type = getOperatorType(operator1Index, str);
double num1 = 0.1000000000000000000001;//存儲第一個操作數的值
double num2 = 0.2000000000000000000002;//存儲第二個操作數的值
num1 = getNum1(operatorIndex, operator0Index, str);
num2 = getNum2(operatorIndex, operator1Index, str);
result = Convert.ToDecimal(getResult(num1, num2, operatorType));
return result;
}
//目前字符串以數字開頭,找到優先操作符,並返回在字符串中的索引
private int getOperatorIndex(string str)
{
int operatorIndex = -1;//存儲第一個操作符所在的索引
for (int i = 0; i < str.Length; i++)
{
switch (str[i])
{
case '*':
{
operatorIndex = i;
} break;
case '/':
{
operatorIndex = i;
} break;
case '+':
{
operatorIndex = i;
} break;
case '-':
{
operatorIndex = i;
} break;
}
}
return operatorIndex;
}
//根據優先操作符索引,操作符所在字符串,返回前一個操作符的索引,有則返回索引,無則返回0
private int getOperator0Index(string str, int operatorIndex)
{
int operator0Index = -1;//存儲前一個操作符所在的索引
for (int i = operatorIndex - 1; i >= 0; i--)
{
switch (str[i])
{
case '*':
{
operator0Index = i;
} break;
case '/':
{
operator0Index = i;
} break;
case '+':
{
operator0Index = i;
} break;
case '-':
{
operator0Index = i;
} break;
}
}
if (operator0Index == -1)
{
operator0Index = 0;
}
return operator0Index;
}
//根據優先操作符索引,操作符所在字符串,返回後一個操作符的索引,有則返回索引,無則返回字符串最後有一位的索引
private int getOperator1Index(string str, int operatorIndex)
{
int operator1Index = -1;//存儲後一個操作符所在的索引
for (int i = operatorIndex + 1; i < str.Length - 1; i++)
{
switch (str[i])
{
case '*':
{
operator1Index = i;
} break;
case '/':
{
operator1Index = i;
} break;
case '+':
{
operator1Index = i;
} break;
case '-':
{
operator1Index = i;
} break;
}
}
if (operator1Index == -1)
{
operator1Index = str.Length - 1;
}
return operator1Index;
}
//根據操作符的索引,返回操作符的類型
private string getOperatorType(int operatorIndex, string str)
{
string operatorType = "";
if (operatorIndex != 0 || operatorIndex != str.Length)
{
operatorType = str[operatorIndex].ToString();
}
return operatorType;
}
//根據當前操作符的索引,前一個操作符的索引,操作符所在字符串,返回當前操作符前一個操作數
private double getNum1(int OperatorIndex, int Operator0Index, string str)
{
double num1 = 0.10000000000000000000001;
num1 = Convert.ToDouble(str.Substring(Operator0Index, OperatorIndex - Operator0Index));
return num1;
}
//根據當前操作符的索引,後一個操作符的索引,操作符所在字符串,返回當前操作符後一個操作數
private double getNum2(int OperatorIndex, int Operator1Index, string str)
{
double num2 = 0.200000000000000000000002;
num2 = Convert.ToDouble(str.Substring(OperatorIndex + 1, Operator1Index - OperatorIndex));
return num2;
}
//根據兩個操作數和一個操作符,返回結果
private double getResult(double num1, double num2, string operatorType)
{
double result = 0.000000000000000000000000001;
switch (operatorType)
{
case "+":
{
result = num1 + num2;
} break;
case "-":
{
result = num1 - num2;
} break;
case "*":
{
result = num1 * num2;
} break;
case "/":
{
result = num1 / num2;
} break;
}
return result;
}
private void btn_Empty_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void btn_Back_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
在此希望有感興趣的同行,非同行提出建議或增加功能,不要忘了,聯系我。