好的,here我问我如何简化在文件中创建某个系列的方法。我得出的结论是我应该使用文件处理。
为了使自己清楚,这是我打算创建的序列的算法:
_____
1 , 1 Take the 1st and 2nd numbers.
_____
1 , 1 , 2 , 1 Append Their sum, and Append the second value.
_____
1 , 1 , 2 , 1 Take the 2nd and 3rd numbers
_____
1 , 1 , 2 , 1 , 3 , 2 Append Their sum, and Append the second value.
_____
1 , 1 , 2 , 1 , 3 , 2 Take the 3rd and 4th numbers
_____
1 , 1 , 2 , 1 , 3 , 2 , 3 , 1 Append Their sum, and Append the second value.
这应该创建序列:1,1,2,1,3,2,3,1,4,3,5 ......
现在,我已经在C中创建了一些应该能够生成此序列的代码,但不知道如何。
#include <stdio.h>
int x=1;
int y=1;
int z=2;
int DataCount = 1;
FILE *fFs;
struct storeX
{
int defX[16];
}stockX[16];
struct storeY
{
int defY[16];
}stockY[16];
int main()
{
fFs=fopen("Fibb.txt","w"); //Clears the file.
fclose(fFs);
fFs=fopen("Fibb.txt","a");
fprintf(fFs,"%d\n",x);
fprintf(fFs,"%d\n",y);
DataCount = 1;
do
{
z = x+y;
fprintf(fFs,"%d\n",z);
fprintf(fFs,"%d\n",y);
DataCount = DataCount + 1;
fscanf(fFs,"%16[^\n]%*c", stockX[DataCount].defX);
fscanf(fFs,"%16[^\n]%*c", stockY[(DataCount+1)].defY);
x= atoi(stockX[DataCount].defX);
y= atoi(stockY[(DataCount+1)].defY);
}
while(DataCount < 100);
fclose(fFs);
}
我认为问题在于扫描,但我无法确定。
文件输出:(逐行下去)
1
1
2
1
0
0
0
0
0
0
0
0
0
0