Calculator

This commit is contained in:
2025-05-13 15:35:41 -05:00
parent dffca865dc
commit 45f1d0f69d

View File

@@ -54,32 +54,44 @@ std::string Calculator::calculate(std::string equation) {
} }
int start = equation.find_last_of('('); int start = equation.find_last_of('(');
int end = equation.find_first_of(')', start); int end = equation.find(')', start);
std::string insert = calculate(equation.substr(start + 1, end - 1)); std::cout << "start with: " << equation.substr(start + 1, end - 2) << std::endl;
equation.replace(start, end, insert.c_str()); 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) { while(equation.find('^') != std::string::npos) {
int mid = equation.find('^'); int mid = equation.find('^');
int start = equation.rfind(' ', mid - 1); int lmid = mid - 2;
int end = equation.find(' ', mid + 1); int rmid = mid + 2;
double base = stod(equation.substr(start + 1, mid - 1)); int start = equation.rfind(' ', lmid) == std::string::npos ? 0 : equation.rfind(' ', lmid) + 1;
double exp = stod(equation.substr(mid + 1, end - 1)); int end = equation.find(' ', rmid) == std::string::npos ? equation.length() - 1 : equation.find(' ', rmid) - 1;
std::string insert = std::to_string(pow(base, exp));
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()); equation.replace(start, end, insert.c_str());
} }
while(equation.find('*') != std::string::npos) { while(equation.find('*') != std::string::npos) {
int mid = equation.find('*'); int mid = equation.find('*');
int start = equation.rfind(' ', mid - 1); int lmid = mid - 2;
int end = equation.find(' ', mid + 1); int rmid = mid + 2;
double a = stod(equation.substr(start + 1, mid - 1)); int start = equation.rfind(' ', lmid) == std::string::npos ? 0 : equation.rfind(' ', lmid) + 1;
double b = stod(equation.substr(mid + 1, end - 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); std::string insert = std::to_string(a * b);
@@ -88,10 +100,13 @@ std::string Calculator::calculate(std::string equation) {
} }
while(equation.find('/') != std::string::npos) { while(equation.find('/') != std::string::npos) {
int mid = equation.find('/'); int mid = equation.find('/');
int start = equation.rfind(' ', mid - 1); int lmid = mid - 2;
int end = equation.find(' ', mid + 1); int rmid = mid + 2;
double a = stod(equation.substr(start + 1, mid - 1)); int start = equation.rfind(' ', lmid) == std::string::npos ? 0 : equation.rfind(' ', lmid) + 1;
double b = stod(equation.substr(mid + 1, end - 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); std::string insert = std::to_string(a / b);
@@ -100,10 +115,16 @@ std::string Calculator::calculate(std::string equation) {
} }
while(equation.find('+') != std::string::npos) { while(equation.find('+') != std::string::npos) {
int mid = equation.find('+'); int mid = equation.find('+');
int start = equation.rfind(' ', mid - 1); int lmid = mid - 2;
int end = equation.find(' ', mid + 1); int rmid = mid + 2;
double a = stod(equation.substr(start + 1, mid - 1));
double b = stod(equation.substr(mid + 1, end - 1)); 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); std::string insert = std::to_string(a + b);
@@ -112,13 +133,14 @@ std::string Calculator::calculate(std::string equation) {
} }
while(equation.find('-') != std::string::npos) { while(equation.find('-') != std::string::npos) {
int mid = equation.find('-'); int mid = equation.find('-');
int start = equation.rfind(' ', mid - 1); int lmid = mid - 2;
if(start == std::string::npos) { int rmid = mid + 2;
break;
} int start = equation.rfind(' ', lmid) == std::string::npos ? 0 : equation.rfind(' ', lmid) + 1;
int end = equation.find(' ', mid + 1); int end = equation.find(' ', rmid) == std::string::npos ? equation.length() - 1 : equation.find(' ', rmid) - 1;
double a = stod(equation.substr(start + 1, mid - 1));
double b = stod(equation.substr(mid + 1, end - 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); std::string insert = std::to_string(a - b);
@@ -147,6 +169,7 @@ void Calculator::basicCalculator() {
if(ImGui::Button("AC")) { if(ImGui::Button("AC")) {
s += " + "; s += " + ";
} }
ImGui::SameLine();
if(ImGui::Button("()")) { if(ImGui::Button("()")) {
if(parenthesis) { if(parenthesis) {
@@ -157,9 +180,11 @@ void Calculator::basicCalculator() {
parenthesis = !parenthesis; parenthesis = !parenthesis;
} }
ImGui::SameLine();
if(ImGui::Button("Gr")) { if(ImGui::Button("Gr")) {
graphing = true; graphing = true;
} }
ImGui::SameLine();
if(ImGui::Button("=")) { if(ImGui::Button("=")) {
s = calculate(s); s = calculate(s);
} }
@@ -190,15 +215,17 @@ void Calculator::basicCalculator() {
} }
ImGui::SameLine(); ImGui::SameLine();
if(ImGui::Button("del")) { if(ImGui::Button("del")) {
if(s.back() == ' ') { if(!s.empty()) {
s.pop_back(); if(s.back() == ' ') {
if(s.back() == '(' || s.back() == ')') { s.pop_back();
parenthesis = !parenthesis; if(s.back() == '(' || s.back() == ')') {
parenthesis = !parenthesis;
}
s.pop_back();
s.pop_back();
} else {
s.pop_back();
} }
s.pop_back();
s.pop_back();
} else {
s.pop_back();
} }
} }
} }