Design and implementation of C # language based calculator

A needs analysis

C # language using a computer program, comprising normal calculator mode, scientific calculator mode; achieve real number (including positive, negative, 0) addition, subtraction, multiplication, division, squares and other basic operators away, and to achieve a non-negative root operation.

Design and implementation of two programs

First, design of the main window, including two TextBox for displaying data, the data used to achieve binding, in the Model INotifyPropertyChanged interface implemented to function as a data change notification. Button Panel as a menu and switching calculator mode. Panel for a final display of the different modes of the calculator input panel;

Followed by standard calculator input panel. It includes conventional numbers and operators, and Delete, CE, and a clear button. Scientific computing model is only a few more operators only. Gets the name of the button is clicked in the post code, Print Method Invocation Model display data in the TextBox.

Model variables declared as the two data strings in two TextBox, Print method further comprising, on the one hand to give the name of the button, the program calls the other hand, the most important processing method PrintText logic method. Pass into the two values, i.e., the TextBox currently displayed data as the data output two values ​​TextBox should appear after a series of processes.

PrintAndExpression TextBox class is primarily responsible for handling the display and generate an arithmetic expression. GetValue method returns the string to be displayed by name. IsOpreation determination method is a digital string, unary and binary operators or other symbols. PrintText processing display format. Substantially in time when the input digital textBox1, each operator is about to press a digital textBox1 is connected in the operator display textBox2 processed simultaneously simple unary operation, to generate expression. The final expression will be passed to AnalyExpressions. After the results of the analytical expression operator returns.

AnalyExpressions analytical expression, the use of the stack to achieve, stored in a string array infix expression after the decomposition, the so-called "postfix expression." Roughly rules:

  • The infix expression translated into postfix notation

  • Input infix expression: A + B * (C + D) -E / F

  • Infix expression into postfix expression translated as follows:

    • From left to right to obtain data ch

    • If the operand ch is output directly

    • If the operator is ch (including left and right parentheses), then:

      • If ch = "(", into the stack

      • If ch = ")", are sequentially output stack operators, until it encounters a "(" up

      • If not ch ")" or "(", and then the top position of the vertex operators do priority comparing stack

        • If ch higher priority than top, then will put the stack ch

        • If the priority is lower than or equal to ch top, then the output top, then the stack into ch

      • If the expression has been completed is read, and the stack when the operator is also sequentially output from the top of

 

Guess you like

Origin blog.csdn.net/asdJJkk/article/details/93377242