本帖最后由 小天 于 2013-8-8 14:54 编辑
希望大家提出宝贵的意见
Form1.cs代码
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 计算器
{
public partial class Form1 : Form
{
//计算器,可以进行加减乘除的计算
string str="";//暂存上一个结果
int i,number1,number2,number3;//判断i的值选择操作符
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
str = str + "1";
txtnumber.Text = str;
}
private void button2_Click(object sender, EventArgs e)
{
str = str + "2";
txtnumber.Text = str;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button11_Click(object sender, EventArgs e)
{
i = 1;
number1 = Convert.ToInt32(txtnumber.Text);
str = "";
}
private void button15_Click(object sender, EventArgs e)
{
number2 = Convert.ToInt32(txtnumber.Text);
switch (i)
{
case 1://加
number3 = number1 + number2;
break;
case 2://减
number3 = number1 - number2;
break;
case 3://乘
number3 = number1 * number2;
break;
case 4://除
if (number2 != 0)
{
number3 = number1 / number2;
}
else
{
MessageBox.Show("被除数不能为0");
txtnumber.Text = "0";
str = "";
}
break;
default:
break;
}
txtnumber.Text = number3.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
str = str + "3";
txtnumber.Text = str;
}
private void button4_Click(object sender, EventArgs e)
{
str = str + "4";
txtnumber.Text = str;
}
private void button5_Click(object sender, EventArgs e)
{
str = str + "5";
txtnumber.Text = str;
}
private void button6_Click(object sender, EventArgs e)
{
str = str + "6";
txtnumber.Text = str;
}
private void button7_Click(object sender, EventArgs e)
{
str = str + "7";
txtnumber.Text = str;
}
private void button8_Click(object sender, EventArgs e)
{
str = str + "8";
txtnumber.Text = str;
}
private void button9_Click(object sender, EventArgs e)
{
str = str + "9";
txtnumber.Text = str;
}
private void button12_Click(object sender, EventArgs e)
{
i = 2;
number1 = Convert.ToInt32(txtnumber.Text);
str = "";
}
private void button13_Click(object sender, EventArgs e)
{
i = 3;
number1 = Convert.ToInt32(txtnumber.Text);
str = "";
}
private void button14_Click(object sender, EventArgs e)
{
i = 4;
number1 = Convert.ToInt32(txtnumber.Text);
str = "";
}
private void button16_Click(object sender, EventArgs e)
{
txtnumber.Text = "0";
str = "";
}
private void button10_Click(object sender, EventArgs e)
{
if (txtnumber.Text != "0")
{
str = str + "0";
txtnumber.Text = str;
}
}
}
}
|
|