Minicurso de C++ para Matemática Ajude a manter o site livre, gratuito e sem propagandas. Colabore!
2.5 Operadores lógicos elementares
Os operadores lógicos elementares são:
Por exemplo, a tabela booleana do and é
O seguinte código, monta essa tabela booleana, verifique!
Código 6: and.cpp
1 #include < iostream >
2 #include < iomanip >
3
4 int main ()
5 {
6 bool T = true ;
7 bool F = false ;
8
9 std :: cout << "A | B | A and B"
10 << std :: endl ;
11 std :: cout << std :: boolalpha
12 << T << " | " << T << " | "
13 << ( T and T ) << std :: endl
14 << T << " | " << F << "| "
15 << ( T and F ) << std :: endl
16 << F << "| " << T << " | "
17 << ( F and T ) << std :: endl
18 << F << "| " << F << "| "
19 << ( F and F ) << std :: endl ;
20
21 return 0;
22 }
E. 2.5.1.
Construa as tabelas booleanas do operador or e do not .
Resposta
E. 2.5.2.
Escreva um código para verificar as seguintes comparações
a)
b)
c)
Resposta
1 #include < iostream >
2 #include < iomanip >
3 #include < cmath >
4
5 int main ()
6 {
7 std :: cout << std :: boolalpha ;
8
9
10 std :: cout << "a) "
11 << (1.4 <= sqrt (2)) and \
12 ( sqrt (2) < 1.5) << std :: endl ;
13
14
15 double x = sin ( M_PI /3.0);
16 std :: cout << "b) "
17 << ( fabs ( x ) < 1.0) << std :: endl ;
18
19
20 x = cos ( M_PI * M_PI );
21 std :: cout << "c) "
22 << ( fabs ( x ) > 0.5) << std :: endl ;
23
24 return 0;
25 }
E. 2.5.3.
Considere um retângulo r : A B D C de vértices A = ( 1,1 ) e D = ( 2,3 ) . Crie um código em que a/o usuária/o informa as coordenadas de um ponto P = ( x , y ) e o código verifica cada um dos seguintes itens:
1.
2.
3.
Resposta
1 #include < iostream >
2 #include < iomanip >
3
4 int main ()
5 {
6
7 double Ax = 1.0;
8 double Ay = 1.0;
9
10 double Dx = 2.0;
11 double Dy = 3.0;
12
13
14 double x , y ;
15 std :: cout << "x: " ;
16 std :: cin >> x ;
17 std :: cout << "y: " ;
18 std :: cin >> y ;
19
20
21 bool interior = ( Ax < x and x < Dx ) and \
22 ( Ay < y and y < Dy );
23 bool borda = ( Ax == x or Dx == x ) and \
24 ( Ay <= y and y <= Dy ) or \
25 ( Ay == y or Dy == y ) and \
26 ( Ax <= x and x <= Dx );
27 bool exterior = not interior and not borda ;
28
29 std :: cout << std :: boolalpha
30 << "P in r: "
31 << interior << std :: endl
32 << "P on \\partial r: "
33 << borda << std :: endl
34 << "P not in \\hat r: "
35 << exterior << std :: endl ;
36
37 return 0;
38 }
E. 2.5.4.
Implemente uma instrução para computar o operador xor (ou exclusivo). Dadas duas afirmações A e B , A xor B é true no caso de uma, e somente uma, das afirmações ser true , caso contrário é false .
Resposta
(A or B) and not(A and B)
Envie seu comentárioAproveito para agradecer a todas/os que de forma assídua ou esporádica contribuem enviando correções, sugestões e críticas!
Este texto é disponibilizado nos termos da Licença Creative Commons Atribuição-CompartilhaIgual 4.0 Internacional . Ícones e elementos gráficos podem estar sujeitos a condições adicionais.