Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <cstdlib>
- #include <cstdio>
- #include <ctime>
- #include <fstream>
- using namespace std;
- struct struktura
- {
- int a;
- char ch;
- float value;
- };
- struktura ** alokacja(int n);
- void wyswietl(struktura ** tab, int n);
- void zwolnij_pamiec(struktura ** tab, int n);
- void sortowanie(struktura ** tab, int n);
- int ile_powtorzen(struktura ** tab, int n, char ch);
- bool czy_liczba_powtarza(struktura **tab, int n, int liczba);
- int main()
- {
- clock_t begin, end;
- double time_spent;
- begin = clock();
- ifstream infile;
- infile.open("inlab01.txt", ios::in);
- if (infile.fail()) {
- cout << "problemy z plikiem";
- return 0;
- }
- int n;
- char ch;
- infile >> n;
- infile >> ch;
- struktura ** tab;
- int n = 10000;
- if (n > 10000) {
- cout << "zakres jest mniejszy od ilosc strukutr" << endl;
- return 0;
- }
- int ile;
- tab = alokacja(n);
- //wyswietl(tab, n);
- sortowanie(tab, n);
- cout << endl;
- wyswietl(tab, n);
- ile = ile_powtorzen(tab, n, 'D');
- cout << "litera D powtorzyła sie " << ile << endl;
- zwolnij_pamiec(tab, n);
- end = clock();
- time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- cout << time_spent;
- }
- struktura** alokacja(int n) {
- srand(time(NULL));
- struktura ** tab = new struktura *[n];
- for (int i = 0; i < n; i++) {
- tab[i] = new struktura;
- bool czy_juz_bylo = false;
- int liczba;
- while (czy_juz_bylo == false) {
- liczba = rand() % 10001 - 1000;
- czy_juz_bylo = czy_liczba_powtarza(tab, i, liczba);
- }
- tab[i]->a = liczba;
- tab[i]->ch = (rand() % 23) + 66;
- tab[i]->value = 1000 + (i + 1);
- }
- return tab;
- }
- void wyswietl(struktura**tab, int n) {
- for (int i = 0; i < 20; i++) {
- cout << tab[i]->a << endl;
- cout << tab[i]->ch << endl;
- cout << tab[i]->value << endl;
- cout << endl;
- }
- }
- void zwolnij_pamiec(struktura ** tab, int n) {
- for (int i = 0; i < n; i++) {
- delete tab[i];
- }
- delete[] tab;
- }
- void sortowanie(struktura ** tab, int n) {
- if (n != 1) {
- for (int i = 0; i < n; i++) {
- int ile = 0;
- for (int j = 0; j < (n - 1) - i; j++) {
- if (tab[j]->a > tab[j + 1]->a) {
- struktura *zmienna_pomoc;
- zmienna_pomoc = tab[j + 1];
- tab[j + 1] = tab[j];
- tab[j] = zmienna_pomoc;
- ile++;
- }
- }
- if (ile == 0) {
- return;
- }
- }
- }
- }
- int ile_powtorzen(struktura ** tab, int n, char ch) {
- int ile = 0;
- for (int i = 0; i < n; i++) {
- if (tab[i]->ch == ch) {
- ile++;
- }
- }
- return ile;
- }
- bool czy_liczba_powtarza(struktura **tab, int n, int liczba) {
- for (int i = 0; i < n; i++) {
- if (tab[i]->a == liczba) {
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement