zoukankan      html  css  js  c++  java
  • C++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();
    }
    //---------------------------------------------------------------------------
  • 相关阅读:
    《数据结构
    《数据结构
    《数据结构
    《算法
    《linux 进程管理》- ps/top/kill/killall/nice
    《linux 字符处理》- grep/sort/uniq/tr/paste/sed/awk
    《linux 文件目录》- touch/rm/mv/cat/head/tail/cp/mkdir/chmod/chown/find/locate/which/whereis/tar
    MySQL优化必须调整的10项配置
    PV-UV
    linux+nginx+mysql+php高性能服务器搭建
  • 原文地址:https://www.cnblogs.com/hkleak/p/4942181.html
Copyright © 2011-2022 走看看