fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. using std::cout; using std::cin; using std::cerr; using std::endl;
  6. using std::vector; using std::string;
  7.  
  8. int main()
  9. {
  10. string input; // for storing our input in
  11.  
  12. cout << "Type your calculations, example \"5+5-2*6/4\" (Spaces are allowed) : "; // tell user how the calculator works
  13. while (getline(cin, input)) { // get inuut
  14. vector<int> iv; // for holding our numbers that we are going to calculate
  15. vector<char> cv; // for checking if user wants to +,-,*,/
  16. if (!input.empty())
  17. {
  18. std::istringstream in(input);
  19.  
  20. while (in)
  21. {
  22. in >> std::ws; // consume whitespace.
  23.  
  24. int value;
  25. if (isdigit(in.peek()) && in >> value)
  26. iv.push_back(value);
  27. else
  28. {
  29. in.clear();
  30. char ch;
  31. if (in >> ch)
  32. {
  33. if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
  34. cv.push_back(ch);
  35. else
  36. {
  37. cerr << "Sorry character/sign not known: '"
  38. << ch << "'. Only +,-,*,/.\n";
  39. return -1;
  40. }
  41. }
  42. }
  43. }
  44.  
  45. int answer = 0, counter = 0; // For holding the answer
  46. auto cvcbegin = cv.cbegin(), cvcend = cv.cend();
  47. while (cvcbegin != cvcend) {
  48. while (*cvcbegin == '+') {
  49. if (counter == 0) {
  50. answer = iv[0] + iv[1];
  51. cout << iv[0] << " + " << iv[1] << " = " << answer << endl;
  52. counter = 2;
  53. ++cvcbegin;
  54. }
  55. else {
  56. cout << answer << " + " << iv[counter] << " = " << answer + iv[counter] << endl;
  57. answer = answer + iv[counter];
  58. ++counter;
  59. ++cvcbegin;
  60. }
  61. }
  62. while (*cvcbegin == '-') {
  63. if (counter == 0) {
  64. answer = iv[0] - iv[1];
  65. cout << iv[0] << " - " << iv[1] << " = " << answer << endl;
  66. counter = 2;
  67. ++cvcbegin;
  68. }
  69. else {
  70. cout << answer << " - " << iv[counter] << " = " << answer - iv[counter] << endl;
  71. answer = answer - iv[counter];
  72. ++counter;
  73. ++cvcbegin;
  74. }
  75. }
  76. while (*cvcbegin == '*') {
  77. if (counter == 0) {
  78. answer = iv[0] * iv[1];
  79. cout << iv[0] << " * " << iv[1] << " = " << answer << endl;
  80. counter = 2;
  81. ++cvcbegin;
  82. }
  83. else {
  84. cout << answer << " * " << iv[counter] << " = " << answer * iv[counter] << endl;
  85. answer = answer * iv[counter];
  86. ++counter;
  87. ++cvcbegin;
  88. }
  89. }
  90. while (*cvcbegin == '/') {
  91. if (counter == 0) {
  92. answer = iv[0] / iv[1];
  93. cout << iv[0] << " / " << iv[1] << " = " << answer << endl;
  94. counter = 2;
  95. ++cvcbegin;
  96. }
  97. else {
  98. cout << answer << " / " << iv[counter] << " = " << answer / iv[counter] << endl;
  99. answer = answer / iv[counter];
  100. ++counter;
  101. ++cvcbegin;
  102. }
  103. }
  104. }
  105. cout << "\nAgain?\n" << endl;
  106. }
  107. else { // If we get no input then...
  108. cerr << "No input, well bye." << endl; // Tell user that there was no input
  109. return -1; // return failiure
  110. }
  111. }
  112. return 0;
  113. }
Success #stdin #stdout 0s 3440KB
stdin
10+5
stdout
Type your calculations, example "5+5-2*6/4" (Spaces are allowed) : 10 + 5 = 15

Again?