Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int iloscPrzestepstw = 0;
- int iloscPrzestepcow = 0;
- int defID = 0;
- int pdefID = 0;
- char* intToStr(int n)
- {
- int i = 0;
- char tmp[12];
- static char ret[12];
- if(n < 0) {
- *ret = '-';
- i++;
- n = -n;
- }
- do {
- *tmp = n % 10 + 48;
- n -= n % 10;
- if(n > 9) (*tmp)++;
- }
- while(n /= 10);
- while(ret[i++] = (*tmp)--);
- return ret;
- }
- typedef struct Przestepstwa{
- int numer;
- char* miasto;
- int data; /// YYYYMMDD 20120130
- char* crims;
- struct Przestepstwa *next;
- } Przestepstwa;
- typedef struct Przestepca{
- char* imie;
- char* nazwisko;
- int wiek;
- int wzrost;
- int waga;
- char* cechy;
- int status;
- int id;
- struct Przestepca *next;
- } Przestepca;
- /* int levenshtein(char *s1, char *s2) {
- unsigned int x, y, s1len, s2len;
- s1len = strlen(s1);
- s2len = strlen(s2);
- unsigned int matrix[s2len+1][s1len+1];
- matrix[0][0] = 0;
- for (x = 1; x <= s2len; x++)
- matrix[x][0] = matrix[x-1][0] + 1;
- for (y = 1; y <= s1len; y++)
- matrix[0][y] = matrix[0][y-1] + 1;
- for (x = 1; x <= s2len; x++)
- for (y = 1; y <= s1len; y++)
- matrix[x][y] = MIN3(matrix[x-1][y] + 1, matrix[x][y-1] + 1, matrix[x-1][y-1] + (s1[y-1] == s2[x-1] ? 0 : 1));
- return(matrix[s2len][s1len]);
- } */
- void pswap(Przestepca* p1, Przestepca* p2){
- char* tname = p1->imie;
- char* tsn = p1->nazwisko;
- char* tcech = p1->cechy;
- int twiek = p1->wiek;
- int twzrost = p1->wzrost;
- int twaga = p1->waga;
- int tstatus = p1->status;
- int tid = p1->id;
- p1->imie = p2->imie;
- p1->nazwisko = p2->nazwisko;
- p1->cechy = p2->cechy;
- p1->wiek = p2->wiek;
- p1->wzrost = p2->wzrost;
- p1->waga = p2->waga;
- p1->status = p2->status;
- p1->id = p2->id;
- p2->imie = tname;
- p2->nazwisko = tsn;
- p2->cechy = tcech;
- p2->wiek = twiek;
- p2->wzrost = twzrost;
- p2->waga = twaga;
- p2->status = tstatus;
- p2->id = tid;
- }
- void ppswap(Przestepstwa* p1, Przestepstwa* p2){
- float tnumer = p1->numer;
- char* tmiasto = p1->miasto;
- int tdata = p1->data;
- char* tcrims = p1->crims;
- p1->crims = p2->crims;
- p1->numer = p2->numer;
- p1->miasto = p2->miasto;
- p1->data = p2->data;
- p2->numer = tnumer;
- p2->miasto = tmiasto;
- p2->data = tdata;
- p2->crims = tcrims;
- }
- void push(Przestepca** p, char* name, char* surname, int age, int height, int weight, char* traits, int stat, int nr){
- Przestepca* nowy = (Przestepca*)malloc(sizeof(Przestepca));
- Przestepca* last = *p;
- nowy->imie = name;
- nowy->nazwisko = surname;
- nowy->wiek = age;
- nowy->wzrost = height;
- nowy->waga = weight;
- nowy->cechy = traits;
- nowy->status = stat;
- nowy->id = nr;
- nowy->next = NULL;
- if(*p == NULL){
- *p = nowy;
- return;
- }
- else{
- while(last->next != NULL){
- last = last->next;
- }
- last->next = nowy;
- return;
- }
- }
- void ppush(Przestepstwa** p, float nr, char* city, int date, char* criminals){
- Przestepstwa* nowy = (Przestepstwa*)malloc(sizeof(Przestepstwa));
- Przestepstwa* last = *p;
- nowy->numer = nr;
- nowy->miasto = city;
- nowy->data = date;
- nowy->next = NULL;
- nowy->crims = criminals;
- if(*p == NULL){
- *p = nowy;
- return;
- }
- else{
- while(last->next != NULL){
- last = last->next;
- }
- last->next = nowy;
- return;
- }
- }
- Przestepca* readFile(const char* path){
- FILE* f = fopen(path, "r");
- char buff[1000];
- int nr = 0;
- Przestepca *p = NULL;
- if(!f) { printf("Nie udalo sie otworzyc pliku %s", path); return p; }
- while(fgets(buff, sizeof(buff), f)){
- iloscPrzestepcow++; defID++;
- char* name = (char*)malloc(sizeof(char) * 50);
- char* sn = (char*)malloc(sizeof(char) * 50);
- char* trt = (char*)malloc(sizeof(char) * 200);
- int age, height, weight, stat, id;
- sscanf(buff, "%s %s %d %d %d %s %d %d", name, sn, &age, &height, &weight, trt, &stat, &id);
- int i = 0;
- for(i; i < strlen(trt); i++){
- if(trt[i] == '-'){
- trt[i] = ' ';
- }
- }
- push(&p, name, sn, age, height, weight, trt, stat, id);
- }
- fclose(f);
- return p;
- }
- Przestepstwa* preadFile(const char* path){
- FILE* f = fopen(path, "r");
- char buff[1000];
- Przestepstwa* c = NULL;
- if(!f) { printf("Nie udalo sie otworzyc pliku %s", path); return c; }
- while(fgets(buff, sizeof(buff), f)){
- iloscPrzestepstw++; pdefID++;
- int pid;
- char* miasto = (char*)malloc(sizeof(char)*100);
- int data;
- char* przest = (char*)malloc(sizeof(char)*100);
- int ilosc;
- sscanf(buff, "%d %s %d %s", &pid, miasto, &data, przest);
- int dl = strlen(przest);
- int i = 0;
- ppush(&c, pid, miasto, data, przest);
- }
- fclose(f);
- return c;
- }
- void printPrzestepcy(Przestepca* p){
- while(p != NULL){
- printf("%s %s %d %d %d %s %d %d\n", p->imie, p->nazwisko, p->wiek, p->wzrost, p->waga, p->cechy, p->status, p->id);
- p = p->next;
- }
- }
- void printPrzestepstwa(Przestepstwa* p){
- while(p != NULL){
- printf("\n%d %s %d %s", p->numer, p->miasto, p->data, p->crims);
- p = p->next;
- }
- }
- void addPrzestepca(Przestepca* p){
- printf("Podaj nowe dane przestepcy: ");
- printf("\nImie: ");
- char* name = (char*)malloc(sizeof(char)*50);
- scanf("%s", name);
- printf("\nNazwisko: ");
- char* sn = (char*)malloc(sizeof(char)*50);
- scanf("%s", sn);
- printf("\nPodaj kolejno wiek, wzrost, wage, status: ");
- int wiek, wzrost, waga, status;
- scanf("%d %d %d %d", &wiek, &wzrost, &waga, &status);
- char* cechy = (char*)calloc(200, sizeof(char));
- int ile = 0;
- printf("\nPodaj ilosc cech (1-5): ");
- scanf("%d", &ile);
- if(ile > 5) ile = 5;
- if(ile < 1 ) ile = 1;
- int i = 0;
- printf("\nPodaj cechy: ");
- for(i; i < ile; i++){
- char* input = (char*)malloc(sizeof(char)*50);
- scanf("%s", input);
- strcat(cechy, " ");
- strcat(cechy, input);
- }
- defID++;
- push(&p, name, sn, wiek, wzrost, waga, cechy, status, defID);
- }
- void addPrzestepstwo(Przestepstwa* p){
- printf("Podaj nowy numer przestepstwa: ");
- printf("\nNumer: ");
- int nr;
- scanf("%d", &nr);
- printf("\nMiasto: ");
- char* city = (char*)malloc(sizeof(char)*50);
- scanf("%s", city);
- printf("\nPodaj date przestepstwa: ");
- int date;
- scanf("%d", &date);
- printf("\nPodaj ilosc bioracych udzial przestepcow: ");
- int ile;
- scanf("%d", &ile);
- char* przest = (char*)calloc(100, sizeof(char));
- int i = 0;
- printf("\nPodaj numery ID przestepcow: ");
- for(i; i < ile; i++){
- char* input = (char*)malloc(sizeof(char)*50);
- scanf("%s", input);
- strcat(przest, " ");
- strcat(przest, input);
- }
- ppush(&p, nr, city, date, przest);
- }
- void remPrzestepca(Przestepca** p, int xid){
- Przestepca* temp = *p, *prev;
- if(temp != NULL && temp->id == xid){
- *p = temp->next;
- free(temp);
- iloscPrzestepcow--;
- return;
- }
- while(temp != NULL && temp->id != xid){
- prev = temp;
- temp = temp->next;
- }
- if(temp == NULL) return;
- prev->next = temp->next;
- free(temp);
- iloscPrzestepcow--;
- return;
- }
- void remPrzestepstwo(Przestepstwa** p, int xnr){
- Przestepstwa* temp = *p, *prev;
- if(temp != NULL && temp->numer == xnr){
- *p = temp->next;
- free(temp);
- iloscPrzestepstw--;
- return;
- }
- while(temp != NULL && temp->numer != xnr){
- prev = temp;
- temp = temp->next;
- }
- if(temp == NULL) return;
- prev->next = temp->next;
- free(temp);
- iloscPrzestepstw--;
- return;
- }
- void editPrzestepca(Przestepca** p, int xid){
- Przestepca* temp = *p;
- while(temp->id != xid && temp != NULL){
- temp = temp->next;
- }
- if(temp == NULL) return;
- printf("Podaj nowe dane przestepcy: ");
- printf("\nImie (stare: %s): ", temp->imie);
- char* name = (char*)malloc(sizeof(char)*50);
- scanf("%s", name);
- printf("\nNazwisko (stare: %s): ", temp->nazwisko);
- char* sn = (char*)malloc(sizeof(char)*50);
- scanf("%s", sn);
- printf("\nPodaj kolejno wiek (%d), wzrost (%d), wage (%d), status(%d): ", temp->wiek, temp->wzrost, temp->waga, temp->status);
- int wk, wt, wa, stat;
- scanf("%d %d %d %d", &wk, &wt, &wa, &stat);
- char* cech = (char*)calloc(200, sizeof(char));
- int ile = 0;
- printf("\nPodaj ilosc cech (1-5): ");
- scanf("%d", &ile);
- if(ile > 5) ile = 5;
- if(ile < 1 ) ile = 1;
- int i = 0;
- printf("\nPodaj cechy (%s): ", temp->cechy);
- for(i; i < ile; i++){
- char* input = (char*)malloc(sizeof(char)*50);
- scanf("%s", input);
- strcat(cech, " ");
- strcat(cech, input);
- }
- temp->imie = name;
- temp->nazwisko = sn;
- temp->status = stat;
- temp->wiek = wk;
- temp->wzrost = wt;
- temp->waga = wa;
- temp->cechy = cech;
- }
- void statusPrzestepcaSort(Przestepca* p){
- int swapped;
- Przestepca* ptr1;
- Przestepca* lptr = NULL;
- if (p == NULL)
- return;
- do
- {
- swapped = 0;
- ptr1 = p;
- while (ptr1->next != lptr)
- {
- if (ptr1->status > ptr1->next->status)
- {
- pswap(ptr1, ptr1->next);
- swapped = 1;
- }
- ptr1 = ptr1->next;
- }
- lptr = ptr1;
- }
- while (swapped);
- }
- void dataPrzestepstwaSort(Przestepstwa* p){
- int swapped;
- Przestepstwa* ptr1;
- Przestepstwa* lptr = NULL;
- if (p == NULL)
- return;
- do
- {
- swapped = 0;
- ptr1 = p;
- while (ptr1->next != lptr)
- {
- if (ptr1->data > ptr1->next->data)
- {
- ppswap(ptr1, ptr1->next);
- swapped = 1;
- }
- ptr1 = ptr1->next;
- }
- lptr = ptr1;
- }
- while (swapped);
- }
- void nazwiskoPrzestepcaSort(Przestepca* p){
- int swapped;
- Przestepca* ptr1;
- Przestepca* lptr = NULL;
- if (p == NULL)
- return;
- do
- {
- swapped = 0;
- ptr1 = p;
- while (ptr1->next != lptr)
- {
- if (strcmp(ptr1->nazwisko, ptr1->next->nazwisko) > 0)
- {
- pswap(ptr1, ptr1->next);
- swapped = 1;
- }
- ptr1 = ptr1->next;
- }
- lptr = ptr1;
- }
- while (swapped);
- }
- void miastoPrzestepstwaSort(Przestepstwa* p){
- int swapped;
- Przestepstwa* ptr1;
- Przestepstwa* lptr = NULL;
- if (p == NULL)
- return;
- do
- {
- swapped = 0;
- ptr1 = p;
- while (ptr1->next != lptr)
- {
- if (strcmp(ptr1->miasto, ptr1->next->miasto) > 0)
- {
- ppswap(ptr1, ptr1->next);
- swapped = 1;
- }
- ptr1 = ptr1->next;
- }
- lptr = ptr1;
- }
- while (swapped);
- }
- int main(){
- Przestepca* p = NULL;
- Przestepstwa* c = NULL;
- p = readFile("przestepcy.txt");
- c = preadFile("przestepstwa.txt");
- addPrzestepstwo(c);
- printPrzestepstwa(c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement