wxWidgets编写的简易计算器

#include
#include


class Calculator : public wxFrame
{
public:
     Calculator(const wxString& title);//初始化
     void OnPlus(wxCommandEvent& event);//加法
     void OnMinus(wxCommandEvent& event);//减法
     void OnDivide(wxCommandEvent& event);//除法
     void OnRide(wxCommandEvent& event);//乘法
     void OnPF(wxCommandEvent& event);//平方
     void OnKF(wxCommandEvent& event);//开方


     void SaveVale1( );//存值
     void Calculate( );//计算


     void OnBtCls(wxCommandEvent& event);//清除
     void OnBtClose(wxCommandEvent& event);//关闭
/*各个按钮函数*/
     void OnBt0(wxCommandEvent& event);
     void OnBt1(wxCommandEvent& event);
     void OnBt2(wxCommandEvent& event);
     void OnBt3(wxCommandEvent& event);
     void OnBt4(wxCommandEvent& event);
     void OnBt5(wxCommandEvent& event);
     void OnBt6(wxCommandEvent& event);
     void OnBt7(wxCommandEvent& event);
     void OnBt8(wxCommandEvent& event);
     void OnBt9(wxCommandEvent& event);
     void OnBtPoint(wxCommandEvent& event);
     void OnBtEqual(wxCommandEvent& event);


/*定义各个按钮*/


     wxButton* btcls;
     wxButton* btbac;
     wxButton* btclose;
     wxButton* bt1;
     wxButton* bt2;
     wxButton* bt3;
     wxButton* bt4;
     wxButton* bt5;
     wxButton* bt6;
     wxButton* bt7;
     wxButton* bt8;
     wxButton* bt9;
     wxButton* bt0;
     wxButton* btplus;
     wxButton* btminus;
     wxButton* btdivide;
     wxButton* btride;
     wxButton* btpoint;
     wxButton* btequal;
     wxButton* pingfang;
     wxButton* kaifang;


     double m_value1;
     double m_value2;
     double result;
     wxString m_str;
     wxGridSizer* gs;
     wxTextCtrl* tc1;
     int c_sign;
};


/*自定义按钮IP*/


const int ID_CLS = 101;
const int ID_BAC = 102;
const int ID_ZERO = 103;
const int ID_ONE = 104;
const int ID_TWO = 105;
const int ID_THREE = 106;
const int ID_FOUR = 107;
const int ID_FIVE = 108;
const int ID_SIX  = 109;
const int ID_SEVEN = 110;
const int ID_EIGHT = 111;
const int ID_NINE = 112;
const int ID_PLUS = 113;
const int ID_MINUS = 114;
const int ID_DIVIDE = 115;
const int ID_RIDE = 116;
const int ID_POINT = 117;
const int ID_EQUAL = 118;
const int ID_PF = 119;
const int ID_KF = 120;


Calculator::Calculator(const wxString& title)
           :wxFrame(NULL,wxID_ANY,title,wxDefaultPosition,wxSize(270,350))
{
     wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);


     tc1 = new wxTextCtrl(this, -1, wxT(""), wxPoint(-1, -1),
     wxSize(-1, -1), wxTE_LEFT);
     vbox->Add(tc1,0,wxEXPAND | wxBOTTOM | wxTOP ,4);


     c_sign = 0;
     m_value1 = 0;
     m_value2 = 0;
     m_str = "";


/*按钮实现*/


     btcls = new wxButton(this,ID_CLS,wxT("Cls"));
     btclose = new wxButton(this,wxID_EXIT,wxT("Close"));
     bt1 = new wxButton(this,ID_ONE,wxT("1"));
     bt2 = new wxButton(this,ID_TWO,wxT("2"));
     bt3 = new wxButton(this,ID_THREE,wxT("3"));
     bt4 = new wxButton(this,ID_FOUR,wxT("4"));
     bt5 = new wxButton(this,ID_FIVE,wxT("5"));
     bt6 = new wxButton(this,ID_SIX,wxT("6"));
     bt7 = new wxButton(this,ID_SEVEN,wxT("7"));
     bt8 = new wxButton(this,ID_EIGHT,wxT("8"));
     bt9 = new wxButton(this,ID_NINE,wxT("9"));
     bt0 = new wxButton(this,ID_ZERO,wxT("0"));
     btpoint = new wxButton(this,ID_POINT,wxT("."));
     btplus = new wxButton(this,ID_PLUS,wxT("+"));
     btminus = new wxButton(this,ID_MINUS,wxT("-"));
     btride = new wxButton(this,ID_RIDE,wxT("*"));
     btdivide = new wxButton(this,ID_DIVIDE,wxT("/"));
     btequal = new wxButton(this,ID_EQUAL,wxT("="));
     pingfang = new wxButton(this,ID_PF,wxT("pf"));
     kaifang = new wxButton(this,ID_KF,wxT("kf"));


/*定义GridSizer,并把相应按钮填充*/


     gs = new wxGridSizer(5,4,4,4);
     gs->Add(btcls,0,wxEXPAND);
     gs->Add(btclose,0,wxEXPAND);
     gs->Add(pingfang,0,wxEXPAND);
     gs->Add(kaifang,0,wxEXPAND);
     gs->Add(bt7,0,wxEXPAND);
     gs->Add(bt8,0,wxEXPAND);
     gs->Add(bt9,0,wxEXPAND);
     gs->Add(btdivide,0,wxEXPAND);
     gs->Add(bt4,0,wxEXPAND);
     gs->Add(bt5,0,wxEXPAND);
     gs->Add(bt6,0,wxEXPAND);
     gs->Add(btride,0,wxEXPAND);
     gs->Add(bt1,0,wxEXPAND);
     gs->Add(bt2,0,wxEXPAND);
     gs->Add(bt3,0,wxEXPAND);
     gs->Add(btminus,0,wxEXPAND);
     gs->Add(bt0,0,wxEXPAND);
     gs->Add(btpoint,0,wxEXPAND);
     gs->Add(btequal,0,wxEXPAND);
     gs->Add(btplus,0,wxEXPAND);
     vbox->Add(gs,1,wxEXPAND);


/*事件连接*/


     Connect(ID_CLS,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBtCls));
     Connect(wxID_EXIT,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBtClose));
     Connect(ID_SEVEN,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBt7));
     Connect(ID_EIGHT,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBt8));
     Connect(ID_NINE,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBt9));
     Connect(ID_DIVIDE,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnDivide));
     Connect(ID_FOUR,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBt4));
     Connect(ID_FIVE,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBt5));
     Connect(ID_SIX,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBt6));
     Connect(ID_RIDE,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnRide));
     Connect(ID_ONE,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBt1));
     Connect(ID_TWO,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBt2));
     Connect(ID_THREE,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBt3));
     Connect(ID_MINUS,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnMinus));
     Connect(ID_ZERO,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBt0));
     Connect(ID_POINT,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBtPoint));
     Connect(ID_EQUAL,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnBtEqual));
     Connect(ID_PLUS,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnPlus));
     Connect(ID_PF,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnPF));
     Connect(ID_KF,wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(Calculator::OnKF));




     SetSizer(vbox);
     SetMinSize(wxSize(270,220));
     Centre();
}


/*把按钮内容显示在文本框*/


void Calculator::OnBt0(wxCommandEvent& event)
{
     wxString num0 = _T("0");
     tc1->WriteText(num0);
}


void Calculator::OnBt1(wxCommandEvent& event)
{
     wxString num1 = _T("1");
     tc1->WriteText(num1);
}


void Calculator::OnBt2(wxCommandEvent& event)
{
     wxString num2 = _T("2");
     tc1->WriteText(num2);
}


void Calculator::OnBt3(wxCommandEvent& event)
{
     wxString num3 = _T("3");
     tc1->WriteText(num3);
}


void Calculator::OnBt4(wxCommandEvent& event)
{
     wxString num4 = _T("4");
     tc1->WriteText(num4);
}


void Calculator::OnBt5(wxCommandEvent& event)
{
     wxString num5 = _T("5");
     tc1->WriteText(num5);
}


void Calculator::OnBt6(wxCommandEvent& event)
{
     wxString num6 = _T("6");
     tc1->WriteText(num6);
}




void Calculator::OnBt7(wxCommandEvent& event)
{
     wxString num7 = _T("7");
     tc1->WriteText(num7);
}


void Calculator::OnBt8(wxCommandEvent& event)
{
     wxString num8 = _T("8");
     tc1->WriteText(num8);
}


void Calculator::OnBt9(wxCommandEvent& event)
{
     wxString num9 = _T("9");
     tc1->WriteText(num9);
}


void Calculator::OnBtPoint(wxCommandEvent& event)
{
     wxString point = _T(".");
     tc1->WriteText(point);
}


void Calculator::OnBtCls(wxCommandEvent& event)
{
     tc1->Clear();//清除文本框内容
     m_str = "";
     m_value1 = 0;
     m_value2 = 0;
}


void Calculator::OnBtClose(wxCommandEvent& event)
{
     Close();
}
void Calculator::SaveVale1()//保存第一次输入数字
{
     m_str = tc1->GetValue();
     m_str.ToDouble(&m_value1);//把字符转化为double类型
     tc1->Clear();
}


void Calculator::OnPlus(wxCommandEvent& event)
{
     SaveVale1();
     c_sign = 0;
}


void Calculator::OnMinus(wxCommandEvent& event)
{
     SaveVale1();
     c_sign = 1;
}


void Calculator::OnRide(wxCommandEvent& event)
{
     SaveVale1();
     c_sign = 2;
}


void Calculator::OnDivide(wxCommandEvent& event)
{
     SaveVale1();
     c_sign = 3;
}


void Calculator::OnPF(wxCommandEvent& event)
{
     wxString str;//用来输出最终结果
     double a1;//用来保存最终结果
     m_str=tc1->GetValue();
     m_str.ToDouble(&m_value1);
     a1 = m_value1*m_value1;
     str<      tc1->Clear();
     tc1->WriteText(str);
}


void Calculator::OnKF(wxCommandEvent& event)
{
     wxString str;
     double a1;
     m_str=tc1->GetValue();
     m_str.ToDouble(&m_value1);
     a1 = sqrt(m_value1);
     str<      tc1->Clear();
     tc1->WriteText(str);
}
void Calculator::Calculate()
{
     wxString str;//输出最终结果
     m_str = tc1->GetValue();
     m_str.ToDouble(&m_value2);


     switch(c_sign)
     {
     case 0:
          {
               result = m_value1 + m_value2;
               break;
          }
     case 1:
          {
               result = m_value1 - m_value2;
               break;
          }
     case 2:
          {
               result = m_value1 * m_value2;
               break;
          }
     case 3:
          {
               result = m_value1 / m_value2;
               break;
          }
     }


     str<      tc1->Clear();
     tc1->WriteText(str);
}


void Calculator::OnBtEqual(wxCommandEvent& event)
{
     Calculate();
}
class MyApp : public wxApp
{
public:
     virtual bool OnInit();
};


IMPLEMENT_APP(MyApp)


bool MyApp::OnInit()
{
     Calculator* calculator = new Calculator(wxT("Calculator"));


     calculator->Show(true);


     return true;
}

你可能感兴趣的:(c++,wxWidgets)