【实验01 扩展实验】C#桌面项目:简易计算器

时间:2024-03-19 12:40:13

【实验要求】

(1)新建一个C#桌面项目Calc,实现简易计算器功能,界面如图1所示。

(2)计算方式:通过点击对应的按钮,输入第1个数(可以是整数或实数),输入运算符(+、-、*或/),再输入第2个数,点击“=”按钮,完成计算。

(3)(选做)改进程序的设计,要求输入一个算术表达式,点击“=”按钮完成输入表达式的计算并输出(可以利用数据结构中的表达式计算原理,实现表达式的计算)。

【实验步骤】

红色为每个按钮的方法名

【Form1.cs代码】

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 Calc
{
    public partial class frmMain : Form
    {
        private double num1;//左操作数
        private double num2;//右操作数
        private string flag;//flag为当前所操作的运算符
        private double res;//计算结果
        private bool isCalc;//判断是否计算
        public frmMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            //次方运算
            num1 = Convert.ToDouble(txtNum.Text);
            flag = "sqr";
            txtNum.Text = "";
        }

        private void btnC_Click(object sender, EventArgs e)
        {
            //按钮“C”,清空
            txtNum.Text = "";
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            //回退键
            if(txtNum.Text.Length > 0)
            {
                txtNum.Text = txtNum.Text.Substring(0, txtNum.Text.Length - 1);
            }
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            //除法按钮
            num1 = Convert.ToDouble(txtNum.Text);
            flag = "/";
            txtNum.Text = "";
        }

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

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

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

        private void btnMul_Click(object sender, EventArgs e)
        {
            //乘法按钮
            num1 = Convert.ToDouble(txtNum.Text);
            flag = "*";
            txtNum.Text = "";
        }

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

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

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

        private void btnSub_Click(object sender, EventArgs e)
        {
            //减法按钮
            num1 = Convert.ToDouble(txtNum.Text);
            flag = "-";
            txtNum.Text = "";
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            txtNum.Text += "1";
        }

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

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

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //加法按钮
            num1 = Convert.ToDouble(txtNum.Text);
            flag = "+";
            txtNum.Text = "";
        }

        private void btnOr_Click(object sender, EventArgs e)
        {
            //正负号按钮
            if(txtNum.Text == "0")
            {
                return;
            }
            if (txtNum.Text.Contains("-"))
            {
                txtNum.Text = txtNum.Text.Substring(1);
            }
            else
            {
                txtNum.Text = "-" + txtNum.Text;
            }
        }

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

        private void btnPoint_Click(object sender, EventArgs e)
        {
            txtNum.Text +=  ".";
        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            int f = 1;
            num2 = Convert.ToDouble(txtNum.Text);
            if(flag == "+")//加法运算
            {
                res = num1 + num2;
            }else if(flag == "-")//减法运算
            {
                res = num1 - num2;
            }else if(flag == "*")//乘法运算
            {
                res = num1 * num2;
            }else if(flag == "/")//除法运算
            {
                if(num2 == 0)
                {
                    txtNum.Text = "除数不能为0";
                    f = 0;
                }
                else
                {
                    res = num1 / num2;
                }
            }
            else 
            {
                //n次方运算
                res = Math.Pow(num1,num2);              
            }
            if(f == 1)
            {
                txtNum.Text = res.ToString();
            }
        }
    }
}

【计算器效果演示】

计算器演示

不知道为什么录屏没给我录到头顶,将就看看。