当前位置:网站首页>实数取整写入文件(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;
}
边栏推荐
- (通过页面)阿里云云效上传jar
- 肝完Alibaba这份面试通关宝典,我成功拿下今年第15个Offer
- mysql advanced (twenty-four) method summary of defense against SQL injection
- 第3章 搭建短视频App基础架构
- 五、函数的调用过程
- Five super handy phone open-source automation tools, which is suitable for you?
- 字节最爱问的智力题,你会几道?
- 利用ChangeStream实现Amazon DocumentDB表级别容灾复制
- Mysql重启后innodb和myisam插入的主键id变化总结
- 长城简漫·暑期安全篇⑤ 这个强,不能逞
猜你喜欢

Unsupervised learning KMeans notes and examples

【精品必知】Pod生命周期

899. 有序队列

After completing the interview and clearance collection of Alibaba, I successfully won the 15th Offer this year

特征降维学习笔记(pca和lda)(1)

无监督学习KMeans学习笔记和实例

net start mysql 启动报错:发生系统错误5。拒绝访问。

(通过页面)阿里云云效上传jar

随机森林项目实战---气温预测

From the physical level of the device to the circuit level
随机推荐
分享一款实用的太阳能充电电路(室内光照可用)
R语言ggplot2可视化:使用patchwork包的plot_layout函数将多个可视化图像组合起来,ncol参数指定行的个数、byrow参数指定按照行顺序排布图
fastposter v2.9.0 程序员必备海报生成器
Five, the function calls
LyScript implements memory stack scanning
Feature dimensionality reduction study notes (pca and lda) (1)
基于php家具销售管理系统获取(php毕业设计)
Free Internet fax platform fax _ don't show number
使用工作队列管理器(三)
基于php旅游网站管理系统获取(php毕业设计)
7月份最后一篇博客
Unsupervised learning KMeans notes and examples
php microtime encapsulates the tool class, calculates the running time of the interface (breakpoint)
flink流批一体有啥条件,数据源是从mysql批量分片读取,为啥设置成批量模式就不行
免费的网络传真平台_发传真不显示发送号码
数据库系统原理与应用教程(075)—— MySQL 练习题:操作题 151-159(十九):综合练习
The new interface, jingdong comment interface
Mysql重启后innodb和myisam插入的主键id变化总结
秋招招工作
awk入门教程

