博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己開發的一個計算器小程序
阅读量:6562 次
发布时间:2019-06-24

本文共 6142 字,大约阅读时间需要 20 分钟。

計算器程序是自己開始學習c#時自己編寫的一小程序,自己開始一直在摸索,在這其中也遇到了很多問題,比如正負號的問題,清零的操作等等。后來在同事的幫忙下順利完成了。畫面有些粗糙,望大家見諒哈~~~

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication45

{
    public partial class Form1 : Form
    {
        static string mystring;//接受的字符串
        bool val1=false;//是否點擊操作符
      
         string leftstring;//操作符左邊的操作數
        string rightstring;//操作符右邊的操作數
        string strOp;//操作符
      
       
        public Form1()
        {
            InitializeComponent();
           
        }
            private void button1_Click(object sender, EventArgs e)
            {
                mystring += (this.button1.Text).ToString();
                mystring = Convert.ToDouble(mystring).ToString();
                this.textBox1.Text = mystring;
              
            }//數字1
            private void button2_Click(object sender, EventArgs e)
            {
                mystring += (this.button2.Text).ToString();
                mystring = Convert.ToDouble(mystring).ToString();
                this.textBox1.Text = mystring;
              
            }//數字2
            private void button3_Click(object sender, EventArgs e)
            {
                mystring += (this.button3.Text).ToString();
                mystring = Convert.ToDouble(mystring).ToString();
                this.textBox1.Text = mystring;
              
            }//數字3
            private void button4_Click_1(object sender, EventArgs e)
            {
                mystring += (this.button4.Text).ToString();
                mystring = Convert.ToDouble(mystring).ToString();
                this.textBox1.Text = mystring;
               
            }//數字4
            private void button5_Click_1(object sender, EventArgs e)
            {
                mystring += (this.button5.Text).ToString();
                mystring = Convert.ToDouble(mystring).ToString();

                this.textBox1.Text = mystring;

            }//數字5
            private void button6_Click_1(object sender, EventArgs e)
            {
                mystring += (this.button6.Text).ToString();
                mystring = Convert.ToDouble(mystring).ToString();
                this.textBox1.Text = mystring;
              
            }//數字6
            private void button7_Click_1(object sender, EventArgs e)
            {
                mystring += (this.button7.Text).ToString();
                mystring = Convert.ToDouble(mystring).ToString();
                this.textBox1.Text = mystring;
               
            }//數字7
            private void button8_Click_1(object sender, EventArgs e)
            {
                mystring += (this.button8.Text).ToString();
                mystring = Convert.ToDouble(mystring).ToString();
                this.textBox1.Text = mystring;
               
            }//數字8
            private void button9_Click_1(object sender, EventArgs e)
            {
                mystring += (this.button9.Text).ToString();
                mystring = Convert.ToDouble(mystring).ToString();
                this.textBox1.Text = mystring;
               
            }//數字9
            private void button10_Click(object sender, EventArgs e)
            {
                mystring += (this.button10.Text).ToString();
                this.textBox1.Text = mystring;
               
            }//數字0

            private void button12_Click(object sender, EventArgs e)

            {
                if (mystring.Contains("."))
                {
                }
                else
                {
                    mystring += (this.button12.Text).ToString();
                }
                this.textBox1.Text = mystring;
            }//小數點操作
            private void button11_Click(object sender, EventArgs e)
            {
                if (this.textBox1.Text.Contains("-"))
                {
                    this.textBox1.Text = this.textBox1.Text.TrimStart('-');
                }
                else
                {
                    this.textBox1.Text = "-" + this.textBox1.Text;
                }
            }//正負號操作

       

            private void button18_Click(object sender, EventArgs e)
            {
                mystring = string.Empty;
                this.textBox1.Text = "";
                val1 = false;
            }//清零操作

            private void button13_Click(object sender, EventArgs e)

            {
                strOp = "+";
               
                if (val1==false)
                {
                    leftstring = mystring;
                    mystring = string.Empty;                   
                }
                else
                {

                    leftstring = (this.textBox1.Text).ToString();

                  
                  
                }
                val1 = true;       
               
            }//加發操作
           

        private void button14_Click(object sender, EventArgs e)

        {
            strOp = "-";
           
            if (val1 == false)
            {
                leftstring = mystring;
                mystring = string.Empty;
            }
            else
            {
                leftstring = (this.textBox1.Text).ToString();
            }
            val1 = true;
           
        }//減發操作

        private void button15_Click(object sender, EventArgs e)

        {
            strOp = "*";
           
            if (val1 == false)
            {
                leftstring = mystring;
                mystring = string.Empty;
            }
            else
            {
                leftstring = (this.textBox1.Text).ToString();
            }
            val1 = true;
           
        }//乘發操作

        private void button16_Click(object sender, EventArgs e)

        {
            strOp = "/";
          
            if (val1 == false)
            {
                leftstring = mystring;
                mystring = string.Empty;
            }
            else
            {
                leftstring = (this.textBox1.Text).ToString();
            }
            val1 = true;
          
        }//除法操作

        private void button17_Click(object sender, EventArgs e)

        {
            rightstring = mystring;
            switch(strOp)
            {
                case "+":
            this.textBox1.Text = (Convert.ToDouble(leftstring) + Convert.ToDouble(rightstring)).ToString();
            mystring = string.Empty;
          
            break;
                case "-":
            this.textBox1.Text = (Convert.ToDouble(leftstring) - Convert.ToDouble(rightstring)).ToString();
            mystring = string.Empty;
            break;
                case "*":
            this.textBox1.Text = (Convert.ToDouble(leftstring) * Convert.ToDouble(rightstring)).ToString();
            mystring = string.Empty;
            break;
                case "/":
            this.textBox1.Text = (Convert.ToDouble(leftstring) / Convert.ToDouble(rightstring)).ToString();
            mystring = string.Empty;
            break;
                case "sin":
            this.textBox1.Text = Math.Sin(Math.PI*(Convert.ToDouble(this.textBox1.Text))/180).ToString();
            break;
                case "cos":
            this.textBox1.Text = Math.Sin(Math.PI * (Convert.ToDouble(this.textBox1.Text)) / 180).ToString();
            break;
                case "tan":
            this.textBox1.Text = Math.Sin(Math.PI * (Convert.ToDouble(this.textBox1.Text)) / 180).ToString();
            break;
                case "sqrt":
            this.textBox1.Text = Math.Sqrt(Convert.ToDouble(this.textBox1.Text)).ToString();
            mystring = this.textBox1.Text;
            break;
               
                default:
            break;

        }

        }//計算操作

        private void button19_Click(object sender, EventArgs e)

        {
            if (this.textBox1.Text !=string.Empty)
            {
               
                this.textBox1.Text = this.textBox1.Text.Remove(this.textBox1.Text.Length -1);
                mystring = this.textBox1.Text;
               
            }
            else
            {
                MessageBox.Show("您好,現在所顯示的數據已經為空了,請您再次輸入正確的數據!");
            }
       }//退格操作

        private void button20_Click(object sender, EventArgs e)

        {
            strOp = "sin";

            if (val1 == false)

            {
                leftstring = mystring;
                mystring = string.Empty;
            }
            else
            {
            }
            val1 = true;
        }// sin 操作

        private void button24_Click(object sender, EventArgs e)

        {
            Help.ShowHelp(this, "C:\\WINDOWS\\system32\\calc.exe");// 显示XP系统自带计算器

           

        }

        private void button21_Click(object sender, EventArgs e)

        {
            strOp = "cos";

            if (val1 == false)

            {
                leftstring = mystring;
                mystring = string.Empty;
            }
            else
            {

            }

            val1 = true;
        }// cos 操作

        private void button22_Click(object sender, EventArgs e)

        {
            strOp = "tan";

            if (val1 == false)

            {
                leftstring = mystring;
                mystring = string.Empty;
            }
            else
            {

            }

            val1 = true;
        }// tan 操作

        private void button23_Click(object sender, EventArgs e)

        {
            strOp = "sqrt";

            if (val1 == false)

            {
                leftstring = mystring;
                mystring = string.Empty;
            }
            else
            {

            }

            val1 = true;
        }// sqrt 操作
         
    }
}

转载于:https://www.cnblogs.com/luoxuefeihua/archive/2011/10/12/2208928.html

你可能感兴趣的文章
加密的类型及其相关算法--单向加密
查看>>
ansible-playbook ERROR! Syntax Error
查看>>
启动归档时提示:ORA-00265: instance recovery required, cannot set ARCHIVELOG mode
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
linux内核参数都有哪些参数需要调优
查看>>
XenMobile 10 FIPS模式配置
查看>>
快速找到是哪个网站发包的方法(windows2003系统)
查看>>
深入理解生成树协议STP
查看>>
测试用例设计方法—场景分析法
查看>>
Bitmap和Drawable相互转换方法
查看>>
CentOS7 firewalld 防火墙与端口 管理
查看>>
error C2061: syntax error : identifier 'HMONITOR'
查看>>
什么是Java内存模型
查看>>
Nginx负载均衡,ssl原理,生成ssl密钥对,Nginx配置ssl
查看>>
TurboMail邮件服务器为用户提供成熟的海外通邮技术
查看>>
代码之美 - 如何写出优雅的PHP代码
查看>>
异常解决 The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one...
查看>>
[Linux CentOS6.2] CentOS 6.2 详细安装教程(全图解,转载)
查看>>