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 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) {
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));
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::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());
}
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));
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);
@@ -88,10 +100,13 @@ std::string Calculator::calculate(std::string equation) {
}
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));
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);
@@ -100,10 +115,16 @@ std::string Calculator::calculate(std::string equation) {
}
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));
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);
@@ -112,13 +133,14 @@ std::string Calculator::calculate(std::string equation) {
}
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));
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);
@@ -147,6 +169,7 @@ void Calculator::basicCalculator() {
if(ImGui::Button("AC")) {
s += " + ";
}
ImGui::SameLine();
if(ImGui::Button("()")) {
if(parenthesis) {
@@ -157,9 +180,11 @@ void Calculator::basicCalculator() {
parenthesis = !parenthesis;
}
ImGui::SameLine();
if(ImGui::Button("Gr")) {
graphing = true;
}
ImGui::SameLine();
if(ImGui::Button("=")) {
s = calculate(s);
}
@@ -190,6 +215,7 @@ void Calculator::basicCalculator() {
}
ImGui::SameLine();
if(ImGui::Button("del")) {
if(!s.empty()) {
if(s.back() == ' ') {
s.pop_back();
if(s.back() == '(' || s.back() == ')') {
@@ -202,6 +228,7 @@ void Calculator::basicCalculator() {
}
}
}
}
ImGui::EndGroup();
ImGui::SameLine();