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)
- {
- if (p == 0)
- {
- return 1;
- }
- for (int i = 0; i < p; i++)
- {
- num *= num;
- }
- return num;
- }
- using namespace std;
- unsigned int f(unsigned int 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;
- }
- 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')
- {
- int p = expression[i];
- int cnt = 1;
- while (expression[i+1] >= '0' && expression[i+1] <= 9)
- {
- p += powOf(10, cnt) * (expression[++i] - '0');
- }
- numbers.push(p);
- }
- 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