String expressions calculate C # programming

xiaoxiao2021-03-06  38

During programming applications, the value of string expressions sometimes requires. Such as string: "23 56 / (102-100) * ((36-24) / (8-6))", result = 191. According to the application of the data structural stack, by converting the expression from the middle order to the reemption, then the stack is calculated. As described above: "23 56 / (102-100) * ((36-24) / (8-6))", the conversion is: "23 | 56 | 102 || | / | * | 36 | 24 | - | 8 | 6 | - | / | * | "(in the character" | "is a separator). This program code is as follows: Compiling the pass under Visual .NET 2003 WinXP. Using system.drawing; using system.componentmodel; using system.windows.form; using system.data;

Namespace stringexpressioncalcalculate {///

/// Form1 summary description. /// 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; ///

/// The required designer variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// 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 = "Calculation (& 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 = "string:"; /// 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 = "calculation results:"; /// 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

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.run (new form1 ());}

Private void button1_click (Object sender, system.eventargs e) {

TextBox2.text = CalculateParentheseSexpression ());

// The middle order is converted into a sequential expression and then calculated //, such as: 23 56 / (102-100) * ((36-24) / (8-6)) // Convert to: 23 | 56 | 102 | 100 | - | / | * | 36 | 24 | - | 8 | 6 | - | / | * | {利 利 利 栈 方式(); String Operator1; String Expressionstring = ""; string operand3; expression = expression.replace (",", "); while (expression.length> 0) {OPERAND3 =" "; // Take Digital Processing IF (char .Isnumber (expression [0])) {while (expression [0])) {OPERAND3 = Expression [0] .tostring (); expression = expression.substring (1); if (expression == " ").

} Expressionstring = OPERAND3 "|";

// Take "C" processing if (Expression.Length> 0 && express [0] .tostring () == "(") {OperatorList.Add ("); Expression = Expression.Substring (1);} / / Take ")" Process 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);

// Remove the operating symbol processing 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.add (operator1); Expressionstring = OperatorList. Add (Operator1);} Expression = expression.substring (1);}} Operand3 = ""; while (operatorlist.count! = 0) {OperatorList.count -1] .tostring () "| "OperatorList.Remove AT (operatorlist.count -1);

Expressionstring = OPERAND3.SUBSTRING (0, Operand3.Length -1);;

Return CalculateparentheseSexPressionEx (Expressionstring);

}

// Step 2: Calculate the formula of the conversion into a sequential expression // 23 | 56 | 102 || - | / | * | 36 | 24 | - | 8 | 6 | - | / | * | " private string CalculateParenthesesExpressionEx (string Expression) {// definition of two stacks ArrayList operandList = new ArrayList (); float operand1; float operand2; string [] operand3; Expression = Expression.Replace ( "", ""); operand3 = Expression. Split ("|")); for (int i = 0; i

// Judgment two operator priority prior level private bool verifyOperatorPriority (string operator1, string operator2) {if (operator1 == "*" && operator2 == " ") Return True; else1 == "*" && operator2 == "-") Return True; Else IF (Operator1 == "&& operator2 ==" ") Return True; Else IF (Operator1 ==" / "&& operator2 ==" - ") Return True; Else Return false;}

转载请注明原文地址:https://www.9cbs.com/read-71122.html

New Post(0)