Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can some one help me the math in my code dose not work when i press the equal button it just clears the operation and

can some one help me the math in my code dose not work when i press the equal button it just clears the operation and anything that follows i cant sema to fix it.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:
buttonText ="=";
OnCalculate(event);
return;
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 (EvaluateExpression(expression, result)){
textControl->SetValue(wxString::Format("%g", result));
}
else {
textControl->SetValue("Error");
}
}
void CalFrame::OnBackspace(wxCommandEvent& event)
{
wxString currentValue = textControl->GetValue();
if (!currentValue.empty()){
currentValue.RemoveLast();
textControl->SetValue(currentValue);
}
}
bool CalFrame::EvaluateExpression(const wxString & expression, double& result)
{
if (expression.empty())
return false;
std::istringstream iss(expression.ToStdString());
std::vector tokens;
std::string token;
while (iss >> token){
tokens.push_back(token);
}
if (tokens.size()%2==0)
return false;
try {
result = std::stod(tokens[0]);
for (size_t i =1; i < tokens.size(); i +=2){
std::string op = tokens[i];
double nextNum = std::stod(tokens[i +1]);
if (op =="+")
result += nextNum;
else if (op =="-")
result -= nextNum;
else if (op =="*")
result *= nextNum;
else if (op =="/")
result /= nextNum;
}
return true;
}
catch (const std::exception&)
{
return false;
}
}

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

Recommended Textbook for

Database And Expert Systems Applications Dexa 2021 Workshops Biokdd Iwcfs Mlkgraphs Al Cares Protime Alsys 2021 Virtual Event September 27 30 2021 Proceedings

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Atif Mashkoor ,Johannes Sametinger ,Anna Fensel ,Jorge Martinez-Gil ,Lukas Fischer

1st Edition

3030871002, 978-3030871000

More Books

Students also viewed these Databases questions