Advertisement
ElooEminem

Untitled

Oct 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. using namespace std;
  5.  
  6. struct zad{
  7. int a;
  8. char b;
  9. float c;
  10. };
  11.  
  12. zad** utworz(int n){
  13. zad** ret=new zad*[n];
  14. for(int i=0;i<n;i++){
  15. ret[i]=new zad;
  16. ret[i]->a=(rand()%10001)-1000;
  17. ret[i]->b=rand()%(int('X')-int('B')+1)+int('B');
  18. ret[i]->c=1000+(float)i;
  19. }
  20. return ret;
  21. }
  22.  
  23. void usun(zad** tab,int n){
  24. for(int i=0;i<n;i++){
  25. delete tab[i];
  26. }
  27. delete [] tab;
  28. }
  29.  
  30. void swap(zad* p,zad* q){
  31. zad temp;
  32.  
  33. temp.a=p->a;
  34. temp.b=p->b; //NOWY TEMP
  35. temp.c=p->c;
  36.  
  37. p->a=q->a;
  38. p->b=q->b; //P = Q
  39. p->c=q->c;
  40.  
  41. q->a=temp.a;
  42. q->b=temp.b; //Q = TEMP
  43. q->c=temp.c;
  44. }
  45.  
  46. void posortuj(zad** tab,int n){
  47. bool swapped;
  48. for(int i=0;i<n-1;i++){
  49. swapped=false;
  50. for(int j=0;j<n-i-1;j++){
  51. if(tab[j]->a > tab[j+1]->a){
  52. swap(tab[j],tab[j+1]);
  53. swapped=true;
  54. }
  55. }
  56. if(!swapped)
  57. break;
  58. }
  59. }
  60.  
  61. int znaki(zad** tab,int n,char p){
  62. int ret=0;
  63. for(int i=0;i<n;i++)
  64. if(tab[i]->b==p)
  65. ret++;
  66. return ret;
  67. }
  68.  
  69. int main() {
  70. srand(time(NULL));
  71. int n;
  72. int x; //ILOSC ZNAKOW!!!
  73. char p;
  74. zad** tab;
  75.  
  76. cin>>n; //Input
  77. cin>>p;
  78.  
  79. clock_t start=clock(); //Czas start
  80.  
  81. tab=utworz(n); //Tworzenie
  82.  
  83. posortuj(tab,n); //Sortowanie
  84.  
  85. x=znaki(tab,n,p); //Zliczanie
  86.  
  87. clock_t stop=clock(); //Czas stop;
  88.  
  89. cout<<"\nPosortowane structy:\n";
  90. for(int i=0;i<20;i++) //MIN N 20 !!!
  91. cout<<tab[i]->a<<" "<<tab[i]->b<<" "<<tab[i]->c<<" "<<endl;
  92.  
  93. usun(tab,n); //Usuwanie
  94.  
  95. cout<<"\nZnaki ("<<p<<") : "<<x;
  96.  
  97. cout<<"\nCzas (ms) :"<<stop-start;
  98.  
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement