当前位置:网站首页>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;} 边栏推荐
- 通过点击CheckBox实现背景变换小案例
- How can I get a city's year-round weather data for free?Precipitation, temperature, humidity, solar radiation, etc.
- 安防监控必备的基础知识「建议收藏」
- Win11怎么禁止软件后台运行?Win11系统禁止应用在后台运行的方法
- An动画优化之补间形状与传统补间的优化
- Image fusion GAN-FM study notes
- 【实战技能】单片机bootloader的CANFD,I2C,SPI和串口方式更新APP视频教程(2022-08-01)
- 别再用if-else了,分享一下我使用“策略模式”的项目经验...
- Random forest project combat - temperature prediction
- shell编程之条件语句
猜你喜欢

长城简漫·暑期安全篇⑤ 这个强,不能逞

shell编程条件语句

YOLOv5训练数据提示No labels found、with_suffix使用、yolov5训练时出现WARNING: Ignoring corrupted image and/or label

An动画优化之传统引导层动画

leetcode 11. 盛最多水的容器

The common problems in the futures account summary

Image fusion SDDGAN article learning

业界新标杆!阿里开源自研高并发编程核心笔记(2022最新版)

海外代购系统/代购网站怎么搭建——源码解析

Oracle is installed (system disk) and transferred from the system disk to the data disk
随机推荐
2022 年 CISO 最关心的是什么?
Mysql重启后innodb和myisam插入的主键id变化总结
层次分析法
899. 有序队列
易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
随机森林项目实战---气温预测
622. 设计循环队列
Yahoo! Answers-数据集
Filebeat 如何保持文件状态?
R语言ggplot2可视化:使用ggpubr包的ggsummarystats函数可视化箱图(通过ggfunc参数设置)、在可视化图像的下方添加描述性统计结果表格
【Verilog】HDLBits题解——Verification: Reading Simulations
想学自动化测试网课哪个好?过了人告诉你:适合自己的才是最重要
链游NFT元宇宙游戏系统开发技术方案及源码
使用工作队列管理器(四)
Jmeter使用
第4章 搭建网络库&Room缓存框架
云计算服务主要安全风险及应对措施初探
The common problems in the futures account summary
Sogou news-数据集
Kubernetes 网络入门

