Calculator
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
#include "Calculator.h"
|
#include "Calculator.h"
|
||||||
#include "modules/ImguiModule/ImguiModule.h"
|
#include "modules/ImguiModule/ImguiModule.h"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
|
||||||
Calculator::Calculator(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
|
Calculator::Calculator(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
|
||||||
|
|
||||||
@@ -41,6 +44,92 @@ void Calculator::run() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string calculate(std::string equation) {
|
||||||
|
std::stringstream s(equation);
|
||||||
|
|
||||||
|
//PEMDAS
|
||||||
|
while(equation.find('(') != std::string::npos) {
|
||||||
|
if(equation.find(')') == std::string::npos) {
|
||||||
|
return "error";
|
||||||
|
}
|
||||||
|
|
||||||
|
int start = equation.find_last_of('(');
|
||||||
|
int end = equation.find_first_of(')', start);
|
||||||
|
|
||||||
|
std::string insert = calculate(equation.substr(start + 1, end - 1));
|
||||||
|
|
||||||
|
equation.replace(start, end, insert.c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
while(equation.find('^') != std::string::npos) {
|
||||||
|
int mid = equation.find('^');
|
||||||
|
int start = equation.rfind(' ', mid - 1);
|
||||||
|
int end = equation.find(' ', mid + 1);
|
||||||
|
double base = stod(equation.substr(start + 1, mid - 1));
|
||||||
|
double exp = stod(equation.substr(mid + 1, end - 1));
|
||||||
|
|
||||||
|
std::string insert = std::to_string(pow(base, exp));
|
||||||
|
|
||||||
|
equation.replace(start, end, insert.c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
while(equation.find('*') != std::string::npos) {
|
||||||
|
int mid = equation.find('*');
|
||||||
|
int start = equation.rfind(' ', mid - 1);
|
||||||
|
int end = equation.find(' ', mid + 1);
|
||||||
|
double a = stod(equation.substr(start + 1, mid - 1));
|
||||||
|
double b = stod(equation.substr(mid + 1, end - 1));
|
||||||
|
|
||||||
|
std::string insert = std::to_string(a * b);
|
||||||
|
|
||||||
|
equation.replace(start, end, insert.c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
while(equation.find('/') != std::string::npos) {
|
||||||
|
int mid = equation.find('/');
|
||||||
|
int start = equation.rfind(' ', mid - 1);
|
||||||
|
int end = equation.find(' ', mid + 1);
|
||||||
|
double a = stod(equation.substr(start + 1, mid - 1));
|
||||||
|
double b = stod(equation.substr(mid + 1, end - 1));
|
||||||
|
|
||||||
|
std::string insert = std::to_string(a / b);
|
||||||
|
|
||||||
|
equation.replace(start, end, insert.c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
while(equation.find('+') != std::string::npos) {
|
||||||
|
int mid = equation.find('+');
|
||||||
|
int start = equation.rfind(' ', mid - 1);
|
||||||
|
int end = equation.find(' ', mid + 1);
|
||||||
|
double a = stod(equation.substr(start + 1, mid - 1));
|
||||||
|
double b = stod(equation.substr(mid + 1, end - 1));
|
||||||
|
|
||||||
|
std::string insert = std::to_string(a + b);
|
||||||
|
|
||||||
|
equation.replace(start, end, insert.c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
while(equation.find('-') != std::string::npos) {
|
||||||
|
int mid = equation.find('-');
|
||||||
|
int start = equation.rfind(' ', mid - 1);
|
||||||
|
if(start == std::string::npos) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int end = equation.find(' ', mid + 1);
|
||||||
|
double a = stod(equation.substr(start + 1, mid - 1));
|
||||||
|
double b = stod(equation.substr(mid + 1, end - 1));
|
||||||
|
|
||||||
|
std::string insert = std::to_string(a - b);
|
||||||
|
|
||||||
|
equation.replace(start, end, insert.c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return equation;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Calculator::basicCalculator() {
|
void Calculator::basicCalculator() {
|
||||||
|
|
||||||
static bool b = true;
|
static bool b = true;
|
||||||
@@ -51,41 +140,47 @@ void Calculator::basicCalculator() {
|
|||||||
|
|
||||||
ImGui::Begin("Basic Calculator", &open);
|
ImGui::Begin("Basic Calculator", &open);
|
||||||
|
|
||||||
ImGui::InputText("equation", &s);
|
ImGui::Text("%s", s.c_str());
|
||||||
|
|
||||||
if(ImGui::Button("7")) {
|
ImGui::BeginGroup();
|
||||||
s += "7";
|
{
|
||||||
|
if(ImGui::Button("AC")) {
|
||||||
|
s += " + ";
|
||||||
}
|
}
|
||||||
|
if(ImGui::Button("()")) {
|
||||||
|
|
||||||
|
if(parenthesis) {
|
||||||
|
s += " ( ";
|
||||||
|
} else {
|
||||||
|
s += " ) ";
|
||||||
|
}
|
||||||
|
|
||||||
|
parenthesis = !parenthesis;
|
||||||
|
}
|
||||||
|
if(ImGui::Button("Gr")) {
|
||||||
|
graphing = true;
|
||||||
|
}
|
||||||
|
if(ImGui::Button("=")) {
|
||||||
|
s = calculate(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImGui::EndGroup();
|
||||||
|
|
||||||
|
ImGui::BeginGroup();
|
||||||
|
{
|
||||||
|
for(int i = 1; i < 4; i++) {
|
||||||
|
ImGui::BeginGroup();
|
||||||
|
for(int j = 6; j >= 0; j -= 3) {
|
||||||
|
if(ImGui::Button(std::to_string(i + j).c_str())) {
|
||||||
|
s += std::to_string(i + j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImGui::EndGroup();
|
||||||
|
if(i < 3) {
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
if(ImGui::Button("8")) {
|
|
||||||
s += "8";
|
|
||||||
}
|
}
|
||||||
ImGui::SameLine();
|
|
||||||
if(ImGui::Button("9")) {
|
|
||||||
s += "9";
|
|
||||||
}
|
|
||||||
if(ImGui::Button("4")) {
|
|
||||||
s += "4";
|
|
||||||
}
|
|
||||||
ImGui::SameLine();
|
|
||||||
if(ImGui::Button("5")) {
|
|
||||||
s += "5";
|
|
||||||
}
|
|
||||||
ImGui::SameLine();
|
|
||||||
if(ImGui::Button("6")) {
|
|
||||||
s += "6";
|
|
||||||
}
|
|
||||||
if(ImGui::Button("1")) {
|
|
||||||
s += "1";
|
|
||||||
}
|
|
||||||
ImGui::SameLine();
|
|
||||||
if(ImGui::Button("2")) {
|
|
||||||
s += "2";
|
|
||||||
}
|
|
||||||
ImGui::SameLine();
|
|
||||||
if(ImGui::Button("3")) {
|
|
||||||
s += "3";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ImGui::Button("0")) {
|
if(ImGui::Button("0")) {
|
||||||
s += "0";
|
s += "0";
|
||||||
}
|
}
|
||||||
@@ -94,15 +189,39 @@ void Calculator::basicCalculator() {
|
|||||||
s += ".";
|
s += ".";
|
||||||
}
|
}
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
if(ImGui::Button("")) {
|
if(ImGui::Button("del")) {
|
||||||
if(s.back() == ' ') {
|
if(s.back() == ' ') {
|
||||||
s.pop_back();
|
s.pop_back();
|
||||||
|
if(s.back() == '(' || s.back() == ')') {
|
||||||
|
parenthesis = !parenthesis;
|
||||||
|
}
|
||||||
s.pop_back();
|
s.pop_back();
|
||||||
s.pop_back();
|
s.pop_back();
|
||||||
} else {
|
} else {
|
||||||
s.pop_back();
|
s.pop_back();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
ImGui::EndGroup();
|
||||||
|
|
||||||
|
ImGui::SameLine();
|
||||||
|
|
||||||
|
ImGui::BeginGroup();
|
||||||
|
|
||||||
|
if(ImGui::Button("+")) {
|
||||||
|
s += " + ";
|
||||||
|
}
|
||||||
|
if(ImGui::Button("-")) {
|
||||||
|
s += " - ";
|
||||||
|
}
|
||||||
|
if(ImGui::Button("*")) {
|
||||||
|
s += " * ";
|
||||||
|
}
|
||||||
|
if(ImGui::Button("/")) {
|
||||||
|
s += " / ";
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui::EndGroup();
|
||||||
|
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,14 @@ class Calculator : public Archimedes::Module {
|
|||||||
private:
|
private:
|
||||||
bool open = true;
|
bool open = true;
|
||||||
|
|
||||||
|
bool parenthesis = true;
|
||||||
|
|
||||||
|
bool graphing = false;
|
||||||
|
|
||||||
void basicCalculator();
|
void basicCalculator();
|
||||||
|
|
||||||
|
std::string calculate(std::string);
|
||||||
|
|
||||||
void graphingCalculator();
|
void graphingCalculator();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user