namespace stringexpressioncalculate
{
/// <summary>
/// form1 的摘要說明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.textbox textbox1;
private system.windows.forms.button button1;
private system.windows.forms.textbox textbox2;
private system.windows.forms.label label1;
private system.windows.forms.label label2;
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗體設(shè)計(jì)器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void initializecomponent()
{
this.textbox1 = new system.windows.forms.textbox();
this.button1 = new system.windows.forms.button();
this.textbox2 = new system.windows.forms.textbox();
this.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.suspendlayout();
//
// textbox1
//
this.textbox1.location = new system.drawing.point(114, 33);
this.textbox1.name = "textbox1";
this.textbox1.size = new system.drawing.size(273, 21);
this.textbox1.tabindex = 0;
this.textbox1.text = "23+56/(102-100)*((36-24)/(8-6))";
//
// button1
//
this.button1.location = new system.drawing.point(180, 144);
this.button1.name = "button1";
this.button1.size = new system.drawing.size(84, 27);
this.button1.tabindex = 1;
this.button1.text = "計(jì)算(&c)";
this.button1.click += new system.eventhandler(this.button1_click);
//
// textbox2
//
this.textbox2.location = new system.drawing.point(114, 72);
this.textbox2.name = "textbox2";
this.textbox2.size = new system.drawing.size(273, 21);
this.textbox2.tabindex = 2;
this.textbox2.text = "";
//
// label1
//
this.label1.autosize = true;
this.label1.location = new system.drawing.point(48, 36);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(54, 17);
this.label1.tabindex = 3;
this.label1.text = "字符串:";
//
// label2
//
this.label2.autosize = true;
this.label2.location = new system.drawing.point(39, 75);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(66, 17);
this.label2.tabindex = 3;
this.label2.text = "計(jì)算結(jié)果:";
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(442, 221);
this.controls.add(this.label1);
this.controls.add(this.textbox2);
this.controls.add(this.button1);
this.controls.add(this.textbox1);
this.controls.add(this.label2);
this.name = "form1";
this.text = "form1";
this.resumelayout(false);
}
#endregion
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void button1_click(object sender, system.eventargs e)
{
textbox2.text = calculateparenthesesexpression(textbox1.text.tostring());
}
//中序轉(zhuǎn)換成后序表達(dá)式再計(jì)算
// 如:23+56/(102-100)*((36-24)/(8-6))
// 轉(zhuǎn)換成:23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+"
//以便利用棧的方式都進(jìn)行計(jì)算。
private string calculateparenthesesexpression(string expression)
{
arraylist operatorlist = new arraylist();
string operator1;
string expressionstring = "";
string operand3;
expression = expression.replace(" ","");
while(expression.length > 0)
{
operand3 = "";
//取數(shù)字處理
if(char.isnumber(expression[0]))
{
while(char.isnumber(expression[0]))
{
operand3 += expression[0].tostring() ;
expression = expression.substring(1);
if(expression == "")break;
}
expressionstring += operand3 + "|";
}
//取“c”處理
if(expression.length >0 && expression[0].tostring() == "(")
{
operatorlist.add("(");
expression = expression.substring(1);
}
//取“)”處理
operand3 = "";
if(expression.length >0 && expression[0].tostring() == ")")
{
do
{
if(operatorlist[operatorlist.count -1].tostring() != "(")
{
operand3 += operatorlist[operatorlist.count -1].tostring() + "|" ;
operatorlist.removeat(operatorlist.count - 1) ;
}
else
{
operatorlist.removeat(operatorlist.count - 1) ;
break;
}
}while(true);
expressionstring += operand3;
expression = expression.substring(1);
}
//取運(yùn)算符號(hào)處理
operand3 = "";
if(expression.length >0 && (expression[0].tostring() == "*" || expression[0].tostring() == "/" || expression[0].tostring() == "+" || expression[0].tostring() == "-"))
{
operator1 = expression[0].tostring();
if(operatorlist.count>0)
{
if(operatorlist[operatorlist.count -1].tostring() == "(" || verifyoperatorpriority(operator1,operatorlist[operatorlist.count - 1].tostring()))
{
operatorlist.add(operator1);
}
else
{
operand3 += operatorlist[operatorlist.count - 1].tostring() + "|";
operatorlist.removeat(operatorlist.count - 1);
operatorlist.add(operator1);
expressionstring += operand3 ;
}
}
else
{
operatorlist.add(operator1);
}
expression = expression.substring(1);
}
}
operand3 = "";
while(operatorlist.count != 0)
{
operand3 += operatorlist[operatorlist.count -1].tostring () + "|";
operatorlist.removeat(operatorlist.count -1);
}
expressionstring += operand3.substring(0, operand3.length -1); ;
return calculateparenthesesexpressionex(expressionstring);
}
// 第二步:把轉(zhuǎn)換成后序表達(dá)的式子計(jì)算
//23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+"
private string calculateparenthesesexpressionex(string expression)
{
//定義兩個(gè)棧
arraylist operandlist =new arraylist();
float operand1;
float operand2;
string[] operand3;
expression = expression.replace(" ","");
operand3 = expression.split(convert.tochar("|"));
for(int i = 0;i < operand3.length;i++)
{
if(char.isnumber(operand3[i],0))
{
operandlist.add( operand3[i].tostring());
}
else
{
//兩個(gè)操作數(shù)退棧和一個(gè)操作符退棧計(jì)算
operand2 =(float)convert.todouble(operandlist[operandlist.count-1]);
operandlist.removeat(operandlist.count-1);
operand1 =(float)convert.todouble(operandlist[operandlist.count-1]);
operandlist.removeat(operandlist.count-1);
operandlist.add(calculate(operand1,operand2,operand3[i]).tostring()) ;
}
}
return operandlist[0].tostring();
}
//判斷兩個(gè)運(yùn)算符優(yōu)先級(jí)別
private bool verifyoperatorpriority(string operator1,string operator2)
{
if(operator1=="*" && operator2 =="+")
return true;
else if(operator1=="*" && operator2 =="-")
return true;
else if(operator1=="/" && operator2 =="+")
return true;
else if(operator1=="/" && operator2 =="-")
return true;
else
return false;
}
//計(jì)算
private float calculate(float operand1, float operand2,string operator2)
{
switch(operator2)
{
case "*":
operand1 *= operand2;
break;
case "/":
operand1 /= operand2;
break;
case "+":
operand1 += operand2;
break;
case "-":
operand1 -= operand2;
break;
default:
break;
}
return operand1;
}
}
}
新聞熱點(diǎn)
疑難解答
圖片精選