Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- /* -- global variable -- */
- FILE *file;
- int customer = 0;
- int filter = 0;
- int flagFilter = 0;
- int coma = 0;
- int flagOutput[6] = {};
- int flagSearch[6] = {};
- char searchFilter[6][100];
- char add[6][100] = {"id", "nama", "kota", "gender", "mobil", "properti"};
- /* -- global variable -- */
- /* -- struct -- */
- struct customerData
- {
- char info[6][101];
- /* 0. id
- * 1. nama
- * 2. kota
- * 3. gender
- * 4. mobil
- * 5. properti*/
- } data[100];
- struct hashData
- {
- int n;
- int idx[100];
- } hashData[100];
- /* -- struct -- */
- /* -- utility function -- */
- int length(char x[])
- {
- int i;
- for(i=0;x[i];++i);
- return i;
- }
- int compare(char x[], char y[])
- {
- int n=length(x), m=length(y);
- if(n!=m)
- return 1;
- for(int i=0;i<n;++i){
- if(x[i]!=y[i])
- return 1;
- }
- return 0;
- }
- void copy(char from[], char dest[])
- {
- int i;
- for(i=0;from[i];++i)
- dest[i] = from[i];
- dest[i] = '\0';
- }
- /* -- utility function -- */
- /* -- program specific function -- */
- void initializeStruct()
- {
- for(int i=0;i<100;++i)
- hashData[i].n = 0;
- }
- void print(int n)
- {
- for(int i=0;i<6;++i){
- if(flagOutput[i])
- {
- if(coma == 1)
- printf(", ");
- printf("%s: %s", add[i], data[n].info[i]);
- coma = 1;
- }
- }
- putchar('\n');
- coma = 0;
- }
- int hash(char x[])
- {
- int ret = 0;
- for(int i=0;x[i];++i){
- ret += (x[i] - '0') * (i+1) * 97;
- ret %= 100;
- }
- return ret;
- }
- /* -- program specific function -- */
- int main()
- {
- //opening file
- file = fopen("data_pelanggan.csv", "r");
- /* -- local variable -- */
- char searchFor[6][100] = {"id", "nama", "kota", "gender", "mobil", "properti"};
- char inp[100];
- char trash[100];
- int len = 0;
- int mode = 0;
- int trashline = 0;
- int idxHash = 0;
- /* -- local variable -- */
- /* -- read file -- */
- while(!feof(file)){
- if(!trashline)
- {
- fscanf(file, "%[^\n]\n", trash);
- trashline = 1;
- continue;
- }
- fscanf(file, "%[^,],", data[customer].info[0]);
- fscanf(file, "%[^,],", data[customer].info[1]);
- fscanf(file, "%[^,],", data[customer].info[2]);
- fscanf(file, "%[^,],", data[customer].info[3]);
- fscanf(file, "%[^,],", data[customer].info[4]);
- fscanf(file, "%[^\n]\n", data[customer].info[5]);
- ++customer;
- }
- /* -- read file -- */
- /* -- hashing data -- */
- initializeStruct();
- for(int i=0;i<customer;++i){
- idxHash = hash(data[i].info[0]);
- hashData[idxHash].idx[hashData[idxHash].n] = i;
- ++hashData[idxHash].n;
- }
- /* -- hashing data -- */
- /* -- get input -- */
- printf("Query : ");
- while(1){
- char x = getchar();
- if(x=='\n' || x==' ' || x==',')
- {
- if(!compare(inp, "SHOW") && mode == 0)
- mode = 1;
- else if(!compare(inp, "WHERE") && mode == 1)
- {
- flagFilter = 1;
- mode = 2;
- }
- if(mode == 1)
- {
- if(!compare(inp, "*"))
- {
- for(int i=0;i<6;++i)
- flagOutput[i] = 1;
- }
- else
- {
- for(int i=0;i<6;++i){
- if(!compare(inp, searchFor[i]))
- flagOutput[i] = 1;
- }
- }
- }
- else if(mode == 2 || mode == 3)
- {
- if(mode == 2)
- {
- for(int i=0;i<6;++i){
- if(!compare(inp, searchFor[i]))
- {
- filter = i;
- flagSearch[i] = 1;
- }
- }
- }
- else if(mode == 3)
- {
- mode = 2;
- copy(inp, searchFilter[filter]);
- }
- if(!compare(inp, "equals"))
- mode = 3;
- }
- inp[0] = '\0';
- len = 0;
- if(x=='\n')
- break;
- continue;
- }
- inp[len] = x;
- inp[len+1] = '\0';
- ++len;
- }
- /* -- get input -- */
- /* -- output -- */
- if(!flagFilter)
- {
- for(int i=0;i<customer;++i)
- print(i);
- }
- else if(flagFilter)
- {
- if(flagSearch[0]) //using hash to implement O(1) worse case scenario O(n)
- {
- int idSearch = hash(searchFilter[0]);
- for(int i=0;i<hashData[idSearch].n;++i){
- if(!compare(data[hashData[idSearch].idx[i]].info[0], searchFilter[0]))
- print(hashData[idSearch].idx[i]);
- }
- }
- else
- {
- for(int i=0;i<customer;++i){
- if(!compare(data[i].info[filter], searchFilter[filter]))
- print(i);
- }
- }
- }
- /* -- output -- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement