Created
July 9, 2018 18:06
-
-
Save AustinDeric/02d402589472052961fe065c45084b84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
//method: parse the string and create a vector of numbers and operators then iterate that vector to create the value | |
int main(){ | |
std::string str_val = "3*5+11+5*20+9*2*4+135"; | |
std::vector<std::string> expression; | |
std::string number_holder; | |
int position = 0; | |
for (std::string::iterator it=str_val.begin(); it!=str_val.end(); ++it) { | |
std::cout << "position: " << position << std::endl; | |
position++; | |
if(std::isdigit(*it)){ | |
number_holder.push_back(*it); | |
std::cout << "number is: " << number_holder << std::endl; | |
} | |
else{ | |
//if the next char is not a number then its an operator | |
expression.push_back(number_holder); | |
number_holder = ""; | |
std::cout << "operator is: " << *it << std::endl; | |
std::string str(1, *it); | |
expression.push_back(str); | |
} | |
} | |
// There will always be a number as the last operand, so lets add it to the expression vector | |
std::cout << "number is: " << number_holder << std::endl; | |
expression.push_back(number_holder); | |
// lets check the expression vector! TODO: delete this code segment, only for debuging | |
//Great! Now lets while loop over the expression vector until its size == 1. | |
int value = 0; | |
//expression.size()>1 | |
bool complete_multiplication = false; | |
while(!complete_multiplication){ | |
//view current expression | |
std::cout << "expression" << std::endl; | |
for(std::vector<std::string>::iterator debug_it=expression.begin(); debug_it!=expression.end(); ++debug_it){ | |
std::cout << *debug_it << std::endl; | |
} | |
for (unsigned i=0; i<expression.size(); ++i){ | |
if (expression[i] == "*"){ | |
expression[i-1] = std::to_string(std::stoi(expression[i-1])*std::stoi(expression[i+1])); | |
expression.erase(expression.begin()+i); | |
expression.erase(expression.begin()+i); | |
break; | |
} | |
if (i==expression.size()-1) { | |
complete_multiplication = true; | |
std::cout << "complete multiplication" << std::endl; | |
} | |
} | |
std::cout << "value: " << value << std::endl; | |
} | |
bool complete_addition = false; | |
while(!complete_addition){ | |
//view current expression | |
std::cout << "expression" << std::endl; | |
for(std::vector<std::string>::iterator debug_it=expression.begin(); debug_it!=expression.end(); ++debug_it){ | |
std::cout << *debug_it << std::endl; | |
} | |
for (unsigned i=0; i<expression.size(); ++i){ | |
if (expression[i] == "+"){ | |
expression[i-1] = std::to_string(std::stoi(expression[i-1])+std::stoi(expression[i+1])); | |
expression.erase(expression.begin()+i); | |
expression.erase(expression.begin()+i); | |
break; | |
} | |
if (i==expression.size()-1) { | |
complete_addition = true; | |
std::cout << "complete addition" << std::endl; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not pretty but here is the console output. scroll down to the bottom and you can see the only value left in the expression vector is the answer 333.