1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <iostream>
#include <vector>
#include <string>
using std::cout; using std::cin; using std::cerr; using std::endl;
using std::vector; using std::string;
int main()
{
string input; // for gettin our input
vector<int> iv; // for holding our numbers that we are going to calculate
vector<char> cv; // for checking if user wants to +,-,*,/
int answer = 0, counter = 0; // For holding the answer
auto cvbegin = cv.begin(), cvend = cv.end();
auto ivbegin = iv.begin(), ivend = iv.end();
cout << "Type your calculations, example \"5+5-2*6/4\" (Spaces are allowed) : "; // tell user how the calculator works
while (getline(cin, input)) { // get input into the string input, whitespaces are included
if (!input.empty()) { // make sure we get input
for (auto c : input) { // if we get input then check the characters in input
if (isdigit(c)) { // if the character is a digit then
iv.push_back(c - '0'); // place it at the end of the int vector iv
}
else if (ispunct(c)) { // if its a puntuation then
if (c == '+' || c == '-' || c == '*' || c == '/') { // check if its a puntuation mark that we can process
cv.push_back(c); // put it at the end of the char vector
} else {
cerr << "Sorry character/sign not known: '" << c << "'. Only +,-,*,/." << endl; // tell user that the character/sign is not known
return -1; // return failiure
}
}
}
answer = 0, counter = 0, cvbegin = cv.begin();
while (*cvbegin == '+') {
if (counter == 0) {
answer = iv[0] + iv[1];
cout << iv[0] << " + " << iv[1] << " = " << answer << endl;
++cvbegin, counter = 2;
} else {
cout << answer << " + " << iv[counter] << " = " << answer + iv[counter] << endl;
answer = answer + iv[counter];
++cvbegin, ++counter;
}
}
while (*cvbegin == '-') {
if (counter == 0) {
answer = iv[0] - iv[1];
cout << iv[0] << "- " << iv[1] << " = " << answer << endl;
++cvbegin, counter = 2;
} else {
cout << answer << " - " << iv[counter] << " = " << answer - iv[counter] << endl;
answer = answer - iv[counter];
++cvbegin, ++counter;
}
}
while (*cvbegin == '*') {
if (counter == 0) {
answer = iv[0] * iv[1];
cout << iv[0] << " * " << iv[1] << " = " << answer << endl;
++cvbegin, counter = 2;
} else {
cout << answer << " * " << iv[counter] << " = " << answer * iv[counter] << endl;
answer = answer * iv[counter];
++cvbegin, ++counter;
}
}
while (*cvbegin == '/') {
if (counter == 0) {
answer = iv[0] / iv[1];
cout << iv[0] << " / " << iv[1] << " = " << answer << endl;
++cvbegin, counter = 2;
} else {
cout << answer << " / " << iv[counter] << " = " << answer / iv[counter] << endl;
answer = answer / iv[counter];
++cvbegin, ++counter;
}
}
iv.erase(ivbegin, ivend-1), cv.erase(cvbegin, cvend-1);
} 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;
}
|