c++分段函数实现

来源:百度知道 编辑:UC知道 时间:2024/05/17 08:10:34
#include "stdafx.h"
#include
#include <iostream>
#include <iomanip>
using namespace std;

double countPAR(double P,int gn1)
{
const int NI=2000;
if(0<gn1<=400)
P=gn1/NI+0.3;
else
{
if(400<gn1<800)
P=gn1/NI+0.2;
else
{
if(800<=gn1<=1200)
P=gn1/NI+0.1;
else
{
if(1200<gn1<1600)
P=gn1/NI;
else
P=gn1/NI-0.1;

}

}
}
return P;
}
int main()
{
int gn;
double PAR=0;
cout<<"please enter gn:";
cin>>gn;
countPAR(PAR,gn);
cout<<PAR;
return 0;
}
这个函数不知道什么地方有错,输出就是不正确

我看了下
我的c++没有库文件'stdafx.h'
c:\hhjf\kj.cpp(1) : fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory
不知道你的是不是这样

//#include <stdafx.h>
#include <iostream>
#include <iomanip>
using namespace std;

double countPAR(double P,int gn1)
{
const int NI=2000;
if(0<gn1<=400)
P=gn1/NI+0.3;
else
{
if(400<gn1<800)
P=gn1/NI+0.2;
else
{
if(800<=gn1<=1200)
P=gn1/NI+0.1;
else
{
if(1200<gn1<1600)
P=gn1/NI;
else
P=gn1/NI-0.1;

}

}
}
return P;
}
int main()
{
int gn;
double PAR=0;
cout<<"please enter gn:";
cin>>gn;
countPAR(PAR,gn);
cout<<PAR;
return 0;
}

修改好了,在C++里面判断400<gn1<800,不能用