C# Realization Calculator (with Java Edition)

嘤嘤嘤~~ Speaking of writing calculators, I have some experience. I wrote it with Java GUI before. It is much more complicated than this. The current C# can only realize the operation of a single operator. I haven't considered many of them, but they are certainly achievable. My Java version is implemented, and I will post the link in a while. The ideas for the implementation are similar, so there is no further implementation here.

There are other JavaGUI examples in this column. . I want to be a web version of the big homework, I don’t know if it can be realized in the end o(╥﹏╥)o
javaGUI to realize the calculator (four remainders and other operations, polynomial operations can be realized)

Because the writing is very simple, so I finished it very quickly. I also found out: C, C++, and C# are all interlinked, and the C family is one family. . . The function or type you want to use is really ok, you don’t need Baidu to check it, nice!
1. Program name:

C# implement calculator

2. Function description:

This calculator can realize the addition, subtraction, multiplication, division and squaring of two operands (both decimal and integer) (this can be achieved by changing a loop or using the pow function to achieve any power), abdicating, and clearing operations. Among them, the division operation is particularly special, because it needs to consider the case where the divisor is 0, and it is also handled here, and a prompt will be given when it is 0.

3. The main interface display:

I didn't do anything to dress up the calculator here. I just changed the background color. I looked at it and it seems that I can't upload pictures. If it can, the background and buttons of the calculator can be made more realistic. By the way, the positions of all the keys are the same size, and the spacing of each row and each column is also calculated (calculated based on the width and position), it will not appear which key position is more than 1 pixel to the right, etc. This situation.
Insert picture description here

4. Effect display:

Always take screenshots of user input first, and then screenshots of the final result

addition:

Insert picture description here
Insert picture description here

Subtraction:

Insert picture description here
Insert picture description here

multiplication:

Insert picture description here
Insert picture description here

division:

Insert picture description here
Insert picture description here

Division by zero type
Insert picture description here
Insert picture description here

square:

The user enters a number, then directly press the x^2 key, and then press the equal sign key.
Insert picture description here
Insert picture description here

Back key:

Insert picture description here
Insert picture description here

Clear the C key:

Insert picture description here
Insert picture description here

Five, functional algorithm description:

What I use is to display all the content entered by the user, that is, the operand + operator, into the text box, so when you finally get it, you need to traverse the entire string in the text box to determine what kind of operation it is , And then calculate the result. For the squaring operation, a tag variable is set, so that only one operand needs to be obtained.
Back key: You can only go back one character at a time. First get all the content in the current text box, and then use the string interception function to intercept it (of course you can also use the address to delete it directly).
Clear key: This is easy to implement, direct text Just set the value of the string in the box to empty.

Six, all codes:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }

        char op; //运算符
        double num1, num2; //运算数

        // 数字键
        private void button1_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text += "1";
        }

        private void button2_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text += "2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text += "3";
        }

        private void button4_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text += "4";
        }

        private void button5_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text += "5";
        }

        private void button6_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text += "6";
        }

        private void button7_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text += "7";
        }

        private void button8_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text += "8";
        }

        private void button9_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text += "9";
        }

        private void button0_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text += "0";
        }

        // 小数点键
        private void buttonDot_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text += ".";
        }
        // 加法
        private void buttonAdd_Click(object sender, EventArgs e)
        {
    
    
            op = '+';
            textBox1.Text += "+";
        }
        // 减法
        private void buttonSub_Click(object sender, EventArgs e)
        {
    
    
            op = '-';
            textBox1.Text += "-";
        }
        // 乘法
        private void buttonMul_Click(object sender, EventArgs e)
        {
    
    
            op = '*';
            textBox1.Text += "*";
        }
        // 除法
        private void buttonDiv_Click(object sender, EventArgs e)
        {
    
    
            op = '/';
            textBox1.Text += "/";
        }
        // 平方
        private void buttonPow_Click(object sender, EventArgs e)
        {
    
    
            op = '^';
        }
        // 退位,回退键
        private void buttonBack_Click(object sender, EventArgs e)
        {
    
    
            String str = textBox1.Text;
            if (str.Length != 0)
            {
    
    
                textBox1.Text = str.Substring(0, str.Length - 1);
            }
        }
        // 等号键,计算结果
        private void buttonRes_Click(object sender, EventArgs e)
        {
    
    
            String str = textBox1.Text;
            bool flag = true; //用来判断是否是平方操作
            int i;
            for(i = 0; i < str.Length; i++)
            {
    
    
                if(!(str[i] >='0' && str[i] <='9' || str[i] == '.')){
    
    
                    num1 = Convert.ToDouble(str.Substring(0, i));
                    num2 = Convert.ToDouble(str.Substring(i + 1));
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
    
    
                num1 = Convert.ToDouble(str);
            }
            if (op == '+')
            {
    
    
                textBox1.Text = "";
                textBox1.Text += (num1 + num2);
            }
            else if (op == '-')
            {
    
    
                textBox1.Text = "";
                textBox1.Text += (num1 - num2);
            }
            else if (op == '*')
            {
    
    
                textBox1.Text = "";
                textBox1.Text += (num1 * num2);
            }
            else if (op == '/')
            {
    
    
                textBox1.Text = "";
                if(num2 == 0)
                {
    
    
                    textBox1.Text = "除数为0,Error!";
                }
                else
                {
    
    
                    textBox1.Text += (num1 / num2);
                }
            }
            else if (op == '^')
            {
    
    
                textBox1.Text = "";
                textBox1.Text += (num1 * num1);
            }
        }
        // 清零键
        private void buttonClc_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text = "";
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/114830001