当前位置:网站首页>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;} 边栏推荐
猜你喜欢

Oracle安装完毕(系统盘),从系统盘转移到数据盘

nacos应用

信创建设看广州|海泰方圆亮相2022 信创生态融合发展论坛

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

Yahoo! Answers-数据集

An动画优化之遮罩层动画

In order to counteract the drop in sales and explore the low-end market, Weilai's new brand products are priced as low as 100,000?

Apache APISIX 2.15 版本发布,为插件增加更多灵活性

An工具介绍之3D工具

shell编程之条件语句
随机推荐
[Verilog] HDLBits Problem Solution - Verification: Writing Testbenches
YOLOv5 training data prompts No labels found, with_suffix is used, WARNING: Ignoring corrupted image and/or label appears during yolov5 training
Feature dimensionality reduction study notes (pca and lda) (1)
【精品必知】Pod生命周期
php microtime 封装工具类,计算接口运行时间(打断点)
The Yangtze river commercial Banks to the interview
想学自动化测试网课哪个好?过了人告诉你:适合自己的才是最重要
图像融合SDDGAN文章学习
论文理解:“Gradient-enhanced physics-informed neural networks for forwardand inverse PDE problems“
基于php网上零食商店管理系统获取(php毕业设计)
【R】用grafify搞定统计绘图、方差分析、干预比较等!
__unaligned修饰指针
【Verilog】HDLBits题解——Circuits/Sequential Logic/Latches and Flip-Flops
类型转换、常用运算符
Image fusion GAN-FM study notes
【实战技能】单片机bootloader的CANFD,I2C,SPI和串口方式更新APP视频教程(2022-08-01)
浅谈程序员的职业操守
Random forest project combat - temperature prediction
Redis连接池工具类
Oracle is installed (system disk) and transferred from the system disk to the data disk

