This question requires the realization of a simple function that splits the integer and decimal parts of a real number.
Function interface definition:
void splitfloat( float x, int *intpart, float *fracpart );
Among them x
are the real number to be split (0≤ x
<10000), *intpart
and *fracpart
are the integer part and the decimal part of the real number x , respectively.
Sample referee test procedure:
#include <stdio.h>
void splitfloat( float x, int *intpart, float *fracpart );
int main()
{
float x, fracpart;
int intpart;
scanf("%f", &x);
splitfloat(x, &intpart, &fracpart);
printf("The integer part is %d\n", intpart);
printf("The fractional part is %g\n", fracpart);
return 0;
}
/* 你的代码将被嵌在这里 */
Input sample:
2.718
Sample output:
The integer part is 2
The fractional part is 0.718
answer:
void splitfloat( float x, int *intpart, float *fracpart )
{
*intpart = (int) x; //double强转int会把后面的后面小数点的数直接舍去(大转小 数会变小) int 2.718 结果是2
*fracpart = x - (int)x;
}