260 lines
7.6 KiB
C++
260 lines
7.6 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(')', start);
|
|
|
|
std::cout << "start with: " << equation.substr(start + 1, end - 2) << std::endl;
|
|
|
|
std::string insert = calculate(equation.substr(start + 1, end - 2));
|
|
|
|
std::cout << "replace with: " << insert << std::endl;
|
|
|
|
equation.replace(start - 1, (end + 1) - (start - 1), insert.c_str());
|
|
|
|
}
|
|
|
|
while(equation.find('^') != std::string::npos) {
|
|
int mid = equation.find('^');
|
|
int lmid = mid - 2;
|
|
int rmid = mid + 2;
|
|
int start = equation.rfind(' ', lmid) == std::string::npos ? 0 : equation.rfind(' ', lmid) + 1;
|
|
int end = equation.find(' ', rmid) == std::string::npos ? equation.length() - 1 : equation.find(' ', rmid) - 1;
|
|
|
|
|
|
double a = stod(equation.substr(start, mid - 1 - start));
|
|
double b = stod(equation.substr(rmid, end - (mid + 1)));
|
|
|
|
std::string insert = std::to_string(pow(a, b));
|
|
|
|
equation.replace(start, end, insert.c_str());
|
|
|
|
}
|
|
while(equation.find('*') != std::string::npos) {
|
|
int mid = equation.find('*');
|
|
int lmid = mid - 2;
|
|
int rmid = mid + 2;
|
|
int start = equation.rfind(' ', lmid) == std::string::npos ? 0 : equation.rfind(' ', lmid) + 1;
|
|
int end = equation.find(' ', rmid) == std::string::npos ? equation.length() - 1 : equation.find(' ', rmid) - 1;
|
|
|
|
|
|
double a = stod(equation.substr(start, mid - 1 - start));
|
|
double b = stod(equation.substr(rmid, end - (mid + 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 lmid = mid - 2;
|
|
int rmid = mid + 2;
|
|
int start = equation.rfind(' ', lmid) == std::string::npos ? 0 : equation.rfind(' ', lmid) + 1;
|
|
int end = equation.find(' ', rmid) == std::string::npos ? equation.length() - 1 : equation.find(' ', rmid) - 1;
|
|
|
|
double a = stod(equation.substr(start, mid - 1 - start));
|
|
double b = stod(equation.substr(rmid, end - (mid + 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 lmid = mid - 2;
|
|
int rmid = mid + 2;
|
|
|
|
int start = equation.rfind(' ', lmid) == std::string::npos ? 0 : equation.rfind(' ', lmid) + 1;
|
|
int end = equation.find(' ', rmid) == std::string::npos ? equation.length() - 1 : equation.find(' ', rmid) - 1;
|
|
|
|
std::cout << "a: " << equation.substr(start, mid - 1 - start) << std::endl;
|
|
std::cout << "b: " << equation.substr(rmid, end - (mid + 1)) << std::endl;
|
|
double a = stod(equation.substr(start, mid - 1 - start));
|
|
double b = stod(equation.substr(rmid, end - (mid + 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 lmid = mid - 2;
|
|
int rmid = mid + 2;
|
|
|
|
int start = equation.rfind(' ', lmid) == std::string::npos ? 0 : equation.rfind(' ', lmid) + 1;
|
|
int end = equation.find(' ', rmid) == std::string::npos ? equation.length() - 1 : equation.find(' ', rmid) - 1;
|
|
|
|
double a = stod(equation.substr(start, mid - 1 - start));
|
|
double b = stod(equation.substr(rmid, end - (mid + 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 += " + ";
|
|
}
|
|
ImGui::SameLine();
|
|
if(ImGui::Button("()")) {
|
|
|
|
if(parenthesis) {
|
|
s += " ( ";
|
|
} else {
|
|
s += " ) ";
|
|
}
|
|
|
|
parenthesis = !parenthesis;
|
|
}
|
|
ImGui::SameLine();
|
|
if(ImGui::Button("Gr")) {
|
|
graphing = true;
|
|
}
|
|
ImGui::SameLine();
|
|
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.empty()) {
|
|
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() {
|
|
|
|
}
|