小白日志:第六天,C++与Python的逻辑、比较运算符

说真的今天的内容不算难。

通用的也比较简单:== ; > ; < ; >= ; <= ; != 。而特殊之处是在于二者的逻辑符号,对应下来是:&&   ||   !和   and  or  not

对于比较运算,一个关键的点在于其优先于逻辑符,也就是说如果用括号去改变操作顺序,那么得到的逻辑结果可能不同

比较运算符的重载都大同小异,至少目前 我只需要接触的是字符串的比较,即比较最先不同的ascii。

    int cpe1 = 3;
	int cpe2 = 4;
	int cpe3 = 5;
	bool flag1= bool("je">="t'aime");
	bool flag2 = true;

	cout << "cpe1 = " << cpe1 << endl; // 输出cpe1 = 3
	cout << "cpe2 = " << cpe2 << endl; // 输出cpe2 = 4
	cout << "cpe3 = " << cpe3 << endl; // 输出cpe3 = 5
	cout << "flag1 = " << "[bool('je'>='t'aime')]" << endl; // 输出flag1 = 0
	cout << "flag2 = " << boolalpha<<flag2 << endl; // 输出flag2 = 1

	cout << " cpe1 == cpe2 ?: " << boolalpha << bool(cpe1 == cpe2) << endl;
	cout << " cpe1 != cpe2 ?: " << boolalpha << bool(cpe1 != cpe2) << endl;
	cout << " cpe1 > cpe2 ?: " << boolalpha << bool(cpe1 > cpe2) << endl;
	cout << " cpe1 < cpe2 ?: " << boolalpha << bool(cpe1 < cpe2) << endl;
	cout << " cpe1 >= cpe2 ?: " << boolalpha << bool(cpe1 >= cpe2) << endl;
	cout << " cpe1 <= cpe2 ?: " << boolalpha << bool(cpe1 <= cpe2) << endl;

	cout << " cpe1!=cpe2 || cpe3<cpe2 ?: " << boolalpha << (cpe1 != cpe2 || cpe3 < cpe2) << endl;
	cout << " cpe1!=cpe2 && cpe3<cpe2 ?: " << boolalpha << (cpe1 != cpe2 && cpe3 < cpe2) << endl;
	cout << " flag1:je>=t'aime ?: "<<boolalpha<< flag1 << endl;
	cout << " !flag2: " << boolalpha << !flag2 << endl;

	double a1;
	double a2;
	double a3;
	
	cout << "input three positive numbers: " << endl;
	cin >> a1 >> a2 >> a3;
	if (a1 > 0 && a2 > 0 && a3 > 0)
	{
		if (a1 + a2 <= a3 || a2 + a3 <= a1 || a3 + a1 <= a2) 
		{
			cout << "not a triangle" << endl;
		}
		else
		{
			cout << "a triangle" << endl;
		}
	}
	else 
	{ 
		cout << "nagetives are not accepted!" << endl;
    }
cpe1 = 1
cpe2 = 2
cpe3 = 3
print(f' cpe1: {cpe1} cpe2: {cpe2} cpe3: {cpe3}')
print(' cpe1==cpe2 ?: ',cpe1==cpe2)
print(' cpe1!=cpe2 ?: ',cpe1!=cpe2)
print(' cpe1>=cpe2 ?: ',cpe1>=cpe2)      
print(' cpe1<=cpe2 ?: ',cpe1<=cpe2)      
print(' cpe1>cpe2 ?: ',cpe1>cpe2)      
print(' cpe1<cpe2 ?: ',cpe1<cpe2)       
print()
print(' cpe1 < cpe3 and cpe2 == cpe3 ?: ', (cpe1 < cpe3) and (cpe2 == cpe3))
print(' cpe1<cpe3 or cpe2==cpe3 ?: ', (cpe1<cpe3) or (cpe2==cpe3)) 
print(' not True ?: ',not True) 
print(' True and False or True or False ?:', True and False or True or False )
print(' (True and False) or (True or False) ?:', (True and False) or (True or False) )
print(' True and (False or True) or False ?:', True and (False or True) or False )
print(' comparision between strings "me" > "joker" ?:','me'>'joker')
print()
cpe1 = 'a'
cpe2 = 'b'
cpe3 = 'c'
print(f' cpe1: {cpe1} cpe2: {cpe2} cpe3: {cpe3}')
print(' cpe1==cpe2 ?: ',cpe1==cpe2)
print(' cpe1!=cpe2 ?: ',cpe1!=cpe2)
print(' cpe1>=cpe2 ?: ',cpe1>=cpe2)      
print(' cpe1<=cpe2 ?: ',cpe1<=cpe2)      
print(' cpe1>cpe2 ?: ',cpe1>cpe2)      
print(' cpe1<cpe2 ?: ',cpe1<cpe2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值