Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- struct zad{
- int a;
- char b;
- float c;
- };
- zad** utworz(int n){
- zad** ret=new zad*[n];
- for(int i=0;i<n;i++){
- ret[i]=new zad;
- ret[i]->a=(rand()%10001)-1000;
- ret[i]->b=rand()%(int('X')-int('B')+1)+int('B');
- ret[i]->c=1000+(float)i;
- }
- return ret;
- }
- void usun(zad** tab,int n){
- for(int i=0;i<n;i++){
- delete tab[i];
- }
- delete [] tab;
- }
- void swap(zad* p,zad* q){
- zad temp;
- temp.a=p->a;
- temp.b=p->b; //NOWY TEMP
- temp.c=p->c;
- p->a=q->a;
- p->b=q->b; //P = Q
- p->c=q->c;
- q->a=temp.a;
- q->b=temp.b; //Q = TEMP
- q->c=temp.c;
- }
- void posortuj(zad** tab,int n){
- bool swapped;
- for(int i=0;i<n-1;i++){
- swapped=false;
- for(int j=0;j<n-i-1;j++){
- if(tab[j]->a > tab[j+1]->a){
- swap(tab[j],tab[j+1]);
- swapped=true;
- }
- }
- if(!swapped)
- break;
- }
- }
- int znaki(zad** tab,int n,char p){
- int ret=0;
- for(int i=0;i<n;i++)
- if(tab[i]->b==p)
- ret++;
- return ret;
- }
- int main() {
- srand(time(NULL));
- int n;
- int x; //ILOSC ZNAKOW!!!
- char p;
- zad** tab;
- cin>>n; //Input
- cin>>p;
- clock_t start=clock(); //Czas start
- tab=utworz(n); //Tworzenie
- posortuj(tab,n); //Sortowanie
- x=znaki(tab,n,p); //Zliczanie
- clock_t stop=clock(); //Czas stop;
- cout<<"\nPosortowane structy:\n";
- for(int i=0;i<20;i++) //MIN N 20 !!!
- cout<<tab[i]->a<<" "<<tab[i]->b<<" "<<tab[i]->c<<" "<<endl;
- usun(tab,n); //Usuwanie
- cout<<"\nZnaki ("<<p<<") : "<<x;
- cout<<"\nCzas (ms) :"<<stop-start;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement