C++Builder编写计算器
2015-11-06 11:51
211 查看
用C++Builder确实能快速上手, 只要是会一点C++基础的,都能很快的编写一些小程序,而且VCL库组件也很丰富,比微软MFC强多了。
自己动手写了一个计算器来增加自己的兴趣。C++基础以后有空还是还得学着走。。。
代码:
自己动手写了一个计算器来增加自己的兴趣。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(); } //---------------------------------------------------------------------------
相关文章推荐
- 面向对象程序设计(多态、转换、继承)
- 四步教你DIY“终极黑客键盘”
- C++sprintf()函数
- C语言中内存对齐方式摘录
- 二叉树中和为某一值的路径
- C/C++缺陷与优化
- C++ Primer 学习笔记_14_动态数组
- C++设计模式[七]组合模式
- C++ Primer 学习笔记_12_指针(续)
- C++ Primer 学习笔记_11_指针
- c++STL容器vector的复制
- C++IO对象不可复制或赋值
- C++运算符优先级
- C++ Primer 学习笔记_10_数组
- C++里类中定义对象与定义局部变量的问题
- Java和C++中的重载,覆盖,隐藏区别
- C++标准转换运算符reinterpret_cast
- C++标准转换运算符reinterpret_cast
- C语言的编程机制
- 码农的取经之路,c++是个什么鬼?