Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <cstring>
- #include "Model.h"
- class OPN{
- private:
- static Model expressions;
- public:
- OPN(const Model&){
- this->expressions = expressions;
- }
- Model get_expression(){
- return this->expressions;
- }
- int priority( char oper ){ //определение приоритета операции
- switch(oper){
- case '~':
- return 5;
- case '&':
- return 4;
- case '|':
- return 3;
- case '(':
- return 2;
- case '=':
- return 1;
- default:
- return 0;
- }
- }
- bool isOperation(char ch){
- return ch == '~' || ch == '&' || ch == '|' || ch == '=';
- }
- std::stack <char> Stack;
- void obr_pol_not(Unit expression) {
- char str[100];//для хранения текущего unit
- char opn[100];//здесь будет лежать unit в ОПН
- char *name = new char[10]; //имена переменных
- char ident[expression.identificator_name.size()+1];//преобразование string в char[]
- char value[expression.value.size()+1];
- strcpy(ident, expression.identificator_name.c_str());
- strcpy(value, expression.value.c_str());
- strcat(ident,value);
- strcat(str,ident);// str = ident+value
- int i = 0;
- while ( str[i] != '\0') {
- if (isalpha(str[i])){
- int j = 0;
- name[j] = str[i];
- }
- else if(!isalpha(str[i])) {
- for (int k = 0; k < sizeof(name); k++){//заносим имя переменной в массив ОПН
- opn[i-k] = name[k];
- delete []name;
- }
- if (str[i] == '0' || str[i] == '1') {
- opn[i] = str[i];
- } else if (isOperation(str[i])) {
- if (Stack.empty() || priority(Stack.top()) < priority(str[i])) {
- Stack.push(str[i]);
- } else if (priority(Stack.top()) >= priority(str[i])) {
- opn[i] = str[i];
- while (priority(Stack.top()) >= priority(str[i]) && !Stack.empty()) {
- opn[i] = Stack.top();
- Stack.pop();
- }
- }
- } else if (str[i] == '(') {
- Stack.push(str[i]);
- } else if (str[i] == ')') {
- while (Stack.top() != '(' && !Stack.empty()) {
- opn[i] = Stack.top();
- Stack.pop();
- }
- Stack.pop();
- }
- }
- i++;
- }
- while (!Stack.empty()) {
- std::cout << Stack.top();
- Stack.pop();
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement