Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. #include <fstream>
  5.  
  6. unsigned int factorial(unsigned int num)
  7. {
  8. if (num == 0 || num == 1)
  9. {
  10. return 1;
  11. }
  12. int result = 1;
  13. for (int i = 0; i < num; i++)
  14. {
  15. result *= num - i;
  16. }
  17. return result;
  18. }
  19.  
  20. unsigned int powOf(unsigned int num, int p)
  21. {
  22. int newNum = 1;
  23. if (p == 0)
  24. {
  25. return 1;
  26. }
  27. for (int i = 0; i < p; i++)
  28. {
  29. newNum = newNum * num;
  30. }
  31. return newNum;
  32. }
  33.  
  34. using namespace std;
  35.  
  36. unsigned int f(unsigned int x)
  37. {
  38. x *= x;
  39. return x % 100;
  40. }
  41.  
  42. unsigned int g(unsigned int x)
  43. {
  44. x %= 5;
  45. return factorial(x);
  46. }
  47.  
  48. unsigned int m(int x, int y)
  49. {
  50. return x < y ? x : y;
  51. }
  52.  
  53. void swapInts(int& a, int& b)
  54. {
  55. int c = a;
  56. a = b;
  57. b = c;
  58. }
  59.  
  60. int getNum(string str, int& start)
  61. {
  62. int num = 0;
  63. int cnt = 0;
  64. int length = str.length();
  65. for (int i = start; i < length; i++)
  66. {
  67. if (str[i] >= '0' && str[i] <= '9')
  68. {
  69. cnt++;
  70. }
  71. else
  72. {
  73. break;
  74. }
  75. }
  76. int i = 0;
  77. int shift = cnt;
  78. while (cnt > 0)
  79. {
  80. num += (str[start + i] - '0') * powOf(10, --cnt);
  81. i++;
  82. }
  83. if (start + shift == length)
  84. {
  85. start += shift - 1;
  86. }
  87. else
  88. {
  89. start += shift;
  90. }
  91. return num;
  92. }
  93. int main()
  94. {
  95. int n;
  96. ifstream iFile("Expression.txt");
  97. if (!iFile)
  98. {
  99. cerr << "File could not be opened!\n";
  100. }
  101. iFile >> n;
  102. string expression;
  103. while (n > 0)
  104. {
  105. int result = 0;
  106. stack<int> numbers;
  107. stack<char> funcs;
  108. iFile >> expression;
  109. int length = expression.length();
  110. for (int i = 0; i < length; i++)
  111. {
  112. int exponent = 0;
  113. if (expression[i] == 'f' || expression[i] == 'g' || expression[i] == 'm')
  114. {
  115. funcs.push(expression[i]);
  116. }
  117. else if (expression[i] >= '0' && expression[i] <= '9')
  118. {
  119. numbers.push(getNum(expression,i));
  120. }
  121. else if (expression[i] == '(' || expression[i] == ')' || expression[i] == ',')
  122. {
  123. continue;
  124. }
  125. else
  126. {
  127. cout << "Expression contains an unrecognized symbol\n";
  128. }
  129. }
  130. while (!funcs.empty())
  131. {
  132. if (funcs.top() == 'f')
  133. {
  134. result = numbers.top();
  135. result = f(result);
  136. numbers.pop();
  137. numbers.push(result);
  138. }
  139. else if (funcs.top() == 'g')
  140. {
  141. result = numbers.top();
  142. result = g(result);
  143. numbers.pop();
  144. numbers.push(result);
  145. }
  146. else if (funcs.top() == 'm')
  147. {
  148. result = numbers.top();
  149. numbers.pop();
  150. int arg2 = numbers.top();
  151. numbers.pop();
  152. result = m(result, arg2);
  153. numbers.push(result);
  154. }
  155. funcs.pop();
  156. }
  157. cout << numbers.top() << endl;
  158. n--;
  159. }
  160. iFile.close();
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement