Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.70 KB | None | 0 0
  1. #include  <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <utility>
  7.  
  8. using namespace std;
  9.  
  10. // Klasa stosu
  11. class Stack{
  12. private:
  13.     struct element{
  14.         element * next, *prev;
  15.         long long value;
  16.     };
  17.     element * first, * last;
  18.     unsigned int counter;
  19. public:
  20.     Stack(){
  21.         first = NULL;
  22.         last = NULL;
  23.         counter  = 0;
  24.     }
  25.  
  26.     ~Stack(){
  27.         element * p;
  28.         while(first){
  29.             p = first->next;
  30.             delete first;
  31.             first = p;
  32.         }
  33.     }
  34.  
  35.     bool empty(){
  36.         if (counter == 0)
  37.             return  true;
  38.         else
  39.             return  false;
  40.     }
  41.  
  42.     int size(){
  43.         return counter;
  44.     }
  45.  
  46.     long long top(){
  47.         return  first->value;
  48.     }
  49.  
  50.  
  51.     void push(long long val){
  52.         element * p = new element;
  53.         p->next = first;
  54.         p->prev = NULL;
  55.         p->value = val;
  56.         if(first)
  57.             first->prev = p;
  58.         first = p;
  59.         if(!last)
  60.             last = first;
  61.         counter++;
  62.     }
  63.  
  64.  
  65.     void pop(){
  66.         element * p;
  67.         if(first){
  68.             p = first;
  69.             first = first->next;
  70.             if(!first)
  71.                 last = NULL;
  72.             else
  73.                 first->prev = NULL;
  74.             counter--;
  75.             delete p;
  76.         }
  77.     }
  78.  
  79.     void show_values(){
  80.         element * p;
  81.         p = first;
  82.         while(p){
  83.           cout << p->value << " ";
  84.           p = p->next;
  85.         }
  86.     }
  87. };
  88.  
  89. // Stos do zamiany na inflix
  90. class StackSubexpressions {
  91. private:
  92.     struct element{
  93.         element * next, *prev;
  94.         string expr;
  95.         string oper;
  96.     };
  97.     element * first, * last;
  98.     unsigned int counter;
  99. public:
  100.     StackSubexpressions(){
  101.         first = NULL;
  102.         last = NULL;
  103.         counter  = 0;
  104.     }
  105.  
  106.     ~StackSubexpressions(){
  107.         element * p;
  108.         while(first){
  109.             p = first->next;
  110.             delete first;
  111.             first = p;
  112.         }
  113.     }
  114.     string top_expr(){
  115.         return  first->expr;
  116.     }
  117.  
  118.     string top_oper(){
  119.         return  first->oper;
  120.     }
  121.  
  122.     void push(string e, string o){
  123.         element * p = new element;
  124.         p->next = first;
  125.         p->prev = NULL;
  126.         p->expr = e;
  127.         p->oper = o;
  128.         if(first)
  129.             first->prev = p;
  130.         first = p;
  131.         if(!last)
  132.             last = first;
  133.         counter++;
  134.     }
  135.  
  136.     void pop(){
  137.         element * p;
  138.         if(first){
  139.             p = first;
  140.             first = first->next;
  141.             if(!first)
  142.                 last = NULL;
  143.             else
  144.                 first->prev = NULL;
  145.             counter--;
  146.             delete p;
  147.         }
  148.     }
  149.     bool empty(){
  150.         if (counter == 0)
  151.             return  true;
  152.         else
  153.             return  false;
  154.     }
  155.  
  156.     int size(){
  157.         return counter;
  158.     }
  159. };
  160.  
  161. // Funkcja obliczajaca wartosc wyrazenia podangego w onp
  162. long long calcRpn(string &expr){
  163.     istringstream iss(expr);
  164.     Stack stack;
  165.     string token;
  166.     while (iss >> token) {
  167.         long long token_digit;
  168.         if (istringstream(token) >> token_digit) {
  169.             stack.push(token_digit);
  170.         }
  171.         else {
  172.             int b = stack.top();
  173.             stack.pop();
  174.             int a = stack.top();
  175.             stack.pop();
  176.             if (token == "+")
  177.                 stack.push(a + b);
  178.             else if (token == "-")
  179.                 stack.push(a - b);
  180.             else if (token == "*")
  181.                 stack.push(a * b);
  182.             else if (token == "/")
  183.                 stack.push(a / b);
  184.             else {
  185.                 return 0;
  186.             }
  187.         }
  188.     }
  189.     return stack.top();
  190. }
  191.  
  192. // Licznik slow w stringu
  193. int wordCounter(string str){
  194.    int words = 1;
  195.     for(int i = 0; i <= str.size(); i++){
  196.             if(isspace(str[i + 1]))
  197.                 words++;
  198.     }
  199.     return words;
  200. }
  201.  
  202. // Funkcja dzielaca string na tokeny i zapisujaca je do tablicy
  203. string  * Split(string str){
  204.     string * arr = new string[str.size()];
  205.     int i = 0;
  206.     istringstream iss(str);
  207.     while (iss.good()){
  208.         iss >> arr[i];
  209.         ++i;
  210.     }
  211.     return arr;
  212. }
  213.  
  214. // Funkcja przeksztalcajaca postfix -> infix
  215.  
  216. string PostfixToInfix(string postfix){
  217.  
  218.     // Tablica przechowujaca podzielone wyrazenie w postfix
  219.     string * postfixTokens = new string[wordCounter(postfix)];
  220.     postfixTokens = Split(postfix);
  221.  
  222.     // Stos do przechowywania podwyrazen inflix
  223.     StackSubexpressions stack;
  224.  
  225.     for(int i = 0; i < wordCounter(postfix); i++){
  226.         string token = postfixTokens[i];
  227.         if (token == "+" || token == "-"){
  228.             pair<string, string> rightSubExpr;
  229.             rightSubExpr.first = stack.top_expr();
  230.             rightSubExpr.second = stack.top_oper();
  231.             stack.pop();
  232.             pair<string, string> leftSubExpr;
  233.             leftSubExpr.first = stack.top_expr();
  234.             leftSubExpr.second = stack.top_oper();
  235.             stack.pop();
  236.             string newExpr = leftSubExpr.first + token + rightSubExpr.first;
  237.             stack.push(newExpr, token);
  238.         }
  239.         else if (token == "*" || token == "/"){
  240.             string leftExpr, rightExpr;
  241.             pair<string, string> rightSubExpr;
  242.             rightSubExpr.first = stack.top_expr();
  243.             rightSubExpr.second = stack.top_oper();
  244.             stack.pop();
  245.             if (rightSubExpr.second == "+" || rightSubExpr.second == "-" ){
  246.                 rightExpr = "(" + rightSubExpr.first + ")";
  247.             }
  248.             else{
  249.                 rightExpr = rightSubExpr.first;
  250.             }
  251.  
  252.             pair<string, string> leftSubExpr;
  253.             leftSubExpr.first = stack.top_expr();
  254.             leftSubExpr.second = stack.top_oper();
  255.             stack.pop();
  256.             if (leftSubExpr.second == "+" || leftSubExpr.second == "-"){
  257.                 leftExpr = "(" + leftSubExpr.first + ")";
  258.             }
  259.             else{
  260.                 leftExpr = leftSubExpr.first;
  261.             }
  262.             string newExpr = leftExpr + token + rightExpr;         cin.ignore();
  263.             stack.push(newExpr, token);
  264.         }
  265.         else
  266.         {
  267.             stack.push(token, "");
  268.         }
  269.     }
  270.     delete [] postfixTokens;
  271.     return stack.top_expr();
  272. }
  273.  
  274. // Struktura do przechowywania wynikow
  275. struct results{
  276.     string expr;
  277.     long long result;
  278.     bool isUsed;
  279.     int bracketsNum;
  280. };
  281.  
  282. // Licznik nawiasow
  283. int bracketsCounter(string str){
  284.     int bracketsNum = 0;
  285.     for(int i = 0; i < str.size(); i++)
  286.         if(str[i] == '(' || str[i] == ')')
  287.             bracketsNum++;
  288.     return bracketsNum;
  289. }
  290.  
  291. // Funkcja sortujaca wyniki wedlug nawiasow
  292. void  Sort(results  arr[], int size){
  293.     results temp;
  294.     int j;
  295.     for(int  i = 1; i < size; i++)
  296.         if (arr[i].bracketsNum < arr[i - 1].bracketsNum){
  297.             temp = arr[i];
  298.             for(j = i - 1; (j  >= 0)&& (arr[j].bracketsNum > temp.bracketsNum); j--)
  299.                 arr[j + 1] = arr[j];
  300.             arr[j + 1] = temp;
  301.         }
  302. }
  303.  
  304. int main(){
  305.     int operations_num;     // Liczba operacji
  306.     char operation_type;    // Rodzaj operacji
  307.     string expr;                    // Wyrazenie w ONP
  308.     int s_counter = 0;
  309.     cin >> operations_num;
  310.     results arr[operations_num];    // Tablica przechowujaca wyrazenia z wynikiem
  311.     int arr_index = 0;  // Indeksy tablicy
  312.     for(int i = 0; i < operations_num; i++){
  313.         cin >> operation_type;
  314.  
  315.         // Dodawanie wyrazenia
  316.         if(operation_type == 'D'){
  317.             cin.ignore();
  318.             getline(cin, expr);
  319.             arr[arr_index].result = calcRpn(expr);
  320.             arr[arr_index].expr = PostfixToInfix(expr);
  321.             arr[arr_index].bracketsNum = bracketsCounter(arr[arr_index].expr);
  322.             arr[arr_index].isUsed = false;
  323.             arr_index++;
  324.         }
  325.         // Przeszukanie wyrazen
  326.         else if(operation_type == 'S'){
  327.             long long res_search;
  328.             string result_expr;
  329.             bool result_found = false;
  330.             cin >> res_search;
  331.             Sort(arr, arr_index);
  332.             for(int i = 0; i < arr_index; i++){
  333.                 if(arr[i].result == res_search && arr[i].isUsed != true){
  334.                     result_expr = arr[i].expr;
  335.                     arr[i].isUsed = true;
  336.                     result_found = true;
  337.                     break;
  338.                 }
  339.             }
  340.  
  341.             if(result_found){
  342.                 cout << "TAK" << endl;
  343.                 cout << result_expr << endl;
  344.             }
  345.  
  346.             else{
  347.                 cout << "NIE" << endl;
  348.             }
  349.         }
  350.     }
  351.  
  352.     return 0;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement