您的位置:首页 > 其它

使用字符串解析的方式完成计算器的设计思路

2015-09-12 19:58 513 查看
声明:

一个正确的计算串是格式良好的,如:{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);
}


在此希望有感兴趣的同行,非同行提出建议或增加功能,不要忘了,联系我。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: