该程序应打印斐波那契数列的前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
您能否删除您的问题中与重现问题无关的所有代码,并添加与其相关的所有代码?在这里很难找到计算斐波那契数列的代码。 – stakx 2014-10-04 07:37:26
不要以为你可以转储代码给我们,我们将解决你所有的问题。 – 2014-10-04 07:39:16
看看你的输出,出错的东西似乎只是你选择的整型不可表示的数字。你的老师可能只是给了你这个练习来检测它,并为你自己提出一个解决方案。 – 2014-10-04 07:42:33