针对上次的计算器代码整理并做了修改之后,主要更正了算法的一个Bug并修改了UI,故另发一博文。V1.20
#pragma once
namespace Calculator {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 摘要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}
protected:
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ txtInfo;
static array<System::Windows::Forms::Button^>^btnNumber=gcnew array<System::Windows::Forms::Button^>(10);
static array<System::Windows::Forms::Button^>^Operator=gcnew array<System::Windows::Forms::Button^>(10);
private: System::Windows::Forms::TextBox^ txtFormula;
private: System::Windows::Forms::Button^ btnDot;
static bool bEquation=false,bNumber=true,bDot=false; //用于记录等号、数字、小数点按钮的状态
static double a=0,b=0,c=0,answer=0;
static char m='\0',n='\0';
static int cOperator=0; //用于记录操作符按钮的状态
protected:
public: void Count(){ //计算函数
switch(n){
case'+':
case'-':
switch(m){
case'+':a=a+b;b=c;m=n;break;
case'-':a=a-b;b=c;m=n;break;
case'*':a=a*b;b=c;m=n;break;
case'/':a=a/b;b=c;m=n;break;
}
break;
case'*':
if(m=='/') {a=a/b;b=c;m=n;break;}
else{ b=b*c;break;}
case'/':
if(m=='/') {a=a/b;b=c;m=n;break;}
else{ b=b/c;break;}
case'\0':break;
}
if(bEquation){ //如果bEquation为真,则执行如下运算
switch(m){
case'+':a=a+b;break;
case'-':a=a-b;break;
case'*':a=a*b;break;
case'/':a=a/b;break;
case'\0':txtFormula->AppendText("Error!");
}
}
}
void Startup(){ //载入时初始化按钮控件
for(int i=0;i<=9;i++){
btnNumber[i]=gcnew Button();
this->Controls->Add(this->btnNumber[i]);
btnNumber[i]->Left=36+60*((i-1)%3);
btnNumber[i]->Top=230-50*((i-1)/3);
btnNumber[i]->Width=50;
btnNumber[i]->Height=35;
btnNumber[i]->Name="btnNumber"+i.ToString();
btnNumber[i]->Text=i.ToString();
btnNumber[i]->Font=(gcnew System::Drawing::Font(L"Microsoft YaHei", 13.5F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,static_cast<System::Byte>(134)));
btnNumber[i]->Click+=gcnew System::EventHandler(this, &Form1::button_Click);
}
btnNumber[0]->Location=System::Drawing::Point(36,280);
btnNumber[0]->Size= System::Drawing::Size(110, 35);
for(int i=0;i<=5;i++){
Operator[i]=gcnew Button();
this->Controls->Add(this->Operator[i]);
Operator[i]->Left=36+60*((5-i));
Operator[i]->Top=80;
Operator[i]->Width=50;
Operator[i]->Height=35;
Operator[i]->Font=(gcnew System::Drawing::Font(L"Microsoft YaHei", 13.5F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,static_cast<System::Byte>(134)));
Operator[i]->Click+=gcnew System::EventHandler(this, &Form1::button_Click);
}
Operator[0]->Text="=";
Operator[1]->Text="+";
Operator[2]->Text="-";
Operator[3]->Text="*";
Operator[4]->Text="/";
Operator[5]->Text="C";
Operator[0]->Left=216;
Operator[0]->Top=230;
Operator[0]->Size=System::Drawing::Size(50,85);
Operator[1]->Left=216;
Operator[1]->Top=130;
Operator[1]->Size=System::Drawing::Size(50,85);
bEquation=false;
}
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->txtInfo = (gcnew System::Windows::Forms::TextBox());
this->txtFormula = (gcnew System::Windows::Forms::TextBox());
this->btnDot = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// txtInfo
//
this->txtInfo->BackColor = System::Drawing::SystemColors::GradientInactiveCaption;
this->txtInfo->BorderStyle = System::Windows::Forms::BorderStyle::None;
this->txtInfo->Cursor = System::Windows::Forms::Cursors::Default;
this->txtInfo->Font = (gcnew System::Drawing::Font(L"Consolas", 15, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtInfo->ImeMode = System::Windows::Forms::ImeMode::Close;
this->txtInfo->Location = System::Drawing::Point(12, 25);
this->txtInfo->Margin = System::Windows::Forms::Padding(3, 4, 3, 4);
this->txtInfo->Name = L"txtInfo";
this->txtInfo->ReadOnly = true;
this->txtInfo->Size = System::Drawing::Size(266, 24);
this->txtInfo->TabIndex = 0;
this->txtInfo->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
this->txtInfo->WordWrap = false;
//
// txtFormula
//
this->txtFormula->BackColor = System::Drawing::SystemColors::GradientInactiveCaption;
this->txtFormula->BorderStyle = System::Windows::Forms::BorderStyle::None;
this->txtFormula->Font = (gcnew System::Drawing::Font(L"Consolas", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtFormula->ImeMode = System::Windows::Forms::ImeMode::Close;
this->txtFormula->Location = System::Drawing::Point(12, 10);
this->txtFormula->Name = L"txtFormula";
this->txtFormula->ReadOnly = true;
this->txtFormula->Size = System::Drawing::Size(266, 15);
this->txtFormula->TabIndex = 1;
this->txtFormula->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
//
// btnDot
//
this->btnDot->BackColor = System::Drawing::SystemColors::GradientActiveCaption;
this->btnDot->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 15, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(134)));
this->btnDot->Location = System::Drawing::Point(156, 280);
this->btnDot->Name = L"btnDot";
this->btnDot->Size = System::Drawing::Size(50, 35);
this->btnDot->TabIndex = 2;
this->btnDot->Text = L".";
this->btnDot->UseVisualStyleBackColor = false;
this->btnDot->Click += gcnew System::EventHandler(this, &Form1::btnDot_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(7, 17);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::SystemColors::GradientActiveCaption;
this->ClientSize = System::Drawing::Size(297, 336);
this->Controls->Add(this->btnDot);
this->Controls->Add(this->txtFormula);
this->Controls->Add(this->txtInfo);
this->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(134)));
this->Margin = System::Windows::Forms::Padding(3, 4, 3, 4);
this->MaximizeBox = false;
this->MaximumSize = System::Drawing::Size(313, 374);
this->MinimumSize = System::Drawing::Size(313, 374);
this->Name = L"Form1";
this->Opacity = 0.99;
this->SizeGripStyle = System::Windows::Forms::SizeGripStyle::Hide;
this->Text = L"Calculator";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
//窗体载入事件
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
txtInfo->Text="0";
Startup();
}
//绝大多数按钮共享事件
private: System::Void button_Click(System::Object^ sender, System::EventArgs^ e) {
String^ stext;
Button^ bClick=(Button^) sender;
stext=bClick->Text;
switch(Convert::ToChar(stext)){
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
if(bEquation){
txtFormula->Clear();
txtInfo->Text=stext->ToString();
}
else if(cOperator&&!bNumber){
txtInfo->Text=stext->ToString();
}
else{
if(txtInfo->Text=="0") txtInfo->Text=stext;
else txtInfo->AppendText(stext->ToString());
}
bEquation=false;
bNumber=true;
bDot=false;
break;
case'+':
case'-':
case'*':
case'/':
if(bEquation){ //如果刚刚按了等号按钮
txtFormula->Text=txtInfo->Text;
txtFormula->AppendText(stext);
a=Convert::ToDouble(txtInfo->Text);
m=Convert::ToChar(stext);
bEquation=false;
cOperator=1;
break;
}
if(cOperator==0){ //第一回按运算符按钮
a=Convert::ToDouble(txtInfo->Text);
m=Convert::ToChar(bClick->Text);
}
else if(cOperator==1){ //第二回按按钮
b=Convert::ToDouble(txtInfo->Text);
n=Convert::ToChar(stext);
}
else{ //第三回按按钮
c=Convert::ToDouble(txtInfo->Text);
Count();
c=0;
n=Convert::ToChar(stext);
}
txtFormula->AppendText(txtInfo->Text);//将txtInfo的数据添加到txtFormula
txtFormula->AppendText(stext);//运算符添加到txtFormula
bEquation=false;
bNumber=false;
cOperator++;
bDot=false;
break;
case'=':
if(bEquation){
break;
}
bEquation=true;
bNumber=false;
txtFormula->AppendText(txtInfo->Text);
txtFormula->AppendText("=");
if(cOperator==0) { break; }
else if(cOperator==1) b=Convert::ToDouble(txtInfo->Text);
else c=Convert::ToDouble(txtInfo->Text);
Count();
answer=a;
txtInfo->Text=answer.ToString();
cOperator=0;
a=b=c=0;
m=n='0';
break;
case 'C'://全部初始化
txtInfo->Text="0";
txtFormula->Text="";
a=b=c=answer=0;
m=n='\0';
bDot=false;
break;
}
}
//小数点按钮
private: System::Void btnDot_Click(System::Object^ sender, System::EventArgs^ e) {
if(bDot==false&&bNumber){
txtInfo->AppendText(".");
}
bDot=true;
bNumber=true;
}
};
}
源码下载 http://cid-c96c6ec8ad8dcfea.office.live.com/browse.aspx/Program/Calculator V1.20