当前位置:网站首页>Real number rounding and writing to file (C language file)
Real number rounding and writing to file (C language file)
2022-08-03 13:03:00 【Four Seasons】
Title requirements: There are several real numbers in the file f1.txt, please read them out separately, and save each real number into the file f2.txt after rounding up.Try to write the corresponding program.
Thinking:
- Open file f1,
- Read in real numbers,
- Split the integer and fractional parts of a real number,
- The fractional part is compared to 0.5,
- The integer part is adjusted accordingly and written to file f2.
File content after running:
![]() | |
f1.txt (enter by yourself) | f2.txt (system generated after running) |
Source program:
#include#includevoid splitfloat(double x,int *intpart,double *fracpart){*intpart=(int)x;//Force cast to integer*fracpart=x-*intpart;//Subtract the integer part directly to get the fractional part}int main(void){FILE *fp1,*fp2;int intpart;//x=345.89, the fractional part is 0.890015double z,fracpart,a;//x=123.456, the fractional part will be output as 0.456001if((fp1=fopen("f1.txt","r"))==NULL){printf("Failed to open file f1!");exit(0);}if((fp2=fopen("f2.txt","w"))==NULL){printf("Failed to open file f2!");exit(0);}while(!feof(fp1)){fscanf(fp1,"%lf",&z);//Readif(z!=EOF){splitfloat(z,&intpart,&fracpart);//Split integer and fractional parts// printf("Integer part: %d\n Fractional part: %lf\n",intpart,fracpart);//Help checkif(fracpart>=0.5){//The fractional part is rounded upintpart=intpart+1;// printf("Integer part: %d\n Fractional part: %lf\n",intpart,fracpart);//Help check}fprintf(fp2,"%d\n",intpart);}}return 0;}
The original title of splitting the integer and fractional parts of a real number:
Requires a custom function void splitfloat ( float x , int * intpart , float * fracpart ), where x is the real number to be split, * intpart and * fracpart are the integer part and the fractional part of the real number x split respectivelypart.Write the main function and call the function splitfloat() in it.
Source program:
#include//Exercise 8.1 Split the integer and fractional parts of real numbersvoid splitfloat(float x,int *intpart,float *fracpart){*intpart=(int)x;//Force cast to integer*fracpart=x-*intpart;//Subtract the integer part directly to get the fractional part}main(void){float x,fracpart;//x=123.456, the fractional part will be output as 0.456001int intpart;//x=345.89, the fractional part is 0.890015printf("Enter x:");scanf("%f",&x);splitfloat(x,&intpart,&fracpart);printf("Integer part: %d\nDecimal part: %lf",intpart,fracpart);return 0;}
边栏推荐
- An工具介绍之形状工具及渐变变形工具
- Secure Custom Web Application Login
- [Verilog] HDLBits Problem Solution - Verification: Writing Testbenches
- 一些测试相关知识
- 使用工作队列管理器(四)
- 一次内存泄露排查小结
- How can I get a city's year-round weather data for free?Precipitation, temperature, humidity, solar radiation, etc.
- 第4章 搭建网络库&Room缓存框架
- [数据仓库]分层概念,ODS,DM,DWD,DWS,DIM的概念「建议收藏」
- 从零开始C语言精讲篇5:指针
猜你喜欢
YOLOv5训练数据提示No labels found、with_suffix使用、yolov5训练时出现WARNING: Ignoring corrupted image and/or label
第5章 实现首页Tab数据展示
(through page) ali time to upload the jar
Station B responded that "HR said that core users are all Loser": the interviewer was persuaded to quit at the end of last year and will learn lessons to strengthen management
【R】用grafify搞定统计绘图、方差分析、干预比较等!
Key points for account opening of futures companies
PolarFormer: Multi-camera 3D Object Detection with Polar Transformers 论文笔记
Image fusion SDDGAN article learning
shell编程之条件语句
How to build an overseas purchasing system/purchasing website - source code analysis
随机推荐
Five, the function calls
超多精美礼品等你来拿!2022年中国混沌工程调查启动
Kubernetes 网络入门
Feature Engineering Study Notes
SQL分页查询_Sql根据某个字段分页
论文理解:“Gradient-enhanced physics-informed neural networks for forwardand inverse PDE problems“
浅谈低代码平台远程组件加载方案
Oracle is installed (system disk) and transferred from the system disk to the data disk
How can I get a city's year-round weather data for free?Precipitation, temperature, humidity, solar radiation, etc.
数据库系统原理与应用教程(076)—— MySQL 练习题:操作题 160-167(二十):综合练习
Win11怎么禁止软件后台运行?Win11系统禁止应用在后台运行的方法
云计算服务主要安全风险及应对措施初探
项目概述、推送和存储平台准备
【Verilog】HDLBits题解——验证:阅读模拟
【必读要点】Pod控制器Deployment更新、回退详解
实数取整写入文件(C语言文件篇)
shell编程条件语句
The Yangtze river commercial Banks to the interview
如何免费获得一个市全年的气象数据?降雨量气温湿度太阳辐射等等数据
为什么越来越多的开发者放弃使用Postman,而选择Eolink?