Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- struct Tree; //prototype
- struct Hand{ //single linked list
- Tree *childs;
- Hand *next;
- };
- struct Tree{ //infinite tries
- char id[20];
- char name[20];
- char gender[10];
- int age;
- char address[50];
- char phoneNumber[30];
- Hand *hands;
- }*root;
- //khusus insert kalau sudah ketemu node yang ingin di insert
- //cuma untuk manage hands nya
- void insert(Tree *curr,char id[20],char name[20],char gender[10],int age,char address[50],char phoneNumber[20]){
- Tree *node = (Tree*)malloc(sizeof Tree);
- strcpy(node->id,id);
- strcpy(node->name,name);
- strcpy(node->gender,gender);
- node->age=age;
- strcpy(node->address,address);
- strcpy(node->phoneNumber,phoneNumber);
- node->hands=NULL;
- if(root==NULL){
- root=node;
- return;
- }
- if(curr->hands == NULL){ //jika handsnya kosong
- curr->hands = (Hand*)malloc(sizeof Hand);
- curr->hands->childs = node;
- curr->hands->next = NULL;
- }else{ //jika handnya ada isi
- Hand *temp = curr->hands;
- while(temp->next != NULL){
- temp=temp->next;
- }
- temp->next = (Hand*)malloc(sizeof Hand);
- temp->next->childs = node;
- temp->next->next = NULL;
- }
- }
- Tree* searchById(Tree *curr,char searchId[]){
- if(curr==NULL)return NULL;
- if(strcmpi(curr->id,searchId)==0){
- return curr;
- }
- else{
- for(Hand *temp=curr->hands;temp!=NULL;temp=temp->next){
- Tree *returner = searchById(temp->childs,searchId);
- if(returner!= NULL)return returner;
- }
- }
- return NULL;
- }
- void insertNewMember(){
- char id[20];
- char name[20];
- char gender[10];
- int age;
- char address[50];
- char phoneNumber[20];
- char referalId[20];
- printf("ID must be based on the following format LXZZZZ\n");
- printf("Where Z is an integer\n\n");
- do{
- printf("Input ID: ");
- gets(id);
- }while(searchById(root,id));
- printf("Input name[3-30 characters]: ");
- gets(name);
- printf("Input address [must be starts with \'Jalan \']: ");
- gets(address);
- printf("Input gender [Male|Female]: ");
- gets(gender);
- printf("Input phone number[10-14 numeric characters]: ");
- gets(phoneNumber);
- printf("Input age[17-60]: ");
- scanf("%d",&age);getchar();
- do{
- printf("Input referral ID:");
- gets(referalId);
- }while(!searchById(root,referalId));
- insert(searchById(root,referalId),id,name,gender,age,address,phoneNumber);
- printf("Insert success");
- getchar();
- }
- void printReferal(Tree* curr){
- if(curr){
- printf("ID : %s\n",curr->id);
- printf("Name : %s\n",curr->name);
- printf("Gender : %s\n",curr->gender);
- printf("Age : %d\n",curr->age);
- printf("Address : %s\n",curr->address);
- printf("Phone number: %s\n\n",curr->phoneNumber);
- for(Hand *temp=curr->hands;temp!=NULL;temp=temp->next){
- printReferal(temp->childs);
- }
- }
- }
- void viewMember(){
- char input[100];
- printf("ID must be based on the following format LXZZZZ\n");
- printf("Where Z is an integer\n");
- printf("Input ID: ");
- gets(input);
- Tree *result = searchById(root,input);
- if(result){
- printf("ID : %s\n",result->id);
- printf("Name : %s\n",result->name);
- printf("Gender : %s\n",result->gender);
- printf("Age : %d\n",result->age);
- printf("Address : %s\n",result->address);
- printf("Phone number: %s\n",result->phoneNumber);
- printf("Referral\n");
- printf("========\n");
- for(Hand *temp=result->hands;temp!=NULL;temp=temp->next){
- printReferal(temp->childs);
- }
- }else{
- printf("ID not found");
- getchar();
- }
- }
- void updateMember(){
- Tree *result;
- char input[100];
- char name[20];
- char gender[10];
- int age;
- char address[50];
- char phoneNumber[20];
- char referalId[20];
- do{
- printf("Input ID: ");
- gets(input);
- result = searchById(root,input);
- }while(!result);
- printf("Input name[3-30 characters]: ");
- gets(name);
- printf("Input address [must be starts with \'Jalan \']: ");
- gets(address);
- printf("Input gender [Male|Female]: ");
- gets(gender);
- printf("Input phone number[10-14 numeric characters]: ");
- gets(phoneNumber);
- printf("Input age[17-60]: ");
- scanf("%d",&age);getchar();
- strcpy(result->name,name);
- strcpy(result->gender,gender);
- result->age=age;
- strcpy(result->address,address);
- strcpy(result->phoneNumber,phoneNumber);
- printf("Update success");
- }
- void appendFile(Tree *curr,Tree *parent){
- if(curr){
- FILE *fp = fopen("data.csv","a");
- fprintf(fp,"%s,%s,%s,%s,%s,%s,%d\n",curr->id,parent->id,curr->name,curr->address,curr->gender,curr->phoneNumber,curr->age);
- fclose(fp);
- for(Hand *temp=curr->hands;temp!=NULL;temp=temp->next){
- appendFile(temp->childs,curr);
- }
- }
- }
- void saveFile(){
- FILE *fp = fopen("data.csv","w");
- fprintf(fp,"%s,NULL,%s,%s,%s,%s,%d\n",root->id,root->name,root->address,root->gender,root->phoneNumber,root->age);
- fclose(fp);
- for(Hand *temp=root->hands;temp!=NULL;temp=temp->next){
- appendFile(temp->childs,root);
- }
- }
- void loadFile(){
- char id[100];
- char name[20];
- char gender[10];
- int age;
- char address[50];
- char phoneNumber[20];
- char referalId[20];
- FILE *fp = fopen("data.csv","r");
- if(fp){
- while(!feof(fp)){
- fscanf(fp,"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%d\n",id,referalId,name,address,gender,phoneNumber,&age);
- if(strcmp(referalId,"NULL")==0)insert(root,id,name,gender,age,address,phoneNumber);
- else insert(searchById(root,referalId),id,name,gender,age,address,phoneNumber);
- }
- fclose(fp);
- }
- }
- int main(){
- int choose=0;
- loadFile();
- do{
- printf("\nmLXm\n");
- printf("multi Level Xtra marketing\n");
- printf("==========================\n");
- printf("1. View member\n");
- printf("2. Insert new member\n");
- printf("3. Update member\n");
- printf("4. Save and Exit\n");
- printf("Choice:");
- scanf("%d",&choose);getchar();
- switch(choose){
- case 1:
- viewMember();
- break;
- case 2:
- insertNewMember();
- break;
- case 3:
- updateMember();
- break;
- }
- }while(choose != 4);
- if(root)saveFile();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement