德·摩根定律的验证(De Morgan’s Laws)

有点怀念高一时的代数课程了,第一章集合还是挺友好的,后面的章节就越来越难了哭

代码如下:

//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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值