Question: i need to add sin , con,tan to this code i tried to add it the same way as + , * , / ,

i need to add sin,con,tan to this code i tried to add it the same way as +,*,/,- but it did not work and crashed. 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 IsOperator(char c){
return c =='+'|| c =='-'|| c =='*'|| c =='/';
}
std::vector Tokenize(const wxString& expression){
std::vector tokens;
std::string number;
for (char c : expression.ToStdString()){
if (IsOperator(c)){
if (!number.empty()){
tokens.push_back(number);
number.clear();
}
tokens.push_back(std::string(1, c)); // Convert char to string
}
else if (isdigit(c)|| c =='.'){
number.push_back(c);
}
}
if (!number.empty()){
tokens.push_back(number);
}
return tokens;
}
bool CalFrame::EvaluateExpression(const wxString & expression, double& result)
{
std::vector tokens = Tokenize(expression);
try {
std::vector stack;
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!