Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int iloscPrzestepstw = 0;
  6. int iloscPrzestepcow = 0;
  7. int defID = 0;
  8. int pdefID = 0;
  9.  
  10. char* intToStr(int n)
  11. {
  12. int i = 0;
  13. char tmp[12];
  14. static char ret[12];
  15. if(n < 0) {
  16. *ret = '-';
  17. i++;
  18. n = -n;
  19. }
  20. do {
  21. *tmp = n % 10 + 48;
  22. n -= n % 10;
  23. if(n > 9) (*tmp)++;
  24. }
  25. while(n /= 10);
  26. while(ret[i++] = (*tmp)--);
  27. return ret;
  28. }
  29.  
  30. typedef struct Przestepstwa{
  31. int numer;
  32. char* miasto;
  33. int data; /// YYYYMMDD 20120130
  34. char* crims;
  35. struct Przestepstwa *next;
  36. } Przestepstwa;
  37.  
  38. typedef struct Przestepca{
  39. char* imie;
  40. char* nazwisko;
  41. int wiek;
  42. int wzrost;
  43. int waga;
  44. char* cechy;
  45. int status;
  46. int id;
  47. struct Przestepca *next;
  48. } Przestepca;
  49. /* int levenshtein(char *s1, char *s2) {
  50. unsigned int x, y, s1len, s2len;
  51. s1len = strlen(s1);
  52. s2len = strlen(s2);
  53. unsigned int matrix[s2len+1][s1len+1];
  54. matrix[0][0] = 0;
  55. for (x = 1; x <= s2len; x++)
  56. matrix[x][0] = matrix[x-1][0] + 1;
  57. for (y = 1; y <= s1len; y++)
  58. matrix[0][y] = matrix[0][y-1] + 1;
  59. for (x = 1; x <= s2len; x++)
  60. for (y = 1; y <= s1len; y++)
  61. 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));
  62.  
  63. return(matrix[s2len][s1len]);
  64. } */
  65. void pswap(Przestepca* p1, Przestepca* p2){
  66. char* tname = p1->imie;
  67. char* tsn = p1->nazwisko;
  68. char* tcech = p1->cechy;
  69. int twiek = p1->wiek;
  70. int twzrost = p1->wzrost;
  71. int twaga = p1->waga;
  72. int tstatus = p1->status;
  73. int tid = p1->id;
  74. p1->imie = p2->imie;
  75. p1->nazwisko = p2->nazwisko;
  76. p1->cechy = p2->cechy;
  77. p1->wiek = p2->wiek;
  78. p1->wzrost = p2->wzrost;
  79. p1->waga = p2->waga;
  80. p1->status = p2->status;
  81. p1->id = p2->id;
  82. p2->imie = tname;
  83. p2->nazwisko = tsn;
  84. p2->cechy = tcech;
  85. p2->wiek = twiek;
  86. p2->wzrost = twzrost;
  87. p2->waga = twaga;
  88. p2->status = tstatus;
  89. p2->id = tid;
  90. }
  91. void ppswap(Przestepstwa* p1, Przestepstwa* p2){
  92. float tnumer = p1->numer;
  93. char* tmiasto = p1->miasto;
  94. int tdata = p1->data;
  95. char* tcrims = p1->crims;
  96. p1->crims = p2->crims;
  97. p1->numer = p2->numer;
  98. p1->miasto = p2->miasto;
  99. p1->data = p2->data;
  100. p2->numer = tnumer;
  101. p2->miasto = tmiasto;
  102. p2->data = tdata;
  103. p2->crims = tcrims;
  104. }
  105.  
  106. void push(Przestepca** p, char* name, char* surname, int age, int height, int weight, char* traits, int stat, int nr){
  107. Przestepca* nowy = (Przestepca*)malloc(sizeof(Przestepca));
  108. Przestepca* last = *p;
  109.  
  110. nowy->imie = name;
  111. nowy->nazwisko = surname;
  112. nowy->wiek = age;
  113. nowy->wzrost = height;
  114. nowy->waga = weight;
  115. nowy->cechy = traits;
  116. nowy->status = stat;
  117. nowy->id = nr;
  118. nowy->next = NULL;
  119.  
  120. if(*p == NULL){
  121. *p = nowy;
  122. return;
  123. }
  124. else{
  125. while(last->next != NULL){
  126. last = last->next;
  127. }
  128. last->next = nowy;
  129. return;
  130. }
  131. }
  132.  
  133. void ppush(Przestepstwa** p, float nr, char* city, int date, char* criminals){
  134. Przestepstwa* nowy = (Przestepstwa*)malloc(sizeof(Przestepstwa));
  135. Przestepstwa* last = *p;
  136.  
  137. nowy->numer = nr;
  138. nowy->miasto = city;
  139. nowy->data = date;
  140. nowy->next = NULL;
  141. nowy->crims = criminals;
  142. if(*p == NULL){
  143. *p = nowy;
  144. return;
  145. }
  146. else{
  147. while(last->next != NULL){
  148. last = last->next;
  149. }
  150. last->next = nowy;
  151. return;
  152. }
  153. }
  154.  
  155. Przestepca* readFile(const char* path){
  156. FILE* f = fopen(path, "r");
  157. char buff[1000];
  158. int nr = 0;
  159. Przestepca *p = NULL;
  160. if(!f) { printf("Nie udalo sie otworzyc pliku %s", path); return p; }
  161.  
  162. while(fgets(buff, sizeof(buff), f)){
  163. iloscPrzestepcow++; defID++;
  164. char* name = (char*)malloc(sizeof(char) * 50);
  165. char* sn = (char*)malloc(sizeof(char) * 50);
  166. char* trt = (char*)malloc(sizeof(char) * 200);
  167. int age, height, weight, stat, id;
  168.  
  169. sscanf(buff, "%s %s %d %d %d %s %d %d", name, sn, &age, &height, &weight, trt, &stat, &id);
  170. int i = 0;
  171. for(i; i < strlen(trt); i++){
  172. if(trt[i] == '-'){
  173. trt[i] = ' ';
  174. }
  175. }
  176.  
  177. push(&p, name, sn, age, height, weight, trt, stat, id);
  178. }
  179. fclose(f);
  180. return p;
  181. }
  182.  
  183. Przestepstwa* preadFile(const char* path){
  184. FILE* f = fopen(path, "r");
  185. char buff[1000];
  186.  
  187. Przestepstwa* c = NULL;
  188. if(!f) { printf("Nie udalo sie otworzyc pliku %s", path); return c; }
  189. while(fgets(buff, sizeof(buff), f)){
  190. iloscPrzestepstw++; pdefID++;
  191. int pid;
  192. char* miasto = (char*)malloc(sizeof(char)*100);
  193. int data;
  194. char* przest = (char*)malloc(sizeof(char)*100);
  195. int ilosc;
  196. sscanf(buff, "%d %s %d %s", &pid, miasto, &data, przest);
  197. int dl = strlen(przest);
  198. int i = 0;
  199. ppush(&c, pid, miasto, data, przest);
  200. }
  201. fclose(f);
  202. return c;
  203. }
  204.  
  205. void printPrzestepcy(Przestepca* p){
  206. while(p != NULL){
  207. 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);
  208. p = p->next;
  209. }
  210. }
  211. void printPrzestepstwa(Przestepstwa* p){
  212. while(p != NULL){
  213. printf("\n%d %s %d %s", p->numer, p->miasto, p->data, p->crims);
  214. p = p->next;
  215. }
  216. }
  217. void addPrzestepca(Przestepca* p){
  218. printf("Podaj nowe dane przestepcy: ");
  219. printf("\nImie: ");
  220. char* name = (char*)malloc(sizeof(char)*50);
  221. scanf("%s", name);
  222. printf("\nNazwisko: ");
  223. char* sn = (char*)malloc(sizeof(char)*50);
  224. scanf("%s", sn);
  225. printf("\nPodaj kolejno wiek, wzrost, wage, status: ");
  226. int wiek, wzrost, waga, status;
  227. scanf("%d %d %d %d", &wiek, &wzrost, &waga, &status);
  228. char* cechy = (char*)calloc(200, sizeof(char));
  229. int ile = 0;
  230. printf("\nPodaj ilosc cech (1-5): ");
  231. scanf("%d", &ile);
  232. if(ile > 5) ile = 5;
  233. if(ile < 1 ) ile = 1;
  234. int i = 0;
  235. printf("\nPodaj cechy: ");
  236. for(i; i < ile; i++){
  237. char* input = (char*)malloc(sizeof(char)*50);
  238. scanf("%s", input);
  239. strcat(cechy, " ");
  240. strcat(cechy, input);
  241. }
  242. defID++;
  243. push(&p, name, sn, wiek, wzrost, waga, cechy, status, defID);
  244. }
  245. void addPrzestepstwo(Przestepstwa* p){
  246. printf("Podaj nowy numer przestepstwa: ");
  247. printf("\nNumer: ");
  248. int nr;
  249. scanf("%d", &nr);
  250. printf("\nMiasto: ");
  251. char* city = (char*)malloc(sizeof(char)*50);
  252. scanf("%s", city);
  253. printf("\nPodaj date przestepstwa: ");
  254. int date;
  255. scanf("%d", &date);
  256. printf("\nPodaj ilosc bioracych udzial przestepcow: ");
  257. int ile;
  258. scanf("%d", &ile);
  259. char* przest = (char*)calloc(100, sizeof(char));
  260. int i = 0;
  261. printf("\nPodaj numery ID przestepcow: ");
  262. for(i; i < ile; i++){
  263. char* input = (char*)malloc(sizeof(char)*50);
  264. scanf("%s", input);
  265. strcat(przest, " ");
  266. strcat(przest, input);
  267. }
  268. ppush(&p, nr, city, date, przest);
  269. }
  270.  
  271. void remPrzestepca(Przestepca** p, int xid){
  272. Przestepca* temp = *p, *prev;
  273. if(temp != NULL && temp->id == xid){
  274. *p = temp->next;
  275. free(temp);
  276. iloscPrzestepcow--;
  277. return;
  278. }
  279. while(temp != NULL && temp->id != xid){
  280. prev = temp;
  281. temp = temp->next;
  282. }
  283. if(temp == NULL) return;
  284. prev->next = temp->next;
  285. free(temp);
  286. iloscPrzestepcow--;
  287. return;
  288. }
  289. void remPrzestepstwo(Przestepstwa** p, int xnr){
  290. Przestepstwa* temp = *p, *prev;
  291. if(temp != NULL && temp->numer == xnr){
  292. *p = temp->next;
  293. free(temp);
  294. iloscPrzestepstw--;
  295. return;
  296. }
  297. while(temp != NULL && temp->numer != xnr){
  298. prev = temp;
  299. temp = temp->next;
  300. }
  301. if(temp == NULL) return;
  302. prev->next = temp->next;
  303. free(temp);
  304. iloscPrzestepstw--;
  305. return;
  306. }
  307.  
  308. void editPrzestepca(Przestepca** p, int xid){
  309. Przestepca* temp = *p;
  310. while(temp->id != xid && temp != NULL){
  311. temp = temp->next;
  312. }
  313. if(temp == NULL) return;
  314. printf("Podaj nowe dane przestepcy: ");
  315. printf("\nImie (stare: %s): ", temp->imie);
  316. char* name = (char*)malloc(sizeof(char)*50);
  317. scanf("%s", name);
  318. printf("\nNazwisko (stare: %s): ", temp->nazwisko);
  319. char* sn = (char*)malloc(sizeof(char)*50);
  320. scanf("%s", sn);
  321. printf("\nPodaj kolejno wiek (%d), wzrost (%d), wage (%d), status(%d): ", temp->wiek, temp->wzrost, temp->waga, temp->status);
  322. int wk, wt, wa, stat;
  323. scanf("%d %d %d %d", &wk, &wt, &wa, &stat);
  324. char* cech = (char*)calloc(200, sizeof(char));
  325. int ile = 0;
  326. printf("\nPodaj ilosc cech (1-5): ");
  327. scanf("%d", &ile);
  328. if(ile > 5) ile = 5;
  329. if(ile < 1 ) ile = 1;
  330. int i = 0;
  331. printf("\nPodaj cechy (%s): ", temp->cechy);
  332. for(i; i < ile; i++){
  333. char* input = (char*)malloc(sizeof(char)*50);
  334. scanf("%s", input);
  335. strcat(cech, " ");
  336. strcat(cech, input);
  337. }
  338. temp->imie = name;
  339. temp->nazwisko = sn;
  340. temp->status = stat;
  341. temp->wiek = wk;
  342. temp->wzrost = wt;
  343. temp->waga = wa;
  344. temp->cechy = cech;
  345. }
  346.  
  347.  
  348. void statusPrzestepcaSort(Przestepca* p){
  349. int swapped;
  350. Przestepca* ptr1;
  351. Przestepca* lptr = NULL;
  352.  
  353. if (p == NULL)
  354. return;
  355.  
  356. do
  357. {
  358. swapped = 0;
  359. ptr1 = p;
  360.  
  361. while (ptr1->next != lptr)
  362. {
  363. if (ptr1->status > ptr1->next->status)
  364. {
  365. pswap(ptr1, ptr1->next);
  366. swapped = 1;
  367. }
  368. ptr1 = ptr1->next;
  369. }
  370. lptr = ptr1;
  371. }
  372. while (swapped);
  373. }
  374.  
  375. void dataPrzestepstwaSort(Przestepstwa* p){
  376. int swapped;
  377. Przestepstwa* ptr1;
  378. Przestepstwa* lptr = NULL;
  379.  
  380. if (p == NULL)
  381. return;
  382.  
  383. do
  384. {
  385. swapped = 0;
  386. ptr1 = p;
  387.  
  388. while (ptr1->next != lptr)
  389. {
  390. if (ptr1->data > ptr1->next->data)
  391. {
  392. ppswap(ptr1, ptr1->next);
  393. swapped = 1;
  394. }
  395. ptr1 = ptr1->next;
  396. }
  397. lptr = ptr1;
  398. }
  399. while (swapped);
  400. }
  401.  
  402. void nazwiskoPrzestepcaSort(Przestepca* p){
  403. int swapped;
  404. Przestepca* ptr1;
  405. Przestepca* lptr = NULL;
  406.  
  407. if (p == NULL)
  408. return;
  409.  
  410. do
  411. {
  412. swapped = 0;
  413. ptr1 = p;
  414.  
  415. while (ptr1->next != lptr)
  416. {
  417. if (strcmp(ptr1->nazwisko, ptr1->next->nazwisko) > 0)
  418. {
  419. pswap(ptr1, ptr1->next);
  420. swapped = 1;
  421. }
  422. ptr1 = ptr1->next;
  423. }
  424. lptr = ptr1;
  425. }
  426. while (swapped);
  427. }
  428.  
  429.  
  430. void miastoPrzestepstwaSort(Przestepstwa* p){
  431. int swapped;
  432. Przestepstwa* ptr1;
  433. Przestepstwa* lptr = NULL;
  434.  
  435. if (p == NULL)
  436. return;
  437.  
  438. do
  439. {
  440. swapped = 0;
  441. ptr1 = p;
  442.  
  443. while (ptr1->next != lptr)
  444. {
  445. if (strcmp(ptr1->miasto, ptr1->next->miasto) > 0)
  446. {
  447. ppswap(ptr1, ptr1->next);
  448. swapped = 1;
  449. }
  450. ptr1 = ptr1->next;
  451. }
  452. lptr = ptr1;
  453. }
  454. while (swapped);
  455. }
  456.  
  457.  
  458.  
  459. int main(){
  460. Przestepca* p = NULL;
  461. Przestepstwa* c = NULL;
  462. p = readFile("przestepcy.txt");
  463. c = preadFile("przestepstwa.txt");
  464. addPrzestepstwo(c);
  465. printPrzestepstwa(c);
  466. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement