A small program that is written for a four-way expression.

zhaozj2021-02-16  74

// You can check the error, including a lot of grammatical errors and zero error. You can add modifications to expressions.

// This program is designed for arithmetic, the operand is assumed to be double.////This program can detect a lot of syntax errors, including illegal operands and illegal expression.//Also the error of divided by zero can be detected , When a Syntax Error Is Detected, A Warning Message Read // "Error! Illegal Expression!" Is Displayed, and The Result IS set to 0.///if the Button1 is Clicked When the Textbox of Expression IS Blank, A Warning Message Read "Error! Blank Expression!" // is displayed.///bit IS set That Illegal Characters Can't Be Input INTO The Expression.

Using system.drawing; using system.componentmodel; using system.windows.form; using system.data;

namespace WindowsApplication8 {///

/// Summary description for Form1 /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.Button button1; private System.Windows. .Forms.TextBox txtExp; private System.Windows.Forms.Label expLbl; private System.Windows.Forms.Label rstLbl; private System.Windows.Forms.TextBox txtRst; private System.Windows.Forms.Button button2; /// < Summary> /// Required Designer Variable. /// private system.componentmodel.container components = null;

Public form1 () {/// Required for Windows Form Designer Support // InitializationComponent ();

// // Todo: add any constructor code after initializationcomponent call //}

///

/// clean up any resources being used. /// protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {components.dispose () }} Base.Dispose (Disposing);

#region Windows Form Designer generated code ///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor /// private void InitializeComponent (). {This.Button1 = new system.windows.Forms.Button (); this.txtexp = new system.windows.forms.textbox (); this.explbl = new system.windows.forms.label (); this.rstlbl = New system.windows.forms.label (); this.txtrst = new system.windows.forms.textbox (); this.button2 = new system.windows.Forms.Button (); this.suspendlayout (); // / / Button1 // this.button1.ancy = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.Bottom | System.Windows.Forms.Anchorstyles.left)); this.button1.location = new system.drawing.point (112, 336); this.button1.name = "button1"; this.button1.size = new system.drawing.size (72, 24); this.button1.tabindex = 0; this .Button1.text = "calculate"; this.button1.click = new system.eventhandler (this. Button1_click; // // txtexp // this.txtexp.location = new system.drawing.point (168, 64); this.txtexp.name = "txtexp"; this.txtexp.size = new system.drawing.size (296, 21); this.txtexp.tabindex = 1; this.txtexp.text = ""; this.txtexp.keypress = new system.windows.Forms.KeyPressEventHandler (this.txtexp_keypress); // // Explbl / / This.explbl.location = new system.drawing.point (56, 64); this.explbl.name = "explbl"; this.explbl.size = new system.drawing.size (92, 23); this.explbl .TabINDEX = 2; this.explbl.text = "expression";

// // RSTLBL /// this.rstem.location = new system.drawing.point (56, 136); this.rstlbl.name = "rsTLBL"; this.rstelbl.size = new system.drawing.size (124, 23); this.rstlbl.tabindex = 3; this.rstlbl.text = "result"; /// TXTRST // this.txtrst.location = new system.drawing.point (168, 136); this.txtrst. Name = "txtrst"; this.txtrst.readonly = true; this.txtrst.size = new system.drawing.size (128, 21); this.txtrst.tabINDEX = 4; this.txtrst.text = "; / / // button2 // this.button2.ancy = (system.windows.form.Anchorstyles) ((System.Windows.Forms.Anchorstyles.Bottom | System.Windows.Forms.Anchorstyles.right)); this.button2 .Location = new system.drawing.point (360, 336); this.button2.name = "button2"; this.button2.tabindex = 5; this.button2.text = "clear"; this.button2.click = New system.eventhandler (this.button2_click); // // Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing .Size (592, 429); this.controls.add (this.button2); this.controls.add (this.TXTRST); this.controls.add (this.rstlbl); this.controls.add (this.Explbl This.controls.add (this.txtex); this.controls.add (this.button1); this.name = "form1"; this.text = "expressionEvaluation_lihui"; this.ResumeLayout (false);} #ENDREGION

///

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

class OpndStack {private ArrayList vectors; private int length; private double element; public OpndStack () {vectors = new ArrayList (20); length = 0;} public int getLength () {length = vectors.Count; return length;}

Public void Put (Double DBL) {VECTORS.ADD (DBL); Length = Length 1;}

Public void Pop () {vectors.removeat (length-1); length = length-1;}

Public double gettop () {element = (double) vectors [length-1]; return element;}

Public void traverse () {for (int i = 0; i

Public void clear () {for (int i = length-1; i> = 0; i -) {vectors.removeat (i);} //console.writeline ("all elements been removed ");}}

Class OptrsTack {Private INTLENGTORS; PRIVATELISTATION; PUBLICOPTRSTACK () {vectors = new arraylist (20); length = 0;}

Public int getLength () {length = product; return length;}

Public Void Put (Char DBL) {Vectors.Add (DBL); Length = Length 1;}

Public void Pop () {vectors.removeat (length-1); length = length-1;}

Public char Gettop () {element = (char) vectors [length-1]; return element;}

Public void traverse () {for (int i = 0; i

Public void clear () {for (int i = length-1; i> = 0; i -) {vectors.removeat (i);} //console.writeline ("all elements been removed ");}}

Class compare {private char [] Operators = new char [7]; private int index

Public compare () {index = 0; Operators [0] = ' '; Operators [1] = '-'; Operators [2] = '*'; Operators [3] = '/'; Operators [4] = '('; Operators [5] = ')'; Operators [6] = '#';} Public Bool Contains (CHAR E) {BOOL FOUND = false; INT i; for (i = 0; i <7&& Found == False; i ) IF (Operators [i] == e) Found = true; if (found == false) Return False; Else Return True;

Public int getIndex (Char e) {INT i; BOOL FOUND = false; for (i = 0; i <7 &&fact == false; i ) IF (Operators [i] == e) {index = i; found = true; }

Return Index;}}

Class expProcs {private char [,] p = new char [7,7]; private optherTack Optr; private opithck Opnd; private company com; public express express, () {// char [,] p = new char [7,7] ; P [0, 0] = '>'; P [0, 1] = '>'; P [0, 2] = '<'; p [0, 3] = '<'; p [0, 4 ] = '<'; p [0, 5] = '>'; p [0, 6] = '>'; p [1,0] = '<'; p [1, 1] = '<'; P [1, 2] = '<'; p [1,3] = '<'; p [1, 4] = '<'; p [1, 5] = '>'; P [1, 6] = '>'; P [2, 0] = '>'; p [2, 2] = '>'; p [2, 3] = '>'; P [2, 4] = '<'; p [2, 5] = '>'; P [2, 6] = '>'; p [3, 0] = '>'; p [3, 1] = '>'; p [3, 2] = '>'; p [3, 3] = '>'; p [3, 4] = '<'; p [3, 5] = '>'; p [ 3, 6] = '>'; p [4,0] = '<'; p [4, 1] = '<'; p [4, 2] = '<'; p [4, 3] = ' <'; p [4, 4] =' <'; P [4, 5] =' = '; P [4, 6] =' 0 '; P [5, 0] ='> '; P [5 1] = '>'; p [5, 2] = '>'; p [5, 4] = '0'; P [5, 5] = '> '; P [5, 6] ='> '; P [6, 0] =' <'; p [6, 1] =' <'; p [6, 2] =' <'; p [6, 3] = '<'; p [6, 4] = '<'; P [6, 5] = '0'; P [6, 6] = '='; Optr = New OPTRSTACK (); OPND = New OpndStack (); com = new compare ();

Public double execute (string e) {INT i = 0; bool errorfound = false; double numafterdot = 1; double d = 0; string expression; expression = e; //expression=form1.txtexp.text; expression = '#' Optr.clear (); opTr.clear (); optr.put ('#'); while (expression [i]! = '#' || optr.gettop ()! = '#') {I (char .Isnumber (Expression [i]) || expression [i] == '.') {While (expression [i]) || expression [i] == '.') {If (expression [i ]! = '.') {D = D * 10 (int) Expression [i] -48; i ;} else {i ; while (ratedot * = 10; D = D ((int) Expression [i] -48) / NumAfterDot; i ;} if (Expression [i] == '.') {MessageBox.show ("ERROR! ILLEGAL Expression!", "Form1", MessageBoxButtons. OK); errorfound = true;}}} f (errorfound == true) Break;} NumAfterDot = 1; opnd.put (d); D = 0;} if (ErrorFound == true) Break; if (! char.isnumber (expression [i]) && expression [i]! = '.' && errorfound == false) {if (i> 0 && expression [i-1]! = '.' &&! char.isnumber (Expression [i-1])) IF (EXPRESSION [I]! = '(' && expression [i-1]! = ') {MessageBox. Show ("Error! Illegal Expression!", "Form1", MessageBoxButtons.ok; errorfound = true;

} If (errorfound == false) {char OPTR_ = optr.gettop (); int x = com.getIndex (OPTR_); int y = com.getIndex (Expression [i]); switch (p [x, y]) {CASE '<': optr.put (Expression [i]); //console.writeline ("operator stack "); optr.traverse (); i ; break; case '=': optr.pop (); / /Conle.writeline; optr.traverse (); i ; break; case '>': char Op = optr.gettop (); optr.pop (); //console.writeline ("Operator Stack "); Optr.traverse (); double b = opnd.gettop (); opND.POP (); double a = opnd.gettop (); opnd.pop ();

//Console.writeline ("Godand Stack"); //opnd.traverse (); switch (com.getIndex (op)) {case 0: opnd.put (a b); Break; Case 1: Opnd.PUT (ab); Break; Case 2: Opnd.Put (a * b); break; case 3: if (b! = 0) opnd.put (A / b); else {// EXIT IF DIVIDED by ZERO // Console.writeline ("Error! Divided by Zero! 2"); MessageBox.show ("Error! Divided by Zero!", "Form1", MessageBoxButtons.ok; errorfound = true;} Break;} //console.writeline ("Operand Stack"); //opnd.traverse (); break; default: {// EXIT ILLEGAL Expression is found //rrole.writeline ("Error! Illegal Expression!"); MessageBox.show ("Error! Illegal expression! "," form1 ", messageboxbuttons.ok; errorfound = true;} Break;}}}}}}}}}}}}} //console.writeline nd.gettop ()); if (errorfound == false) {return Opnd.gettop ();} else return 0;}}

private void button1_Click (object sender, System.EventArgs e) {double result; ExpProcs expprocs = new ExpProcs (); string expression = txtExp.Text; if (! expression = "") {if (expression.Equals ( ""). ) MessageBox.Show ( "!! Error illegal expression", "Form1", MessageBoxButtons.OK); else {result = expprocs.execute (expression); txtRst.Text = result.ToString ();}} else {MessageBox.Show ("Error! Blank Expression!", "Form1", MessageBoxButtons.ok;}} private void button2_click (object sender, system.eventargs e) {txtExp.text = "; txtrst.text ="

}

Private void txtexp_keypress (object sender, system.windows e) {double result; express () {Double Result; ExpProcs ExpProcs = new expresss (); string expression = txtexp.text;

IF (e.keychar == (char) 13) {if (expression! = "") {if (Expression.Equals (".")) MessageBox.show ("Error! Illegal Expression!", "Form1", MessageBoxButtons .Ok); else {result = expression; txtrst.text = Result.tostring ();}} else {MessageBox.show ("Error! Blank Expression!", "Form1", MessageBoxButtons.ok); }}} Else if (! Char.iskber (e.keychar) && e.Keychar! = (Char) 8 && e.Keychar! = '-' && E.Keychar! = '*' && E.Keychar ! = '/' && e.Keychar! = '(' && E.Keychar! = ')' && E.Keychar! = '#' && E.Keychar! = '.') E.handled = True;}

}

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

New Post(0)