Calculator
This commit is contained in:
@@ -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,15 +215,17 @@ void Calculator::basicCalculator() {
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if(ImGui::Button("del")) {
|
||||
if(s.back() == ' ') {
|
||||
s.pop_back();
|
||||
if(s.back() == '(' || s.back() == ')') {
|
||||
parenthesis = !parenthesis;
|
||||
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();
|
||||
}
|
||||
s.pop_back();
|
||||
s.pop_back();
|
||||
} else {
|
||||
s.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user