Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CalFrame::CalFrame ( const wxString& title, const wxPoint& pos, const wxSize& size ) : wxFrame ( nullptr , wxID _ ANY, title, pos, size ) {

CalFrame::CalFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(nullptr, wxID_ANY, title, pos, size)
{
textControl = new wxTextCtrl(this, wxID_ANY, "", wxPoint(10,10), wxSize(260,30), wxTE_READONLY);
new wxButton(this, ID_BUTTON_1,"1", wxPoint(10,50), wxSize(50,50));
new wxButton(this, ID_BUTTON_2,"2", wxPoint(60,50), wxSize(50,50));
new wxButton(this, ID_BUTTON_3,"3", wxPoint(110,50), wxSize(50,50));
new wxButton(this, ID_BUTTON_4,"4", wxPoint(10,100), wxSize(50,50));
new wxButton(this, ID_BUTTON_5,"5", wxPoint(60,100), wxSize(50,50));
new wxButton(this, ID_BUTTON_6,"6", wxPoint(110,100), wxSize(50,50));
new wxButton(this, ID_BUTTON_7,"7", wxPoint(10,150), wxSize(50,50));
new wxButton(this, ID_BUTTON_8,"8", wxPoint(60,150), wxSize(50,50));
new wxButton(this, ID_BUTTON_9,"9", wxPoint(110,150), wxSize(50,50));
new wxButton(this, ID_BUTTON_DECIMAL, ".", wxPoint(10,200), wxSize(50,50));
new wxButton(this, ID_BUTTON_0,"0", wxPoint(60,200), wxSize(50,50));
new wxButton(this, ID_BUTTON_NEGATIVE, "(-)", wxPoint(110,200), wxSize(50,50));
new wxButton(this, ID_BUTTON_PLUS, "+", wxPoint(170,50), wxSize(50,50));
new wxButton(this, ID_BUTTON_MINUS, "-", wxPoint(220,50), wxSize(50,50));
new wxButton(this, ID_BUTTON_MULTIPLY, "*", wxPoint(170,100), wxSize(50,50));
new wxButton(this, ID_BUTTON_DIVIDE, "/", wxPoint(220,100), wxSize(50,50));
new wxButton(this, ID_BUTTON_MOD, "%", wxPoint(170,150), wxSize(50,50));
new wxButton(this, ID_BUTTON_SIN, "SIN", wxPoint(220,150), wxSize(50,50));
new wxButton(this, ID_BUTTON_CON, "CON", wxPoint(170,200), wxSize(50,50));
new wxButton(this, ID_BUTTON_TAN, "TAN", wxPoint(220,200), wxSize(50,50));
new wxButton(this, ID_BUTTON_BACKSPACE, "<-", wxPoint(170,250), wxSize(100,50));
new wxButton(this, ID_BUTTON_EQUAL, "=", wxPoint(10,250), wxSize(150,50));
new wxButton(this, ID_BUTTON_CLEAR, "CLEAR", wxPoint(10,300), wxSize(260,50));
Bind(wxEVT_BUTTON, &CalFrame::OnButtonClicked, this, ID_BUTTON_0, ID_BUTTON_TAN);
Bind(wxEVT_BUTTON, &CalFrame::OnClearButtonClick, this, ID_BUTTON_CLEAR);
Bind(wxEVT_BUTTON, &CalFrame::OnBackspace, this, ID_BUTTON_BACKSPACE);
Bind(wxEVT_BUTTON, &CalFrame::OnCalculate, this, ID_BUTTON_EQUAL);
}
void CalFrame::OnButtonClicked(wxCommandEvent& event)
{
int buttonId = event.GetId();
wxString buttonText;
if (buttonId >= ID_BUTTON_0 && buttonId <= ID_BUTTON_9){
buttonText = wxString::Format("%c", buttonId - ID_BUTTON_0+'0');
}
else {
switch (buttonId){
case ID_BUTTON_PLUS:
buttonText ="+";
break;
case ID_BUTTON_MINUS:
buttonText ="-";
break;
case ID_BUTTON_MULTIPLY:
buttonText ="*";
break;
case ID_BUTTON_DIVIDE:
buttonText ="/";
break;
case ID_BUTTON_MOD:
buttonText ="%";
break;
case ID_BUTTON_DECIMAL:
buttonText =".";
break;
case ID_BUTTON_CLEAR:
OnClearButtonClick(event);
return;
break;
case ID_BUTTON_EQUAL:
OnCalculate(event);
break;
case ID_BUTTON_TAN:
buttonText = "TAN";
break;
case ID_BUTTON_SIN:
buttonText = "SIN";
break;
case ID_BUTTON_CON:
buttonText = "CON";
break;
case ID_BUTTON_NEGATIVE:
buttonText ="(-)";
break;
case ID_BUTTON_BACKSPACE:
OnBackspace(event);
return;
break;
}
}
textControl->AppendText(buttonText);
}
void CalFrame::OnClearButtonClick(wxCommandEvent& event)
{
textControl->Clear();
}
void CalFrame::OnCalculate(wxCommandEvent& event)
{
wxString expression = textControl->GetValue();
double result;
if (wxString(expression).ToCDouble(&result))
{
textControl->SetValue(wxString::Format("%g", result));
}
else
{
wxMessageBox("Invalid expression", "Error", wxICON_ERROR | wxOK);
}
}
void CalFrame::OnBackspace(wxCommandEvent& event)
{
wxString currentValue = textControl->GetValue();
if (!currentValue.empty()){
currentValue.RemoveLast();
textControl->SetValue(currentValue);
}
} the onCalculation fuction does not work and i cant sema to fix it i dont know what i am doing wrong.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Draft a proposal for a risk assessment exercise.

Answered: 1 week ago