我是一个初学者,从这些简单的开始做起。
把简单的练熟,便是成功了一小步。
下面是代码完整版,可以实现简单的运算,可以实现连续的运算。但是有些细节没考虑。
希望大神们帮我指正。
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 Frm_Main : Form
{
int a = 0, b = 0; //储存输入数字的整型
int total=0; //用于储存结果
int m = 1; //用于乘法第一项乘以1
string s; //用来储存输入的每个数
string d; //用来辨别进行何种运算
bool c=false;
public Frm_Main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
s += "1";
textBox1.Text += "1";
}
private void button2_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
s += "2";
textBox1.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
s += "3";
textBox1.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
s += "4";
textBox1.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
s += "5";
textBox1.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
s += "6";
textBox1.Text += "6";
}
private void button7_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
s += "7";
textBox1.Text += "7";
}
private void button8_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
s += "8";
textBox1.Text += "8";
}
private void button9_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
s += "9";
textBox1.Text += "9";
}
private void button12_Click(object sender, EventArgs e) //实现加法
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
d = "+";
a = int.Parse(s);
total += a;
textBox1.Text += "+";
s = "";
}
private void button11_Click(object sender, EventArgs e) //清空
{
textBox1.Text = "";
s = "";
total = 0;
}
private void button14_Click(object sender, EventArgs e) // 实现减法
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
d = "-";
a = int.Parse(s);
if (textBox1.Text ==a.ToString()) //判断是不是第一个数
total = a;
else
total -= a;
textBox1.Text += "-";
s = "";
}
private void button16_Click(object sender, EventArgs e) //实现除法
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
d = "/";
a = int.Parse(s);
if (textBox1.Text == a.ToString()) //判断是不是第一个数
total = a;
else
total /= a;
textBox1.Text += "/";
s = "";
}
private void button15_Click(object sender, EventArgs e) //实现乘法
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
d = "*";
a = int.Parse(s);
m*= a;
total+= m;
textBox1.Text += "*";
s = "";
}
private void button13_Click(object sender, EventArgs e) //输出结果
{
switch(d)
{
case "+":
b = int.Parse(s);
total += b;
textBox1.Text += "=";
textBox1.Text += total.ToString();
break;
case "*":
b = int.Parse(s);
total *= b;
textBox1.Text += "=";
textBox1.Text += total.ToString();
m = 1;
break;
case "-":
b = int.Parse(s);
total -= b;
textBox1.Text += "=";
textBox1.Text += total.ToString();
break;
case "/":
b = int.Parse(s);
total /= b;
textBox1.Text += "=";
textBox1.Text += total.ToString();
break;
}
}
private void button10_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
s += "0";
textBox1.Text += "0";
}
}
}
欢迎指正,以及提出更多建议。谢谢!