Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "PolishNotation.h"
  3.  
  4. namespace ExpressionChanging
  5. {
  6.     int priority(char symbol)
  7.     {
  8.         if (symbol == '+' || symbol == '-')return 2;
  9.         if (symbol == '*' || symbol == '/')return 3;
  10.         return 4;
  11.     }
  12.  
  13.     void copy_LT(LT::LexTable& lextable, LT::LexTable& lextable2, int i)
  14.     {
  15.         lextable.table[i].lexema = lextable2.table[i].lexema;
  16.         lextable.table[i].sn = lextable2.table[i].sn;
  17.         lextable.table[i].tokenId = lextable2.table[i].tokenId;
  18.         lextable.table[i].idxTI = lextable2.table[i].idxTI;
  19.         lextable.table[i].operation = lextable2.table[i].operation;
  20.         lextable.table[i].value = lextable2.table[i].value;
  21.     }
  22.  
  23.     void Polish_notation_inverse(int &start_position, LT::LexTable& lextable, IT::IdTable& idtable, LT::LexTable& lextable2)
  24.     {
  25.         std::stack <int> buffer;
  26.         int last_postion;
  27.         for (last_postion = start_position; lextable.table[last_postion].lexema != SEMICOLON; last_postion++);
  28.         vector<int> EXPRESSION;
  29.  
  30.         for (int i = start_position; i < last_postion; i++)
  31.         {
  32.             if (lextable.table[i].lexema == '(')
  33.                 buffer.push(i);
  34.  
  35.             if (lextable.table[i].lexema == ')')
  36.             {
  37.                 while (!buffer.empty() && lextable.table[buffer.top()].lexema != '(')
  38.                 {
  39.                     EXPRESSION.push_back(buffer.top());
  40.                     buffer.pop();
  41.                 }
  42.                 buffer.pop();
  43.             }
  44.  
  45.             if (lextable.table[i].lexema == LEX_ID && lextable.table[i + 1].lexema == '(')
  46.             {
  47.                 EXPRESSION.push_back(i);
  48.                 EXPRESSION.push_back(-1);
  49.                 i += 2;
  50.                 while (lextable.table[i].lexema != ')' && lextable.table[i - 1].lexema != ')')
  51.                 {
  52.                     EXPRESSION.push_back(i);
  53.                     i += 2;
  54.                 }
  55.                 EXPRESSION.push_back(-2);
  56.             }
  57.  
  58.             if (lextable.table[i].lexema == 'v')
  59.             {
  60.                 while (!buffer.empty() && priority(lextable.table[i].operation) <= priority(lextable.table[buffer.top()].operation) && lextable.table[buffer.top()].lexema != '(')
  61.                 {
  62.                     EXPRESSION.push_back(buffer.top());
  63.                     buffer.pop();
  64.                 }
  65.                 buffer.push(i);
  66.             }
  67.  
  68.             if (lextable.table[i].lexema == LEX_ID || lextable.table[i].lexema == LEX_LITERAL)
  69.                 EXPRESSION.push_back(i);
  70.         }
  71.  
  72.         while (!buffer.empty())
  73.         {
  74.             EXPRESSION.push_back(buffer.top());
  75.             buffer.pop();
  76.         }
  77.  
  78.         for (int i = 0; i <EXPRESSION.size(); i++)
  79.         {
  80.             if (EXPRESSION[i] == -1)
  81.             {
  82.                 lextable2.table[i + start_position].lexema = '(';
  83.                 lextable2.table[i + start_position].sn = lextable2.table[i + start_position - 1].sn;
  84.                 continue;
  85.             }
  86.             if (EXPRESSION[i] == -2)
  87.             {
  88.                 lextable2.table[i + start_position].lexema = ')';
  89.                 lextable2.table[i + start_position].sn = lextable2.table[i + start_position - 1].sn;
  90.                 continue;
  91.             }
  92.             lextable2.table[i + start_position].lexema = lextable.table[EXPRESSION[i]].lexema;
  93.             lextable2.table[i + start_position].sn = lextable.table[EXPRESSION[i]].sn;
  94.             lextable2.table[i + start_position].idxTI = lextable.table[EXPRESSION[i]].idxTI;
  95.             lextable2.table[i + start_position].tokenId = lextable.table[EXPRESSION[i]].tokenId;
  96.             lextable2.table[i + start_position].operation = lextable.table[EXPRESSION[i]].operation;
  97.             lextable2.table[i + start_position].value = lextable.table[EXPRESSION[i]].value;
  98.         }
  99.  
  100.         lextable2.table[EXPRESSION.size() + start_position].lexema = SEMICOLON;
  101.         lextable2.table[EXPRESSION.size() + start_position].sn = lextable2.table[EXPRESSION.size() + start_position - 1].sn;
  102.  
  103.         for (int i = EXPRESSION.size() + start_position + 1; i < last_postion + 1; i++)
  104.         {
  105.             lextable2.table[i].lexema = NULL;
  106.             lextable2.table[i].sn = lextable2.table[i - 1].sn;
  107.         }
  108.  
  109.         start_position = last_postion;
  110.     }
  111.  
  112.     void Polish_Notation_main(LT::LexTable& lextable, IT::IdTable& idtable)
  113.     {
  114.         LT::LexTable lextable2 = LT::Create(200);
  115.         IT::IdTable ID_Table2 = IT::Create(200);
  116.  
  117.  
  118.         for (int i = 1; i < lextable.size; i++)
  119.         {
  120.             if (lextable.table[i - 2].lexema == LEX_ID && lextable.table[i - 1].lexema == '=')
  121.             {
  122.                 int st = i;
  123.                 Polish_notation_inverse(i, lextable, idtable, lextable2);
  124.                 for (; st < i + 1; st++)
  125.                     copy_LT(lextable, lextable2, st);
  126.             }
  127.         }
  128.  
  129.         for (int i = 1; i < lextable.size; i++)
  130.         {
  131.             if (lextable.table[i].lexema == 'v')
  132.                 lextable.table[i].lexema = lextable.table[i].operation;
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement