Advertisement
rfop

TabelaVerdade

May 26th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <vector>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. bool compLen(string &a, string &b){ //tamanho
  12.     return a.size() <= b.size();
  13. }
  14.  
  15. bool compLex(string &a, string &b){ // lexixografica
  16.     for(int i = 0, len = a.size(); i < len; i++){
  17.         if(a[i] < b[i]) return true;
  18.         if(a[i] > b[i]) return false;
  19.     }
  20.     return false;
  21. }
  22.  
  23.  
  24. void selectionSort(vector<string> &str){ // ordem de tamanho
  25.     for(int i=0,len=str.size()-1; i<len; i++){
  26.         for(int j=i+1,len2=str.size(); j<len2; j++){
  27.             if(!compLen(str[i], str[j])) swap(str[i], str[j]);
  28.         }
  29.     }
  30.  
  31.  
  32.  
  33.     for(int i=0,len=str.size()-1; i<len; i++){ // ordem lexixografica caso o tamanho seja o msm
  34.         for(int j=i+1,len2=str.size(); j<len2; j++){
  35.             if(str[i].size() != str[j].size()) continue;
  36.             if(!compLex(str[i], str[j])) swap(str[i], str[j]);
  37.         }
  38.     }
  39. }
  40.  
  41.  
  42.  
  43. string tamanho(int n){
  44.     string s="";
  45.     --n;
  46.     while(n--) s+= " ";
  47.  
  48.  
  49.     return s;
  50. }
  51.  
  52. void replaceAll(string& s, char antigo, char novo)
  53. {
  54.     for (int i = 0, len = s.size(); i < len; i++)
  55.         if (s[i] == antigo)
  56.             s[i] = novo;
  57. }
  58.  
  59. vector<char> parentesis;
  60. vector<char> operadores;
  61.  
  62. char resolverAtomica(char m, char op, char n)
  63. {
  64.     int a = m == '0' ? 0 : 1;
  65.     int b = n == '0' ? 0 : 1;
  66.  
  67.     if (op == '.')
  68.         return (a && b) ? '1' : '0';
  69.     if (op == '+')
  70.         return (a || b) ? '1' : '0';
  71.     if (op == '>')
  72.         return ((!a) || b) ? '1' : '0';
  73. }
  74.  
  75. char resolver(string exp)
  76. {
  77.     for (int i = 0, len = exp.size(); i < len; i++) {
  78.         if (exp[i] == '(') {
  79.             parentesis.push_back('(');
  80.         }
  81.         else if (exp[i] == ')') {
  82.             if (operadores[(int)operadores.size() - 2] == '-') {
  83.                 char resultado = operadores[(int)operadores.size() - 1] == '0' ? '1' : '0';
  84.                 operadores.pop_back();
  85.                 operadores.pop_back();
  86.                 parentesis.pop_back();
  87.                 operadores.push_back(resultado);
  88.             }
  89.             else {
  90.                 char resultado = resolverAtomica(operadores[operadores.size() - 3], operadores[operadores.size() - 2], operadores[operadores.size() - 1]);
  91.                 operadores.pop_back();
  92.                 operadores.pop_back();
  93.                 operadores.pop_back();
  94.                 parentesis.pop_back();
  95.                 operadores.push_back(resultado);
  96.             }
  97.         }
  98.         else {
  99.             operadores.push_back(exp[i]);
  100.         }
  101.     }
  102.  
  103.     return operadores[0];
  104. }
  105.  
  106. char validade(string exp, int x, int y, int z, int t)
  107. {
  108.     replaceAll(exp, 'x', x == 0 ? '0' : '1');
  109.     replaceAll(exp, 'y', y == 0 ? '0' : '1');
  110.     replaceAll(exp, 'z', z == 0 ? '0' : '1');
  111.     replaceAll(exp, 't', t == 0 ? '0' : '1');
  112.  
  113.     parentesis.clear();
  114.     operadores.clear();
  115.  
  116.     return resolver(exp);
  117. }
  118.  
  119. int main()
  120. {
  121.     int numeroTabela=1;
  122.     std::string entrada, saida, subexpressao,qntdexpress;
  123.     freopen("Expressoes.in", "r", stdin);
  124.     freopen("Expressoes.out", "w", stdout);
  125.     std::getline (std::cin,qntdexpress);
  126.     int nconvertido = std::stoi(qntdexpress);
  127.     while(nconvertido--){
  128.         std::getline (std::cin,entrada);
  129.         std::vector<std::string> strVector;
  130.         string reserva = entrada;
  131.         int segundaParada, primeiraParada;
  132.         bool temx = false, temy = false, temz = false, temt = false;
  133.         for (int i = 0; i < entrada.length(); i++) {
  134.             if (entrada[i] == 'x') {
  135.                 temx = true;
  136.             }
  137.             else if (entrada[i] == 'y') {
  138.                 temy = true;
  139.             }
  140.             else if (entrada[i] == 'z') {
  141.                 temz = true;
  142.             }
  143.             else if (entrada[i] == 't') {
  144.                 temt = true;
  145.             }
  146.         }
  147.  
  148.         while (1) {
  149.             primeiraParada = entrada.find_first_of(')');
  150.             if (primeiraParada == -1)
  151.                 break;
  152.             for (int desce = primeiraParada - 1; desce >= 0; desce--) {
  153.                 if (entrada[desce] == '(') {
  154.                     segundaParada = desce;
  155.                     break;
  156.                 }
  157.             }
  158.  
  159.             entrada[segundaParada] = '_', entrada[primeiraParada] = '_';
  160.             subexpressao = reserva.substr(segundaParada, primeiraParada - segundaParada + 1);
  161.             strVector.push_back(subexpressao);
  162.         }
  163.         selectionSort(strVector);
  164.         int pos=0;
  165.         bool duplicado=true;
  166.         while(duplicado){
  167.             duplicado=false;
  168.             for(int i=0,len=strVector.size()-1; i<len; i++){
  169.                 if(strVector[i] == strVector[i+1]){
  170.                     duplicado=true;
  171.                     pos=i;
  172.                 }
  173.             }
  174.             if(duplicado)strVector.erase(strVector.begin()+pos);
  175.         }
  176.         int c=0;
  177.         cout<<"Tabela #"<<numeroTabela<<endl;
  178.         if(temx) strVector.insert(strVector.begin() +(c++),"x");
  179.         if(temy) strVector.insert(strVector.begin() +(c++),"y");
  180.         if(temz) strVector.insert(strVector.begin() +(c++),"z");
  181.         if(temt) strVector.insert(strVector.begin() +(c++),"t");
  182.         int tam=0;
  183.         for(int i = 0; i < strVector.size(); i++){
  184.             tam+=strVector[i].size();
  185.         }
  186.         tam += strVector.size()+1;
  187.         string sep(tam,'-');
  188.         cout<<sep<<endl;
  189.         cout<<"|";
  190.         for(auto i:strVector)cout<<i<<"|"; cout<<endl<<sep<<endl;
  191.         bool tem0=false,tem1=false;
  192.         std::vector<char> respostas;
  193.         for (int x = 0; x < 2; x++)
  194.             for (int y = 0; y < 2; y++)
  195.                 for (int z = 0; z < 2; z++)
  196.                     for (int t = 0; t < 2; t++)
  197.                         for (int i = 0; i < strVector.size(); i++) {
  198.                             if(!temx && x == 1) continue;
  199.                             if(!temy && y == 1) continue;
  200.                             if(!temz && z == 1) continue;
  201.                             if(!temt && t == 1) continue;
  202.                             if(!i) cout<<"|";
  203.                             char r = validade(strVector[i], x, y, z, t);
  204.                             cout<<tamanho(strVector[i].size())<<r<<"|";
  205.                             if(i==strVector.size()-1){
  206.                                 tem0 = tem0 || (r == '0');
  207.                                 tem1 = tem1 || (r == '1');
  208.                                 cout<<endl<<sep<<endl;
  209.                             }
  210.                         }
  211.         if(tem1 && tem0)cout<<"satisfativel e refutavel"<<endl;
  212.         else if(tem1)cout<<"satisfativel e tautologia"<<endl;
  213.         else cout<<"insatisfativel e refutavel"<<endl;
  214.         numeroTabela++;
  215.         cout<<endl;
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement