3
嘿大家。我想用C++編寫一個程序來計算斐波那契數列。我創建了一個執行計算和輸出的線程。但是我的for循環中沒有任何東西似乎被執行。任何人都可以看看我的代碼,並告訴我我可能會做錯什麼?螺紋程序計算斐波納契數字
#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//iterative with output
DWORD WINAPI fib3(LPVOID param){
double u = 0;
double v = 1;
double t;
int upper = *(int*)param;
for(int i = 2; i <= upper; i++){
cout << v << " ";
t = u + v;
u = v;
v = t;
cout << "testing" << endl;
}
cout << v << " ";
return 0;
}
int main(int argc, char *argv[]){
cout << "This will compute the fibonacci series.\n" << endl;
bool done = true;
double x;
DWORD ThreadId;
HANDLE ThreadHandle;
while(done){
cout << "Enter a number: ";
cin >> x;
if(x == -1){
cout << "\nExiting" << endl;
return 0;
}
ThreadHandle = CreateThread(NULL, 0, fib3, &x, 0, &ThreadId);
if(ThreadHandle != NULL){
WaitForSingleObject(ThreadHandle, INFINITE);
CloseHandle(ThreadHandle);
}
}
return 0;
}
您將收到夢寐以求的混淆獎(第三課),以便將'done'選作變量名稱。 – TonyK 2011-02-28 11:50:16
與您的問題沒有直接關係,但是使用循環計算斐波納契數是一種相當低效的方法。你有使用這種方法的原因嗎? – 2011-02-28 11:55:45
我知道我可以遞歸地做到這一點。但我只是認爲用這種方式輸出每個數字會更容易? – d2jxp 2011-02-28 11:58:30