美文网首页
【MAC 上学习 C++】Day 49-2. 实验8-1-3 拆

【MAC 上学习 C++】Day 49-2. 实验8-1-3 拆

作者: RaRasa | 来源:发表于2019-10-12 20:30 被阅读0次

实验8-1-3 拆分实数的整数与小数部分 (15 分)

1. 题目摘自

https://pintia.cn/problem-sets/13/problems/547

2. 题目内容

本题要求实现一个拆分实数的整数与小数部分的简单函数。

函数接口定义:

void splitfloat( float x, int intpart, float fracpart );
其中x是被拆分的实数(0≤x<10000),
intpart和
fracpart分别是将实数x拆分出来的整数部分与小数部分。

输入样例:

2.718

输出样例:

The integer part is 2
The fractional part is 0.718

3. 源码参考
#include <iostream>

using namespace std;

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

int main()
{
    float x, fracpart;
    int intpart;

    cin >> x;
    splitfloat(x, &intpart, &fracpart);
    cout << "The integer part is " << intpart << endl;
    cout << "The fractional part is " << fracpart << endl;

    return 0;
}

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

  return;
}

相关文章

网友评论

      本文标题:【MAC 上学习 C++】Day 49-2. 实验8-1-3 拆

      本文链接:https://www.haomeiwen.com/subject/mcsgectx.html