2014-10-04 109 views
-5

该程序应打印斐波那契数列的前50个数字。但是只有前47张打印正确,最后三张打印不正确。斐波那契数列错误

/*FreeRTOS.org includes.*/ 
#include "FreeRTOS.h" 
#include "task.h" 
#include "queue.h" 
#include "stdint.h" 
#include "basic_io.h" 

static void vSenderTask(void *pvParameters); 
static void vReceiverTask(void *pvParameters); 
int long resultado; 

xQueueHandle xQueue; 

int main(void) 
{ 
    xQueue = xQueueCreate(10, sizeof(long)); 

    if(xQueue != NULL) 
    { 
     xTaskCreate(vSenderTask, "Sender1", 240, (long *) resultado, 2, NULL); 
     xTaskCreate(vReceiverTask, "Receiver", 240, (long *) resultado, 1, NULL); 
     vTaskStartScheduler(); 
    } 
    else 
    { 
    } 
    for(;;); 
    return 0; 
} 

/*---------------------------------------------------------------------------------------------*/ 

static void vSenderTask(void *pvParameters) 
{ 
    portBASE_TYPE xStatus; 
    const portTickType xTicksToWait = 1000/portTICK_RATE_MS; 
    long s =1; 
    long r=0; 
    int k=1; 
    uint64_t resultado; 
    resultado = (long) pvParameters; 
    for (;;){ 
     for (k; k<=61; k++) { 
      resultado= s+r; 
      s=r; 
      r=resultado; 

      xStatus = xQueueSendToBack(xQueue, &resultado, xTicksToWait); 
     } 
     if(xStatus != pdPASS) 
     { 
      vPrintString("Could not send to the queue.\n"); 
     } 
    } 
} 

/*---------------------------------------------------------------------------------------------*/ 

static void vReceiverTask(void *pvParameters) 
{ 
    uint64_t resultado; 
    portBASE_TYPE xStatus; 

    for(;;) 
    { 
     if(uxQueueMessagesWaiting(xQueue) != 10) 
     { 
      vPrintString("Queue should have been full!\n"); 
     } 

     xStatus = xQueueReceive(xQueue, &resultado , 0); 
     if(xStatus == pdPASS) 
     { 
      vPrintStringAndNumber("Received = ", resultado); 
     } 
     else 
     { 
      vPrintString("Could not receive from the queue.\r\n"); 
     } 
    } 
} 

/*---------------------------------------------------------------------------------------------*/ 
void vApplicationMallocFailedHook(void) 
{ 
    /* This function will only be called if an API call to create a task, queue 
    or semaphore fails because there is too little heap RAM remaining. */ 
    for(;;); 
} 
/*-----------------------------------------------------------*/ 

void vApplicationStackOverflowHook(xTaskHandle *pxTask, signed char *pcTaskName) 
{ 
    /*This function will only be called if a task overflows its stack. Note 
    that stack overflow checking does slow down the context switch 
    implementation. */ 
    for(;;); 
} 
/*-----------------------------------------------------------*/ 

void vApplicationIdleHook(void) 
{ 
    /* This example does not use the idle hook to perform any processing. */ 
} 
/*-----------------------------------------------------------*/ 

void vApplicationTickHook(void) 
{ 
    /* This example does not use the tick hook to perform any processing. */ 
} 

输出是:

Received = 1 
Received = 1 
Received = 2 
Received = 3 
Received = 5 
Received = 8 
Received = 13 
Received = 21 
Received = 34 
Received = 55 
Received = 89 
Received = 144 
Received = 233 
Received = 377 
Received = 610 
Received = 987 
Received = 1597 
Received = 2584 
Received = 4181 
Received = 6765 
Received = 10946 
Received = 17711 
Received = 28657 
Received = 46368 
Received = 75025 
Received = 121393 
Received = 196418 
Received = 317811 
Received = 514229 
Received = 832040 
Received = 1346269 
Received = 2178309 
Received = 3524578 
Received = 5702887 
Received = 9227465 
Received = 14930352 
Received = 24157817 
Received = 39088169 
Received = 63245986 
Received = 102334155 
Received = 165580141 
Received = 267914296 
Received = 433494437 
Received = 701408733 
Received = 1134903170 
Received = 1836311903 
Received = 2971215073 
Received = 512559680 
Received = 3483774753 
Received = 3996334433 
+1

您能否删除您的问题中与重现问题无关的所有代码,并添加与其相关的所有代码?在这里很难找到计算斐波那契数列的代码。 – stakx 2014-10-04 07:37:26

+0

不要以为你可以转储代码给我们,我们将解决你所有的问题。 – 2014-10-04 07:39:16

+1

看看你的输出,出错的东西似乎只是你选择的整型不可表示的数字。你的老师可能只是给了你这个练习来检测它,并为你自己提出一个解决方案。 – 2014-10-04 07:42:33

回答

0

在您的系统,long为32位。它可以容纳的最大正整数是4294967295. 但是1836311903 + 2971215073 = 4807526976,它更大,所以它溢出了,你得到了512559680(它是4807526976 - 4294967296)。如果你想超过第47个斐波那契数,你需要一个更大的数据类型或做多精度算术。

+0

嗨,对不起,我是新的在此,但我怎么能做一个多精度算术? – 2014-10-04 21:03:38

+0

@CesarSaldaña对于第61个斐波那契数,如果您的编译器支持它,您可以使用无符号long long。对于MP,请参阅https://gmplib.org/ – 2014-10-05 00:16:51

+0

我很欣赏取消反社会病态驾驶的downvote。 – 2014-10-15 07:13:47