Update of algorithm for four operations

zhaozj2021-02-16  84

// This new version is derived from the prvious one which was changed to test the // aquirement of the learning of the Exceptions.////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 copy is blank, a warning message read "Error! Blank Expression!" // is displayed.////bit IS Set That Some 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 Stack {protected ArrayList vectors; protected int length; protected object element; public Stack () {vectors = new ArrayList (20); length = 0;} public virtual int getLength () {length = vectors.Count; return length;}

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

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

/ * Public virtual object gettop () {element = (object) vectors [length-1]; return element;} * /

Public virtual void traverse () {}

Public Virtual Void Clear () {for (int i = length-1; i> = 0; I -) {vectors.removeat (i);}}}

Class OpndStack: stack {public opndstack (): base () {vectors = new arraylist (20); length = 0;}

Public Override Void Put (Object DBL) {VECTORS.ADD (DBL); Length = Length 1;}

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

Class Optrstack: stack {public optics (): base () {vectors = new arraylist (20); length = 0;}

Public Override Void Put (Object DBL) {VECTORS.ADD (DBL); Length = Length 1;}

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

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 &&fact == false; i ) IF (Operators [i] == e) Found = true; if (Found == false 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 IllegalExpressionException: ApplicationException {public IllegalExpressionException (string message): base (message) {} public IllegalExpressionException (string message, Exception innerException): base (message, innerException) {}}

class DividedByZeroException: ApplicationException {public DividedByZeroException (string message): base (message) {} public DividedByZeroException (string message, Exception innerException): base (message, innerException) {}}

Class expProcs {private char [,] p = new char [7,7]; private optherTack Optr; private opctorock opctor; private company card; public express expresss () {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, 1] = '>'; 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, 3] ='> '; 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 optherTack (); opnd = new opndstack (); com = new compare ();}

Public double execute (string exp) {INT i = 0; bool errorfound = false; double numafterdot = 1; double d = 0; string expression; expression = exp; expression = '#'; optr.clear (); opnd.clear (); Optr.put ('#'); try {whele (expression [i]! = '#' || optr.gettop ()! = '#') {Ix (char.isnumber (expness [i]) || Expression [i] == '.') {while (rate (expression [i]) || expression [i] == '.') {if (Expression [i]! = '.') { D = D * 10 (int) expression [i] -48; i ;} else {i ; while (char.isnumber (expression [i])) {numafterDot * = 10; D = D (int) Expression [ I] -48) / NumafterDot; i ;} if (Expression [i] == '.') {throw new IllegalExpressionException ("Error! Illegal Expression!");}}} NumAfterDot = 1; Opnd.Put (D) D = 0 } If (! Char.isnumber (expression [i]) && expression [i]! = '.') {If (i> 0 && expression [i-1]! = '.' &&! Char.isnumber (expression [i- 1])) IF (EXPRESSION [I]! = '(' && expression [i-1]! = ')') {Throw new illegalExpressionException ("Error! Illegal Expression!");} 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 (); ////console.writeline ("operator stack "); 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 {throw new DividedByzeroException ("Error! Divided by ! zero ");} break;} break; default: {throw new IllegalExpressionException (" Error illegal expression ");} // break;}}}} return opnd.getTop ();} catch (IllegalExpressionException e) {!! MessageBox.Show (e.Message, "Form1", MessageBoxButtons.OK); return 0;} catch (DividedByZeroException e) {MessageBox.Show (e.Message, "Form1", MessageBoxButtons.OK); return 0;}}} Private void button1_click (Object sender, system.eventargs e) {Double Result; ExpProcs Exprocs = new express (); string expression = txtexp.text; if (expression! = ") {if (Expression.equals (". ")).. 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.Forms.KeyPressEventArgs e) {double result; ExpProcs expprocs = new ExpProcs (); string expression = txtExp.Text; if (e.KeyChar == (char) 13) {if ( Expression! = "") {IF (Expression.equals ("))) MessageBox.show (" ERROR! ILLEGAL Expression! "," Form1 ", MessageBoxButtons.ok; else {result = expresss.execute (expression); TXTRST.TEXT = Result.toString ();}} else {messagebox.show ("Error! Blank Expression!", "Form1", MessageBoxButtons.ok);}} else if (! char.isnumber (e.Keychar) && E .Keychar! = (Char) 8 && e.Keychar! = '-' && e.Keycha! = '*' && e.Keycha! = '/' && e.Keychar! = '(' && e. Keychar! = ')' && E.Keychar! = '#' && e.keychar! = '.') E.handled = true;

}

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

New Post(0)