fork download
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5. const int MAX = 100;
  6. typedef string mytype;
  7.  
  8. class Add_Stack{
  9. public:
  10. Add_Stack();
  11. void push(mytype num);
  12. void pop();
  13. mytype Top();
  14. bool IsEmpty() const;
  15. bool IsFull() const;
  16. private:
  17. string arr[MAX];
  18. int top;
  19. };
  20. Add_Stack::Add_Stack(){ // 생성자
  21.  
  22. top = -1;
  23. }
  24. bool Add_Stack::IsEmpty() const
  25. {
  26. return(top == -1);
  27. }
  28. bool Add_Stack::IsFull() const
  29. {
  30. return (top == MAX - 1);
  31. }
  32. void Add_Stack::push(mytype num)
  33. {
  34. if (IsFull())
  35. cout << "Error: the stack is full." << endl;
  36. top++;
  37. arr[top] = num[top];
  38. }
  39. void Add_Stack::pop()
  40. {
  41. if (IsEmpty())
  42. {
  43. cout << "Error : Stack is Empty" << endl;
  44. }
  45. else
  46. {
  47. top--;
  48. }
  49.  
  50. }
  51. mytype Add_Stack::Top()
  52. {
  53. if (IsEmpty())
  54. cout << "Error : Stack is Empty" << endl;
  55. else
  56. {
  57. return arr[top];
  58. }
  59. }
  60.  
  61. int main(){
  62.  
  63. Add_Stack one, two, result;
  64. mytype a, b;
  65. int result_num, carry=0;
  66. cout << "덧셈할 두 수를 입력해주세요 :";
  67. cin >> a >> b;
  68. for (int i = 0;; i++)
  69. {
  70. if (a[i] == NULL)
  71. {
  72. int one_last_index = i - 1;
  73. break;
  74. }
  75. one.push(a);
  76. }
  77. for (int i = 0;; i++)
  78. {
  79. if (b[i] == NULL)
  80. {
  81. int two_last_index = i - 1;
  82. break;
  83. }
  84. two.push(b);
  85. }
  86. while (!one.IsEmpty() || !two.IsEmpty())
  87. {
  88. result_num = atoi(one.Top().c_str()) + atoi(two.Top().c_str());
  89. one.pop();
  90. two.pop();
  91. result.push(to_string((result_num % 10 )+carry));
  92. carry = (result_num / 10);
  93. }
  94. if (carry != 0)
  95. {
  96. result.push(to_string(carry));
  97. }
  98. while (!result.IsEmpty())
  99. {
  100. cout << result.Top() << endl;
  101. result.pop();
  102. }
  103. return 0;
  104. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:70:15: warning: NULL used in arithmetic [-Wpointer-arith]
   if (a[i] == NULL)
               ^
prog.cpp:79:15: warning: NULL used in arithmetic [-Wpointer-arith]
   if (b[i] == NULL)
               ^
prog.cpp:88:38: error: 'atoi' was not declared in this scope
   result_num = atoi(one.Top().c_str()) + atoi(two.Top().c_str());
                                      ^
prog.cpp:91:49: error: 'to_string' was not declared in this scope
   result.push(to_string((result_num % 10 )+carry));
                                                 ^
prog.cpp:96:30: error: 'to_string' was not declared in this scope
   result.push(to_string(carry));
                              ^
stdout
Standard output is empty