Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.30 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <locale.h>
  5. #include <string.h>
  6. #include <conio.h>
  7.  
  8. struct Pupil
  9. {
  10.     char name[50];
  11.     char surname[50];
  12.     int index;
  13. };
  14.  
  15. struct List
  16. {
  17.     struct List* prev;
  18.     struct Pupil pupil;
  19.     struct List* next;
  20. };
  21.  
  22. struct List* active;
  23. struct List* head;
  24. struct List* last;
  25.  
  26. void LoadContacts();
  27. void InsertAtHead(char *name, char *surname, int index);
  28. struct List* GetNew(char *name, char *surname, int index);
  29. void Print();
  30. void PrintPupil(int index);
  31. int Check(int index);
  32.  
  33.  
  34. int main ()
  35. {
  36.     setlocale(LC_ALL, "polish_poland");
  37.     char choice;
  38.     char text[50];
  39.     int index;
  40.     char password[50];
  41.     char pass_base[] = "abcd";
  42.     int teachers_number;
  43.     int Tnumber = 1234;
  44.     int comparison;
  45.     int check;
  46.     int action;
  47.  
  48.  
  49.     head = NULL;
  50.     last = NULL;
  51.  
  52.     LoadContacts();
  53.  
  54.     printf(":::JSOS 3.0:::\n");
  55.    
  56.     while (1)
  57.     {
  58.         printf("Jaki rodzaj logowania wybierasz?\n");
  59.         printf("1 - nauczyciel.\n");
  60.         printf("2 - uczen.\n");
  61.         printf("0 - wyjscie.\n");
  62.  
  63.         choice = getchar();
  64.         while (getchar() != '\n');  
  65.  
  66.         switch (choice)
  67.         {
  68.         case '1':
  69.             while (1)
  70.             {
  71.                 printf("Podaj numer identyfikacyjny.\n");
  72.                 scanf("%d", &teachers_number);
  73.                 if ((teachers_number) == (Tnumber))
  74.                 {
  75.                     Print();
  76.                     while (getchar() != '\n');
  77.                     printf("Wcisnij 0 aby sie wylogowac.\n");
  78.                     while (1)
  79.                     {
  80.                         scanf("%d", &action);
  81.                         if (action == 0)
  82.                             break;
  83.                     }
  84.                     if (action == 0)
  85.                     {
  86.                         while (getchar() != '\n');
  87.                         break;
  88.                     }                  
  89.                 }
  90.                 else
  91.                 {
  92.                     printf("Podales bledny numer.\n");
  93.                 }
  94.             }
  95.            
  96.             break;
  97.  
  98.         case '2':
  99.             printf("Podaj login - numer indeksu.\n");
  100.             gets(text);
  101.             sscanf(text, "%d", &index);
  102.             printf("Podaj haslo.\n");
  103.             gets(password);
  104.             check = Check(index);
  105.             comparison = strcoll(password, pass_base);
  106.             if ((comparison = 0) && (check = 1))
  107.             {
  108.                 PrintPupil(index);
  109.                 while (getchar() != '\n');
  110.                 printf("Wcisnij 0 aby sie wylogowac.\n");
  111.                 break;
  112.             }
  113.             else
  114.             {
  115.                 printf("Podales bledne haslo lub zly login.\n");
  116.             }
  117.             break;
  118.  
  119.         case '0':
  120.             goto end;
  121.             break;
  122.  
  123.         default:
  124.             printf("Nie wybrales zadnej z opcji logowania.\n");
  125.             break;
  126.  
  127.         }
  128.     }
  129.  
  130. end:
  131.     return 0;
  132. }
  133.  
  134. void LoadContacts()
  135. {
  136.     char text[150];
  137.     char name[50];
  138.     char surname[50];
  139.     char index[50];
  140.     int index_int;
  141.  
  142.     FILE *file;
  143.     file = fopen("klasa.txt", "r");
  144.  
  145.     while (1)
  146.     {
  147.         fscanf(file, "%s", text);
  148.         sscanf(text, "%[^;] ; %[^;] ; %[^;]", name, surname, index);
  149.         sscanf(index, "%d", &index_int);
  150.         if (fgetc(file) == EOF)
  151.         {
  152.             InsertAtHead(name, surname, index_int);
  153.             break;
  154.         }  
  155.         else
  156.         {
  157.             InsertAtHead(name, surname, index_int); //przekazanie danych do funkcji tworzacej strukture i dodajacej ja do listy
  158.         }
  159.            
  160.     }
  161.        
  162.     fclose(file);
  163. }
  164.  
  165. void InsertAtHead(char *name, char *surname, int index)
  166. {
  167.     struct List* new = GetNew(name, surname, index);
  168.  
  169.     if (head == NULL)  //jesli nic nie ma w liscie to nowy element jest pierwszym elementem
  170.     {
  171.         head = new;
  172.         last = new;
  173.     }
  174.     else
  175.     {
  176.         head->prev = new;
  177.         new->next = head;
  178.         head = new;
  179.     }
  180. }
  181.  
  182. struct List* GetNew(char *name, char *surname, int index)
  183. {
  184.     int i;
  185.     struct List* new = (struct List*)malloc(sizeof(struct List));
  186.  
  187.     for (i = 0; i < 50; i++)
  188.     {
  189.         new->pupil.name[i] = name[i];
  190.     }
  191.     for (i = 0; i < 50; i++)
  192.     {
  193.         new->pupil.surname[i] = surname[i];
  194.     }
  195.  
  196.     new->pupil.index = index;
  197.  
  198.     new->prev = NULL;
  199.     new->next = NULL;
  200.  
  201.     return new;
  202. }
  203.  
  204. void Print()
  205. {
  206.     active = head;
  207.     printf("Lista uczniow.\n");
  208.     while (active != NULL)
  209.     {
  210.         printf("Imie: %s, Nazwisko: %s, Numer indeksu: %d\n", active->pupil.name, active->pupil.surname, active->pupil.index);
  211.         active = active->next;
  212.     }
  213.     printf("\n");
  214.     active = NULL;
  215. }
  216.  
  217. void PrintPupil(int index)
  218. {
  219.     active = head;
  220.  
  221.     while (active != NULL)
  222.     {
  223.         if (active->pupil.index == index)
  224.         {
  225.             printf("Imie i nazwisko: %s %s, Numer indeksu: %d\n", active->pupil.name, active->pupil.surname, active->pupil.index);
  226.         }
  227.         active = active->next;
  228.     }
  229.     printf("\n");
  230.     active = NULL;
  231.  
  232. }
  233.  
  234. int Check(int index)
  235. {
  236.     int flag = 0;
  237.     active = head;
  238.  
  239.     while (active != NULL)
  240.     {
  241.         if (index == active->pupil.index)
  242.         {
  243.             flag = 1;
  244.         }
  245.         active = active->next;
  246.     }
  247.     active = NULL;
  248.     return flag;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement