Question: Create a calculator using wxWidgets in c++. My assignment is to implement the shunting yard algorithm. I have implemented it and gotten everything working. However,

Create a calculator using wxWidgets in c++. My assignment is to implement the shunting yard algorithm. I have implemented it and gotten everything working. However, I am having trouble adding the precedence for the more complicated operations of sin, cos, and tan, and modulo.

Part Two: Processor Singleton Instructions 1. Create a new branch from your

The shunting yard algorithm I have implemented works fine with the basic operations. I need to make sure that it returns the correct value, i.e. the example in the photo 2+2.5/Sin5 should return -.60708. I will leave my files in the code block below. Note, right now I have modulo listed under a bool check IsBasicOperator, I'm not sure if the precedence is the same as the basic operations or higher precedence like sin,cos, and tan. Thank you for reviewing my question and helping, if I can provide clarification on anything that would make your life easier, please let me know :)

 

side note:  I did not want to overcrowd the code blocks with information you might not need, so If you decide it would be easier to have all my header and cpp files so you can paste it in and debug just let me know and I can include the rest.

SingletonProcessor.h  ---------------  #pragma once  #include   #include   #include   #include     class ProcessorSingleton  {  public:  static ProcessorSingleton* GetInstance();    double Calculate(const wxString& expression);  bool IsOperator(const wxString& op);  bool HasHigherPrecedence(const wxString& op1, const wxString& op2);  bool IsBasicOperator(const wxString& op);  bool IsUnaryOperator(const wxString& op);    int GetOperatorPrecedence(const wxString& op);  void ApplyOperator(const wxString& op, std::stack& values);    // Function to check if a string is a number  //Created my own because wxString IsNumber() should not be used in new code    bool IsNumber(const wxString str) {  for (char const& c : str) {  if (!std::isdigit(c))  return false;  }  return true;  }      private:  ProcessorSingleton() = default;  static ProcessorSingleton* instance;  };  ---------------------------------------  End of SingletonProcessor.h  ---------------------------------------  SingletonProcessor.cpp    #include "SingletonProcessor.h"  #include   #include   #include     ProcessorSingleton* ProcessorSingleton::instance = nullptr;    ProcessorSingleton* ProcessorSingleton::GetInstance()  {      if (!instance)      {          instance = new ProcessorSingleton();      }      return instance;  }    double ProcessorSingleton::Calculate(const wxString& expression)  {      wxStringTokenizer tokenizer(expression, " ");      wxString token = "";      std::stack values;      std::stack operators;        while (tokenizer.HasMoreTokens())      {          wxString token = tokenizer.GetNextToken();            if (IsNumber(token))          {              double number;              token.ToDouble(&number);              values.push(number);          }          else if (IsOperator(token))          {              while (!operators.empty() && HasHigherPrecedence(operators.top(), token))              {                  ApplyOperator(operators.top(), values);                  operators.pop();              }              operators.push(token);          }      }        while (!operators.empty())      {          ApplyOperator(operators.top(), values);          operators.pop();      }        if (!values.empty()) {          return values.top();      }        return 0; // Default return value for an empty expression  }    bool ProcessorSingleton::IsOperator(const wxString& op)  {      return op == "+" || op == "-" || op == "*" || op == "/" || op == "%" ||          op == "sin" || op == "cos" || op == "tan";  }    bool ProcessorSingleton::HasHigherPrecedence(const wxString& op1, const wxString& op2)  {      return (IsBasicOperator(op1) && GetOperatorPrecedence(op1) >= GetOperatorPrecedence(op2));  }    bool ProcessorSingleton::IsBasicOperator(const wxString& op)  {      return op == "+" || op == "-" || op == "*" || op == "/" || op == "%";  }    bool IsUnaryOperator(const wxString& op)  {      return op == "sin" || op == "cos" || op == "tan";  }    int ProcessorSingleton::GetOperatorPrecedence(const wxString& op)  {      if (op == "+" || op == "-") {          return 1;      }      else if (op == "*" || op == "/") {          return 2;      }      else {          return 0; // Default precedence for non-basic operators      }  }    void ProcessorSingleton::ApplyOperator(const wxString& op, std::stack& values)  {      if (IsBasicOperator(op))      {          if (values.size() (operand1) % static_cast(operand2));              }              else {                  // Division by zero, handle this error appropriately                  // For now, push a placeholder value                  values.push(0);              }          }      }      // Handle other operators if needed      else if (!IsBasicOperator(op))      {          double valueForUnary = values.top();          values.pop();                   if (op == "sin")          {              values.push(sin(valueForUnary));          }          else if(op == "cos")          {              values.push(cos(valueForUnary));          }          else if (op == "tan")          {              values.push(tan(valueForUnary));          }      }  }


Part Two: Processor Singleton Instructions 1. Create a new branch from your existing main branch. Give the branch a name that will be recognizable to you later as calculator processor work. 2. On your newly created branch, create a new class called CalculatorProcessor. 3. Implement this object as a Singleton following the example from Lecture 6. 4. All string parsing and calculations will be performed inside the ProcessorSingleton. a. You should be able to pass your text control string into your ProcessorSingleton's Calculate method, which will return the correct result of the calculation. Something like: ProcessorSingleton::GetInstance()->Calculate("2+2.5/Sin5"); Should return -0.60708803192 5. Your calculator should be able to handle more complex calculations that are processed in the correct order of operations. One way to accomplish this is through the Shunting Yard Algorithm. a. Treat Sin/Cos/Tan with the highest precedence. You should also be able to perform multiple Sin/Cos/Tan operations in a single expression. Exception Handling Be sure to fully error test every button. Points will be deducted for every unhandled exception encountered. Double check that exceptions that were handled previously are still being handled gracefully.

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 Algorithms Questions!