1. 程式人生 > >C#編輯計算器 應用程式

C#編輯計算器 應用程式

C#編輯計算器

1.經過一週的學習,以及自己的c基礎,終於把這個計算器編輯出來了,來給大家分享一下,當做自己的第一篇部落格啦也就。
2.計算器的功能如下:實現兩位實數的加減乘除運算,有次冪運算,雙行顯示:第一行為你計算的過程,第二行為你輸入的資料以及結果。
缺點是:沒有實現連加連乘遠算,也沒有什麼科學運演算法,希望以後能進一步改進。
3.計算器的程式程式碼如下:

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 計算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    string number, number3;
    double number1, number2;
    int op;

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "0")
        {
            if (number3 == "0")
            {
                number3 = "1";
            }
            else
            {
                number3 = number3 + "1";
            }
            number = "1";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
        else
        {
            if (number3 == "0")
            {
                number3 = "1";
            }
            else
            {
                number3 = number3 + "1";
            }
            number += "1";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "0")
        {
            if (number3 == "0")
            {
                number3 = "2";
            }
            else
            {
                number3 = number3 + "2";
            }
            number = "2";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
        else
        {
            if (number3 == "0")
            {
                number3 = "2";
            }
            else
            {
                number3 = number3 + "2";
            }
            number += "2";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "0")
        {
            if (number3 == "0")
            {
                number3 = "3";
            }
            else
            {
                number3 = number3 + "3";
            }
            number = "3";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
        else
        {
            if (number3 == "0")
            {
                number3 = "3";
            }
            else
            {
                number3 = number3 + "3";
            }
            number += "3";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void button4_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "0")
        {
            if (number3 == "0")
            {
                number3 = "4";
            }
            else
            {
                number3 = number3 + "4";
            }
            number = "4";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
        else
        {
            if (number3 == "0")
            {
                number3 = "4";
            }
            else
            {
                number3 = number3 + "4";
            }
            number += "4";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void button5_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "0")
        {
            if (number3 == "0")
            {
                number3 = "5";
            }
            else
            {
                number3 = number3 + "5";
            }
            number = "5";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
        else
        {
            if (number3 == "0")
            {
                number3 = "5";
            }
            else
            {
                number3 = number3 + "5";
            }
            number += "5";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void button6_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "0")
        {
            if (number3 == "0")
            {
                number3 = "6";
            }
            else
            {
                number3 = number3 + "6";
            }
            number = "6";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
        else
        {
            if (number3 == "0")
            {
                number3 = "6";
            }
            else
            {
                number3 = number3 + "6";
            }
            number += "6";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void button7_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "0")
        {
            if (number3 == "0")
            {
                number3 = "7";
            }
            else
            {
                number3 = number3 + "7";
            }
            number = "7";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
        else
        {
            if (number3 == "0")
            {
                number3 = "7";
            }
            else
            {
                number3 = number3 + "7";
            }
            number += "7";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void button8_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "0")
        {
            if (number3 == "0")
            {
                number3 = "8";
            }
            else
            {
                number3 = number3 + "8";
            }
            number = "8";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
        else
        {
            if (number3 == "0")
            {
                number3 = "8";
            }
            else
            {
                number3 = number3 + "8";
            }
            number += "8";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void button9_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "0")
        {
            if (number3 == "0")
            {
                number3 = "9";
            }
            else
            {
                number3 = number3 + "9";
            }
            number = "9";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
        else
        {
            if (number3 == "0")
            {
                number3 = "9";
            }
            else
            {
                number3 = number3 + "9";
            }
            number += "9";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void buttonequ_Click(object sender, EventArgs e)
    {
        switch (op)
        {
            case 1: number2 = number1 + double.Parse(textbox1.Text); break;
            case 2: number2 = number1 - double.Parse(textbox1.Text); break;
            case 3: number2 = number1 * double.Parse(textbox1.Text); break;
            case 4: number2 = number1 / double.Parse(textbox1.Text); break;
            case 5: number2 = Math.Pow(number1, double.Parse(textbox1.Text));break;
        }
        textbox1.Text = number2.ToString();
        number3 = number3 + "=" + number2.ToString();
        textbox2.Text = number3.ToString();
    }

    private void buttonsub_Click(object sender, EventArgs e)
    {
        op = 2;
        if (textbox1.Text != "")
        {
            number3 = number3 + "-";
            number1 = Convert.ToDouble(textbox1.Text);
            number = "-";
            number = "";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void buttonmul_Click(object sender, EventArgs e)
    {
        op = 3;
        if (textbox1.Text != "")
        {
            number3 = number3 + "*";
            number1 = Convert.ToDouble(textbox1.Text);
            number = "*";
            number = "";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void buttondiv_Click(object sender, EventArgs e)
    {
        op = 4;
        if (textbox1.Text != "")
        {
            number3 = number3 + "/";
            number1 = Convert.ToDouble(textbox1.Text);
            number = "/";
            number = "";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void buttonac_Click(object sender, EventArgs e)
    {
        number = "";
        number3 = "";
        textbox1.Text = "";
        textbox2.Text = "";
    }

    private void buttondel_Click(object sender, EventArgs e)
    {
        if(textbox1.Text.Length <2)
        {
            textbox1.Text = "0";
            number3 = textbox2.Text.Substring(0, textbox2.Text.Length - 1);
            number3 = number3 + "0";
            textbox2.Text = number3;
        }
        else
        {
            number = textbox1.Text.Substring(0, textbox1.Text.Length - 1);
            textbox1.Text = number;
            number3 = textbox2.Text.Substring(0, textbox2.Text.Length - 1);
            textbox2.Text = number3;
        }
    }

    private void buttonpoint_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "0")
        {
            if (number3 == "0")
            {
                number3 = ".";
            }
            else
            {
                number3 = number3 + ".";
            }
            number = ".";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
        else
        {
            if (number3 == "0")
            {
                number3 = ".";
            }
            else
            {
                number3 = number3 + ".";
            }
            number += ".";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void buttonpow_Click(object sender, EventArgs e)
    {
        op = 5;
        if (textbox1.Text != "")
        {
            number3 = number3 + "^";
            number1 = Convert.ToDouble(textbox1.Text);
            number = "^";
            number = "";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void buttonadd_Click(object sender, EventArgs e)
    {
        op = 1;
        if (textbox1.Text != "")
        {
            number3 = number3 + "+";
            number1 = Convert.ToDouble(textbox1.Text);
            number = "+";
            number = "";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }

    private void button0_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "0")
        {
            if (number3 == "0")
            {
                number3 = "0";
            }
            else
            {
                number3 = number3 + "0";
            }
            number = "0";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
        else
        {
            if (number3 == "0")
            {
                number3 = "0";
            }
            else
            {
                number3 = number3 + "0";
            }
            number  += "0";
            textbox1.Text = number;
            textbox2.Text = number3;
        }
    }
}

}