国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

字符串表達(dá)式計(jì)算C#程序設(shè)計(jì)

2024-07-21 02:17:37
字體:
供稿:網(wǎng)友

在編程應(yīng)用程序過程中,有時(shí)需要字符串表達(dá)式的值。如字符串:"23+56/(102-100)*((36-24)/(8-6))",結(jié)果=191。
      根據(jù)數(shù)據(jù)結(jié)構(gòu)棧的應(yīng)用介紹,通過把表達(dá)式由中序式轉(zhuǎn)換成后序式,再用棧來進(jìn)行計(jì)算。如上述字符串表達(dá)式:"23+56/(102-100)*((36-24)/(8-6))",轉(zhuǎn)換為后序時(shí)為:"23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+"(其中字符"|"為分隔符)。
      本程序代碼如下:在visual .net 2003 +winxp下編譯通過。

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;

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;
  }
 
 }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 区。| 偏关县| 广元市| 贵阳市| 赤水市| 荣昌县| 黄骅市| 牡丹江市| 麻阳| 周口市| 华亭县| 赣榆县| 贵德县| 乳源| 肃宁县| 新津县| 通河县| 高州市| 襄城县| 湄潭县| 庄浪县| 招远市| 吐鲁番市| 上杭县| 大冶市| 仁怀市| 绥滨县| 涞水县| 遵化市| 陕西省| 林周县| 邢台市| 内乡县| 轮台县| 玉田县| 罗平县| 遵义县| 凤山县| 靖远县| 顺义区| 罗定市|