参考解答:
#include<iostream>
using namespace std;
const double x1=0.3, x2=0.8; //定义为全局常变量,方便,易维护
double f(double); //此处加入对自定义函数f的声明
int main( )
{
double x;
cout<<"请输入x的值:";
cin>>x;
cout<<"解得:f("<<x<<")="<<f(x)<<"\n";
return 0;
}
//下面是自定义函数的定义部分
//当x<0.3时,f(x)=0; 当0.3≤x≤0.8时,f(x)=(x-0.3)/(0.8-0.3);当x>0.8时,f(x)=1;
double f(double x)
{
double result;
if(x<x1)
result=0;
else if (x>x2)
result=1;
else
result = (x-x1)/(x2-x1);
return result; //用于返回函数值
}