Advertisement
vitoroz

Untitled

Jan 15th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.33 KB | None | 0 0
  1. #include<stdio.h>
  2. /* -- global variable -- */
  3. FILE *file;
  4. int customer = 0;
  5. int filter = 0;
  6. int flagFilter = 0;
  7. int coma = 0;
  8. int flagOutput[6] = {};
  9. int flagSearch[6] = {};
  10. char searchFilter[6][100];
  11. char add[6][100] = {"id", "nama", "kota", "gender", "mobil", "properti"};
  12. /* -- global variable -- */
  13.  
  14. /* -- struct -- */
  15. struct customerData
  16. {  
  17.     char info[6][101];
  18.     /* 0. id
  19.      * 1. nama
  20.      * 2. kota
  21.      * 3. gender
  22.      * 4. mobil
  23.      * 5. properti*/
  24. } data[100];
  25.  
  26. struct hashData
  27. {
  28.     int n;
  29.     int idx[100];
  30. } hashData[100];
  31. /* -- struct -- */
  32.  
  33. /* -- utility function -- */
  34. int length(char x[])
  35. {
  36.     int i;
  37.     for(i=0;x[i];++i);
  38.     return i;
  39. }
  40. int compare(char x[], char y[])
  41. {
  42.     int n=length(x), m=length(y);
  43.     if(n!=m)
  44.         return 1;
  45.     for(int i=0;i<n;++i){
  46.         if(x[i]!=y[i])
  47.             return 1;
  48.     }
  49.     return 0;
  50. }
  51. void copy(char from[], char dest[])
  52. {
  53.     int i;
  54.     for(i=0;from[i];++i)
  55.         dest[i] = from[i];
  56.     dest[i] = '\0';
  57. }
  58. /* -- utility function -- */
  59.  
  60. /* -- program specific function -- */
  61. void initializeStruct()
  62. {
  63.     for(int i=0;i<100;++i)
  64.         hashData[i].n = 0;
  65. }
  66. void print(int n)
  67. {
  68.     for(int i=0;i<6;++i){
  69.         if(flagOutput[i])
  70.         {
  71.             if(coma == 1)
  72.                 printf(", ");
  73.            
  74.             printf("%s: %s", add[i], data[n].info[i]);
  75.            
  76.             coma = 1;
  77.         }
  78.     }
  79.     putchar('\n');
  80.    
  81.     coma = 0;
  82. }
  83. int hash(char x[])
  84. {
  85.     int ret = 0;
  86.     for(int i=0;x[i];++i){
  87.         ret += (x[i] - '0') * (i+1) * 97;
  88.         ret %= 100;
  89.     }
  90.     return ret;
  91. }
  92. /* -- program specific function -- */
  93. int main()
  94. {
  95.     //opening file
  96.     file = fopen("data_pelanggan.csv", "r");
  97.    
  98.     /* -- local variable -- */
  99.     char searchFor[6][100] = {"id", "nama", "kota", "gender", "mobil", "properti"};
  100.     char inp[100];
  101.     char trash[100];
  102.     int len = 0;
  103.     int mode = 0;
  104.     int trashline = 0;
  105.     int idxHash = 0;
  106.     /* -- local variable -- */
  107.    
  108.     /* -- read file -- */
  109.     while(!feof(file)){
  110.         if(!trashline)
  111.         {
  112.             fscanf(file, "%[^\n]\n", trash);
  113.             trashline = 1;
  114.             continue;
  115.         }
  116.        
  117.         fscanf(file, "%[^,],", data[customer].info[0]);
  118.         fscanf(file, "%[^,],", data[customer].info[1]);
  119.         fscanf(file, "%[^,],", data[customer].info[2]);
  120.         fscanf(file, "%[^,],", data[customer].info[3]);
  121.         fscanf(file, "%[^,],", data[customer].info[4]);
  122.         fscanf(file, "%[^\n]\n", data[customer].info[5]);
  123.        
  124.         ++customer;
  125.     }
  126.     /* -- read file -- */
  127.    
  128.     /* -- hashing data -- */
  129.     initializeStruct();
  130.     for(int i=0;i<customer;++i){
  131.         idxHash = hash(data[i].info[0]);
  132.         hashData[idxHash].idx[hashData[idxHash].n] = i;
  133.         ++hashData[idxHash].n;
  134.     }
  135.     /* -- hashing data -- */
  136.    
  137.     /* -- get input -- */
  138.     printf("Query : ");
  139.     while(1){
  140.         char x = getchar();
  141.         if(x=='\n' || x==' ' || x==',')
  142.         {
  143.             if(!compare(inp, "SHOW") && mode == 0)
  144.                 mode = 1;
  145.             else if(!compare(inp, "WHERE") && mode == 1)
  146.             {
  147.                 flagFilter = 1;
  148.                 mode = 2;
  149.             }
  150.            
  151.             if(mode == 1)
  152.             {
  153.                 if(!compare(inp, "*"))
  154.                 {
  155.                     for(int i=0;i<6;++i)
  156.                         flagOutput[i] = 1;
  157.                 }
  158.                 else
  159.                 {
  160.                     for(int i=0;i<6;++i){
  161.                         if(!compare(inp, searchFor[i]))
  162.                             flagOutput[i] = 1;
  163.                     }
  164.                 }
  165.             }
  166.             else if(mode == 2 || mode == 3)
  167.             {
  168.                 if(mode == 2)
  169.                 {
  170.                     for(int i=0;i<6;++i){
  171.                         if(!compare(inp, searchFor[i]))
  172.                         {
  173.                             filter = i;
  174.                             flagSearch[i] = 1;
  175.                         }
  176.                     }
  177.                 }
  178.                 else if(mode == 3)
  179.                 {
  180.                     mode = 2;
  181.                     copy(inp, searchFilter[filter]);
  182.                 }
  183.  
  184.                 if(!compare(inp, "equals"))
  185.                     mode = 3;
  186.             }
  187.            
  188.             inp[0] = '\0';
  189.             len = 0;
  190.             if(x=='\n')
  191.                 break;
  192.             continue;
  193.         }
  194.        
  195.         inp[len] = x;
  196.         inp[len+1] = '\0';
  197.         ++len;
  198.     }
  199.     /* -- get input -- */
  200.  
  201.     /* -- output -- */
  202.     if(!flagFilter)
  203.     {
  204.         for(int i=0;i<customer;++i)
  205.             print(i);
  206.     }
  207.     else if(flagFilter)
  208.     {
  209.         if(flagSearch[0]) //using hash to implement O(1) worse case scenario O(n)
  210.         {
  211.             int idSearch = hash(searchFilter[0]);
  212.             for(int i=0;i<hashData[idSearch].n;++i){
  213.                 if(!compare(data[hashData[idSearch].idx[i]].info[0], searchFilter[0]))
  214.                     print(hashData[idSearch].idx[i]);
  215.             }
  216.         }
  217.         else
  218.         {
  219.             for(int i=0;i<customer;++i){
  220.                 if(!compare(data[i].info[filter], searchFilter[filter]))
  221.                         print(i);
  222.             }
  223.         }
  224.     }
  225.     /* -- output -- */
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement