Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::string Line;
- std::string tmp = "";
- std::vector<int> numerics;
- std::vector<int> ops;
- const int OperatorAddition = 5;
- const int OperatorSubtraction = 10;
- int opsCount = 0;
- int opsPos = 0;
- int result = 0;
- bool isNum = false;
- getline(cin, Line);
- for(int i = 0;i<Line.length();i++)
- {
- if(Line[i] == 43)//ascii +
- {
- ops.push_back(OperatorAddition);
- //Code reused, consider a function instead.
- if(isNum == true)
- {
- int r = atoi(tmp.c_str());
- numerics.push_back(r);
- tmp = "";
- isNum = false;
- }
- }
- if(Line[i] == 45)//ascii -
- {
- ops.push_back(OperatorSubtraction);
- //Code reused, consider a function instead.
- if(isNum == true)
- {
- int r = atoi(tmp.c_str());
- numerics.push_back(r);
- tmp = "";
- isNum = false;
- }
- }
- else if(Line[i]>=48&&Line[i]<=57)
- {
- isNum = true;
- tmp += Line[i];
- }
- else
- {
- if(isNum == true)
- {
- int r = atoi(tmp.c_str());
- numerics.push_back(r);
- tmp = "";
- isNum = false;
- }
- }
- }
- if(isNum == true)
- {
- int r = atoi(tmp.c_str());
- numerics.push_back(r);
- }
- std::cout << "\n\n";
- for(int i =0; i < numerics.size(); i++)
- {
- std::cout << "Variable " << i << " = " << numerics[i] << "\n";
- }
- for(int i =0; i < ops.size(); i++)
- {
- std::cout << "operator " << i << " = ";
- if(ops[i] == OperatorAddition)
- {
- std::cout << "+\n";
- }
- else if(ops[i] == OperatorSubtraction)
- {
- std::cout << "-\n";
- }
- }
- std::cout << "\n\n";
- opsCount = ops.size();
- result = numerics[0];
- for(int i =1; i < numerics.size(); i++)
- {
- if(opsCount !=0)
- {
- if(ops[opsPos] == OperatorAddition)
- {
- result+= numerics[i];
- }
- else if(ops[opsPos] == OperatorSubtraction)
- {
- result-= numerics[i];
- }
- else
- {
- cout << "Unrecognized op..";
- }
- opsCount--;
- opsPos++;
- }
- else cout << "Handle this condition..";
- }
- cout << "Answer = " << result;
- cin.get();
- return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement