当前位置:网站首页>实数取整写入文件(C语言文件篇)
实数取整写入文件(C语言文件篇)
2022-08-03 12:28:00 【一沐四季】
题目要求:文件f1.txt中有若干个实数,请分别读出,将每个实数按四舍五入取整后存入存入文件f2.txt中。试编写相应程序。
思路:
- 打开文件f1,
- 读入实数,
- 将实数的整数部分和小数部分拆开,
- 小数部分与0.5相比较,
- 整数部分作出对应调整并写入文件f2。
运行后文件内容:
| ![]() |
| f1.txt(自行输入) | f2.txt(运行后系统生成) |
源程序:
#include<stdio.h>
#include<stdlib.h>
void splitfloat(double x,int *intpart,double *fracpart)
{
*intpart=(int)x;//强制转换为整型
*fracpart=x-*intpart;//直接减去整数部分可得小数部分
}
int main(void)
{
FILE *fp1,*fp2;
int intpart;//x=345.89,小数部分为0.890015
double z,fracpart,a;//x=123.456,小数部分会输出为0.456001
if((fp1=fopen("f1.txt","r"))==NULL){
printf("文件f1打开失败!");
exit(0);
}
if((fp2=fopen("f2.txt","w"))==NULL){
printf("文件f2打开失败!");
exit(0);
}
while(!feof(fp1)){
fscanf(fp1,"%lf",&z);//读取
if(z!=EOF){
splitfloat(z,&intpart,&fracpart);//拆分整数和小数部分
// printf("整数部分:%d\n小数部分:%lf\n",intpart,fracpart);//帮助检验
if(fracpart>=0.5){//小数部分 四舍五入
intpart=intpart+1;
// printf("整数部分:%d\n小数部分:%lf\n",intpart,fracpart);//帮助检验
}
fprintf(fp2,"%d\n",intpart);
}
}
return 0;
}其中拆分实数的整数与小数部分原题目:
要求自定义一个函数 void splitfloat ( float x , int * intpart , float * fracpart ),其中 x 是被拆分的实数,* intpart 和* fracpart 分别是将实数 x 拆分出来的整数部分与小数部分。编写主函数,并在其中调用函数 splitfloat ()。
源程序:
#include<stdio.h>//习题8.1 拆分实数的整数与小数部分
void splitfloat(float x,int *intpart,float *fracpart)
{
*intpart=(int)x;//强制转换为整型
*fracpart=x-*intpart;//直接减去整数部分可得小数部分
}
main(void)
{
float x,fracpart;//x=123.456,小数部分会输出为0.456001
int intpart;//x=345.89,小数部分为0.890015
printf("Enter x:");
scanf("%f",&x);
splitfloat(x,&intpart,&fracpart);
printf("整数部分:%d\n小数部分:%lf",intpart,fracpart);
return 0;
}
边栏推荐
- Blog records life
- 15. PARTITIONS「建议收藏」
- 如图,想批量读取mysql,批量处理,有哪个地方参数需要改变呢?
- 图像融合SDDGAN文章学习
- 数据库系统原理与应用教程(073)—— MySQL 练习题:操作题 131-140(十七):综合练习
- Feature dimensionality reduction study notes (pca and lda) (1)
- 用C语言解决A+B问题,A-B问题,A*B问题
- R language ggplot2 visualization: use the patchwork bag plot_layout function will be more visual image together, ncol parameter specifies the number of rows, specify byrow parameters configuration dia
- 想学自动化测试网课哪个好?过了人告诉你:适合自己的才是最重要
- 【Verilog】HDLBits题解——验证:阅读模拟
猜你喜欢

链游NFT元宇宙游戏系统开发技术方案及源码

The common problems in the futures account summary

ROS中编译通过但是遇到可执行文件找不到的问题

setTimeout 、setInterval、requestAnimationFrame

《数字经济全景白皮书》金融数字用户篇 重磅发布!

浅谈程序员的职业操守

PolarFormer: Multi-camera 3D Object Detection with Polar Transformers 论文笔记

Random forest project combat - temperature prediction

基于php校园医院门诊管理系统获取(php毕业设计)

分享一款实用的太阳能充电电路(室内光照可用)
随机推荐
Blazor Server(6) from scratch--policy-based permission verification
word标尺有哪些作用
如图,想批量读取mysql,批量处理,有哪个地方参数需要改变呢?
Random forest project combat - temperature prediction
想学自动化测试网课哪个好?过了人告诉你:适合自己的才是最重要
7月份最后一篇博客
Five super handy phone open-source automation tools, which is suitable for you?
"Digital Economy Panorama White Paper" Financial Digital User Chapter released!
From scratch Blazor Server (6) - authentication based on strategy
fastposter v2.9.0 程序员必备海报生成器
[Verilog] HDLBits Problem Solution - Verification: Writing Testbenches
R语言ggplot2可视化:使用patchwork包的plot_layout函数将多个可视化图像组合起来,ncol参数指定行的个数、byrow参数指定按照行顺序排布图
Grafana 高可用部署最佳实践
安全自定义 Web 应用程序登录
How can I get a city's year-round weather data for free?Precipitation, temperature, humidity, solar radiation, etc.
图像融合SDDGAN文章学习
论文理解:“Gradient-enhanced physics-informed neural networks for forwardand inverse PDE problems“
信创建设看广州|海泰方圆亮相2022 信创生态融合发展论坛
图像融合GAN-FM学习笔记
The common problems in the futures account summary

