说真的今天的内容不算难。
通用的也比较简单:== ; > ; < ; >= ; <= ; != 。而特殊之处是在于二者的逻辑符号,对应下来是:&& || !和 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)