在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 入门级计算器源码(实现了加减乘除等)

C# 入门级计算器源码(实现了加减乘除等)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.11M
  • 下载次数:18
  • 浏览次数:2891
  • 发布时间:2016-02-02
  • 实例类别:C#语言基础
  • 发 布 人:001Software
  • 文件格式:.rar
  • 所需积分:2
 相关标签: C# 计算 窗口程序

实例介绍

【实例简介】

之前做的一个小计算器,算法有点复杂QAQ

已知Bug:连续点击两次运算符号会导致结果出错。

【实例截图】

【核心代码】


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 _001Software_计算器
{
    public partial class Form1 : Form
    {
        bool Cal_Started, Res = false, Cltxt = true;
        int type, ltype;
        double first, second, result;
        public Form1()
        {
            InitializeComponent();
        }

        private void button_cls_Click(object sender, EventArgs e)
        {
            Cls();
        }

        public void Cls()
        {
            Cal_Started = false;
            Output.Text = "";
        }

        public void Cal()
        {
            Cltxt = true;
            if (Cal_Started == false)
            {
                Cal_Started = true;
                ltype = type;
                //try ... catch
                first = double.Parse(Output.Text);
            }
            else if (Cal_Started == true)
            {
                //try ... catch
                second = double.Parse(Output.Text);
                if (Res == true)
                {
                    switch (type)
                    {
                        case 1:
                            result = first   second;
                            break;
                        case 2:
                            result = first - second;
                            break;
                        case 3:
                            result = first * second;
                            break;
                        case 4:
                            result = first / second;
                            break;
                    }
                }
                else
                {
                    switch (ltype)
                    {
                        case 1:
                            result = first   second;
                            break;
                        case 2:
                            result = first - second;
                            break;
                        case 3:
                            result = first * second;
                            break;
                        case 4:
                            result = first / second;
                            break;
                    }

                }
                first = result;
                Output.Text = result.ToString();
            }
            
        }

        public void Input(string text)
        {
            if (Cltxt == false)
            {
                Output.Text  = text;
            }
            else
            {
                Output.Text = text;
                Cltxt = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Cal_Started = false;
        }

        private void button_0_Click(object sender, EventArgs e)
        {
            Input(button_0.Text);
        }

        private void button_1_Click(object sender, EventArgs e)
        {
            Input(button_1.Text);
        }

        private void button_2_Click(object sender, EventArgs e)
        {
            Input(button_2.Text);
        }

        private void button_3_Click(object sender, EventArgs e)
        {
            Input(button_3.Text);
        }

        private void button_4_Click(object sender, EventArgs e)
        {
            Input(button_4.Text);
        }

        private void button_5_Click(object sender, EventArgs e)
        {
            Input(button_5.Text);
        }

        private void button_6_Click(object sender, EventArgs e)
        {
            Input(button_6.Text);
        }

        private void button_7_Click(object sender, EventArgs e)
        {
            Input(button_7.Text);
        }

        private void button_8_Click(object sender, EventArgs e)
        {
            Input(button_8.Text);
        }

        private void button_9_Click(object sender, EventArgs e)
        {
            Input(button_9.Text);
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            type = 1;
            Cal();
        }

        private void button_sub_Click(object sender, EventArgs e)
        {
            type = 2;
            Cal();
        }

        private void button_mul_Click(object sender, EventArgs e)
        {
            type = 3;
            Cal();
        }

        private void button_div_Click(object sender, EventArgs e)
        {
            type = 4;
            Cal();
        }

        private void button_dot_Click(object sender, EventArgs e)
        {
            if (Cltxt == true)
            {
                Output.Text = "0.";
                Cltxt = false;
            }
            else
            {
                Output.Text  = ".";
            }
        }

        private void button_res_Click(object sender, EventArgs e)
        {
            Res = true;
            Cal();
            Cal_Started = false;
        }
    }
}


标签: C# 计算 窗口程序

实例下载地址

C# 入门级计算器源码(实现了加减乘除等)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 q14275 发表于: 2017-12-05 14:03 13
下载了运行不了

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警