#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using std::cout; using std::cin; using std::cerr; using std::endl;
using std::vector; using std::string;
int main()
{
string input; // for storing our input in
cout << "Type your calculations, example \"5+5-2*6/4\" (Spaces are allowed) : "; // tell user how the calculator works
while (getline(cin, input)) { // get inuut
vector<int> iv; // for holding our numbers that we are going to calculate
vector<char> cv; // for checking if user wants to +,-,*,/
if (!input.empty())
{
std::istringstream in(input);
while (in)
{
in >> std::ws; // consume whitespace.
int value;
if (isdigit(in.peek()) && in >> value)
iv.push_back(value);
else
{
in.clear();
char ch;
if (in >> ch)
{
if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
cv.push_back(ch);
else
{
cerr << "Sorry character/sign not known: '"
<< ch << "'. Only +,-,*,/.\n";
return -1;
}
}
}
}
int answer = 0, counter = 0; // For holding the answer
auto cvcbegin = cv.cbegin(), cvcend = cv.cend();
while (cvcbegin != cvcend) {
while (*cvcbegin == '+') {
if (counter == 0) {
answer = iv[0] + iv[1];
cout << iv[0] << " + " << iv[1] << " = " << answer << endl;
counter = 2;
++cvcbegin;
}
else {
cout << answer << " + " << iv[counter] << " = " << answer + iv[counter] << endl;
answer = answer + iv[counter];
++counter;
++cvcbegin;
}
}
while (*cvcbegin == '-') {
if (counter == 0) {
answer = iv[0] - iv[1];
cout << iv[0] << " - " << iv[1] << " = " << answer << endl;
counter = 2;
++cvcbegin;
}
else {
cout << answer << " - " << iv[counter] << " = " << answer - iv[counter] << endl;
answer = answer - iv[counter];
++counter;
++cvcbegin;
}
}
while (*cvcbegin == '*') {
if (counter == 0) {
answer = iv[0] * iv[1];
cout << iv[0] << " * " << iv[1] << " = " << answer << endl;
counter = 2;
++cvcbegin;
}
else {
cout << answer << " * " << iv[counter] << " = " << answer * iv[counter] << endl;
answer = answer * iv[counter];
++counter;
++cvcbegin;
}
}
while (*cvcbegin == '/') {
if (counter == 0) {
answer = iv[0] / iv[1];
cout << iv[0] << " / " << iv[1] << " = " << answer << endl;
counter = 2;
++cvcbegin;
}
else {
cout << answer << " / " << iv[counter] << " = " << answer / iv[counter] << endl;
answer = answer / iv[counter];
++counter;
++cvcbegin;
}
}
}
cout << "\nAgain?\n" << endl;
}
else { // If we get no input then...
cerr << "No input, well bye." << endl; // Tell user that there was no input
return -1; // return failiure
}
}
return 0;
}