Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <cstring>
  4. #include "Model.h"
  5.  
  6. class OPN{
  7. private:
  8.     static Model expressions;
  9. public:
  10.     OPN(const Model&){
  11.         this->expressions = expressions;
  12.     }
  13.     Model get_expression(){
  14.         return this->expressions;
  15.     }
  16.     int priority( char oper ){ //определение приоритета операции
  17.         switch(oper){
  18.             case '~':
  19.                 return 5;
  20.             case '&':
  21.                 return 4;
  22.             case '|':
  23.                 return 3;
  24.             case '(':
  25.                 return 2;
  26.             case '=':
  27.                 return 1;
  28.             default:
  29.                 return 0;
  30.         }
  31.     }
  32.  
  33.     bool isOperation(char ch){
  34.         return ch == '~' || ch == '&' || ch == '|' || ch == '=';
  35.     }
  36.  
  37.     std::stack <char> Stack;
  38.  
  39.     void obr_pol_not(Unit expression) {
  40.         char str[100];//для хранения текущего unit
  41.         char opn[100];//здесь будет лежать unit  в ОПН
  42.         char *name = new char[10]; //имена переменных
  43.  
  44.         char ident[expression.identificator_name.size()+1];//преобразование string в char[]
  45.         char value[expression.value.size()+1];
  46.         strcpy(ident, expression.identificator_name.c_str());
  47.         strcpy(value, expression.value.c_str());
  48.  
  49.         strcat(ident,value);
  50.         strcat(str,ident);// str = ident+value
  51.  
  52.         int i = 0;
  53.         while ( str[i] != '\0') {
  54.             if (isalpha(str[i])){
  55.                 int j = 0;
  56.                 name[j] = str[i];
  57.             }
  58.             else if(!isalpha(str[i])) {
  59.                 for (int k = 0; k < sizeof(name); k++){//заносим имя переменной в массив ОПН
  60.                     opn[i-k] = name[k];
  61.                     delete []name;
  62.                 }
  63.                 if (str[i] == '0' || str[i] == '1') {
  64.                     opn[i] = str[i];
  65.                 } else if (isOperation(str[i])) {
  66.  
  67.                     if (Stack.empty() || priority(Stack.top()) < priority(str[i])) {
  68.                         Stack.push(str[i]);
  69.                     } else if (priority(Stack.top()) >= priority(str[i])) {
  70.                         opn[i] = str[i];
  71.                         while (priority(Stack.top()) >= priority(str[i]) && !Stack.empty()) {
  72.                             opn[i] = Stack.top();
  73.                             Stack.pop();
  74.                         }
  75.                     }
  76.                 } else if (str[i] == '(') {
  77.                     Stack.push(str[i]);
  78.                 } else if (str[i] == ')') {
  79.                     while (Stack.top() != '(' && !Stack.empty()) {
  80.                         opn[i] = Stack.top();
  81.                         Stack.pop();
  82.                     }
  83.                     Stack.pop();
  84.                 }
  85.             }
  86.             i++;
  87.         }
  88.         while (!Stack.empty()) {
  89.             std::cout << Stack.top();
  90.             Stack.pop();
  91.         }
  92.     }
  93. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement