|
우선 숫자 버튼에 대해서 말씀드리면 숫자 버튼에는 그 캡션으로 그 버튼이 눌렸을 때의 숫자를 표시하기 위해
Caption 에 1, 2, 3, ... 의 숫자를 입력하셨을 것입니다.
그럼 0 ~ 9 까지의 버튼 핸들러는 다음과 같이
Label1->Caption = Label1->Caption + ((TButton *)(Sender))->Caption;
하시면 됩니다.
Label1 은 입력된 숫자가 표시될 라벨이죠.
참고로 각 버튼을 더블-클릭해서 위의 코드를 넣지 마시고 0번 코드에만 위의 코드를 넣고
다른 버튼들은 Event 를 연결만 시켜주시면 됩니당...
그리고 + 만 해보도록 하죠.
+ 버튼을 누르면 일반계산기와 같이 기존에 입력된 숫자들이 지워지고 새로운 입력을 기다리게 하기 위해서
기존에 입력된 값을 임시변수에 저장합니다. 이 임시변수는 핸들러 내에 선언하시면 안되고
클래스 내부에 넣어주세요.
int m_iSrc1;
그리고 어떤 연산을 진행중인지를 알기 위해서 이를 저장할 목적의 변수를 선언합니다. 이것역시 클래스내에 선언하시면 됩니다.
AnsiString m_szCalc;
그럼 + 버튼 핸들러에는
m_iSrc1 = Label1->Caption.ToInt();
Label1->Caption = "";
m_szCalc = ((TButton *)(Sender))->Caption;
이제 다시 0 ~ 9 버튼을 누르고 = 버튼을 눌렀을 때 의 버튼 핸들러는
if( m_szCalc.AnsiCompare( "+" ) == 0 )
{
Label1.Caption = IntToStr( m_iSrc1 + Label1.Caption.ToInt() );
}
이런식으로 작성하시면 됩니다.
정확히 계산기를 어떻게 작성할건지를 명확히 해야 올바른 설계가 가능할 듯하구요..
언어라는 것은 원하는 것을 명확히 하면 금방 배울 수 있습니다 ^^... 좀 애매하죠?
쥬신 님이 쓰신 글 :
: 쥬신 님이 쓰신 글 :
: : 문법적으로도 이해가 가지가 않습니다. 아직 프로그램을 짜고 있거든요.
: //---------------------------------------------------------------------------
:
: #include <vcl.h>
: #pragma hdrstop
:
: #include "Unit4.h"
: //---------------------------------------------------------------------------
: #pragma package(smart_init)
: #pragma resource "*.dfm"
: TForm1 *Form1;
: //---------------------------------------------------------------------------
: __fastcall TForm1::TForm1(TComponent* Owner)
: : TForm(Owner)
: {
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button1Click(TObject *Sender)
: {
: int iBut1;
: iBut1=1;
: Label1->Caption=iBut1;
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button2Click(TObject *Sender)
: {
: int iBut2;
: iBut2=2;
: Label1->Caption=iBut2;
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button3Click(TObject *Sender)
: {
: int iBut3;
: iBut3=3;
: Label1->Caption=iBut3;
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button4Click(TObject *Sender)
: {
: int iBut4;
: iBut4=4;
: Label1->Caption=iBut4;
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button5Click(TObject *Sender)
: {
: int iBut5;
: iBut5=5;
: Label1->Caption=iBut5;
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button6Click(TObject *Sender)
: {
: int iBut6;
: iBut6=6;
: Label1->Caption=iBut6;
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button7Click(TObject *Sender)
: {
: int iBut7;
: iBut7=7;
: Label1->Caption=iBut7;
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button8Click(TObject *Sender)
: {
: int iBut8;
: iBut8=8;
: Label1->Caption=iBut8;
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button9Click(TObject *Sender)
: {
: int iBut9;
: iBut9=9;
: Label1->Caption=iBut9;
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button10Click(TObject *Sender)
: {
: int iBut10;
: iBut10=0;
: Label1->Caption=iBut10;
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button13Click(TObject *Sender)
: {
: int i = ((TButton *)Sender)->Tag;
: int j = ((TButton *)Sender)->Tag;
: int temp = Label1->Caption.ToInt();
: Label1->Caption = temp*j+ i;
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::Button12Click(TObject *Sender)
: {
: int i = ((TButton *)Sender)->Tag;
: int temp = Label1->Caption.ToInt();
: Label1->Caption = temp-10+ i;
: }
: //---------------------------------------------------------------------------
:
: void __fastcall TForm1::Button11Click(TObject *Sender)
: {
: int i = ((TButton *)Sender)->Tag;
: int temp = Label1->Caption.ToInt();
: Label1->Caption = temp+10+ i;
: }
: //---------------------------------------------------------------------------
:
: void __fastcall TForm1::Button14Click(TObject *Sender)
: {
: int i = ((TButton *)Sender)->Tag;
: int temp = Label1->Caption.ToInt();
: Label1->Caption = temp/2+ i;
: }
: //---------------------------------------------------------------------------
:
: void __fastcall TForm1::Button15Click(TObject *Sender)
: {
: int i = ((TButton *)Sender)->Tag;
: int temp = Label1->Caption.ToInt();
: Label1->Caption = i;
: }
|