有点怀念高一时的代数课程了,第一章集合还是挺友好的,后面的章节就越来越难了
代码如下:
//JHTP Exercise 5.23: De Morgan’s Laws
//by pandenghuang@163.com
/*(De Morgan’s Laws) In this chapter, we discussed the logical operators &&, &, ||, |, ^ and !.
De Morgan’s laws can sometimes make it more convenient for us to express a logical expression.
These laws state that the expression !(condition1 && condition2) is logically equivalent to the expression
(!condition1 || !condition2). Also, the expression !(condition1 || condition2) is logically
equivalent to the expression (!condition1 && !condition2). Use De Morgan’s laws to write equivalent
expressions for each of the following, then write an application to show that both the original expression
and the new expression in each case produce the same value:
a) !(x < 5) && !(y >= 7)
b) !(a == b) || !(g != 5)
c) !((x <= 8) && (y > 4))
d) !((i > 4) || (j <= 6))*/
import java.util.Scanner;
public class DeMorganLaw
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
boolean ea,eaDM,eb,ebDM,ec,ecDM,ed,edDM;
int a,b,g,i,j,x,y;
a=b=g=i=j=x=y=0;
ea=!(x < 5) && !(y >= 7);
eaDM=!((x < 5) || !(y >= 7));
eb=!(a == b) || !(g != 5);
ebDM=!((a == b) && !(g != 5));
ec=!((x <= 8) && (y > 4));
ecDM=!(x <= 8) || !(y > 4);
ed=!((i > 4) || (j <= 6));
edDM=!(i > 4) && !(j <= 6);
System.out.printf("请依次输入整数a,b,g,i,j,x,y的值:\n");
a=input.nextInt();
System.out.printf("a=%d\n",a);
b=input.nextInt();
System.out.printf("b=%d\n",b);
g=input.nextInt();
System.out.printf("g=%d\n",g);
i=input.nextInt();
System.out.printf("i=%d\n",i);
j=input.nextInt();
System.out.printf("j=%d\n",j);
x=input.nextInt();
System.out.printf("x=%d\n",x);
y=input.nextInt();
System.out.printf("y=%d\n",y);
System.out.printf("\n以上数值的逻辑运算结果:\n");
System.out.printf("!(x < 5) && !(y >= 7)=%b\n",!(x < 5) && !(y >= 7));
System.out.printf("!((x < 5) ||(y >= 7))=%b\n\n",!((x < 5) || (y >= 7)));
System.out.printf("!(a == b) || !(g != 5)=%b\n",!(a == b) || !(g != 5));
System.out.printf("!((a == b) && !(g != 5))=%b\n\n",!((a == b) && !(g != 5)
));
System.out.printf("!((x <= 8) && (y > 4))=%b\n",!((x <= 8) && (y > 4)));
System.out.printf("!(x <= 8) || !(y > 4)=%b\n\n",!(x <= 8) || !(y > 4));
System.out.printf("!((i > 4) || (j <= 6))=%b\n",!((i > 4) || (j <= 6)));
System.out.printf("!(i > 4) && !(j <= 6)=%b\n",!(i > 4) && !(j <= 6));
}
}
运算结果:
请依次输入整数a,b,g,i,j,x,y的值:
10
a=10
20
b=20
30
g=30
40
i=40
50
j=50
60
x=60
70
y=70
以上数值的逻辑运算结果:
!(x < 5) && !(y >= 7)=false
!((x < 5) ||(y >= 7))=false
!(a == b) || !(g != 5)=true
!((a == b) && !(g != 5))=true
!((x <= 8) && (y > 4))=true
!(x <= 8) || !(y > 4)=true
!((i > 4) || (j <= 6))=false
!(i > 4) && !(j <= 6)=false