Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <utility>
  5. #include <sstream>
  6. #include <string>
  7. #include <stack>
  8. #include <algorithm>
  9.  
  10. #include "tester.h"
  11.  
  12. #define debug(x) std::cout << __FILE__ << " (" << __LINE__ << ") " << #x << " == " << x << std::endl;
  13.  
  14.  
  15. using namespace std;
  16.  
  17. std::string trim (std::string s)
  18. {
  19. auto poczatek = s.find_first_not_of(" \t\r\n");
  20. if (poczatek != std::string::npos)
  21. s = s.substr(poczatek);
  22. auto koniec = s.find_last_not_of(" \t\r\n");
  23. if (koniec != std::string::npos)
  24. s = s.substr(0, koniec + 1);
  25.  
  26. return s;
  27. }
  28.  
  29. double parsuj(const std::string & s)
  30. {
  31. double wynik = 0;
  32. double number = 0;
  33. size_t znacznik = 0;
  34. stack<double> stos;
  35. size_t operacja[4] = {0,0,0,0}; //n, +, -, *
  36. string String_N = s;
  37. size_t String_S = 0;
  38. while(1)
  39. {
  40. if(String_N.size() && String_N[0] == '+')
  41. {
  42. double pierwsza = stos.top();
  43. stos.pop();
  44. pierwsza += stos.top();
  45. stos.pop();
  46. stos.push(pierwsza);
  47. String_S = String_N.size();
  48. znacznik = 0;
  49. String_N = String_N.substr(1);
  50. continue;
  51. }
  52. try{
  53. number = stod(String_N, &znacznik);
  54. String_S = String_N.size();
  55. String_N = String_N.substr(znacznik);
  56. stos.push(number);
  57. }
  58. catch(std::invalid_argument &e)
  59. {
  60. if(znacznik > String_S){
  61. if(!stos.empty()){
  62. wynik = stos.top();
  63. stos.pop();
  64. }
  65. return wynik;
  66. }
  67. else{
  68. operacja[0] = String_N.find('n');
  69. operacja[1] = String_N.find('+');
  70. operacja[2] = String_N.find('-');
  71. operacja[3] = String_N.find('*');
  72. size_t polecenie = 0;
  73. size_t *minimalny = min_element(operacja, operacja+4);
  74. polecenie = minimalny-operacja;
  75.  
  76. switch(polecenie){
  77. case 0:{
  78. if(operacja[0] != string::npos){
  79. double pierwsza = stos.top();
  80. stos.pop();
  81. pierwsza *= -1;
  82. stos.push(pierwsza);
  83. }
  84. break;
  85. }
  86. case 1:{
  87. double pierwsza = stos.top();
  88. stos.pop();
  89. pierwsza += stos.top();
  90. stos.pop();
  91. stos.push(pierwsza);
  92. break;
  93. }
  94. case 2:{
  95. double pierwsza = stos.top();
  96. stos.pop();
  97. pierwsza = stos.top() - pierwsza;
  98. stos.pop();
  99. stos.push(pierwsza);
  100. break;
  101. }
  102. case 3:{
  103. double pierwsza = stos.top();
  104. stos.pop();
  105. pierwsza *= stos.top();
  106. stos.pop();
  107. stos.push(pierwsza);
  108. }
  109. }
  110. String_S = String_N.size();
  111. znacznik = operacja[polecenie];
  112. String_N = String_N.substr(operacja[polecenie]+1);
  113. }
  114.  
  115. }
  116. }
  117.  
  118. return wynik;
  119. }
  120.  
  121. int main ()
  122. {
  123.  
  124.  
  125.  
  126. Tester tester; // Obiekt tester realizuje testy i zwraca raport z testow
  127. std::string raport = tester.testuj(parsuj);
  128. std::cout << raport << std::endl;
  129.  
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement