Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!