fork download
  1. #include "StdAfx.h"
  2. #include "Calc.h"
  3. #include "string"
  4. Calc::Calc()
  5. {
  6. result = "";
  7. operation = "";
  8. ilLiczb = 0;
  9. ilZnakow = 0;
  10.  
  11. }
  12.  
  13.  
  14. void Calc::RPN(string operation)
  15. {
  16. Stos *stack = new Stos;
  17. result = "";
  18. ilLiczb=0;
  19. ilZnakow=0;
  20. for( unsigned int i=0; i < operation.size() ; i++)
  21. {
  22.  
  23.  
  24. if ((operation[i] >= '0' && operation[i] <= '9')) {
  25. result += operation[i];
  26. if (!(operation[i+1] >= '0' && operation[i+1] <= '9')) {
  27. result += " ";
  28. ilLiczb++;
  29. }
  30. }
  31. else if (operation[i] == '*' || operation[i] == '/' || operation[i] == '-'|| operation[i] == '+')
  32. {
  33.  
  34. if(stack->Top()==NULL)
  35. {
  36. stack->Push((int)operation[i]);
  37. ilZnakow++;
  38. }
  39. else
  40. {
  41. char x = operation[i];
  42. char y = stack->Pop();
  43.  
  44. if((y == '*' || y == '/') || ((y == '+' || y == '-') && x != '*' && x != '/'))
  45. {
  46. result += y;
  47. result += " ";
  48. while(stack->GetNumbers()>0)
  49. {
  50. result += stack->Pop();
  51. result += " ";
  52. }
  53.  
  54. stack->Push(x);
  55. }
  56. else
  57. {
  58. stack->Push(y);
  59. stack->Push(x);
  60. }
  61. ilZnakow++;
  62. }
  63.  
  64. }
  65. else
  66. {
  67. if(operation[i] != ' ')
  68. {
  69. ex = "Wystapily niepoprane znaki w wyrazeniu (np. litery)!";
  70. throw ex;
  71.  
  72. }
  73. }
  74. }
  75. while(stack->GetNumbers()>0)
  76. {
  77. result += stack->Pop();
  78. result += " ";
  79. }
  80.  
  81. if(ilLiczb != ilZnakow + 1)
  82. {
  83. ex = "Niepoprawne wyrazenie (ilosc znakow musi byc o jeden mniejsza od ilosci liczb) ";
  84. throw ex;
  85. }
  86. Result(result);
  87.  
  88. }
  89.  
  90. void Calc::Result(string operation)
  91. {
  92.  
  93. Stos *stack2 = new Stos;
  94. stack2->Pop();
  95. int op;
  96. int op2;
  97. //std::string wymik;
  98. string wymik;
  99. string res;
  100. for(unsigned int i=0; i < operation.size(); i++)
  101. {
  102. res = "";
  103. int k = 200;
  104. if ((operation[i] >= '0' && operation[i] <= '9')) {
  105. for(int j=i; j < k; j++)
  106. {
  107. if(operation[j] != ' ')
  108. {
  109. res += operation[j];
  110. i=j;
  111.  
  112. }
  113. else
  114. {
  115. k=j;
  116. }
  117.  
  118. }
  119.  
  120. stack2->Push(atoi(res.c_str()));
  121.  
  122. }
  123. else if(operation[i] == '*')
  124. {
  125. op2 = stack2->Pop() * stack2->Pop();
  126. stack2->Push(op2);
  127. }
  128. else if(operation[i] == '/')
  129. {
  130. op2 = stack2->Pop();
  131. if (op2 != 0)
  132. {
  133. op = stack2->Pop() / op2;
  134. stack2->Push(op);
  135. }
  136. else {
  137. ex = "Blad dzielenia przez zero!";
  138. throw ex;
  139.  
  140. }
  141. }
  142. else if(operation[i] == '+')
  143. {
  144. op2 = stack2->Pop() + stack2->Pop();
  145. stack2->Push(op2);
  146.  
  147. }
  148. else if(operation[i] == '-')
  149. {
  150. op2 = stack2->Pop();
  151. op = stack2->Pop() - op2;
  152. stack2->Push(op);
  153. }
  154. else if(operation[i] == ' ')
  155. {
  156. }
  157. else
  158. {
  159. ex = "Blad - nieznany znak!";
  160. throw ex;
  161. }
  162. }
  163. wymik = stack2->Pop();
  164.  
  165. }
  166.  
  167. Calc::~Calc(void)
  168. {;
  169. }
  170.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:20: fatal error: StdAfx.h: No such file or directory
 #include "StdAfx.h"
                    ^
compilation terminated.
stdout
Standard output is empty