scanf函数无法正确读取双精度数

时间:2015-12-20 16:10:05

标签: floating-point double scanf dev-c++

在我的作业中,scanf函数存在一些问题:

/* Can Doğu */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* My Function prototypes */
double calculate_profile_area(int edgecount,double edgelength); // Has two parameters: edgecount & edgelength. Returns double type value.
double calculate_volume(double a,double h); // Has two parameters: profilearea & boxheight. Returns double type value.
double calculate_price(double p); // Has one parameter: volume. Returns double type value.

/* My main function */
int main(void)
{

/* Variables & input-output part */
int count; double length,height,area,volume,price;
area=0.0,volume=0.0,price=0.0;
printf("Enter edge count for box profile: ");
scanf("%d",&count);
printf("Enter the profile-edge length and box-height: ");
scanf("%lf%lf",&length,&height);

/* Calling my functions */
area=calculate_profile_area(count,length);
volume=calculate_volume(area,height);
price=calculate_price(volume);

/* Printing the results */
printf("Box profile area is %.2lf cm square.\n",area);
printf("Box volume is %.2lf cm cube.\n",volume);
printf("Total sugar price is %.2lf TL.\n",price);
system("pause");
return(0);
}

/* calculate_profile_area function */
double calculate_profile_area(int edgecount,double edgelength)
{
double profilearea=0.0;
profilearea=edgecount*((edgelength*edgelength)/(4*tan(M_PI/edgecount)));
return(profilearea);
}

/* calculate_volume function */
double calculate_volume(double a,double h)
{
double v=0.0;
v=a*h;
return(v);
}

/* calculate_price function */
double calculate_price(double p)
{
double cost=0.0;
if(p>=0 && p<=500)
    cost=p*120;
else if(p>=501 && p<=1000)
    cost=p*100;
else if(p>=1001 && p<=2500)
    cost=p*80;
else if(p>=2501 && p<=5000)
    cost=p*65;
else if(p>=5001)
    cost=p*50;
return(cost);
}

当我输入一个double值(2.15)时,我的编译器将其读取为2.14999999。 因此,我的地区&#39;结果计算为54.59(应为54.60)。知道为什么我的结果不正确吗?

0 个答案:

没有答案