Guest User

Untitled

a guest
Jan 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <windows.h>
  4.  
  5. long int fibonacciIteracyjny(int ktory_wyraz)
  6. {
  7. int i=0, a=0, b=1;
  8. if (ktory_wyraz==0) return 0;
  9. for(i; i<(ktory_wyraz-1); i++)
  10. {
  11. b+=a;
  12. a=b-a;
  13. }
  14. return b;
  15. }
  16.  
  17. long int fibonacciRekurencyjny(int ktory_wyraz)
  18. {
  19. if(ktory_wyraz==0) return 0;
  20. if(ktory_wyraz==1) return 1;
  21. else return (fibonacciRekurencyjny(ktory_wyraz-1)+fibonacciRekurencyjny(ktory_wyraz-2));
  22.  
  23. }
  24.  
  25. int main()
  26. {
  27.  
  28. FILE *plik;
  29. int n,i;
  30. long int czas, czas2;
  31. long double ile;
  32. long long int ileTaktowUplynelo;
  33. long double ileMikroSekundUplynelo, ileMikroSekundUplynelo2;
  34. LARGE_INTEGER taktPoczatku, taktKonca, taktowNaSekunde;
  35. QueryPerformanceFrequency(&taktowNaSekunde);
  36. QueryPerformanceCounter(&taktPoczatku);
  37.  
  38. printf("podaj wyraz ciagu Fibonacciego: ");
  39. scanf("%d", &n);
  40. plik=fopen("plik.csv","w");
  41. if (plik == NULL) {
  42. perror("Nie udalo sie otworzyc pliku 'plik.dat' do zapisu");
  43. return 1;
  44. }
  45.  
  46. fprintf(plik, "lp.;wartość;czas iteracyjnie;czas rekurencyjnie\n");
  47. for(i=1;i<=n;i++)
  48. {
  49.  
  50. czas=fibonacciIteracyjny(i);
  51. QueryPerformanceCounter(&taktKonca);
  52. ileTaktowUplynelo=taktKonca.QuadPart-taktPoczatku.QuadPart;
  53. ileMikroSekundUplynelo=((long double)ileTaktowUplynelo*1000000)/(long double)taktowNaSekunde.QuadPart;
  54.  
  55. czas2=fibonacciRekurencyjny(i);
  56. QueryPerformanceCounter(&taktKonca);
  57. ileTaktowUplynelo=taktKonca.QuadPart-taktPoczatku.QuadPart;
  58. ileMikroSekundUplynelo2=((long double)ileTaktowUplynelo*1000000)/(long double)taktowNaSekunde.QuadPart;
  59. fprintf(plik,"%d;%d;%.2f;%.2f\n", i, czas,ileMikroSekundUplynelo, ileMikroSekundUplynelo2 );
  60. }
  61. fclose(plik);
  62. return 0;
  63. }
Add Comment
Please, Sign In to add comment