程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++Builder編寫計算器,builder編寫計算器

C++Builder編寫計算器,builder編寫計算器

編輯:C++入門知識

C++Builder編寫計算器,builder編寫計算器


用C++Builder確實能快速上手, 只要是會一點C++基礎的,都能很快的編寫一些小程序,而且VCL庫組件也很豐富,比微軟MFC強多了。

自己動手寫了一個計算器來增加自己的興趣。C++基礎以後有空還是還得學著走。。。

 

代碼:

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

#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();
}
//---------------------------------------------------------------------------

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved