Advertisement
Guest User

Untitled

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