Ir para conteúdo
Fórum Script Brasil
  • 0

Conversão de C++ para C


spittfire

Pergunta

Sou aluno de CC e tenho que realizar um trabalho de programação envolvendo tabelas verdade.

O problema e que fiz todo o programa em C++ e acabei de ficar sabendo que a professora só aceitara o trabalho em C!

Eu costumo fazer tudo em C++ e não tenho muito conhecimento de C sinceramente, então estava querendo uma ajuda se possivel!

Segue o código:

 

#include <iostream>
#include <string>
using namespace std;

int main() 

{
bool p[4] = { true, true, false, false };
bool q[4] = { true, false, true, false };


for (int i = 0; i <= 10; i++)  {


cout << "\nVoce quer usar AND ou OR com as variaveis?" << endl;
string andor;
cin >> andor;

cout << "Voce quer NEGAR p? S/N" << endl;
string snp;
cin >> snp;

cout << "Voce quer negar q? S/N" << endl;
string snq;
cin >> snq;

cout << "\n";

if (andor == "AND" || andor == "OR" &&
    snq == "S" || snq == "N" &&
    snp == "S" || snp == "N") {
    if (andor == "AND" && snp == "N" && snq == "N") {
        cout << "p | q" << " | " << "p AND q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << p << " | ";
            for (int j = 0; j < 1; j++) {
                cout << q << " | ";
                cout << (p && q);
            }
            cout << endl;
        }
    }
    else if (andor == "AND" && snp == "S" && snq == "N") {
        cout << "p | q" << " | " << "~p AND q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << p << " | ";
            for (int j = 0; j < 1; j++) {
                cout << q << " | ";
                cout << (!(p) && q);
            }
            cout << endl;
        }
    }
    else if (andor == "AND" && snp == "N" && snq == "S") {
        cout << "p | q" << " | " << "p AND ~q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << p << " | ";
            for (int j = 0; j < 1; j++) {
                cout << q << " | ";
                cout << (p && !(q));
            }
            cout << endl;
        }
    }
    else if (andor == "AND" && snp == "S" && snq == "S") {
        cout << "p | q" << " | " << "~p AND ~q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << p << " | ";
            for (int j = 0; j < 1; j++) {
                cout << q << " | ";
                cout << (!(p) && !(q));
            }
            cout << endl;
        }
    }
    else if (andor == "OR" && snp == "N" && snq == "N") {
        cout << "p | q" << " | " << "p OR q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << p << " | ";
            for (int j = 0; j < 1; j++) {
                cout << q << " | ";
                cout << (p || q);
            }
            cout << endl;
        }
    }
    else if (andor == "OR" && snp == "S" && snq == "N") {
        cout << "p | q" << " | " << "~p OR q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << p << " | ";
            for (int j = 0; j < 1; j++) {
                cout << q << " | ";
                cout << (!(p) || q);
            }
            cout << endl;
        }
    }
    else if (andor == "OR" && snp == "N" && snq == "S") {
        cout << "p | q" << " | " << "p OR ~q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << p << " | ";
            for (int j = 0; j < 1; j++) {
                cout << q << " | ";
                cout << (p || !(q));
            }
            cout << endl;
        }
    }
    else if (andor == "OR" && snp == "S" && snq == "S") {
        cout << "p | q" << " | " << "~p OR ~q" << endl;
        for (int i = 0; i < 4; i++) {
            cout << p << " | ";
            for (int j = 0; j < 1; j++) {
                cout << q << " | ";
                cout << (!(p) || !(q));
            }
            cout << endl;
        }
    }       
}
else {
    cerr << "Erro: Favor digitar valores validos - Exemplo(AND, OR, S, N)." << endl;
}

}
return 0;
}

 

Se alguém pudesse me dar uma luz. Tenho como mudar algumas partes do código e ele passaria como C? Ou terei que começar do 0 e me virar?

Link para o comentário
Compartilhar em outros sites

2 respostass a esta questão

Posts Recomendados

  • 0

Consegui traduzir pra C mas por alguma razao ele nao esta salvando os valores nos strings:

 

#include <stdio.h>
#include <string.h>
#define bool    _Bool


typedef int bool;

int main() 

{
bool p[4] = { true, true, false, false };
bool q[4] = { true, false, true, false };
char andor[3];
char snq[1];
char snp[1];

for (int i = 0; i <= 10; i++)  {


printf("\nVoce quer usar AND ou OR com as variaveis?\n");
scanf("%s", andor);

printf("Voce quer NEGAR p? S/N\n");
scanf("%s", snp);

printf("Voce quer NEGAR q? S/N\n");
scanf("%s", snq);

printf("\n");

if (andor == "AND" || andor == "OR" &&  snq == "S" || snq == "N" &&  snp == "S" || snp == "N") {
    if (andor == "AND" && snp == "N" && snq == "N") {
        printf("p | q | p AND q\n");
        for (int i = 0; i < 4; i++) {
            printf("%s | ", p);
            for (int j = 0; j < 1; j++) {
                printf("%s | ", q);
                printf("%s", p && q);
            }

        }
    }
    else   if (andor == "AND" && snp == "S" && snq == "N") {
        printf("p | q | p AND q\n");
        for (int i = 0; i < 4; i++) {
            printf("%s | ", p);
            for (int j = 0; j < 1; j++) {
                printf("%s | ", q);
                printf("%s", p && q);
            }
        }
    
    }
    else   if (andor == "AND" && snp == "N" && snq == "S") {
        printf("p | q | p AND q\n");
        for (int i = 0; i < 4; i++) {
            printf("%s | ", p);
            for (int j = 0; j < 1; j++) {
                printf("%s | ", q);
                printf("%s", p && q);
            }
        
        }
    }
    else    if (andor == "AND" && snp == "S" && snq == "S") {
        printf("p | q | p AND q\n");
        for (int i = 0; i < 4; i++) {
            printf("%s | ", p);
            for (int j = 0; j < 1; j++) {
                printf("%s | ", q);
                printf("%s", p && q);
            }
        
        }
    }
    else    if (andor == "OR" && snp == "N" && snq == "N") {
        printf("p | q | p AND q\n");
        for (int i = 0; i < 4; i++) {
            printf("%s | ", p);
            for (int j = 0; j < 1; j++) {
                printf("%s | ", q);
                printf("%s", p && q);
            }
        
        }
    }
    else    if (andor == "OR" && snp == "S" && snq == "N") {
        printf("p | q | p AND q\n");
        for (int i = 0; i < 4; i++) {
            printf("%s | ", p);
            for (int j = 0; j < 1; j++) {
                printf("%s | ", q);
                printf("%s", p && q);
            }
        
        }
    }
    else    if (andor == "OR" && snp == "N" && snq == "S") {
        printf("p | q | p AND q\n");
        for (int i = 0; i < 4; i++) {
            printf("%s | ", p);
            for (int j = 0; j < 1; j++) {
                printf("%s | ", q);
                printf("%s", p && q);
            }
     
        }
    }
    else    if (andor == "OR" && snp == "S" && snq == "S") {
        printf("p | q | p AND q\n");
        for (int i = 0; i < 4; i++) {
            printf("%s | ", p);
            for (int j = 0; j < 1; j++) {
                printf("%s | ", q);
                printf("%s", p && q);
            }
        
        }
    }       
else {
    printf("Erro: Favor digitar valores validos - Exemplo(AND, OR, S, N).\n");
}
}
}
return 0;
}

 

 

Alguem sabe o que eu devo mudar?

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      652k
×
×
  • Criar Novo...