在C语言中,我们学习过:
算数运算符(+、-、*、/、%)
自增自减运算符(++、--)
赋值运算符(=)
关系运算符(>、< 、>=、<=、!=、==)
逻辑运算符(&&、||、!)
等等
由于知识语法基本没有发生变化,本部分内容我们将重点用实际程序演示的方式进行
例如拆分位数,我们可以C++如下实现:
#include<iostream>
using namespace std;
int main()
{
int a;//待判断的这个三位数
int ge;//三位数中的个位
int shi;//三位数中的十位
int bai;//三位数中的百位
cin>>a;
ge = a%10;
shi = a%100/10;
bai = a/100;
cout<<ge<<" "<<shi<<" "<<bai<<endl;
return 0;
}
同样的,分段函数求值,我们依旧用C++完成:
#include<iostream>
using namespace std;
int main()
{
int x,y;
cin>>x;
if(x<1)
{
y=x;
}
else if(1<=x && x<10)
{
y=2*x-1;
}
else
{
y=3*x-11;
}
cout<<y<<endl;
return 0;
}运行结果如下:

大家依旧注意逻辑运算符&&的使用场景,千万不要出现类似1<=x<10的连写写法!
好,请大家自行实操敲代码
028-61775817 蜀ICP备2021001672号