/** * @title: fibo.cpp * @date: 2004.10.07 - 2:54 pm * @author: vinnie * @source for fibo.exe */ #include #include #include #include #include #include struct myTime { clock_t beginIterTime; clock_t endIterTime; clock_t beginRecurTime; clock_t endRecurTime; } mt; // =^=^=^=^=->>> ITERATIVE version long double iterFib( int limit) { long double result = 0; long double x, z; mt.beginIterTime = clock(); if ( limit == 0) goto ABORT; x = 0; result = 1; --limit; for (int i = 0; i < limit; i++) { z = x + result; x = result; result = z; } ABORT: mt.endIterTime = clock(); return result; } // =^=^=^=^=->>> RECURSIVE version long double recurFib( int limit) { if ( limit == 0 ) return 0; else if ( limit == 1 ) return 1; else return recurFib( limit-1) + recurFib( limit-2); } // ===== *** ===== *** ===== *** ===== *** ===== *** ==== int main( ) { system("color 1f"); // menu' label MYMENU: // begin // print intro & menu system("cls"); printf(" SLCC - CS 2310-01 - Fall '04 - Fibonacci - Assign. #5\n"); printf("\n Fibonacci Test\n\n"); // ask user input char cUI[10]; printf(" Input the number of the first X positive integers\n"); printf(" for which you want the Fibonacci' number: "); gets( cUI); int iUI = atoi( cUI); // --- calculus start : --- // iterations long double res1 = iterFib( iUI); // recursion mt.beginRecurTime = clock(); long double res2 = recurFib( iUI); mt.endRecurTime = clock(); // === *** === PRINT RESULTS =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* int diff1, diff2; diff1 = mt.endIterTime - mt.beginIterTime; diff2 = mt.endRecurTime - mt.beginRecurTime; gotoxy( 8, 8); printf("Iter.s Result"); gotoxy( 48, 8); printf("entryT exitT diff.in ms"); gotoxy( 1, 10); printf("iter %d", iUI ); gotoxy( 19, 10); printf("%LE", res1); gotoxy( 48, 10); printf("%d", mt.beginIterTime); gotoxy( 58, 10); printf("%d", mt.endIterTime); gotoxy( 68, 10); printf("%d", diff1); gotoxy( 1, 11); printf("recur %d", iUI); gotoxy( 19, 11); printf("%LE", res2); gotoxy( 48, 11); printf("%d", mt.beginRecurTime); gotoxy( 58, 11); printf("%d", mt.endRecurTime); gotoxy( 68, 11); printf("%d", diff2); // *_* ask for an other round *_* printf("\n\n Another one [Y/y/N/n]? "); while ( !kbhit() ); char c = getchar( ); if ( c == 'Y' || c == 'y' ) goto MYMENU; // *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_ printf("\n Thanks for using Fibonacci Test\n"); printf(" Vincenzo Maggio CodeŠ - released under GPL\n"); system("color 0f"); return 0; }