Files
Archimedes/modules/Archimedes-Modules/Calculator/Calculator.cpp
2025-05-13 08:35:22 -05:00

233 lines
6.2 KiB
C++

#include "Calculator.h"
#include "modules/ImguiModule/ImguiModule.h"
#include <sstream>
#include <cmath>
Calculator::Calculator(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
name = "Calculator";
ImguiModule* im = new ImguiModule(a, h);
deps[*im] = im;
}
Calculator::~Calculator() {
if(app) {
ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; }
im->releaseContext(ImGui::GetCurrentContext());
}
}
void Calculator::onLoad() {
ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; }
if(!im) {
std::cout << "No ImguiModule for Calculator!\n";
std::abort();
}
ImGui::SetCurrentContext(im->aquireContext());
}
void Calculator::run() {
if(open) {
basicCalculator();
} else {
app->emitEvent(new Archimedes::DoUnloadModuleEvent(name));
}
}
std::string Calculator::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() {
static bool b = true;
static std::string s;
if(b) {
ImGui::Begin("Basic Calculator", &open);
ImGui::Text("%s", s.c_str());
ImGui::BeginGroup();
{
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();
}
}
if(ImGui::Button("0")) {
s += "0";
}
ImGui::SameLine();
if(ImGui::Button(".")) {
s += ".";
}
ImGui::SameLine();
if(ImGui::Button("del")) {
if(s.back() == ' ') {
s.pop_back();
if(s.back() == '(' || s.back() == ')') {
parenthesis = !parenthesis;
}
s.pop_back();
s.pop_back();
} else {
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();
}
}
void Calculator::graphingCalculator() {
}