Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #pragma region Struktury
- typedef struct ElementTelefon {
- int unikalnyNumer;
- char* marka;
- char* model;
- int masa;
- int czasCzuwania;
- char* stan;
- int wartosc;
- struct ElementTelefon* nast;
- }Telefon;
- typedef struct ElementKlient {
- char* imie;
- char* nazwisko;
- int* tablicaTelefonow;
- int kwotaZakopow;
- struct ElementKlient* nast;
- }Klient;
- #pragma endregion
- #pragma region ZmienneGlobalne
- Telefon* telefonyPocz;
- Klient* klienciPocz;
- int najwyzszyUnikalnyNumer = 0;
- #pragma endregion
- #pragma region ZarzadzanieTelefony
- int getIloscTelefonow() {
- Telefon* tmp = telefonyPocz;
- int iloscTel = 0;
- while (tmp != NULL)
- {
- tmp = tmp->nast;
- iloscTel++;
- }
- return iloscTel;
- }
- Telefon* getTelefon(int unikalnyNr) {
- Telefon* tmp = telefonyPocz;
- while (tmp != NULL && tmp->unikalnyNumer != unikalnyNr)
- {
- tmp = tmp->nast;
- }
- return tmp;
- }
- Telefon* dodajTelefonDoListy(Telefon* nowy) {
- if (nowy->unikalnyNumer != NULL && nowy->unikalnyNumer >= najwyzszyUnikalnyNumer)
- {
- najwyzszyUnikalnyNumer = nowy->unikalnyNumer + 1;
- }
- if (nowy->unikalnyNumer == NULL)
- {
- nowy->unikalnyNumer = ++najwyzszyUnikalnyNumer;
- }
- if (telefonyPocz == NULL)
- {
- nowy->nast = NULL;
- return nowy;
- }
- nowy->nast = telefonyPocz;
- return nowy;
- }
- void usunTelefonZListy(int unikalnyNr) {
- Telefon* usuwany = getTelefon(unikalnyNr);
- if (usuwany == telefonyPocz)
- {
- telefonyPocz = usuwany->nast;
- }
- else
- {
- Telefon* poprzedni = telefonyPocz;
- while (poprzedni->nast != usuwany)
- {
- poprzedni = poprzedni->nast;
- }
- poprzedni->nast = usuwany->nast;
- }
- free(usuwany);
- }
- void edytujTelefonZListy(Telefon* edytowany) {
- if (telefonyPocz == NULL)
- {
- return;
- }
- Telefon* tmp = telefonyPocz;
- while (tmp != edytowany)
- {
- tmp = tmp->nast;
- }
- strcpy(tmp->marka, edytowany->marka);
- strcpy(tmp->model, edytowany->model);
- tmp->masa = edytowany->masa;
- tmp->czasCzuwania = edytowany->czasCzuwania;
- strcpy(tmp->stan, edytowany->stan);
- tmp->wartosc = edytowany->wartosc;
- }
- #pragma endregion
- #pragma region SortowanieTelefony
- int comparatorMarka(const void* v1, const void* v2)
- {
- const Telefon* const* pp = v1;
- const Telefon* const* qq = v2;
- return strcmp((*pp)->marka, (*qq)->marka);
- }
- void sortujTelefonMarka() {
- int iloscTelefonow = getIloscTelefonow();
- Telefon** tablicaTelefonow = malloc(iloscTelefonow * sizeof(Telefon*));
- Telefon* tmp = telefonyPocz;
- for (int i = 0; i < iloscTelefonow; i++)
- {
- tablicaTelefonow[i] = tmp;
- tmp = tmp->nast;
- }
- qsort(tablicaTelefonow, iloscTelefonow, sizeof(Telefon*), comparatorMarka);
- telefonyPocz = tablicaTelefonow[0];
- tmp = telefonyPocz;
- for (int i = 1; i < iloscTelefonow; i++)
- {
- tmp->nast = tablicaTelefonow[i];
- tmp = tmp->nast;
- }
- tmp->nast = NULL;
- free(tablicaTelefonow);
- }
- int comparatorModel(const void* v1, const void* v2)
- {
- const Telefon* const* pp = v1;
- const Telefon* const* qq = v2;
- return (*pp)->wartosc > (*qq)->wartosc;
- }
- void sortujTelefonModel() {
- int iloscTelefonow = getIloscTelefonow();
- Telefon** tablicaTelefonow = malloc(iloscTelefonow * sizeof(Telefon*));
- Telefon* tmp = telefonyPocz;
- for (int i = 0; i < iloscTelefonow; i++)
- {
- tablicaTelefonow[i] = tmp;
- tmp = tmp->nast;
- }
- qsort(tablicaTelefonow, iloscTelefonow, sizeof(Telefon*), comparatorModel);
- telefonyPocz = tablicaTelefonow[0];
- tmp = telefonyPocz;
- for (int i = 1; i < iloscTelefonow; i++)
- {
- tmp->nast = tablicaTelefonow[i];
- tmp = tmp->nast;
- }
- tmp->nast = NULL;
- free(tablicaTelefonow);
- }
- int comparatorCena(const void* v1, const void* v2)
- {
- const Telefon* const* pp = v1;
- const Telefon* const* qq = v2;
- return (*pp)->wartosc > (*qq)->wartosc;
- }
- void sortujTelefonCena() {
- int iloscTelefonow = getIloscTelefonow();
- Telefon** tablicaTelefonow = malloc(iloscTelefonow * sizeof(Telefon*));
- Telefon* tmp = telefonyPocz;
- for (int i = 0; i < iloscTelefonow; i++)
- {
- tablicaTelefonow[i] = tmp;
- tmp = tmp->nast;
- }
- qsort(tablicaTelefonow, iloscTelefonow, sizeof(Telefon*), comparatorCena);
- telefonyPocz = tablicaTelefonow[0];
- tmp = telefonyPocz;
- for (int i = 1; i < iloscTelefonow; i++)
- {
- tmp->nast = tablicaTelefonow[i];
- tmp = tmp->nast;
- }
- tmp->nast = NULL;
- free(tablicaTelefonow);
- }
- #pragma endregion
- #pragma region WyswietlanieTelefony
- void wyswietlTelefony() {
- Telefon* tmp = telefonyPocz;
- printf("unikalnyNumer marka model masa czasCzuwania stan wartosc\n");
- while (tmp != NULL)
- {
- printf("%d %s %s %d %d %s %d\n", tmp->unikalnyNumer, tmp->marka, tmp->model, tmp->masa, tmp->czasCzuwania, tmp->stan, tmp->wartosc);
- tmp = tmp->nast;
- }
- }
- #pragma endregion
- #pragma region ZarzadzanieKlienci
- int getIloscKlientow() {
- Klient* tmp = klienciPocz;
- int iloscKlientow = 0;
- while (tmp != NULL)
- {
- tmp = tmp->nast;
- iloscKlientow++;
- }
- return iloscKlientow;
- }
- void dodajTelefonDoKlienta(Klient* klient, int numerTelefonu) {
- if (klient->tablicaTelefonow == NULL)
- {
- klient->tablicaTelefonow = malloc(sizeof(int));
- klient->tablicaTelefonow[0] = numerTelefonu;
- return;
- }
- int sizeNewTable = sizeof(klient->tablicaTelefonow) / sizeof(int)+1;
- int* nowaTablicaTelefonow = NULL;
- nowaTablicaTelefonow = (int*)realloc(klient->tablicaTelefonow, sizeNewTable * sizeof(int));
- nowaTablicaTelefonow[sizeNewTable - 1] = numerTelefonu;
- return;
- }
- Klient* dodajKlientaDoListy(Klient* nowy) {
- nowy->tablicaTelefonow = NULL;
- if (klienciPocz == NULL)
- {
- nowy->nast = NULL;
- return nowy;
- }
- nowy->nast = klienciPocz;
- return nowy;
- }
- void usunKlientaZListy(Klient* usuwany) {
- if (usuwany == klienciPocz)
- {
- klienciPocz = usuwany->nast;
- free(usuwany);
- }
- else
- {
- Klient* poprzedni = usuwany;
- while (poprzedni->nast != usuwany)
- {
- poprzedni = poprzedni->nast;
- }
- if (poprzedni->nast == usuwany)
- {
- poprzedni->nast = usuwany->nast;
- free(usuwany);
- }
- }
- }
- void edytujKlientaZListy(Klient* edytowany) {
- if (klienciPocz == NULL)
- {
- return;
- }
- Klient* tmp = klienciPocz;
- while (tmp != edytowany)
- {
- tmp = tmp->nast;
- }
- if (tmp == edytowany)
- {
- strcpy(tmp->imie, edytowany->imie);
- strcpy(tmp->nazwisko, edytowany->nazwisko);
- }
- }
- #pragma endregion
- #pragma region wyswietlanieKlienci
- void wyswietlKlienta(Klient* tmp) {
- printf("Imie: %s\nNazwisko: %s\nKwota zakopow: %d\n", tmp->imie, tmp->nazwisko, tmp->kwotaZakopow);
- }
- void wyswietlKlienci() {
- Klient* tmp = klienciPocz;
- printf("LP imie nazwisko kwota\n");
- int numer = 1;
- while (tmp != NULL)
- {
- printf("%d %s %s %d\n", numer++, tmp->imie, tmp->nazwisko, tmp->kwotaZakopow);
- tmp = tmp->nast;
- }
- }
- #pragma endregion
- #pragma region menu
- void menuKlient()
- {
- wyswietlKlienci();
- }
- void menuGlowne() {
- while (1 == 1)
- {
- printf("1 Wyswietl Klientow\n");
- printf("2 Wyswietl Telefony\n\n");
- printf("Wybierz akcje");
- int liczba = scanf("%d");
- switch (liczba)
- {
- case 1:menuKlient(); break;
- case 2: wyswietlTelefony(); break;
- default:
- break;
- }
- system("cls");
- }
- }
- #pragma endregion
- #pragma region Pliki
- void dodajTelefonZlinii(char* liniaTelefon) {
- char* pt = strtok(liniaTelefon, ",");
- Telefon* nowy = malloc(sizeof(Telefon));
- nowy->unikalnyNumer = atoi(pt);
- pt = strtok(NULL, ",");
- printf("%s", pt);
- nowy->marka = pt;
- pt = strtok(NULL, ",");
- nowy->model = pt;
- pt = strtok(NULL, ",");
- nowy->stan = pt;
- pt = strtok(NULL, ",");
- nowy->czasCzuwania = atoi(pt);
- pt = strtok(NULL, ",");
- nowy->masa = atoi(pt);
- pt = strtok(NULL, ",");
- nowy->wartosc = atoi(pt);
- pt = strtok(NULL, ",");
- telefonyPocz = dodajTelefonDoListy(nowy);
- }
- void czytajTelefonyZPliku() {
- FILE* fp = fopen("telefony.txt", "r");
- char* liniaTMP;
- char chunk[128];
- // Store the chunks of text into a line buffer
- size_t len = sizeof(chunk);
- char* line = malloc(len);
- if (line == NULL) {
- perror("Unable to allocate memory for the line buffer.");
- exit(1);
- }
- line[0] = '\0';
- while (fgets(chunk, sizeof(chunk), fp) != NULL) {
- size_t len_used = strlen(line);
- size_t chunk_used = strlen(chunk);
- if (len - len_used < chunk_used) {
- len *= 2;
- if ((line = realloc(line, len)) == NULL) {
- perror("Unable to reallocate memory for the line buffer.");
- free(line);
- exit(1);
- }
- }
- strncpy(line + len_used, chunk, len - len_used);
- len_used += chunk_used;
- if (line[len_used - 1] == '\n') {
- liniaTMP = malloc(len_used * sizeof(char));
- strcpy(liniaTMP, line);
- dodajTelefonZlinii(liniaTMP);
- // "Empty" the line buffer
- line[0] = '\0';
- }
- }
- fclose(fp);
- free(line);
- }
- void dodajKlientaZlinii(char* liniaKlient) {
- char* pt = strtok(liniaKlient, ",");
- Klient* nowy = malloc(sizeof(Klient));
- nowy->imie = pt;
- pt = strtok(NULL, ",");
- nowy->nazwisko = pt;
- pt = strtok(NULL, ",");
- nowy->nazwisko = pt;
- klienciPocz = dodajKlientaDoListy(nowy);
- }
- void czytajKlienciZPliku() {
- FILE* fp = fopen("klienci.txt", "r");
- char* liniaTMP;
- char chunk[128];
- // Store the chunks of text into a line buffer
- size_t len = sizeof(chunk);
- char* line = malloc(len);
- if (line == NULL) {
- perror("Unable to allocate memory for the line buffer.");
- exit(1);
- }
- line[0] = '\0';
- while (fgets(chunk, sizeof(chunk), fp) != NULL) {
- size_t len_used = strlen(line);
- size_t chunk_used = strlen(chunk);
- if (len - len_used < chunk_used) {
- len *= 2;
- if ((line = realloc(line, len)) == NULL) {
- perror("Unable to reallocate memory for the line buffer.");
- free(line);
- exit(1);
- }
- }
- strncpy(line + len_used, chunk, len - len_used);
- len_used += chunk_used;
- if (line[len_used - 1] == '\n') {
- liniaTMP = malloc(len_used * sizeof(char));
- strcpy(liniaTMP, line);
- dodajKlientaZlinii(liniaTMP);
- line[0] = '\0';
- }
- }
- fclose(fp);
- free(line);
- }
- void czytajTelefonyKlientowZPliku() {
- FILE* fp = fopen("telefonyKlient.txt", "r");
- char* liniaTMP;
- char chunk[128];
- // Store the chunks of text into a line buffer
- size_t len = sizeof(chunk);
- char* line = malloc(len);
- if (line == NULL) {
- perror("Unable to allocate memory for the line buffer.");
- exit(1);
- }
- line[0] = '\0';
- Klient* tmp = klienciPocz;
- while (fgets(chunk, sizeof(chunk), fp) != NULL) {
- size_t len_used = strlen(line);
- size_t chunk_used = strlen(chunk);
- if (len - len_used < chunk_used) {
- len *= 2;
- if ((line = realloc(line, len)) == NULL) {
- perror("Unable to reallocate memory for the line buffer.");
- free(line);
- exit(1);
- }
- }
- strncpy(line + len_used, chunk, len - len_used);
- len_used += chunk_used;
- if (line[len_used - 1] == '\n') {
- liniaTMP = malloc(len_used * sizeof(char));
- strcpy(liniaTMP, line);
- char* pt = strtok(liniaTMP, ",");
- while (pt != NULL)
- {
- dodajTelefonDoKlienta(tmp, atoi(pt));
- pt = strtok(NULL, ",");
- }
- line[0] = '\0';
- }
- }
- fclose(fp);
- free(line);
- }
- #pragma endregion
- int main()
- {
- czytajTelefonyZPliku();
- czytajKlienciZPliku();
- czytajTelefonyKlientowZPliku();
- menuGlowne();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement