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();
    }
    //---------------------------------------------------------------------------
  • 相关阅读:
    dll相对路径设置
    OPC UA Error: Could not add self-signed certificate to certficate store.
    PB调用C#封装的视频插件中拍照方法不生效的问题
    net反射加载出现错误: ex = {“无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性。”}
    xtralayout模式下,修改 layOutcontrolgroup分组框表头背景颜色步骤
    abap 本地包 生成请求号
    PB调用SAP的Web Service结构体数组参数方法详解(使用C#调用Web Service生成dll)
    zookeeper NIOServerCnxn: Too many connections from IP- max i
    mysql 10038错误解决方案
    PHP获取文件夹内所有文件包括子目录文件的名称或路径
  • 原文地址:https://www.cnblogs.com/hkleak/p/4942181.html
Copyright © 2011-2022 走看看