【发布时间】:2012-11-08 13:40:46
【问题描述】:
我的代码有问题。有一个斐波那契函数,我希望你知道它是做什么的。还有两个文件:In0201.txt 和 Out0201.txt。同样,程序应该从文件“In0201.txt”中获取值并将结果写入 Out0201.txt。
正在写入一些值,但不是写入数字序列(到文件),而是写入一个值,就像它是序列中所有这些数字的总和。有人知道为什么会这样吗?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Fibonacci
long double fib(int n) {
if(n == 0)
{
return 0;
}
if(n == 1)
{
return 1;
}
return fib(n-1) + fib(n-2);
}
int main()
{
int a;
int tmp;
ifstream inputFile("In0201.txt");
if (inputFile.is_open()) {
inputFile >> a;
cout << "Loaded the value 'n' from file: " << endl;
cout << a << " " << endl;
inputFile.close();
}
ofstream outputFile("Out0201.txt");
if (outputFile.is_open()) {
tmp = fib(a);
cout << "Fibonacci's sequence number: " << tmp << endl;
outputFile << tmp << ", ";
outputFile.close();
}
return 0;
}
【问题讨论】:
-
但是帖子中的代码,没有包含指向它的链接。
-
举例说明你的输入、实际输出和预期输出。从问题中不清楚问题是什么。
-
我不明白。你告诉它在文件中写入一个数字,你不知道它为什么在其中写入一个数字?
-
在这里工作,有什么问题?
-
问题是输出文件中没有算法的序列号。