要求键盘输入一个数,经分段函数计算,输出计算结果。
分段函数为:y= 2x+20(x>0)
0(x=0)
3x(x<0)
#include<iostream>
using namespace std;
int main()
{
double x,y;
cout<<"请输入一个数值:";
cin>>x;
cout<<"经分段函数计算后可得:";
if(x==0)
y=0;
else if(x>0)
y=2*x+20;
else if(x<0)
y=3*x;
cout<<y<<endl;
return 0;
}