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 SimpleCalculator
{
public partial class frmCalculator : Form
{
private int left = 0;
private int right =0;
private string oper = "+";
private bool isOperator = false;
public frmCalculator()
{
InitializeComponent();
}
private void btnEqual_Click(object sender, EventArgs e)
{
try
{
switch (oper)
{
case "+":
left = left + right;
break;
case "-":
left = left - right;
break;
case "*":
left = left * right;
break;
case "/":
left = left / right;
break;
default:
break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
tbShow.Text = Convert.ToString(left);
right = left;
isOperator = false;
oper = "";
}
private void btnNum_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (isOperator)
{
right +=Convert.ToInt32( btn.Text);
tbShow.Text = right.ToString();
}
else
{
left +=Convert.ToInt32( btn.Text);
tbShow.Text = left.ToString();
}
}
private void btnOperator_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
oper = btn.Text;
isOperator = true;
}
}
在private void btnEqual_Click(object sender, EventArgs e)
{
try
{
switch (oper)
{
case "+":
left = left + right;
break;
case "-":
left = left - right;
break;
case "*":
left = left * right;
break;
case "/":
left = left / right;
break;
default:
break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
tbShow.Text = Convert.ToString(left);
right = left;
isOperator = false;
oper = "";
}中最后加一条right=0;即可