Advertisement
weeez

bead2 - kész

Nov 24th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h> // for open()
  4. #include <errno.h> // for perror()
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/wait.h> //waitpid
  8. #include <string.h> //for strcpy()
  9. #include <unistd.h> //for file write() / fork() /pipe()
  10. #include <time.h>
  11.  
  12. struct Kivansag {
  13.   char date[100];
  14.   char name[100];
  15.   char city[100];
  16.   char address[100];
  17.   char gift[100];
  18. };
  19.  
  20. const char *FILE_NAME = "kivansagok.scv";
  21. FILE *fileContents;
  22. int list_size = 0;
  23. struct Kivansag gifts[1000];  
  24.  
  25. struct Kivansag rowToKivansag(char row[1000]);
  26. char *kivansagToRow(struct Kivansag kivansag);
  27. char *swapSpace(char *row);
  28. int isCorrect(char answer[10]);
  29. void menu_print();
  30. void cutEnter(char string[100]);
  31. struct Kivansag getEmptyKivansag();
  32. int isEmptyKivansag(struct Kivansag kivansag);
  33.  
  34. int main(){
  35.        
  36.   char msg[200000];
  37.   char msg2[200000];
  38.   int status;
  39.  
  40.  
  41.     if((fileContents= fopen(FILE_NAME, "rb")) == NULL){
  42.         if(errno == ENOENT){//if "kivansagok" file doesn't exists
  43.             printf("A Kivansagok lista fajlja nem letezik! \tKilepes...\n");
  44.             exit(1);
  45.         }
  46.     }else{
  47.         //file read start
  48.         fscanf(fileContents,"%d\n",&list_size); //init list_size
  49.        
  50.         if(list_size > 0){
  51.             int i; 
  52.                
  53.             for(i = 0; i < list_size; i++){ // init gifts
  54.                 struct Kivansag kivansag;
  55.                 char row[1000];
  56.                 fscanf(fileContents,"%s",row);
  57.                 kivansag = rowToKivansag(row);
  58.                 gifts[i] = kivansag;
  59.             }
  60.  
  61.             printf("TESZT*****************\n");
  62.             for(i = 0; i < list_size; i++){ // init gifts
  63.                 printf("Datum: %s Nev: %s Varos: %s Pontos cim: %s Ajandek: %s\n",gifts[i].date,gifts[i].name,gifts[i].city,gifts[i].address,gifts[i].gift);               
  64.             }
  65.             printf("***********************\n");
  66.  
  67.         }
  68.         fclose(fileContents);  
  69.         //file read ending
  70.                    
  71.        
  72.         int pipe_santa_to_child[2];
  73.         int pipe_child_to_santa[2];
  74.         int pipeee[2];
  75.        
  76.         if (pipe(pipe_santa_to_child) == -1 || pipe(pipe_child_to_santa) == -1 || pipe(pipeee) == -1) {
  77.             perror("Hiba a pipe nyitaskor!");
  78.             exit(EXIT_FAILURE);
  79.         }
  80.        
  81.         pid_t child;
  82.         child = fork();
  83.        
  84.                
  85.         if (child<0){
  86.             perror("The fork calling was not succesful\n");
  87.             exit(1);
  88.         }
  89.         else if(child > 0){ //santa session
  90.             printf("Mikulas folyamat indul...\n");
  91.            
  92.             close(pipe_santa_to_child[0]);
  93.            
  94.             char answer[10];
  95.             int check = 0;
  96.             do{  
  97.                 menu_print();
  98.                 fgets(answer,10, stdin);
  99.  
  100.                 check = isCorrect(answer);
  101.  
  102.                 if(check < 1 || check > 5){
  103.                     printf("\nHibas adatbevitel!\n");
  104.                 }else{   
  105.                     if(check == 1){        
  106.                         char city[100];
  107.                         city[0] = '\0';                
  108.                         char solution[100];
  109.                         int solInt = -1;
  110.                         int city_counter = 0;
  111.                         char citys[1000][1000];
  112.                         citys[city_counter][1000] = '\0';
  113.                         int j = -1;
  114.                         do{
  115.                             printf("\n");
  116.                             printf("Valaszon az alabbi varosok kozul: \n");                
  117.                             for(j = 0; j < list_size; j++){
  118.                                 if(city_counter > 0){
  119.                                     int k;
  120.                                     int hadBeen = 0;
  121.                                     for(k = 0; k < city_counter; ++k){
  122.                                         if(strcmp(citys[k],gifts[j].city) == 0)
  123.                                         {
  124.                                             hadBeen = 1;
  125.                                         }                          
  126.                                     }
  127.                                     if(hadBeen == 0){
  128.                                         strcat(citys[city_counter],gifts[j].city);
  129.                                         ++city_counter;
  130.                                     }
  131.                                 }else{
  132.                                     strcat(citys[city_counter],gifts[j].city);
  133.                                     ++city_counter;
  134.                                 }
  135.                             }              
  136.                             for(j = 0; j < city_counter; ++j){
  137.                                 printf("    (%d) - %s \n", j, citys[j]);
  138.                             }
  139.                             printf("    (%d) - Kilepes \n", j);
  140.                             fgets(solution,100,stdin);
  141.                             cutEnter(solution);
  142.                             solInt = isCorrect(solution);                  
  143.                             if(solInt < j+1 && solInt >= 0){
  144.                                 strcat(city,citys[solInt]);
  145.                                 solInt = j;
  146.                             }else{
  147.                                 printf("\n Hibas parameter! \n");
  148.                             }
  149.                         }while(solInt != j);
  150.                                            
  151.                        
  152.                         char toImp[100];
  153.                         toImp[0] = '\0';
  154.                         strcat(toImp,"1");
  155.                         strcat(toImp,",");
  156.                         strcat(toImp, city);
  157.  
  158.                         write(pipe_santa_to_child[1], toImp ,18);   //!!!                  
  159.                         check = 3;
  160.                     }
  161.                     if(check == 2){
  162.                         char fromListGift[100];
  163.                         fromListGift[0] = '\0';                
  164.                         char solution[100];
  165.                         int solInt = -1;
  166.                         int gift_counter = 0;
  167.                         char listgifts[1000][1000];
  168.                         listgifts[gift_counter][1000] = '\0';
  169.                         int j = -1;
  170.                         do{
  171.                             printf("\n");
  172.                             printf("Valaszon az alabbi ajandekok kozul: \n");                  
  173.                             for(j = 0; j < list_size; j++){
  174.                                 if(gift_counter > 0){
  175.                                     int k;
  176.                                     int hadBeen = 0;
  177.                                     for(k = 0; k < gift_counter; ++k){
  178.                                         if(strcmp(listgifts[k],gifts[j].gift) == 0)
  179.                                         {
  180.                                             hadBeen = 1;
  181.                                         }                          
  182.                                     }
  183.                                     if(hadBeen == 0){
  184.                                         strcat(listgifts[gift_counter],gifts[j].gift);
  185.                                         ++gift_counter;
  186.                                     }
  187.                                 }else{
  188.                                     strcat(listgifts[gift_counter],gifts[j].gift);
  189.                                     ++gift_counter;
  190.                                 }
  191.                             }              
  192.                             for(j = 0; j < gift_counter; ++j){
  193.                                 printf("    (%d) - %s \n", j, listgifts[j]);
  194.                             }
  195.                             printf("    (%d) - Kilepes \n", j);
  196.                             fgets(solution,100,stdin);
  197.                             cutEnter(solution);
  198.                             solInt = isCorrect(solution);                  
  199.                             if(solInt < j+1 && solInt >= 0){
  200.                                 strcat(fromListGift,listgifts[solInt]);
  201.                                 solInt = j;
  202.                             }else{
  203.                                 printf("\n Hibas parameter! \n");
  204.                             }
  205.                         }while(solInt != j);                                           
  206.                        
  207.                         char toImp[100];
  208.                         toImp[0] = '\0';
  209.                         strcat(toImp,"2");
  210.                         strcat(toImp,",");
  211.                         strcat(toImp, fromListGift);
  212.  
  213.                         write(pipe_santa_to_child[1], toImp ,18);   //!!!                  
  214.                         check = 3;
  215.                     }
  216.                     if(check == 3){
  217.                         write(pipe_santa_to_child[1], "3",2);                      
  218.                     }
  219.  
  220.                 }
  221.             }
  222.             while(check != 3);                                   
  223.                                
  224.             close(pipe_santa_to_child[1]);
  225.                  
  226.             fflush(NULL);
  227.             waitpid(child, &status, 0);
  228.            
  229.             close(pipe_child_to_santa[1]);
  230.             read(pipe_child_to_santa[0], msg2, sizeof(msg2));
  231.             printf("Ezt kuldtek a manok: %s \n", msg2 );
  232.             close(pipe_child_to_santa[0]);
  233.                                
  234.             printf("Mikulas folyamat befejezodik...\n");
  235.        
  236.         }
  237.         else if(child == 0){ // elf session                
  238.             printf("Mano folyamat indul...\n");
  239.            
  240.             close(pipe_santa_to_child[1]);
  241.             read(pipe_santa_to_child[0], msg, sizeof(msg));
  242.             if(msg[0] == '3'){
  243.                 msg[0] = '\0';
  244.             }          
  245.            
  246.             printf("Ezt kuldte a mikulas: %s \n", msg);
  247.             close(pipe_santa_to_child[0]);
  248.            
  249.             char to_child[200][200];
  250.             to_child[0][200] = '\0';
  251.             //char arr[MAX_NUMBER_STRINGS][MAX_STRING_SIZE];           
  252.             char sublogs[1000][1000];
  253.             int log_counter = 0;           
  254.             sublogs[log_counter][1000] = '\0';
  255.            
  256.            
  257.             if(msg[0] == '1'){
  258.                 int l = 2;     
  259.                 int j = 0;             
  260.                 char city[100];
  261.                 city[0] = '\0';
  262.                 while( l < strlen(msg)){                   
  263.                     city[j] = msg[l];
  264.                     ++l;               
  265.                     ++j;                   
  266.                     city[j] = '\0';
  267.                 }              
  268.  
  269.  
  270.                 char subgift[100];             
  271.                 char *oldRows;                     
  272.                 int sub_size = list_size;
  273.                
  274.                 /*printf("Adja meg a varost nevet, ahova az ajandekokat szallitjuk!\n");
  275.                 fgets(city,100,stdin);
  276.                 cutEnter(city);*/
  277.                
  278.  
  279.                
  280.                
  281.                 int i;
  282.                 for(i = 0; i < list_size; i++){
  283.                     if(strcmp(city, gifts[i].city)==0){                            
  284.                         subgift[0] = '\0';
  285.                         strcat(subgift,gifts[i].gift); 
  286.                         strcat(sublogs[log_counter],subgift);                      
  287.                         ++log_counter;
  288.                         gifts[i] = getEmptyKivansag();                             
  289.                         --sub_size;
  290.                     }
  291.                 }
  292.                
  293.                 list_size = sub_size;
  294.                
  295.                 fileContents = fopen(FILE_NAME,"wb");      
  296.                 fprintf(fileContents, "%d\n",list_size);
  297.                 for(i = 0; i < list_size; i++){
  298.                     if(isEmptyKivansag(gifts[i]) == 0){
  299.                         oldRows = kivansagToRow(gifts[i]);
  300.                         oldRows = swapSpace(oldRows);
  301.                         fprintf(fileContents, "%s",oldRows);   
  302.                     }
  303.                 }
  304.                 fclose(fileContents);    
  305.                
  306.                 to_child[0][200] = '\0';
  307.                 if(log_counter == 1){
  308.                     strcat(to_child[0],"\n");  
  309.                     strcat(to_child[0],"Ebbe a varosba erkezett meg az osszes ajandek: "); 
  310.                     strcat(to_child[0],city);
  311.                     strcat(to_child[0],",");
  312.                     strcat(to_child[0]," ezt az ajandekot kaptak: ");
  313.                     strcat(to_child[0], sublogs[log_counter-1]);               
  314.                     strcat(to_child[0],"\n");  
  315.                    
  316.                 }else if(log_counter > 1){
  317.                     int i;
  318.                     for(i = 0; i < log_counter; ++i){
  319.                         strcat(to_child[i],"\n");
  320.                         strcat(to_child[i],"Ebbe a varosba erkezett meg az osszes ajandek: "); 
  321.                         strcat(to_child[i],city);
  322.                         strcat(to_child[i],",");
  323.                         strcat(to_child[i]," ezt az ajandekot kaptak: ");
  324.                         strcat(to_child[i],sublogs[i]);                                    
  325.                     }
  326.                     strcat(to_child[i],"\n");
  327.                 }else if(log_counter < 1){                                 
  328.                     strcat(to_child[0],"Nem volt ilyen varos");                
  329.                     strcat(to_child[0],"\n");      
  330.                 }              
  331.                
  332.             }
  333.             if(msg[0] == '2'){
  334.                 int l = 2;     
  335.                 int j = 0;             
  336.                 char fromListGift[100];
  337.                 fromListGift[0] = '\0';
  338.                 while( l < strlen(msg)){                   
  339.                     fromListGift[j] = msg[l];
  340.                     ++l;               
  341.                     ++j;                   
  342.                     fromListGift[j] = '\0';
  343.                 }              
  344.  
  345.  
  346.                 char subCity[100];             
  347.                 char *oldRows;                     
  348.                 int sub_size = list_size;
  349.                
  350.                 /*printf("Adja meg a varost nevet, ahova az ajandekokat szallitjuk!\n");
  351.                 fgets(city,100,stdin);
  352.                 cutEnter(city);*/
  353.                
  354.  
  355.                
  356.                
  357.                 int i;
  358.                 for(i = 0; i < list_size; i++){
  359.                     if(strcmp(fromListGift, gifts[i].gift)==0){                            
  360.                         subCity[0] = '\0';
  361.                         strcat(subCity,gifts[i].city); 
  362.                         strcat(sublogs[log_counter],subCity);                      
  363.                         ++log_counter;
  364.                         gifts[i] = getEmptyKivansag();                             
  365.                         --sub_size;
  366.                     }
  367.                 }
  368.                
  369.                 list_size = sub_size;
  370.                
  371.                 fileContents = fopen(FILE_NAME,"wb");      
  372.                 fprintf(fileContents, "%d\n",list_size);
  373.                 for(i = 0; i < list_size; i++){
  374.                     if(isEmptyKivansag(gifts[i]) == 0){
  375.                         oldRows = kivansagToRow(gifts[i]);
  376.                         oldRows = swapSpace(oldRows);
  377.                         fprintf(fileContents, "%s",oldRows);   
  378.                     }
  379.                 }
  380.                 fclose(fileContents);    
  381.                
  382.                 to_child[0][200] = '\0';
  383.                 if(log_counter == 1){
  384.                     strcat(to_child[0],"\n");  
  385.                     strcat(to_child[0],"Ez a fajta ajandek erkezett meg: ");   
  386.                     strcat(to_child[0],fromListGift);
  387.                     strcat(to_child[0],",");
  388.                     strcat(to_child[0]," ebbe a varosba: ");   
  389.                     strcat(to_child[0], sublogs[log_counter-1]);               
  390.                     strcat(to_child[0],"\n");  
  391.                    
  392.                 }else if(log_counter > 1){
  393.                     int i;
  394.                     for(i = 0; i < log_counter; ++i){
  395.                         strcat(to_child[i],"\n");
  396.                         strcat(to_child[i],"Ez a fajta ajandek erkezett meg: ");   
  397.                         strcat(to_child[i],fromListGift);
  398.                         strcat(to_child[i],",");
  399.                         strcat(to_child[i]," ebbe a varosba: ");   
  400.                         strcat(to_child[i],sublogs[i]);                                    
  401.                     }
  402.                     strcat(to_child[i],"\n");
  403.                 }else if(log_counter < 1){                                 
  404.                     strcat(to_child[0],"Nem volt ilyen ajandek");                  
  405.                     strcat(to_child[0],"\n");      
  406.                 }              
  407.                
  408.             }          
  409.  
  410.             int sizeOfArray = log_counter;
  411.             log_counter = 0;
  412.             msg[0] = '\0';
  413.            
  414.            
  415.             if(sizeOfArray == 0){
  416.                 sizeOfArray = 1;
  417.             }
  418.             char to_parent [sizeOfArray * 200];
  419.             to_parent[0] = '\0';
  420.  
  421.             int h;
  422.             for(h = 0; h < sizeOfArray; h++){
  423.                 strcat(to_parent, to_child[h]);
  424.             }          
  425.             close(pipe_child_to_santa[0]);
  426.             write(pipe_child_to_santa[1], to_parent,sizeof(to_parent));
  427.             close(pipe_child_to_santa[1]);
  428.  
  429.             printf("\nMano folyamat befejezodik...\n");
  430.         }
  431.        
  432.  
  433.     }      
  434.    
  435.     return 0;
  436. }
  437.  
  438. struct Kivansag getEmptyKivansag(){
  439.     struct Kivansag kivansag;
  440.     strcat(kivansag.gift,"");
  441.     return kivansag;
  442. }
  443.  
  444. int isEmptyKivansag(struct Kivansag kivansag){
  445.     return (strcmp(kivansag.gift,"") == 0);
  446. }
  447.  
  448. char *swapSpace(char *row){
  449.     int count = 0;
  450.     while(*row != '\0'){
  451.         ++count;
  452.         if(*row == ' '){
  453.             *row = '_';
  454.         }
  455.         ++row;
  456.     }
  457.     int i;
  458.     for(i = 0; i < count; i++){
  459.         --row;
  460.     }
  461.     return row;
  462. }
  463.  
  464. char *kivansagToRow(struct Kivansag kivansag){
  465.     char *row;
  466.     row = kivansag.date;   
  467.     strcat(row,",");
  468.     strcat(row,kivansag.name); 
  469.     strcat(row,",");
  470.     strcat(row,kivansag.city); 
  471.     strcat(row,",");
  472.     strcat(row,kivansag.address);
  473.     strcat(row,",");
  474.     strcat(row,kivansag.gift); 
  475.     strcat(row,"\n");
  476.     return row;
  477. }
  478.  
  479. void cutEnter(char string[100]){
  480.     char *pos;
  481.     if ((pos=strchr(string, '\n')) != NULL)
  482.         *pos = '\0';
  483. }
  484.  
  485. void menu_print(){
  486.     printf("\n*********************************************************\n");
  487.                 printf("Udv a Mikulas kezelofeluleten! Az alabbiak kozul valaszthat:\n"
  488.                 "  (1) Ajandek kuldese gyujtohelyrol, egy varosba, sok ajandekot\n"
  489.                 "  (2) Ajandek kuldese uzembol, sok varosba, egy fele ajandekot\n"                         
  490.                 "  (3) Kilepes\n");
  491. }
  492.  
  493. int isCorrect(char answer[10]){
  494.    return *answer-48;
  495. }
  496.  
  497. struct Kivansag rowToKivansag(char row[1000]){
  498.     struct Kivansag kivansag;
  499.     int i = 0;
  500.     int j = 0;
  501.     int count = 0;
  502.     int lastPos = 0;
  503.     while(row[i] != '\0'){
  504.         if(row[i] == ','){
  505.             ++count;
  506.             lastPos = j;
  507.             j = 0;
  508.         }else{
  509.             if(count == 0){        
  510.                 if(row[i] == '_'){
  511.                     kivansag.date[j] = ' ';
  512.                 }
  513.                 else{
  514.                     kivansag.date[j] = row[i];
  515.                 }
  516.                 ++j;
  517.             }
  518.             if(count == 1){
  519.                 kivansag.date[lastPos] = '\0';
  520.                 if(row[i] == '_'){
  521.                     kivansag.name[j] = ' ';
  522.                 }
  523.                 else{
  524.                     kivansag.name[j] = row[i];
  525.                 }
  526.                 ++j;
  527.             }
  528.             if(count == 2){
  529.                 kivansag.name[lastPos] = '\0';
  530.                 if(row[i] == '_'){
  531.                     kivansag.city[j] = ' ';
  532.                 }
  533.                 else{
  534.                     kivansag.city[j] = row[i];
  535.                 }
  536.                 ++j;
  537.             }
  538.             if(count == 3){
  539.                 kivansag.city[lastPos] = '\0';
  540.                 if(row[i] == '_'){
  541.                     kivansag.address[j] = ' ';
  542.                 }
  543.                 else{
  544.                     kivansag.address[j] = row[i];
  545.                 }
  546.                 ++j;
  547.             }
  548.             if(count == 4){
  549.                 kivansag.address[lastPos] = '\0';
  550.                 if(row[i] == '_'){
  551.                     kivansag.gift[j] = ' ';
  552.                 }
  553.                 else{
  554.                     kivansag.gift[j] = row[i];
  555.                 }
  556.                 ++j;
  557.             }      
  558.         }
  559.         ++i;
  560.     }          
  561.     kivansag.gift[j] = '\0';   
  562.     return kivansag;
  563. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement