Integer and fractional part of the real split Problem 8-1

This problem required to achieve a simple integer function of the fractional part of a real number split.

void splitfloat( float x, int *intpart, float *fracpart );

Where x is a real number of split (0≤x <10000), intpart and fracpart are split out of the real number x and an integer portion of the fractional part.
Referee test program Example:

#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
Output Sample:
of The IS Integer Part 2
of The fractional IS 0.718 Part
answer codes

void splitfloat( float x, int *intpart, float *fracpart ){   
 *intpart = (int)x;   //取出整数部分
  *fracpart = x- (*intpart); //取出小数部分
   }

*

Personal appreciated: *
taking the integer value of the entire portion of the direct selection of shaping, thereby directly removing the fractional part.
And removed prior to the fractional portion can directly use the integer part of a single precision x * intpart subtracted directly shaping the fractional part can be directly derived.

Published an original article · won praise 0 · Views 21

Guess you like

Origin blog.csdn.net/weixin_46396354/article/details/104482719