Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<conio.h>
- #include<ctype.h>
- #include<time.h>
- struct data{
- char fullname[100];
- char type[50];
- int save;
- char pin[7];
- struct data *left,*right;
- }*root;
- void push(struct data **node, char name[],char type[],int save,char pin[]){
- if((*node) == NULL){
- *node = (struct data*) malloc(sizeof(struct data));
- strcpy((*node)->fullname,name) ;
- strcpy((*node)->type,type) ;
- strcpy((*node)->pin,pin) ;
- (*node)->save = save;
- (*node)->left = (*node)->right =NULL;
- }
- else{
- if(strcmpi((*node)->fullname,name) > 0 ){
- push(&(*node)->left,name,type,save,pin);
- }
- else if(strcmpi((*node)->fullname,name) < 0 ){
- push(&(*node)->right,name,type,save,pin);
- }
- else{
- printf("SAME DATA DETECTED\n");
- getchar();
- }
- }
- }
- void inorder(struct data *node){
- if(node != NULL){
- inorder(node->left);
- printf("Fullname :%s\nType :%s\nSave :%d\nPIN :%s\n",node->fullname,node->type,node->save,node->pin);
- inorder(node->right);
- }
- }
- void preorder(struct data *node){
- if(node != NULL){
- printf("Fullname :%s\nType :%s\nSave :%d\nPIN :%s\n",node->fullname,node->type,node->save,node->pin);
- preorder(node->left);
- preorder(node->right);
- }
- }
- void postorder(struct data *node){
- if(node != NULL){
- postorder(node->left);
- postorder(node->right);
- printf("Fullname :%s\nType :%s\nSave :%d\nPIN :%s\n",node->fullname,node->type,node->save,node->pin);
- }
- }
- //buat ngecek alphabet semua
- int cekAlpha(char temp[])
- {
- for(int i=0;i<strlen(temp);i++){
- if(isdigit(temp[i])){
- return 1;break;
- }
- }
- return 0;
- }
- // buat ngecek 2 kata
- int cekKata(char temp[]){
- char *path;
- int kata=0;
- path = strtok(temp," ");
- while (path != NULL)
- {
- kata ++;
- path = strtok (NULL, " ");
- }
- return kata;
- }
- // buat ngecek Huruf kapital di awal aja
- int cekCap(char temp[]){
- for(int i=0;i<strlen(temp);i++){
- if(temp[0] < 'A' || temp[0] >'Z'){
- return 2;break;
- }
- else if(temp[i] > 'A' && temp[i] <'Z'){
- return 2;break;
- }
- }
- return 1;
- }
- void add(){
- char nama[100];
- char type[100];
- char pin[7];
- int safe;
- do{
- printf("Input panjang nama[alphabet, 2 kata , min 5 char] :");
- scanf("%[^\n]",&nama);fflush(stdin);
- }while(strlen(nama) < 5 && cekAlpha(nama) == 1 && cekKata(nama) != 2 && cekCap(nama) != 1);
- do{
- printf("Input Type[ Regular || VIP ] :");
- scanf("%s",&type);fflush(stdin);
- }while(strcmp(type,"Regular") != 0 && strcmp(type,"VIP") != 0);
- if(strcmp(type,"Regular") == 0){
- do{
- printf("Input Tabungan[500000-5000000] :");
- scanf("%d",&safe);fflush(stdin);
- }while(safe < 500000 || safe>5000000);
- }
- else if(strcmp(type,"VIP") == 0){
- do{
- printf("Input Tabungan[1000000-10000000] :");
- scanf("%d",&safe);fflush(stdin);
- }while(safe < 1000000 || safe>10000000);
- }
- //BUAT GENERATE PIN
- srand(time(NULL));
- for(int i =0;i<6;i++){
- int j= rand()%3+1;
- if( j == 1 )pin[i]= (rand()%26)+65;
- else if(j == 2) pin[i]= (rand()%26)+97;
- else if(j == 3) pin[i]=(rand()%10)+48;
- }
- pin[6] =NULL;
- push(&root,nama,type,safe,pin);
- }
- void view(){
- if(root != NULL){
- printf("View inorder :\n");
- inorder(root);
- printf("\nView preorder :\n");
- preorder(root);
- printf("\nView postorder :\n");
- postorder(root);
- getchar();
- }
- }
- void popAll(struct data **temp){
- if(*temp != NULL){
- popAll(&(*temp)->left);
- popAll(&(*temp)->right);
- free((*temp));
- (*temp) = NULL;
- }
- }
- void main(){
- int input;
- do{
- printf("1.insert\n");
- printf("2.view\n");
- printf("3.exit\n");
- do{
- printf("choose :");
- scanf("%d",&input);
- fflush(stdin);
- }while(input <1 || input >3);
- switch(input){
- case 1 :
- add();
- break;
- case 2 :
- view();
- break;
- }
- }while(input != 3);
- popAll(&root);
- }
- /* P.S :
- Pelajari juga cara search !!
- */
Add Comment
Please, Sign In to add comment