//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "About.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Application->Title="ZEC 四则运算";
numMax=10;
srand((unsigned)time(NULL));
//算式编辑框文本靠右
SetWindowLong(edtEquation->Handle, GWL_STYLE,
GetWindowLong(edtEquation->Handle, GWL_STYLE)|ES_RIGHT);
ActiveControl=Edit1;
tag=2; //专项练习默认为乘法运算
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
numMax=10;
Edit1->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton2Click(TObject *Sender)
{
numMax=20;
Edit1->SetFocus();
}
//---------------------------------------------------------------------------
//点击下一题按钮
void __fastcall TForm1::btnNextClick(TObject *Sender)
{
if(mnuSpecial->Checked)
op=tag;
else
op=rand()%4;
a=rand()%numMax;
b=rand()%numMax;
switch(op)
{
case 0: //加法
edtEquation->Text=IntToStr(a)+" + "+IntToStr(b)+" =";
rightAnswer=a+b;
Label1->Caption="加法题:"+IntToStr(++addEquationCount)+" 道";
break;
case 1: //减法
if(a<b)
{
temp=a;
a=b;
b=temp;
}
edtEquation->Text=IntToStr(a)+" - "+IntToStr(b)+" =";
rightAnswer=a-b;
Label2->Caption="减法题:"+IntToStr(++subEquationCount)+" 道";
break;
case 2: //乘法
edtEquation->Text=IntToStr(a)+" × "+IntToStr(b)+" =";
rightAnswer=a*b;
Label3->Caption="乘法题:"+IntToStr(++mulEquationCount)+" 道";
break;
case 3: //除法
if(b==0)
b=rand()%(numMax-1)+1;
rightAnswer=a;
a=b*rightAnswer;
edtEquation->Text=IntToStr(a)+" ÷ "+IntToStr(b)+" =";
Label4->Caption="除法题:"+IntToStr(++divEquationCount)+" 道";
break;
}
lblTitle->Caption=AnsiString().sprintf("第 %.2d 道题",++total);
Edit1->Text="";
Edit1->SetFocus();
lblResult->Caption="";
btnCheck->Enabled=true;
}
//---------------------------------------------------------------------------
//点击检查按钮
void __fastcall TForm1::btnCheckClick(TObject *Sender)
{
try
{
answer=Edit1->Text.ToInt();
if(answer==rightAnswer)
{
lblResult->Caption="√";
lblRight->Caption="答对题:"+IntToStr(++rightAnswerCount)+" 道";
}
else
{
lblResult->Caption="X";
lblWrong->Caption="答错题:"+IntToStr(++wrongAnswerCount)+" 道";
}
btnCheck->Enabled=false;
btnNext->SetFocus();
}
catch(...)
{
ShowMessage("请输入数字");
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
mnuNew->Click(); //窗体第一次显示时自动点击重新开始菜单
}
//---------------------------------------------------------------------------
//点击加法运算菜单项,减法、乘法、除法复用该事件处理函数
void __fastcall TForm1::mnuAddClick(TObject *Sender)
{
TMenuItem *obj=(TMenuItem *)Sender;
obj->Checked=true; //菜单项打勾
tag=obj->Tag; //取菜单项的Tag
}
//---------------------------------------------------------------------------
//点击退出菜单
void __fastcall TForm1::mnuExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
//点击关于菜单
void __fastcall TForm1::mnuAboutClick(TObject *Sender)
{
frmAbout->ShowModal();
}
//---------------------------------------------------------------------------
//点击重新开始菜单
void __fastcall TForm1::mnuNewClick(TObject *Sender)
{
total=0;
addEquationCount=0;
subEquationCount=0;
mulEquationCount=0;
divEquationCount=0;
rightAnswerCount=0;
wrongAnswerCount=0;
Label1->Caption="加法题:0 道";
Label2->Caption="减法题:0 道";
Label3->Caption="乘法题:0 道";
Label4->Caption="除法题:0 道";
lblRight->Caption="答对题:0 道";
lblWrong->Caption="答错题:0 道";
btnNext->Click(); //自动点击下一题按钮
}
//---------------------------------------------------------------------------