fork(1) download
  1.  
  2. #include<iostream>
  3. #include<string>
  4. using namespace std;
  5. typedef string ItemType;
  6. struct NodeType;
  7. class StackType
  8. {
  9. public:
  10. StackType();
  11. ~StackType();
  12. void push(ItemType newItem);
  13. void pop();
  14. ItemType Top();
  15. bool IsFull() const;
  16. bool IsEmpty() const;
  17.  
  18. private:
  19. NodeType* topPtr;
  20. };
  21. struct NodeType{
  22. ItemType info;
  23. NodeType* next;
  24. };
  25. StackType::StackType()
  26. {
  27. topPtr = NULL;
  28. }
  29. StackType::~StackType()
  30. {
  31. NodeType* tempPtr;
  32. while (topPtr != NULL)
  33. {
  34. tempPtr = topPtr;
  35. topPtr = topPtr->next;
  36. delete tempPtr;
  37. }
  38. }
  39. void StackType::push(ItemType newItem)
  40. {
  41. if (IsFull())
  42. cout << "Error : the Stack is Full" << endl;
  43. else
  44. {
  45. NodeType* location;
  46. location = new NodeType;
  47. location->info = newItem;
  48. location->next = topPtr;
  49. topPtr = location;
  50.  
  51. }
  52. }
  53. void StackType::pop()
  54. {
  55. if (IsEmpty())
  56. {
  57. cout << "Error : The Stack Is Empty" << endl;
  58. }
  59. else
  60. {
  61. NodeType* tempPtr;
  62. tempPtr = topPtr;
  63. topPtr = topPtr->next;
  64. delete tempPtr;
  65. }
  66. }
  67. ItemType StackType::Top()
  68. {
  69. if (IsEmpty())
  70. cout << "Error : The Stack Is Empty 표시할 것 이없습니다." << endl;
  71. else
  72. return topPtr->info;
  73. }
  74. bool StackType::IsEmpty() const{
  75. if (topPtr == NULL)
  76. return true;
  77. else
  78. return false;
  79. }
  80. bool StackType::IsFull() const
  81. {
  82. NodeType* location;
  83. try
  84. {
  85. location = new NodeType;
  86. delete location;
  87. return false;
  88. }
  89. catch (std::bad_alloc exception)
  90. {
  91. return true;
  92. }
  93. }
  94. int main()
  95. {
  96. StackType Postfix;
  97. ItemType input;
  98. cout << "Input Postfix 수식: ";
  99. getline(cin, input);
  100. int i;
  101. int c = 0;
  102. int input_digit=0;// 숫자가 몇번 들어왔는지 알려주는 정수<전부쪼갬>
  103. int result_num = 0; // 스택에 쪼개져서 들어간 수를 다시 정수형으로 맞추기 위한 수
  104. int result = 0; // 연산을 위한 result
  105.  
  106. for (i = 0; i != input.length(); i++)
  107. {
  108. if (input[i] == ' ') // 공백을 느낌!<저장 X>
  109. {
  110.  
  111. ItemType Top = Postfix.Top(); // 전에 저장한 수를 꺼내옴
  112. if (isdigit(Top[0]) || Top[0] == '_') //전의 저장한 수가 숫자이거나, 단항연산자 이면
  113. {
  114. if (Top[0] == '_') //단항연산자일경우
  115. {
  116. }
  117. else{
  118. ItemType nexttop;
  119. do{
  120. ;//승수를 위한 정수
  121. result_num = result_num + ((atoi(Top.substr(0, Top.length()).c_str()) - 48)* pow(10, c));
  122. c++;
  123. Postfix.pop();
  124. if (Postfix.IsEmpty())
  125. {
  126. break;
  127. }
  128. nexttop = Postfix.Top();
  129. } while (!Postfix.IsEmpty() && isdigit(nexttop[0]) && result_num<10); // 입력받은수에대해 10이상의 수 계산
  130. Postfix.push(to_string(result_num));//최종결과 스택에 push
  131. c = 0; // 재 초기화
  132. result_num = 0; // 다시 0으로 만들어줌
  133. }
  134. }
  135. }
  136. else if (isdigit(input[i]))
  137. {
  138. Postfix.push(to_string(input[i]));
  139. input_digit++;
  140. }
  141. else if (input[i] == '*')
  142. {
  143. if (Postfix.Top() == "0")
  144. {
  145. Postfix.pop();
  146. }
  147. ItemType opr1 = Postfix.Top();
  148. Postfix.pop();
  149. ItemType opr2 = Postfix.Top();
  150. Postfix.pop();
  151. int opr1_ = atoi(opr1.substr(0, opr1.length()).c_str());
  152. int opr2_ = atoi(opr2.substr(0, opr2.length()).c_str());
  153. result = opr1_ * opr2_;
  154. Postfix.push(to_string(result));
  155. Postfix.push(" ");// 다음 공백을 입력받을때 만약 스택에 숫자가 존재하면 그 다다음수도
  156. // 연속된 수를 묶어주려는 경향이 있기때문에 그것을 방지
  157. }
  158. else if (input[i] == '+')
  159. {
  160. if (Postfix.Top() == "0")
  161. {
  162. Postfix.pop();
  163. }
  164. ItemType opr1 = Postfix.Top();
  165. Postfix.pop();
  166. ItemType opr2 = Postfix.Top();
  167. Postfix.pop();
  168. int opr1_ = atoi(opr1.substr(0, opr1.length()).c_str());
  169. int opr2_ = atoi(opr2.substr(0, opr2.length()).c_str());
  170. result = opr1_ + opr2_;
  171. Postfix.push(to_string(result));
  172. Postfix.push(" ");
  173.  
  174. }
  175. else if (input[i] == '-')
  176. {
  177. if (Postfix.Top() == "0")
  178. {
  179. Postfix.pop();
  180. }
  181. ItemType opr1 = Postfix.Top();
  182. Postfix.pop();
  183. ItemType opr2 = Postfix.Top();
  184. Postfix.pop();
  185. int opr1_ = atoi(opr1.substr(0, opr1.length()).c_str());
  186. int opr2_ = atoi(opr2.substr(0, opr2.length()).c_str());
  187. result = opr1_ - opr2_;
  188. Postfix.push(to_string(result));
  189. Postfix.push(" ");
  190. }
  191. else if (input[i] == '/')
  192. {
  193. if (Postfix.Top() == "0")
  194. {
  195. Postfix.pop();
  196. }
  197. ItemType opr1 = Postfix.Top();
  198. Postfix.pop();
  199. ItemType opr2 = Postfix.Top();
  200. Postfix.pop();
  201. int opr1_ = atoi(opr1.substr(0, opr1.length()).c_str());
  202. int opr2_ = atoi(opr2.substr(0, opr2.length()).c_str());
  203. result = opr1_ / opr2_;
  204. Postfix.push(to_string(result));
  205. Postfix.push(" ");
  206. }
  207. else if (input[i] == '%')
  208. {
  209. if (Postfix.Top() == "0")
  210. {
  211. Postfix.pop();
  212. }
  213. ItemType opr1 = Postfix.Top();
  214. Postfix.pop();
  215. ItemType opr2 = Postfix.Top();
  216. Postfix.pop();
  217. int opr1_ = atoi(opr1.substr(0, opr1.length()).c_str());
  218. int opr2_ = atoi(opr2.substr(0, opr2.length()).c_str());
  219. result = opr1_ * opr2_;
  220. Postfix.push(to_string(result));
  221. Postfix.push(" ");
  222. }
  223. else if (input[i] == '_')
  224. {
  225. ItemType pre_num = Postfix.Top();
  226. Postfix.pop();
  227. int opr1_ = atoi(pre_num.substr(0, pre_num.length()).c_str());
  228. result = opr1_ * -1;
  229. Postfix.push(to_string(result));
  230.  
  231. }
  232. }
  233. if (Postfix.Top() == " ")
  234. {
  235. Postfix.pop();
  236. }
  237. cout << Postfix.Top() << endl;
  238. Postfix.~StackType();
  239.  
  240. return 0;
  241. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:121:76: error: 'atoi' was not declared in this scope
        result_num = result_num + ((atoi(Top.substr(0, Top.length()).c_str()) - 48)* pow(10, c));
                                                                            ^
prog.cpp:121:94: error: 'pow' was not declared in this scope
        result_num = result_num + ((atoi(Top.substr(0, Top.length()).c_str()) - 48)* pow(10, c));
                                                                                              ^
prog.cpp:130:40: error: 'to_string' was not declared in this scope
       Postfix.push(to_string(result_num));//최종결과 스택에 push
                                        ^
prog.cpp:138:35: error: 'to_string' was not declared in this scope
    Postfix.push(to_string(input[i]));
                                   ^
prog.cpp:151:58: error: 'atoi' was not declared in this scope
    int opr1_ = atoi(opr1.substr(0, opr1.length()).c_str());
                                                          ^
prog.cpp:154:33: error: 'to_string' was not declared in this scope
    Postfix.push(to_string(result));
                                 ^
prog.cpp:168:58: error: 'atoi' was not declared in this scope
    int opr1_ = atoi(opr1.substr(0, opr1.length()).c_str());
                                                          ^
prog.cpp:171:33: error: 'to_string' was not declared in this scope
    Postfix.push(to_string(result));
                                 ^
prog.cpp:185:58: error: 'atoi' was not declared in this scope
    int opr1_ = atoi(opr1.substr(0, opr1.length()).c_str());
                                                          ^
prog.cpp:188:33: error: 'to_string' was not declared in this scope
    Postfix.push(to_string(result));
                                 ^
prog.cpp:201:58: error: 'atoi' was not declared in this scope
    int opr1_ = atoi(opr1.substr(0, opr1.length()).c_str());
                                                          ^
prog.cpp:204:33: error: 'to_string' was not declared in this scope
    Postfix.push(to_string(result));
                                 ^
prog.cpp:217:58: error: 'atoi' was not declared in this scope
    int opr1_ = atoi(opr1.substr(0, opr1.length()).c_str());
                                                          ^
prog.cpp:220:33: error: 'to_string' was not declared in this scope
    Postfix.push(to_string(result));
                                 ^
prog.cpp:227:64: error: 'atoi' was not declared in this scope
    int opr1_ = atoi(pre_num.substr(0, pre_num.length()).c_str());
                                                                ^
prog.cpp:229:33: error: 'to_string' was not declared in this scope
    Postfix.push(to_string(result));
                                 ^
stdout
Standard output is empty