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