fork(5) download
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. bool is_number(const std::string& s)
  9. {
  10. return !s.empty() && std::find_if(s.begin(),
  11. s.end(), [](char c) { return !std::isdigit(c); }) == s.end();
  12. }
  13.  
  14. int main(int argc, const char * argv[])
  15. {
  16. stack<int> stack1, stack2, resultStack;
  17. string input;
  18.  
  19. // Build stack 1
  20. input = "455";
  21. //cin >> input
  22. if(!is_number(input))
  23. std::cerr << "Not a number";
  24.  
  25. for (auto &c : input) {
  26. stack1.push(c - '0');
  27. }
  28.  
  29. // Build stack 2
  30. input = "467";
  31. if(!is_number(input))
  32. std::cerr << "Not a number";
  33.  
  34. //cin >> input;
  35. for (auto &c : input) {
  36. stack2.push(c - '0');
  37. }
  38.  
  39.  
  40.  
  41. int carry=0, op1=0, op2=0;
  42. while (!stack1.empty() && !stack2.empty()) {
  43. op1=0,op2=0;
  44. if (stack1.empty() && stack2.empty()) break;
  45. if (!stack1.empty()) { op1 = stack1.top(); stack1.pop(); }
  46. if (!stack2.empty()) { op2 = stack2.top(); stack2.pop(); }
  47. int opTotal = 0;
  48. opTotal = op1 + op2 + carry;
  49. std::cout << op1 << " + " << op2 << " + " << carry << " = " << opTotal << "\n";
  50. resultStack.push(opTotal%10);
  51. if (opTotal >= 10) carry = 1; else carry = 0;
  52. }
  53.  
  54. cout << "\nResult is: ";
  55. while (!resultStack.empty()) {
  56. cout << resultStack.top();
  57. resultStack.pop();
  58. }
  59.  
  60. cout <<"\n\n";return 0;
  61. }
  62.  
Success #stdin #stdout 0s 3432KB
stdin
10000
20323
stdout
5 + 7 + 0 = 12
5 + 6 + 1 = 12
4 + 4 + 1 = 9

Result is: 922