C++Builder编写计算器

用C++Builder确实能快速上手, 只要是会一点C++基础的,都能很快的编写一些小程序,而且VCL库组件也很丰富,比微软MFC强多了。

自己动手写了一个计算器来增加自己的兴趣。C++基础以后有空还是还得学着走。。。

C++Builder编写计算器

 

代码:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "CalcMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "RzPanel"
#pragma resource "*.dfm"
TForm1 *Form1;
//声明全局变量
float Num1;
float Result;
int sort; //判断是何种运算


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
	Form1->Edit1->Clear();//清空文本框
	Num1=0;
	Result=0;   //赋初值
	sort=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
	Edit1->Text = Edit1->Text + SpeedButton1->Caption;
	Edit1->SelStart =ByteLength(Edit1->Text);  //设置光标位置
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton12Click(TObject *Sender)
{
	sort = 1; //变量为1 表示加法运算
	Num1 = StrToFloat(Edit1->Text);//赋值给第一个变量
	Edit1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton17Click(TObject *Sender)
{
	switch (sort) //判断运算
	{
		case
		1:
		Result= Num1 + StrToFloat(Edit1->Text); //计算相加
		break;
		case
		2:
		Result= Num1 - StrToFloat(Edit1->Text); //计算相减
		break;
		case
		3:
		Result= Num1 * StrToFloat(Edit1->Text); //计算相乘
		break;
		case
		4:
		Result= Num1 / StrToFloat(Edit1->Text); //计算除法
		break;

	   default:
	   Edit1->Text= "";
	}
	 Edit1->Text =FloatToStr(Result); //显示运算结果
	 Edit1->SelStart =ByteLength(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
	Edit1->Text = Edit1->Text + SpeedButton2->Caption;
	Edit1->SelStart =ByteLength(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
	Edit1->Text = Edit1->Text + SpeedButton3->Caption;
	Edit1->SelStart =ByteLength(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton4Click(TObject *Sender)
{
	Edit1->Text = Edit1->Text + SpeedButton4->Caption;
	Edit1->SelStart =ByteLength(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton5Click(TObject *Sender)
{
	Edit1->Text = Edit1->Text + SpeedButton5->Caption;
	Edit1->SelStart =ByteLength(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton6Click(TObject *Sender)
{
	Edit1->Text = Edit1->Text + SpeedButton6->Caption;
	Edit1->SelStart =ByteLength(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton7Click(TObject *Sender)
{
	Edit1->Text = Edit1->Text + SpeedButton7->Caption;
	Edit1->SelStart =ByteLength(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton8Click(TObject *Sender)
{
	Edit1->Text = Edit1->Text + SpeedButton8->Caption;
	Edit1->SelStart =ByteLength(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton9Click(TObject *Sender)
{
	Edit1->Text = Edit1->Text + SpeedButton9->Caption;
	Edit1->SelStart =ByteLength(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton10Click(TObject *Sender)
{
	Edit1->Text = Edit1->Text + SpeedButton10->Caption;
	Edit1->SelStart =ByteLength(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton11Click(TObject *Sender)
{
	Edit1->Text = Edit1->Text + SpeedButton11->Caption;
	Edit1->SelStart =ByteLength(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton16Click(TObject *Sender)
{
	Edit1->Clear();
	Num1=0;
	Result=0;
	sort=0;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton13Click(TObject *Sender)
{
	sort = 2;
	Num1 = StrToFloat(Edit1->Text);
	Edit1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton14Click(TObject *Sender)
{
	sort = 3;
	Num1 = StrToFloat(Edit1->Text);
	Edit1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton15Click(TObject *Sender)
{
	sort = 4;
	Num1 = StrToFloat(Edit1->Text);
	Edit1->Clear();
}
//---------------------------------------------------------------------------

原文链接: https://www.cnblogs.com/hkleak/p/4942181.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++Builder编写计算器

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/224142

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午12:20
下一篇 2023年2月13日 下午12:20

相关推荐