Advertisement
Strzyk

wczytywanie z pliku

Jan 25th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int liczR(char name[]){
  8. ifstream p;
  9. string tmp;
  10. int i=0;
  11. p.open(name);
  12. while(p.good())
  13. {
  14. getline(p,tmp);
  15. i++;
  16. }
  17. p.close();
  18. return i;
  19. }
  20.  
  21. int liczC(char name[]){
  22. ifstream p;
  23. char c;
  24. int i=0,tmp;
  25. p.open(name);
  26. while(p.good())
  27. {
  28. p >> tmp;
  29. p.get(c);
  30. i++;
  31. if(c=='\n')break;
  32. }
  33. p.close();
  34. return i;
  35. }
  36.  
  37. int** generuj(int R, int C){
  38. int **tmp;
  39.  
  40. tmp = new int *[R];
  41.  
  42. for (int i = 0; i < R; i++)
  43. tmp[i] = new int [C];
  44.  
  45. return tmp;
  46. }
  47.  
  48. //czytaj
  49. int** czytaj(char name[],int*R,int*C){
  50. int** tmp;
  51. ifstream p;
  52. *R=liczR(name);
  53. *C=liczC(name);
  54. tmp=generuj(*R,*C);
  55. p.open(name);
  56. for(int i=0;i<*R;i++){
  57. for(int j=0;j<*C;j++){
  58. p>>tmp[i][j];
  59. p.get();//przeskakuje o jeden zank
  60. }
  61. }
  62. p.close();
  63. return tmp;
  64. }
  65. //oblicz
  66. void oblicz(float*wsk,int**tab,int R,int C,int S){
  67. for(int i=0;i<49;i++) wsk[i]=0;
  68. for(int i=0;i<R;i++)
  69. for(int j=S-1;j<C;j++)
  70. wsk[tab[i][j]-1]++;
  71. for(int i=0;i<49;i++)
  72. wsk[i]/=R/100;
  73.  
  74. }
  75. //extrema
  76. void extrema(float*wsk,float*wmin,float*wmax){
  77. float tmp,ymax=0,ymin=100;
  78. for(int=0;i<49;i++){
  79. temp=wsk[i];
  80. if(tmp<ymin) ymin=tmp;
  81. if(tmp>ymax) ymax=tmp;
  82. }
  83. *wmin=wmin;
  84. *wmax=wmax;
  85. }
  86. //zapisz
  87.  
  88. int main(){
  89.  
  90. int **tab, R,C;
  91. float *wsk, wynik[49],wmin,wmax;
  92. char fileO[]={"lotto.csv"};
  93. char fileZ[]={"wyniki.csv"};
  94. wsk=wynik;
  95.  
  96. tab = czytaj(fileO,&R,&C);
  97. /*for(int i=0;i<R;i++){
  98. for(int j=0;j<C;j++){
  99. cout<<tab[i][j]<<";";
  100. }
  101. cout<<endl;
  102. }
  103. *///wyswietla zawartość
  104. oblicz(wsk,tab,R,C,5);
  105. for(int i=0;i<49;i++){
  106. cout<<i+1<<"->"<<wynik[i]<<"%"<<endl;
  107. }
  108.  
  109. extrema(wsk, &wmin, &wmax);
  110.  
  111. cout<<"minimum:"<<wmin<<"~"<<floor(wmin)<<endl;
  112. count<<"maksimum:"<<wmax<<"~"ceil(wmax)<<endl;
  113. //floor(), ceil() zaokragla w dol iw gore
  114.  
  115. //TUTAJ JESZCZE WYŚWIETLANIE W KONSOLI :)
  116.  
  117. /*
  118. if(zapisz(wsk,fileZ)==0)
  119. cout << endl << "SAVE SUKCES" << endl;
  120. else
  121. cout << "SAVE ERROR" << endl;
  122. */
  123.  
  124. delete [] tab;
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement