Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stack>
- #include <fstream>
- unsigned int factorial(unsigned int num)
- {
- if (num == 0 || num == 1)
- {
- return 1;
- }
- int result = 1;
- for (int i = 0; i < num; i++)
- {
- result *= num - i;
- }
- return result;
- }
- unsigned int powOf(unsigned int num, int p)
- {
- int newNum = 1;
- if (p == 0)
- {
- return 1;
- }
- for (int i = 0; i < p; i++)
- {
- newNum = newNum * num;
- }
- return newNum;
- }
- using namespace std;
- unsigned int f(unsigned int x)
- {
- x *= x;
- return x % 100;
- }
- unsigned int g(unsigned int x)
- {
- x %= 5;
- return factorial(x);
- }
- unsigned int m(int x, int y)
- {
- return x < y ? x : y;
- }
- void swapInts(int& a, int& b)
- {
- int c = a;
- a = b;
- b = c;
- }
- int getNum(string str, int& start)
- {
- int num = 0;
- int cnt = 0;
- int length = str.length();
- for (int i = start; i < length; i++)
- {
- if (str[i] >= '0' && str[i] <= '9')
- {
- cnt++;
- }
- else
- {
- break;
- }
- }
- int i = 0;
- int shift = cnt;
- while (cnt > 0)
- {
- num += (str[start + i] - '0') * powOf(10, --cnt);
- i++;
- }
- if (start + shift == length)
- {
- start += shift - 1;
- }
- else
- {
- start += shift;
- }
- return num;
- }
- int main()
- {
- int n;
- ifstream iFile("Expression.txt");
- if (!iFile)
- {
- cerr << "File could not be opened!\n";
- }
- iFile >> n;
- string expression;
- while (n > 0)
- {
- int result = 0;
- stack<int> numbers;
- stack<char> funcs;
- iFile >> expression;
- int length = expression.length();
- for (int i = 0; i < length; i++)
- {
- int exponent = 0;
- if (expression[i] == 'f' || expression[i] == 'g' || expression[i] == 'm')
- {
- funcs.push(expression[i]);
- }
- else if (expression[i] >= '0' && expression[i] <= '9')
- {
- numbers.push(getNum(expression,i));
- }
- else if (expression[i] == '(' || expression[i] == ')' || expression[i] == ',')
- {
- continue;
- }
- else
- {
- cout << "Expression contains an unrecognized symbol\n";
- }
- }
- while (!funcs.empty())
- {
- if (funcs.top() == 'f')
- {
- result = numbers.top();
- result = f(result);
- numbers.pop();
- numbers.push(result);
- }
- else if (funcs.top() == 'g')
- {
- result = numbers.top();
- result = g(result);
- numbers.pop();
- numbers.push(result);
- }
- else if (funcs.top() == 'm')
- {
- result = numbers.top();
- numbers.pop();
- int arg2 = numbers.top();
- numbers.pop();
- result = m(result, arg2);
- numbers.push(result);
- }
- funcs.pop();
- }
- cout << numbers.top() << endl;
- n--;
- }
- iFile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement