Guest User

Untitled

a guest
Jan 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include<conio.h>
  6. #include <time.h>
  7. #define LICZBA_PROBEK 100
  8.  
  9.  
  10. void dziedzina(double *dmin, double *dmax);
  11. void parametry(double wsp[]);
  12. void generuj(double wsp[], double dmin, double dmax, double wyniki[], int wielkosc);
  13. void zapisz(double wyniki[], int wymiar, double dmin, double dmax);
  14. //void szum(double suma[], double wyniki[], int rozmiar);
  15. double wczytaj(void);
  16. void zamien(double *argument1, double *argument2);
  17.  
  18. void zamien(double *argument1, double *argument2)
  19. {
  20. double temp;
  21. temp=*argument1;
  22. *argument1=*argument2;
  23. *argument2=temp;
  24. }
  25.  
  26.  
  27.  
  28. void szum(double suma[], double szum[], double wyniki[], int rozmiar)
  29. {
  30. // srand((unsigned int) time(NULL));
  31. // double l;
  32. int i;
  33. double amplituda;
  34. //double szum[100];
  35. printf("\npodaj amplitude: ");
  36. scanf("%lf", &amplituda);
  37. srand((unsigned int) time(NULL));
  38. for (i=0; i<rozmiar; i++)
  39. {
  40.  
  41. szum[i]=(((double)rand()/RAND_MAX)*amplituda - (amplituda/2));
  42. printf("\n szum =%lf \n", szum);
  43. suma[i]=wyniki[i]+szum[i];
  44. printf("\n suma sygnalow= %lf \n", suma[i]);
  45. }
  46. }
  47.  
  48. double wczytaj(void)
  49. {
  50.  
  51. double wyjscie;
  52. char wartosc[100];
  53. int poprawne_dane=0;
  54. char *nieprzeczytane_dane;
  55.  
  56. while(poprawne_dane==0)
  57. {
  58. scanf("%s", wartosc);
  59. wyjscie = strtod(wartosc, &nieprzeczytane_dane); //jezeli wsyztskie dane byly poprawne to wczyta się (do nieprzeczytanych danych) tylko znak konca linii
  60. if (*nieprzeczytane_dane != '\0')
  61. {
  62. printf("podales niewlasciwa wartosc, sprobuj jeszcze raz\n");
  63. poprawne_dane=0;
  64. }
  65. else
  66. {
  67. poprawne_dane=1;
  68. }
  69. }
  70. return wyjscie;
  71. }
  72.  
  73.  
  74.  
  75. void dziedzina(double *dmin, double *dmax)
  76. {
  77. printf("podaj parametry określające dziedzinę: \n");
  78. // scanf("%lf %lf", dmin, dmax);
  79. *dmin=wczytaj();
  80. *dmax=wczytaj();
  81. while (*dmin>*dmax)
  82. {
  83. zamien(&dmin, &dmax);
  84. printf("\npodales początek wiekszy niz koniec, wiec parametry dziedziny ulegly zamianie na wlasciwe\n ");
  85. // scanf("%lf %lf", dmin, dmax);
  86. }
  87. printf("\nzakres Twojej dziedziny jest od %lf do %lf \n", *dmin, *dmax);
  88. }
  89.  
  90. void parametry(double wsp[])
  91. {
  92. int i;
  93. for (i=0; i<3; i++)
  94. {
  95. printf("podaj %d współczynnik wielomianu: ", i+1);
  96. //scanf("%lf %lf %lf", &wsp[0], &wsp[1], &wsp[2]);
  97.  
  98. wsp[i]=wczytaj();
  99. }
  100. printf("\nTwoje współczynniki to a=%lf, b=%lf, c=%lf \n", wsp[0], wsp[1], wsp[2]);
  101. }
  102.  
  103. void generuj(double wsp[], double dmin, double dmax, double wyniki[], int wielkosc)
  104. {
  105. double x, delta; // delta oznacza wartość bezwzględną różnicy między dmin a dmax
  106. int i = 0;
  107. // printf("podaj ilość próbek: ");
  108. // scanf_s("%d", wielkosc);
  109. // printf("Twoja liczba próbek wynosi: %d\n", *wielkosc);
  110. // wyniki = (double *) malloc((int)*wielkosc * sizeof (double));
  111.  
  112.  
  113. delta = (dmax - dmin)/(wielkosc);
  114. printf("delta = %lf\n", delta);
  115.  
  116. x = dmin;
  117.  
  118. for(i=0; i<wielkosc; i++)
  119. {
  120. wyniki[i]=(wsp[0]*(x*x))+(wsp[1]*x)+wsp[2];
  121. printf("dla wartosci x = %1.2lf wartosc wielomianu wynosi: %1.2lf\n", x, wyniki[i]);
  122. x = x + delta;
  123. }
  124.  
  125. }
  126.  
  127. //fgets
  128. //"%lf\n"
  129.  
  130. void zapisz(double wyniki[], int wymiar, double dmin, double dmax)
  131. {
  132. int i;
  133. FILE *f;
  134. //f=fopen("wielomian.csv", "w")
  135. if ((f=fopen("wielomian.csv", "w"))!=NULL)
  136. {
  137. fprintf(f, "wyniki");
  138. for(i=0; i<wymiar; i++)
  139. {
  140. fprintf(f, "\n%f;", wyniki[i]);
  141. }
  142. printf("\nzapis poprawny\n");
  143. }
  144. else printf("błąd przy zapisie");
  145. fclose(f);
  146.  
  147. }
  148.  
  149.  
  150. int main(void)
  151. {
  152. double Dmin, Dmax;
  153. double tab[4];
  154. int rozmiar=5;
  155. double wynikowa[LICZBA_PROBEK];
  156. //double l;
  157. double szumik[100];
  158. double suma[LICZBA_PROBEK];
  159.  
  160. setlocale(LC_ALL, "polish");
  161. dziedzina(&Dmin, &Dmax);
  162. parametry(tab);
  163. generuj(tab, Dmin, Dmax, wynikowa, rozmiar);
  164. zapisz(wynikowa, rozmiar, Dmin, Dmax);
  165.  
  166. szum(suma, wynikowa,szumik, rozmiar);
  167.  
  168. // l=(double)rand()/RAND_MAX;
  169. //printf("\n%lf \n", l);
  170. getch();
  171. return 0;
  172. }
Add Comment
Please, Sign In to add comment