Advertisement
Crackbone

SP-Zadatak11

Jan 17th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. /*Napisati kod koji za zadane podatke studenata (matični broj, ime i prezime) pravi hash tablicu sa zasebnim redovima.
  2. Tablica ima 11 mjesta, a funkcija za preslikavanje ključ računa da se zbraja ASCII vrijednost prvih pet slova
  3. prezimena i zatim računa ostatak cjelobrojnog dijeljenja te vrijednosti s veličinom tablice.
  4. Studenti s istim ključem se pohranjuju u vezanu listu sortiranu po prezimenima i imenima.
  5. Kada se tablica izgradi treba ispisati cijelu tablicu (naglasiti na kojem ključu se nalaze koji podaci),
  6. te ponuditi mogućnostda se za određenog studenta (prezime i ime) ispiše njegov matični broj.*/
  7.  
  8.  
  9.  
  10.  
  11. #define _CRT_SECURE_NO_WARNINGS
  12. #define MAXNAME 50
  13. #define TableSize 11
  14.  
  15. #include<stdio.h>
  16. #include<errno.h>
  17. #include<string.h>
  18. #include<stdlib.h>
  19. #include<ctype.h>
  20. #include <stdbool.h>
  21.  
  22.  
  23. typedef struct Student;
  24. typedef struct Student* Pointer_Student;
  25.  
  26. struct Student
  27. {
  28.     char First_Name[MAXNAME];
  29.     char Last_Name[MAXNAME];
  30.     int Number;
  31.     Pointer_Student Next;
  32.  
  33. };
  34. //Funkcije
  35. //ClearScren
  36. void clrscr()
  37. {
  38.     system("@cls||clear");
  39. }
  40.  
  41. int Key(char *LastName);
  42. void menu();
  43. //Funkcija za unos podataka u tablicu
  44. int Input(Pointer_Student [],char[], char[], int);
  45.  
  46. int main()
  47. {
  48.     Pointer_Student Table[TableSize];
  49.    
  50.  
  51.  
  52.     for (int i = 0; i < TableSize; i++)
  53.     {
  54.         Table[i] = malloc(sizeof(struct Student));
  55.         Table[i]->Next = NULL;
  56.     }
  57.     bool Condition = true;
  58.     int x = 99,IdNumber=0;
  59.     char FirtName[MAXNAME], LastName[MAXNAME];
  60.  
  61.     while (Condition == true)
  62.     {
  63.         //Dodaj Clearscreen ovde
  64.         menu();
  65.         scanf(" %d", &x);
  66.         if (1 == x)
  67.         {
  68.             printf("Unesi ime studenta:\n");
  69.             scanf(" %s", FirtName);
  70.             printf("Unesi prezime studenta:\n");
  71.             scanf(" %s", LastName);
  72.             printf("Unesi maticni broj studenta:\n");
  73.             scanf(" %d", &IdNumber);
  74.             Input(Table,&FirtName,&LastName,IdNumber);
  75.  
  76.         }
  77.         else if (2 == x)
  78.         {
  79.             Print_Table(Table);
  80.         }
  81.         else if (x == 9)
  82.         {
  83.             Condition = false;
  84.         }
  85.     }
  86.  
  87.     getchar();
  88.     getchar();
  89.     return 1;
  90.  
  91. }
  92.  
  93.  
  94.  
  95. int Key(char * LastName)
  96. {
  97.     int key = 11;
  98.     char letters[5];
  99.     int i = 0,sum=0;
  100.     while (LastName[i]!='\0'&&i<5)
  101.     {
  102.         letters[i] = LastName[i];
  103.         sum = sum + letters[i];
  104.         i++;
  105.  
  106.     }
  107.     key = sum % TableSize;
  108.     return key;
  109. }
  110.  
  111.  
  112. void menu()
  113. {
  114.     printf("Odaberi sta oces:\n");
  115.     printf("Dodaj studenta:    1\n");
  116.     printf("Ispisi tablicu:    2\n");
  117.     printf("Izadji:            9\n");
  118. }
  119.  
  120.  
  121.  
  122. int Input(Pointer_Student Tablica[], char FirstName[], char LastName[], int IdNumber)
  123. {
  124.     int i = Key(LastName);
  125.     Pointer_Student Row = Tablica[i];
  126.     while ((Row->Next != NULL) && (strcmp(Row->Last_Name, LastName) < 0))
  127.         Row = Row->Next;
  128.     Pointer_Student q = (Pointer_Student)malloc(sizeof(struct Student));
  129.     q->Number = IdNumber;
  130.     strcpy(q->First_Name, FirstName);
  131.     strcpy(q->Last_Name, LastName);
  132.     q->Next = Row->Next;
  133.     Row->Next = q;
  134.     return 0;
  135. }
  136.  
  137.  
  138. int Print_Table(Pointer_Student Tablica[])
  139. {
  140.     for (int i = 0; i < TableSize; i++)
  141.     {
  142.         printf("\n %d. red tablice:\n",i);
  143.         Pointer_Student Row = Tablica[i];
  144.         while(Row->Next != NULL)
  145.         {
  146.             printf("\nIme: %s \nPrezime: %s \nMaticni broj: %d", Row->First_Name, Row->Last_Name, Row->Number);
  147.         }
  148.     }
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement