#include<fstream>
#include<iostream>
#include<cstdlib>
using namespace std;
struct fnum{
char a[1001];
int len;
};
void ff(fnum *f1,fnum *f2)
{
fnum f3;
int len,jw=0;
len=f1->len>f2->len?f1->len:f2->len;
f3.len=len;
for(int i=0;i<len;i++){
f3.a[i]=f1->a[i]-'0'+f2->a[i]-'0'+jw;
jw=f3.a[i]/10;
f3.a[i]=f3.a[i]%10+'0';
}
if(jw==1){
f3.a[i]='1';
f3.len++;}
for(i=0;i<f3.len;i++){
f1->a[i]=f2->a[i];f1->len=f2->len;
f2->a[i]=f3.a[i];f2->len=f3.len;
}
}
int main()
{
fnum f1,f2;
int i,num;
for(i=1;i<1001;i++){
f1.a[i]='0';
f2.a[i]='0';
}
f1.a[0]='1';
f2.a[0]='1';
f1.len=1;f2.len=1;
ifstream inf("in.txt",ios::in);
if(!inf){
cerr<<"can't open"<<endl;
exit(1);}
ofstream out("out.txt",ios::out);
if(!out){
cerr<<"can't open"<<endl;
exit(1);}
while(!inf.eof()){
inf>>num;
while(num!=2){
ff(&f1,&f2);
num--;}
for(i=f2.len-1;i>=0;i--){
out<<f2.a[i];}
out<<endl;
}
return 0;
}
小弟刚学C++,正想学文件输入输出,编了一Fibonacci Numbers的高位数的求解,我在("in.txt")输入的两个数
15和120;但("out.txt")得到三个数,,
610
2791715456571051233611642553
2791715456571051233611642553
有一个重了,但小弟不知道是,咋的。。是不是我的文件输入输出用错了?还是。。。。?请大侠指点一下,,谢谢!!!