Guest User

Untitled

a guest
Aug 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<conio.h>
  5. #include<ctype.h>
  6. #include<time.h>
  7.  
  8.  
  9.  
  10. struct data{
  11. char fullname[100];
  12. char type[50];
  13. int save;
  14. char pin[7];
  15. struct data *left,*right;
  16. }*root;
  17.  
  18. void push(struct data **node, char name[],char type[],int save,char pin[]){
  19. if((*node) == NULL){
  20. *node = (struct data*) malloc(sizeof(struct data));
  21. strcpy((*node)->fullname,name) ;
  22. strcpy((*node)->type,type) ;
  23. strcpy((*node)->pin,pin) ;
  24. (*node)->save = save;
  25. (*node)->left = (*node)->right =NULL;
  26.  
  27. }
  28. else{
  29. if(strcmpi((*node)->fullname,name) > 0 ){
  30. push(&(*node)->left,name,type,save,pin);
  31. }
  32. else if(strcmpi((*node)->fullname,name) < 0 ){
  33. push(&(*node)->right,name,type,save,pin);
  34. }
  35. else{
  36. printf("SAME DATA DETECTED\n");
  37. getchar();
  38. }
  39. }
  40. }
  41.  
  42. void inorder(struct data *node){
  43. if(node != NULL){
  44. inorder(node->left);
  45. printf("Fullname :%s\nType :%s\nSave :%d\nPIN :%s\n",node->fullname,node->type,node->save,node->pin);
  46. inorder(node->right);
  47. }
  48. }
  49.  
  50. void preorder(struct data *node){
  51. if(node != NULL){
  52. printf("Fullname :%s\nType :%s\nSave :%d\nPIN :%s\n",node->fullname,node->type,node->save,node->pin);
  53. preorder(node->left);
  54. preorder(node->right);
  55. }
  56. }
  57.  
  58. void postorder(struct data *node){
  59. if(node != NULL){
  60. postorder(node->left);
  61. postorder(node->right);
  62. printf("Fullname :%s\nType :%s\nSave :%d\nPIN :%s\n",node->fullname,node->type,node->save,node->pin);
  63. }
  64. }
  65.  
  66. //buat ngecek alphabet semua
  67. int cekAlpha(char temp[])
  68. {
  69. for(int i=0;i<strlen(temp);i++){
  70. if(isdigit(temp[i])){
  71. return 1;break;
  72. }
  73. }
  74. return 0;
  75. }
  76.  
  77.  
  78. // buat ngecek 2 kata
  79. int cekKata(char temp[]){
  80. char *path;
  81. int kata=0;
  82. path = strtok(temp," ");
  83. while (path != NULL)
  84. {
  85. kata ++;
  86. path = strtok (NULL, " ");
  87. }
  88. return kata;
  89. }
  90.  
  91. // buat ngecek Huruf kapital di awal aja
  92. int cekCap(char temp[]){
  93. for(int i=0;i<strlen(temp);i++){
  94. if(temp[0] < 'A' || temp[0] >'Z'){
  95. return 2;break;
  96. }
  97. else if(temp[i] > 'A' && temp[i] <'Z'){
  98. return 2;break;
  99. }
  100. }
  101. return 1;
  102. }
  103.  
  104.  
  105. void add(){
  106. char nama[100];
  107. char type[100];
  108. char pin[7];
  109. int safe;
  110. do{
  111. printf("Input panjang nama[alphabet, 2 kata , min 5 char] :");
  112. scanf("%[^\n]",&nama);fflush(stdin);
  113. }while(strlen(nama) < 5 && cekAlpha(nama) == 1 && cekKata(nama) != 2 && cekCap(nama) != 1);
  114.  
  115. do{
  116. printf("Input Type[ Regular || VIP ] :");
  117. scanf("%s",&type);fflush(stdin);
  118. }while(strcmp(type,"Regular") != 0 && strcmp(type,"VIP") != 0);
  119.  
  120. if(strcmp(type,"Regular") == 0){
  121. do{
  122. printf("Input Tabungan[500000-5000000] :");
  123. scanf("%d",&safe);fflush(stdin);
  124. }while(safe < 500000 || safe>5000000);
  125. }
  126. else if(strcmp(type,"VIP") == 0){
  127. do{
  128. printf("Input Tabungan[1000000-10000000] :");
  129. scanf("%d",&safe);fflush(stdin);
  130. }while(safe < 1000000 || safe>10000000);
  131. }
  132.  
  133. //BUAT GENERATE PIN
  134. srand(time(NULL));
  135. for(int i =0;i<6;i++){
  136. int j= rand()%3+1;
  137. if( j == 1 )pin[i]= (rand()%26)+65;
  138. else if(j == 2) pin[i]= (rand()%26)+97;
  139. else if(j == 3) pin[i]=(rand()%10)+48;
  140. }
  141. pin[6] =NULL;
  142. push(&root,nama,type,safe,pin);
  143. }
  144.  
  145.  
  146. void view(){
  147. if(root != NULL){
  148. printf("View inorder :\n");
  149. inorder(root);
  150. printf("\nView preorder :\n");
  151. preorder(root);
  152. printf("\nView postorder :\n");
  153. postorder(root);
  154. getchar();
  155. }
  156. }
  157.  
  158.  
  159. void popAll(struct data **temp){
  160. if(*temp != NULL){
  161. popAll(&(*temp)->left);
  162. popAll(&(*temp)->right);
  163. free((*temp));
  164. (*temp) = NULL;
  165. }
  166. }
  167.  
  168.  
  169.  
  170. void main(){
  171. int input;
  172. do{
  173. printf("1.insert\n");
  174. printf("2.view\n");
  175. printf("3.exit\n");
  176.  
  177. do{
  178. printf("choose :");
  179. scanf("%d",&input);
  180. fflush(stdin);
  181. }while(input <1 || input >3);
  182.  
  183. switch(input){
  184. case 1 :
  185. add();
  186. break;
  187. case 2 :
  188. view();
  189. break;
  190. }
  191. }while(input != 3);
  192. popAll(&root);
  193. }
  194.  
  195.  
  196. /* P.S :
  197. Pelajari juga cara search !!
  198.  
  199. */
Add Comment
Please, Sign In to add comment