![]() |
#2
书中叶2011-05-10 18:26
|
static double tmp=0;
static int state=0;//1为+,2为-,3为*,4为/
tmp用于暂存得数,state用于指明上一次运算的类型,之后我又在public ref class Form1里面写了:static int state=0;//1为+,2为-,3为*,4为/

private: void judge()
{
double t;
t=Convert::ToDouble(this->text->Text);
switch(state)
{
case 0:
{
tmp=t;
}
case 1:
{
tmp+=t;
}
case 2:
{
tmp-=t;
}
case 3:
{
tmp*=t;
}
case 4:
{
tmp/=t;
}
}
}
数字键:(以1为例){
double t;
t=Convert::ToDouble(this->text->Text);
switch(state)
{
case 0:
{
tmp=t;
}
case 1:
{
tmp+=t;
}
case 2:
{
tmp-=t;
}
case 3:
{
tmp*=t;
}
case 4:
{
tmp/=t;
}
}
}
private: System::Void b1_Click(System::Object^ sender, System::EventArgs^ e)
{
this->text->Text+="1";
}
加号:{
this->text->Text+="1";
}

private: System::Void bplu_Click(System::Object^ sender, System::EventArgs^ e)
{
judge();
state=1;
this->text->Text="";
}
等号:{
judge();
state=1;
this->text->Text="";
}

private: System::Void bequ_Click(System::Object^ sender, System::EventArgs^ e)
{
judge();
state=0;
this->text->Text=Convert::ToString(tmp);
tmp=0;
}
在我运行程序的时候,我先点“1”的按钮(b1),文本框(text)里显示“1”,然后我点“+”(bplu),文本框内清空,之后我再点“2”的按钮(b2),文本框(text)里显示“2”,我再按“=”按钮(bequ),文本框内就显示“1”。这就不对了。{
judge();
state=0;
this->text->Text=Convert::ToString(tmp);
tmp=0;
}
tmp只能记录第一次输入的数(第一次按加减乘除之前的那个数),judge()里的计算似乎只有tmp=t;执行了,请问这是为什么?