2016-10-12 2 views
-4

나는 내 프로그램에 어려움을 겪고 있습니다. 새로운 피보나치 숫자를 출력해야한다. 피보나치 수가 unsigned int의 범위를 초과하면 프로그램을 종료해야합니다. 더욱이, "n"의 피보나치가 몇 개 표시되는지 새 줄에 인쇄해야합니다. 그래서 여기 "n"(입력) 피보나치 숫자를 출력하는 프로그램을 작성하려고합니다.

#include<iostream> 
#include<limits> 

using namespace std; 

int main() 
{ 
    unsigned int n; 

    cout << "Please enter the amount of fibonaccis you would like to compute: " << endl; 
    cin >> n; 

    unsigned int next=1; 
    unsigned int current=0; 
    unsigned int c = current; 
    unsigned int temp; 
    unsigned int counter=1; 

    //This bool returns true as soon as an overflow occurs 
    bool overflow; 

/*This bool checks, whether the newly computed 
    number is bigger than the previous number 
    (which may not be the case if an overflow occurs)*/ 

    bool nextBigger; 



    /*Somehow, I could only handle the first 
    inputs by using "bruteforce". 
    If I tried to combine it with the "main loop", 
    it got all messy. */ 

    if(n==0) 
    { 
    std::cout << "0" << " of " << n << endl; 
    } 

    else if(n==1) 
    { 
    std::cout << "0" << endl << "1 of " << n << endl; 
    } 

    else if(n==2) 
    { 
    std::cout << "0" << endl << "1" << endl << "2 of " << n << endl; 
    } 

    else 
    { /* This for-loop increases (at least it should) a counter 
     by one for each computation of a valid fibonacci number*/ 

     for(counter=1;counter<n;++counter) 
     { 

     overflow = (c > (std::numeric_limits<unsigned int>::max()-temp)); 

     if(!overflow && nextBigger) 
     { 
      cout << next << endl; 

     } 

     else 
     { 
      break; //If overflow or next number < previous number, exit program 
     } 

     temp = next; //temp is storage variable for next 
     c = current; //storage variable for current 
     next += current; //next is being altered: it becomes the new fibonacci number 
     current = temp; //current gets value of temp(value of next before being altered) 
    } 

    nextBigger = (next > current); 

    cout << counter << " of " << n << endl; //Output of how many fibonaccis were computed 
} 

    return 0; 
} 

는 것입니다 : 여기

는 지금까지 코드입니다. CodeBlocks에서 완벽하게 작동하도록 프로그래밍했습니다. 그런 다음 코드 보드 (과제로)에 업로드하려고했습니다. Codeboard에서 갑자기 전혀 작동하지 않았습니다. 어쩌면 다른 컴파일러와 관련이 있을지 모르지만,이 문제를 어떻게 해결할 수 있는지 전혀 모른다. 그래서 저는 매우 당혹스럽고 어떤 힌트, 아이디어, 수정 또는 영감에 대해 매우 감사 할 것입니다. 코드를 보면

(나는 초보자입니다, 그래서 코드를 이해하고 읽을 수 있기를 바랍니다. 나는 제안 개선을위한 개방입니다.)

+2

오류 메시지가 나타 납니까? 잘못된 결과? 아니면 정확히 작동하지 않는 것은 무엇입니까? – deviantfan

+3

[* * did not work *] (http://importblogkit.com/2015/07/does-not-work/)보다 더 많이 제공해야합니다. – Biffen

+1

스택 오버플로에 오신 것을 환영합니다! 디버거를 사용하여 코드를 단계별로 실행하는 방법을 배워야 할 필요가있는 것 같습니다. 좋은 디버거를 사용하면 한 줄씩 프로그램을 실행하고 예상 한 곳에서 벗어난 곳을 볼 수 있습니다. 프로그래밍을 할 때 필수적인 도구입니다. 추가 읽기 : [작은 프로그램을 디버깅하는 방법] (http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) –

답변

0

을 그것은 if 문

if(!overflow && nextBigger) 
    { 
     cout << next << endl; 
    } 
의 몸을 보인다

은 실행되지 않습니다. 어쩌면 당신은 무슨 일이 일어나고 있는지 디버깅 할 수 있도록 각 루프 반복마다 overflow 및 nextBigger 값을 인쇄 할 수 있습니다.

관련 문제