找传奇、传世资源到传世资源站!

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

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

之前做的一个小计算器,算法有点复杂QAQ已知Bug:连续点击两次运算符号会导致结果出错。C# 入门级计算器源码(实现了加减乘除等) C#语言基础-第1张
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; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复