当然,你输入的值超出了double所能精确表达的范围,结果当然是有误差的。
注意double的精度。

九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。 -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。 -毛泽东
本文获得 ![]() ![]() | |
![]() |
发表于 2004-5-2 12:43:38 [200分] ![]() ![]() ![]() ![]() ![]()
------------------------签----名---- /*热心斑竹*/ E_mail:manesking@163.com MSN:sunxiangpost@hotmail.com |
![]() |
第1楼 回复于2004-5-2 12:48:43 ![]() ![]() ![]() |
![]() |
第2楼 回复于2004-5-2 14:06:45 ![]() ![]() ![]() |
![]() |
第3楼 回复于2004-5-2 14:48:25 ![]() ![]() ![]() |
![]() |
第4楼 回复于2004-5-2 18:09:32 ![]() ![]() ![]() |
![]() |
第5楼 回复于2004-5-2 20:20:38 ![]() ![]() ![]() |
![]() |
第6楼 回复于2004-5-3 7:29:29 ![]() ![]() ![]() |
![]() |
第7楼 回复于2004-5-3 10:43:58 ![]() ![]() ![]() |
![]() |
第8楼 回复于2004-5-3 16:52:05 ![]() ![]() ![]() |
[此贴子已经被作者于2004-08-19 10:58:42编辑过]
这个方法乌鸦已经提了,不过怎么把“浮点数 => 字符串”
这里也麻烦啊!
乌鸦的方法是输入字符串,然后判断~~这点被你用 不符合大工程 顶回去了,所以我根本没必要再说. 还有 浮点数转成字符串只用一个函数就可以了,没有必要像他们一样写得这个一大堆.
函数: char *gcvt(double value, int ndec, char *buf) 各个参数的意义:value是你想要转换的浮点数; ndec 是转换的有效数字位数; *buf 是转换后的字符串. 举个例子:
main() { char str[25]; double num;
num=9.876; gcvt(num,4,str);
printf("String= %s\n",str);
getch(); }
看清楚了吗? 浮点转换成字符串之后我想你应该知道该做些什么了吧~~
乌鸦的方法是输入字符串,然后判断~~这点被你用 不符合大工程 顶回去了,所以我根本没必要再说. 还有 浮点数转成字符串只用一个函数就可以了,没有必要像他们一样写得这个一大堆.
函数: char *gcvt(double value, int ndec, char *buf) 各个参数的意义:value是你想要转换的浮点数; ndec 是转换的有效数字位数; *buf 是转换后的字符串. 举个例子:
main() { char str[25]; double num;
num=9.876; gcvt(num,4,str);
printf("String= %s\n",str);
getch(); }
看清楚了吗? 浮点转换成字符串之后我想你应该知道该做些什么了吧~~
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <dos.h> void main() { double num; long a1=0,a2=0; char str[50]; FILE *fp; clrscr(); if((fp=fopen("e:\\bc31\\num.txt","wb"))==NULL) { printf("ERROR"); exit(0); } scanf("%lf",&num);//先把数据以双精度浮点数存入文件! fprintf(fp,"%lf",num); fclose(fp); if((fp=fopen("e:\\bc31\\num.txt","rb"))==NULL) { printf("ERROR!"); exit(0); } rewind(fp); fscanf(fp,"%50s",str); sscanf(str,"%ld.%ld",&a1,&a2);//或去掉上一句直接写成fscanf(fp,"%ld.%ld",&a1,&a2);也可以! printf("%s*%ld.%ld\n",str,a1,a2); while(1) {//因为后面多出的数肯定都是零,所以余10不为0,就是小数部分。 if(a2%10==0) a2=a2/10; else break; } printf("The result is:%ld\n",a2); fclose(fp); getche(); }
[此贴子已经被作者于2004-08-19 16:53:46编辑过]
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <dos.h> void main() { double num; long a1=0,a2=0; char str[50]; FILE *fp; clrscr(); if((fp=fopen("e:\\bc31\\num.txt","wb"))==NULL) { printf("ERROR"); exit(0); } scanf("%lf",&num);//先把数据以双精度浮点数存入文件! fprintf(fp,"%lf",num); fclose(fp); if((fp=fopen("e:\\bc31\\num.txt","rb"))==NULL) { printf("ERROR!"); exit(0); } rewind(fp); fscanf(fp,"%50s",str); sscanf(str,"%ld.%ld",&a1,&a2);//或去掉上一句直接写成fscanf(fp,"%ld.%ld",&a1,&a2);也可以! printf("%s*%ld.%ld\n",str,a1,a2); while(1) {//因为后面多出的数肯定都是零,所以余10不为0,就是小数部分。 if(a2%10==0) a2=a2/10; else break; } printf("The result is:%ld\n",a2); fclose(fp); getche(); }
我晕~~~你的代码好像太那个了吧~~~杀鸡焉用恐龙刀!再说,效率不高~~~