实验8-1-4 拆分实数的整数与小数部分

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

函数接口定义:

c 复制代码
void splitfloat( float x, int *intpart, float *fracpart );

其中x是被拆分的实数(0≤x<10000),intpart和 fracpart分别是将实数x拆分出来的整数部分与小数部分。
输入样例:

c 复制代码
2.718

输出样例:

c 复制代码
The integer part is 2
The fractional part is 0.718

程序:

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#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;
}

void splitfloat(float x, int* intpart, float* fracpart)
{
    *intpart = (int)x;
    while (x > 1)
    {
        x--;
    }
    *fracpart = x;
}

效果如下:

相关推荐
Zwawa7 小时前
串口为什么会丢字节:从轮询到中断和环形缓冲区
c语言·单片机·嵌入式
揽星河..10 小时前
【C语言】分支和循环
c语言·c++·算法
揽星河..12 小时前
【C语言】猜数字游戏
c语言·算法·游戏
听风说ml1 天前
socket编程 【C语言】
c语言·udp·tcp·socket编程
学逆向的1 天前
C 局部静态变量
c语言·开发语言·网络安全
Zwawa1 天前
从 printf 到曲线:把数据送上 VOFA+
c语言·单片机·嵌入式
二十七日秋2 天前
算法刷题笔记「Day 1|四道 OJ 入门题里的深坑」
c语言·汇编·数据结构·c++·笔记·python·算法
abc light2 天前
ARM 裸机开发(1)——ARM 体系结构
c语言·arm开发
Navigator_Z2 天前
LeetCode //C - 1309. Decrypt String from Alphabet to Integer Mapping
c语言·算法·leetcode
爱聊灵光的维C2 天前
C语言初阶:关于函数设计的详细讲解和注意事项
c语言·开发语言·青少年编程·蓝桥杯·学习方法