【2019-2020春學期】數據庫實驗1:Calcultor簡單計算器

實驗目的:

1、熟悉控件和編程

實驗內容:

用Visual Studio編寫一個簡單計算器
目標效果:
在這裏插入圖片描述

實驗步驟:

1、創建新項目
在這裏插入圖片描述
2、根據需求選擇項目模板:
在這裏插入圖片描述
3、根據目標效果從工具箱添加公共組件
一開始的界面是醬嬸的,爲了讓它和目標更加的接近,我們可以在右側的屬性欄改變它的長寬屬性,對於控件的其他屬性,也可以在此處進行修改
在這裏插入圖片描述
初步的效果就是醬紫的
在這裏插入圖片描述

4、爲按鈕添加點擊事件
(1)數字
可先用代碼進行測試,若點擊按鈕button18後出現彈窗,則說明爲按鈕添加點擊事件的行爲成功。

private void button18_Click(object sender, EventArgs e)
        {
            MessageBox.Show("1");
        }

接着就開始爲數字按鈕添加點擊事件,點擊事件的結果就是在文本框textBox1輸入字符
舉例:

private void button18_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("1");
            textBox1.Text += "1"; //注意
        }

(2)運算符
舉例“+”:
因爲在計算器中,點擊運算符,會使得原文本框裏的內容清空,所以此時需要用全局變量保存下來,同時該文本框清空。

private void button14_Click(object sender, EventArgs e)
        {
            i = Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";
        }

(3)等號
在等號中應該判斷,該運算是哪種運算,所以還應該設置一個全局變量,該變量用於判斷是應該進行那種運算

private void button10_Click(object sender, EventArgs e)
        {
            j = Convert.ToDouble(textBox1.Text);
            if (k == 1)
            {
                j = i + j;
            }else if (k == 2)
            {
                j = i - j;
            }else if (k == 3)
            {
                j = i * j;
            }else if (k == 4)
            {
                j = i / j;
            }
            textBox1.Text = j.ToString();
        }

(4)清零按鈕C

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

(5)1/x按鈕

private void button1_Click(object sender, EventArgs e)
        {
            i = Convert.ToDouble(textBox1.Text);
            i = 1 / i;
            textBox1.Text = i.ToString();
        }

(6)考慮特殊情況,比如除數爲0,此時設置事件爲彈出提示“除數不能爲0”,且清空textBox1的內容,所以需要對 button10的點擊事件進行修改

private void button10_Click(object sender, EventArgs e)
        {
            j = Convert.ToDouble(textBox1.Text);
            if (k == 1)
            {
                j = i + j;
            }else if (k == 2)
            {
                j = i - j;
            }else if (k == 3)
            {
                j = i * j;
            }else if (k == 4)
            {
                if (j == 0)
                {
                    MessageBox.Show("除數不能爲0");
                    textBox1.Text = "";
                }
                else
                {
                    j = i / j;
                }
                
            }
            if (!(k == 4 && j == 0))
            {
                textBox1.Text = j.ToString();
            }
            
        }

運行效果:

(1)四則運算:
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
(2)1/x
在這裏插入圖片描述
(3)清零:
在這裏插入圖片描述
(4)除數爲0
在這裏插入圖片描述
在這裏插入圖片描述
點擊確定以後:
在這裏插入圖片描述

代碼:

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 Calculator1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double i,j,k;
        private void button2_Click(object sender, EventArgs e)
        {
            i = Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";
            k = 3;

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button18_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("1");
            textBox1.Text += "1";
        }

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

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

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

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

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

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

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

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

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

        private void button14_Click(object sender, EventArgs e)
        {
            i = Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";
            k = 1;
        }

        private void button19_Click(object sender, EventArgs e)
        {
            i = Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";
            k = 4;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            i = Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";
            k = 2;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            j = Convert.ToDouble(textBox1.Text);
            if (k == 1)
            {
                j = i + j;
            }else if (k == 2)
            {
                j = i - j;
            }else if (k == 3)
            {
                j = i * j;
            }else if (k == 4)
            {
                if (j == 0)
                {
                    MessageBox.Show("除數不能爲0");
                    textBox1.Text = "";
                }
                else
                {
                    j = i / j;
                }
                
            }
            if (!(k == 4 && j == 0))
            {
                textBox1.Text = j.ToString();
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            i = Convert.ToDouble(textBox1.Text);
            i = 1 / i;
            textBox1.Text = i.ToString();
        }

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

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

心得體會:

心得:過程很有趣的,而且我覺得我的計算器看起來還是挺好看的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章