Skip to content

Instantly share code, notes, and snippets.

@AustinDeric
Created July 9, 2018 18:06
Show Gist options
  • Save AustinDeric/02d402589472052961fe065c45084b84 to your computer and use it in GitHub Desktop.
Save AustinDeric/02d402589472052961fe065c45084b84 to your computer and use it in GitHub Desktop.
#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;
}
@AustinDeric
Copy link
Author

AustinDeric commented Jul 9, 2018

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.

position: 0
number is: 3
position: 1
operator is: *
position: 2
number is: 5
position: 3
operator is: +
position: 4
number is: 1
position: 5
number is: 11
position: 6
operator is: +
position: 7
number is: 5
position: 8
operator is: *
position: 9
number is: 2
position: 10
number is: 20
position: 11
operator is: +
position: 12
number is: 9
position: 13
operator is: *
position: 14
number is: 2
position: 15
operator is: *
position: 16
number is: 4
position: 17
operator is: +
position: 18
number is: 1
position: 19
number is: 13
position: 20
number is: 135
number is: 135
expression
3
*
5
+
11
+
5
*
20
+
9
*
2
*
4
+
135
value: 0
expression
15
+
11
+
5
*
20
+
9
*
2
*
4
+
135
value: 0
expression
15
+
11
+
100
+
9
*
2
*
4
+
135
value: 0
expression
15
+
11
+
100
+
18
*
4
+
135
value: 0
expression
15
+
11
+
100
+
72
+
135
complete multiplication
value: 0
expression
15
+
11
+
100
+
72
+
135
expression
26
+
100
+
72
+
135
expression
126
+
72
+
135
expression
198
+
135
expression
333
complete addition

Process finished with exit code 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment