Advertisement
weeez

beadando_nemertem

Mar 28th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h> // file írás, olvasás
  5. #include <sys/types.h> //file
  6. #include <string.h>
  7. #include <time.h>
  8. #include <stdbool.h>
  9. #include <sys/stat.h>//időponthoz
  10. #include <unistd.h> //időponthoz
  11.  
  12. typedef struct
  13. {
  14.     char* megfigyelo_neve;
  15.     char* telepules_nev;
  16.     int nyuszi_db;
  17.     char* megfigyelesi_ido;
  18.     char* nyuszi_kor;
  19. } megfigyeles;
  20.  
  21. static struct stat st; 
  22. static int db = 0;
  23. static int *db_p = &db;
  24. static size_t memoria = 100;
  25. static size_t charhossz = 100;
  26. static char* filenev = "eredmeny.txt";
  27.  
  28. void file_olvasas(megfigyeles* tomb, int* darab_p)
  29. {  
  30.     FILE *file= fopen(filenev, "r");
  31.     *darab_p = 0;
  32.    
  33.     char output[512];
  34.    
  35.     while (fgets(output, 100, file) != NULL)
  36.     {      
  37.         megfigyeles mf;
  38.         sscanf (output, "%s\t %s\t %d\t %s\t %s\t",
  39.                 mf.megfigyelo_neve,
  40.                 mf.telepules_nev,
  41.                 &mf.nyuszi_db,
  42.                 mf.nyuszi_kor,
  43.                 mf.megfigyelesi_ido);
  44.         tomb[*darab_p] = mf;
  45.         ++(*darab_p);
  46.     }
  47.    
  48.     fclose(file);
  49. }
  50. void file_iras(megfigyeles *mf){
  51.     FILE *file;
  52.    
  53.     file = fopen(filenev, "a");
  54.    
  55.     fprintf(file, "%s\t %s\t %d\t %s\t %s\n",
  56.         mf->megfigyelo_neve,
  57.         mf->telepules_nev,
  58.         mf->nyuszi_db,
  59.         mf->nyuszi_kor,
  60.         mf->megfigyelesi_ido);
  61.     fclose(file);
  62. }
  63.  
  64. void torles(megfigyeles *tomb)
  65. {
  66.     int i;
  67.     char *nev;
  68.     char *tnev;
  69.     nev = (char *)malloc(charhossz);
  70.     tnev = (char *)malloc(charhossz);
  71.     printf("Adja meg a modositani kivant adatokat!\n");
  72.     printf("Adja meg a keresett nevet!\n");
  73.     scanf("%s", nev);  
  74.     printf("Adja meg a keresett telepulest!\n");
  75.     scanf("%s", tnev); 
  76.  
  77.     megfigyeles *tomb2;
  78.     tomb2 = (megfigyeles *)malloc(memoria);
  79.     int j = 0;
  80.     for(i = 0; i < db; ++i)
  81.     {
  82.         if(strcmp(tomb[i].megfigyelo_neve, nev) != 0)
  83.         {
  84.             if(strcmp(tomb[i].telepules_nev, tnev) != 0)
  85.             {
  86.                 tomb2[j] = tomb[i];
  87.                 ++j;
  88.             }      
  89.         }
  90.  
  91.     }
  92.  
  93.     tomb = tomb2;
  94.     file_iras(tomb2);
  95. }
  96. void modositas(megfigyeles *tomb)
  97. {
  98.     int i;
  99.     char *nev;
  100.     char *tnev;
  101.     nev = (char *)malloc(charhossz);
  102.     tnev = (char *)malloc(charhossz);
  103.     printf("Adja meg a modositani kivant adatokat!\n");
  104.     printf("Adja meg a keresett nevet!\n");
  105.     scanf("%s", nev);  
  106.     printf("Adja meg a keresett telepulest!\n");
  107.     scanf("%s", tnev); 
  108.     for(i = 0; i < db; ++i)
  109.     {
  110.         if(strcmp(tomb[i].megfigyelo_neve, nev) == 0)
  111.         {
  112.             if(strcmp(tomb[i].telepules_nev, tnev) == 0)
  113.             {
  114.                 int darab = 0;
  115.                 printf("Adja meg a nyuszik szamat!\n");
  116.                 scanf("%d", &darab);
  117.                 tomb[i].nyuszi_db = darab;
  118.  
  119.                 int val = -1;
  120.                 printf("Adja meg, milyen idos a nyul!\n"); 
  121.                 printf("(1) Gyerek\n");
  122.                 printf("(2) Felnott\n");
  123.                 printf("(3) Idos\n");
  124.                 while(val != 0)
  125.                 {
  126.                     scanf("%d", &val);
  127.            
  128.                     switch(val)
  129.                     {
  130.                         case 1:
  131.                             tomb[i].nyuszi_kor = "GYEREK";
  132.                             val = 0;
  133.                             break;
  134.                         case 2:
  135.                             tomb[i].nyuszi_kor = "FELNOTT";
  136.                             val = 0;
  137.                             break;
  138.                         case 3:
  139.                             tomb[i].nyuszi_kor = "OREG";
  140.                             val = 0;
  141.                             break;
  142.                         default:
  143.                             printf("\n\nHIBAS ERTEKET ADOTT MEG\n\n");
  144.                         break;
  145.                     }
  146.                 }
  147.                 time_t datum;
  148.                 struct tm * ido;
  149.                
  150.                 time (&datum);
  151.                 ido = localtime (&datum);
  152.                
  153.                 strftime(tomb[i].megfigyelesi_ido,80,"%Y.%m.%d._%H:%M:%S",ido);                    
  154.             }      
  155.         }
  156.  
  157.     }
  158.  
  159.     file_iras(tomb);
  160. }
  161. void menu()
  162. {
  163.     printf("----------------------------------------------\n");
  164.     printf("\n  Valasszon az alabbi opciok kozul\n");
  165.     printf("\t(1) - Megfigyeles rogzitese\n");
  166.     printf("\t(2) - Megfigyeles modositasa\n");
  167.     printf("\t(3) - Megfigyeles torlese\n");
  168.     printf("\t(4) - Megfigyeles listazasa\n");
  169.     printf("\t(0) - Kilepes\n");
  170.     printf("----------------------------------------------\n>> ");
  171. }
  172. void rogzites(megfigyeles* tomb)
  173. {
  174.     megfigyeles mf;
  175.     char* nev;
  176.     nev = (char *) malloc(charhossz);
  177.     char* tnev;
  178.     tnev = (char *) malloc(charhossz);
  179.     int darab;
  180.     mf.megfigyelesi_ido = (char *) malloc(charhossz);
  181.    
  182.     printf("Adja meg a megfigyelo nevet!\n");
  183.     scanf("%s", nev);
  184.     mf.megfigyelo_neve = nev;
  185.  
  186.     printf("Adja meg a telepules nevet!\n");
  187.     scanf("%s", tnev);
  188.     mf.telepules_nev = tnev;
  189.      
  190.     printf("Adja meg a nyuszik szamat!\n");
  191.     scanf("%d", &darab);
  192.     mf.nyuszi_db = darab;
  193.  
  194.     int val = -1;
  195.     printf("Adja meg, milyen idos a nyul!\n"); 
  196.     printf("(1) Gyerek\n");
  197.     printf("(2) Felnott\n");
  198.     printf("(3) Idos\n");
  199.      while(val != 0){
  200.         scanf("%d", &val);
  201.        
  202.         switch(val)
  203.         {
  204.             case 1:
  205.                 mf.nyuszi_kor = "GYEREK";
  206.                 val = 0;
  207.                 break;
  208.             case 2:
  209.                 mf.nyuszi_kor = "FELNOTT";
  210.                 val = 0;
  211.                 break;
  212.             case 3:
  213.                 mf.nyuszi_kor = "OREG";
  214.                 val = 0;
  215.                 break;
  216.             default:
  217.                 printf("\n\nHIBAS ERTEKET ADOTT MEG\n\n");
  218.                 break;
  219.         }
  220.     }
  221.    
  222.     time_t datum;
  223.     struct tm * ido;
  224.    
  225.     time (&datum);
  226.     ido = localtime (&datum);
  227.    
  228.     strftime(mf.megfigyelesi_ido,80,"%Y.%m.%d._%H:%M:%S",ido);
  229.  
  230.     tomb[db] = mf;
  231.     ++db;
  232.     file_iras(tomb);
  233.     //realloc//
  234.     /*
  235.     if(db >= (memoria/2))
  236.     {
  237.         printf("wtf\n");
  238.         memoria += sizeof(megfigyeles);
  239.         tomb = (megfigyeles *)realloc(tomb,memoria);
  240.     }*/
  241. }
  242.  
  243. void listazas(megfigyeles* tomb){
  244.     int darab;
  245.     int *darab_p = &darab;
  246.     file_olvasas(tomb, darab_p);
  247.     int i;
  248.     for(i = 0; i < darab; ++i){
  249.         printf("%s\t %s\t %d\t %s\t %s\n",
  250.             tomb[i].megfigyelo_neve,
  251.             tomb[i].telepules_nev,
  252.             tomb[i].nyuszi_db,
  253.             tomb[i].nyuszi_kor,
  254.             tomb[i].megfigyelesi_ido);
  255.     }
  256.     db = darab;
  257.     printf("\n");
  258. }
  259. int main(int argc, char** argv)
  260. {
  261.     megfigyeles *tomb;
  262.     tomb = (megfigyeles *)malloc(memoria);
  263.     stat(argv[0],&st);
  264.  
  265.     printf("hellobello!\n");
  266.  
  267.     //menü
  268.     char* valasz;
  269.     valasz = (char*)malloc(charhossz);
  270.     do{
  271.         menu();
  272.         scanf("%s", valasz);
  273.         if(strcmp(valasz, "1") == 0){
  274.             printf("\n\nRogzites\n\n");
  275.             rogzites(tomb);
  276.         }
  277.         else if(strcmp(valasz, "2") == 0){
  278.             printf("\n\nModositas\n\n");
  279.             modositas(tomb);
  280.         }
  281.         else if(strcmp(valasz, "3") == 0){
  282.             printf("\n\nTorles\n\n");
  283.             torles(tomb);
  284.         }
  285.         else if(strcmp(valasz, "4") == 0){
  286.             printf("\n\nListazas\n\n");
  287.             listazas(tomb);
  288.         }
  289.         else if(strcmp(valasz, "0") == 0){
  290.         }
  291.         else{
  292.             printf("\n\nHIBAS ERTEKET ADOTT MEG\n\n");
  293.         }
  294.     }while(strcmp(valasz, "0") != 0);
  295.  
  296.     return 0;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement