Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. struct Tree; //prototype
  6.  
  7. struct Hand{ //single linked list
  8. Tree *childs;
  9. Hand *next;
  10. };
  11.  
  12. struct Tree{ //infinite tries
  13. char id[20];
  14. char name[20];
  15. char gender[10];
  16. int age;
  17. char address[50];
  18. char phoneNumber[30];
  19. Hand *hands;
  20. }*root;
  21.  
  22. //khusus insert kalau sudah ketemu node yang ingin di insert
  23. //cuma untuk manage hands nya
  24. void insert(Tree *curr,char id[20],char name[20],char gender[10],int age,char address[50],char phoneNumber[20]){
  25.  
  26. Tree *node = (Tree*)malloc(sizeof Tree);
  27. strcpy(node->id,id);
  28. strcpy(node->name,name);
  29. strcpy(node->gender,gender);
  30. node->age=age;
  31. strcpy(node->address,address);
  32. strcpy(node->phoneNumber,phoneNumber);
  33. node->hands=NULL;
  34. if(root==NULL){
  35. root=node;
  36. return;
  37. }
  38. if(curr->hands == NULL){ //jika handsnya kosong
  39. curr->hands = (Hand*)malloc(sizeof Hand);
  40. curr->hands->childs = node;
  41. curr->hands->next = NULL;
  42. }else{ //jika handnya ada isi
  43. Hand *temp = curr->hands;
  44. while(temp->next != NULL){
  45. temp=temp->next;
  46. }
  47. temp->next = (Hand*)malloc(sizeof Hand);
  48. temp->next->childs = node;
  49. temp->next->next = NULL;
  50. }
  51.  
  52. }
  53.  
  54. Tree* searchById(Tree *curr,char searchId[]){
  55. if(curr==NULL)return NULL;
  56. if(strcmpi(curr->id,searchId)==0){
  57. return curr;
  58. }
  59. else{
  60. for(Hand *temp=curr->hands;temp!=NULL;temp=temp->next){
  61. Tree *returner = searchById(temp->childs,searchId);
  62. if(returner!= NULL)return returner;
  63. }
  64. }
  65. return NULL;
  66. }
  67.  
  68. void insertNewMember(){
  69. char id[20];
  70. char name[20];
  71. char gender[10];
  72. int age;
  73. char address[50];
  74. char phoneNumber[20];
  75. char referalId[20];
  76. printf("ID must be based on the following format LXZZZZ\n");
  77. printf("Where Z is an integer\n\n");
  78.  
  79. do{
  80. printf("Input ID: ");
  81. gets(id);
  82. }while(searchById(root,id));
  83. printf("Input name[3-30 characters]: ");
  84. gets(name);
  85. printf("Input address [must be starts with \'Jalan \']: ");
  86. gets(address);
  87. printf("Input gender [Male|Female]: ");
  88. gets(gender);
  89. printf("Input phone number[10-14 numeric characters]: ");
  90. gets(phoneNumber);
  91. printf("Input age[17-60]: ");
  92. scanf("%d",&age);getchar();
  93. do{
  94. printf("Input referral ID:");
  95. gets(referalId);
  96. }while(!searchById(root,referalId));
  97. insert(searchById(root,referalId),id,name,gender,age,address,phoneNumber);
  98. printf("Insert success");
  99. getchar();
  100. }
  101.  
  102.  
  103. void printReferal(Tree* curr){
  104. if(curr){
  105. printf("ID : %s\n",curr->id);
  106. printf("Name : %s\n",curr->name);
  107. printf("Gender : %s\n",curr->gender);
  108. printf("Age : %d\n",curr->age);
  109. printf("Address : %s\n",curr->address);
  110. printf("Phone number: %s\n\n",curr->phoneNumber);
  111.  
  112. for(Hand *temp=curr->hands;temp!=NULL;temp=temp->next){
  113. printReferal(temp->childs);
  114. }
  115. }
  116. }
  117.  
  118. void viewMember(){
  119. char input[100];
  120.  
  121. printf("ID must be based on the following format LXZZZZ\n");
  122. printf("Where Z is an integer\n");
  123. printf("Input ID: ");
  124. gets(input);
  125. Tree *result = searchById(root,input);
  126. if(result){
  127. printf("ID : %s\n",result->id);
  128. printf("Name : %s\n",result->name);
  129. printf("Gender : %s\n",result->gender);
  130. printf("Age : %d\n",result->age);
  131. printf("Address : %s\n",result->address);
  132. printf("Phone number: %s\n",result->phoneNumber);
  133. printf("Referral\n");
  134. printf("========\n");
  135. for(Hand *temp=result->hands;temp!=NULL;temp=temp->next){
  136. printReferal(temp->childs);
  137. }
  138. }else{
  139. printf("ID not found");
  140. getchar();
  141. }
  142. }
  143.  
  144. void updateMember(){
  145. Tree *result;
  146. char input[100];
  147. char name[20];
  148. char gender[10];
  149. int age;
  150. char address[50];
  151. char phoneNumber[20];
  152. char referalId[20];
  153. do{
  154. printf("Input ID: ");
  155. gets(input);
  156. result = searchById(root,input);
  157. }while(!result);
  158. printf("Input name[3-30 characters]: ");
  159. gets(name);
  160. printf("Input address [must be starts with \'Jalan \']: ");
  161. gets(address);
  162. printf("Input gender [Male|Female]: ");
  163. gets(gender);
  164. printf("Input phone number[10-14 numeric characters]: ");
  165. gets(phoneNumber);
  166. printf("Input age[17-60]: ");
  167. scanf("%d",&age);getchar();
  168.  
  169. strcpy(result->name,name);
  170. strcpy(result->gender,gender);
  171. result->age=age;
  172. strcpy(result->address,address);
  173. strcpy(result->phoneNumber,phoneNumber);
  174.  
  175. printf("Update success");
  176. }
  177.  
  178. void appendFile(Tree *curr,Tree *parent){
  179. if(curr){
  180. FILE *fp = fopen("data.csv","a");
  181.  
  182. fprintf(fp,"%s,%s,%s,%s,%s,%s,%d\n",curr->id,parent->id,curr->name,curr->address,curr->gender,curr->phoneNumber,curr->age);
  183.  
  184. fclose(fp);
  185.  
  186. for(Hand *temp=curr->hands;temp!=NULL;temp=temp->next){
  187. appendFile(temp->childs,curr);
  188. }
  189. }
  190. }
  191.  
  192.  
  193. void saveFile(){
  194. FILE *fp = fopen("data.csv","w");
  195.  
  196. fprintf(fp,"%s,NULL,%s,%s,%s,%s,%d\n",root->id,root->name,root->address,root->gender,root->phoneNumber,root->age);
  197.  
  198. fclose(fp);
  199. for(Hand *temp=root->hands;temp!=NULL;temp=temp->next){
  200. appendFile(temp->childs,root);
  201. }
  202. }
  203.  
  204. void loadFile(){
  205. char id[100];
  206. char name[20];
  207. char gender[10];
  208. int age;
  209. char address[50];
  210. char phoneNumber[20];
  211. char referalId[20];
  212. FILE *fp = fopen("data.csv","r");
  213. if(fp){
  214. while(!feof(fp)){
  215. fscanf(fp,"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%d\n",id,referalId,name,address,gender,phoneNumber,&age);
  216.  
  217. if(strcmp(referalId,"NULL")==0)insert(root,id,name,gender,age,address,phoneNumber);
  218. else insert(searchById(root,referalId),id,name,gender,age,address,phoneNumber);
  219. }
  220. fclose(fp);
  221. }
  222.  
  223. }
  224.  
  225. int main(){
  226. int choose=0;
  227. loadFile();
  228.  
  229. do{
  230. printf("\nmLXm\n");
  231. printf("multi Level Xtra marketing\n");
  232. printf("==========================\n");
  233. printf("1. View member\n");
  234. printf("2. Insert new member\n");
  235. printf("3. Update member\n");
  236. printf("4. Save and Exit\n");
  237. printf("Choice:");
  238. scanf("%d",&choose);getchar();
  239. switch(choose){
  240. case 1:
  241. viewMember();
  242. break;
  243. case 2:
  244. insertNewMember();
  245. break;
  246. case 3:
  247. updateMember();
  248. break;
  249. }
  250. }while(choose != 4);
  251.  
  252. if(root)saveFile();
  253.  
  254. return 0;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement