下面是计算器的代码,里边有许多错误,每找到一处,加3分;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double num1 = 0, num2 = 0, result = 0; //用于描述第一个操作数,第二个操作数和结果
int type = 0; //用于描述运算符的类型
int dot_num = 1;
bool is_num1 = false; //描述是否为第一个操作符
bool is_dot = false; //描述是否为小数
bool is_fushu = false; //描述是否为负数
bool doted = false; //为小数的标识
void func(int numb)
{
if (is_num1 ==false )
{
if (is_dot == true)
{
double tempNum = 1;
for (int i = 0; i < dot_num; i++)
{
tempNum = 10 * tempNum;
}
if (!is_fushu)
num1 = num1 + (double)numb / (double)(tempNum);
else
num1 = num1 - (double)numb / (double)(tempNum);
dot_num++;
}
else
{
if (!is_fushu)
num1 = num1 * 10 + numb;
else
num1 = num1 * 10 - numb;
}
textBox1.Text = num1.ToString();
}
else
{
if(is_dot==true)
{
double tempNum = 1;
for (int i=0;i<dot_num;i++)
{
tempNum=10*tempNum;
}
if(!is_fushu)
num2=num2+(double)numb/(double)(tempNum);
else
num2=num2-(double)numb/(double)(tempNum);
dot_num++;
}
else
{
if(!is_fushu)
num2=num2*10+numb;
else
num2=num2*10-numb;
}
textBox1.Text=num2.ToString();
}
}
void calType()
{
is_num1 = true; //第一个操作数标识为真
dot_num = 1;
is_dot = false;
is_fushu = false;
doted = false;
}
private void button1_Click(object sender, EventArgs e)
{
func(1);
}
private void button2_Click(object sender, EventArgs e)
{
func(2);
}
private void button3_Click(object sender, EventArgs e)
{
func(3);
}
private void button4_Click(object sender, EventArgs e)
{
func(4);
}
private void button5_Click(object sender, EventArgs e)
{
func(5);
}
private void button6_Click(object sender, EventArgs e)
{
func(6);
}
private void button7_Click(object sender, EventArgs e)
{
func(7);
}
private void button8_Click(object sender, EventArgs e)
{
func(8);
}
private void button9_Click(object sender, EventArgs e)
{
func(9);
}
private void button10_Click(object sender, EventArgs e)
{
func(0);
}
[此贴子已经被作者于2007-8-22 8:35:59编辑过]