#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define sizeofhash 30

struct nsbh
{   
    char nama[100];
    char norek[20];
    char ttl[40];
    char jk[20];
    char alamat[30];
    char kota[50];
    char nohp[15];
    int saldo;
    struct nsbh *next;
};


struct nsbh bank;

typedef struct nsbh atm;
atm *hash[sizeofhash];
atm *a=NULL;
atm *b=NULL;
atm *c=NULL;

//Nasabah bank;
//Nasabah * pbank = &bank;

FILE *fp;

void inithashtable();
int hashFunction(char norek[20]);
void menu();
void input_nsbh();
void read();
void inithash();
void display();
void load();

int main()
{
    inithashtable();
    int ch;
    do
    {
        menu();
        scanf("%d",&ch);scanf("%*c"); 
        switch(ch) 
        {
            case 1:
            {
                printf("Tambah ke FILE\n");
                input_nsbh();
                break;
            }
            case 2:
            {
                printf("\n");
                read();
                break;
            }

            case 3:
            {
                printf("\n");
                load();
                break;
            }

            case 4:
            {
                printf("\n");
                display();
                break;
            }

            case 5:
            {
                printf("\n");
                break;
            }
            default:
            {
                printf("Pilihan yang Dimasukkan SALAHH!!\n");
                printf("\n");
                ch=5;
                break;
            }
        }
    }
    while(ch!=5); 
    fclose(fp);
    return 0;

}

int hashFunction(char norek[20])
{
    int nilai,i,val;
    nilai=0;
    for(i=0;i<strlen(norek);i++)     where is code ascii?????????????????????????????????????
    {
        val=norek[i];
        nilai=nilai+val;
    }
    return nilai%sizeofhash;
}

void menu()
{
    printf("Menu\n");
    printf("1.Masukan Data\n");
    printf("2.Read Data\n");
    printf("3.Load Data\n");
    printf("4.Tampilan Node\n");
    printf("5.Exit\n");
    printf("Pilih:");
}

void input_nsbh()
{
    struct nsbh coba; 
    int i,n;
    i=0;
    if((fp=fopen("nasabah1.txt","a+"))==NULL) {
        printf("file tidak ditemukan!!!\n");
        exit(1);
    }
    printf("Jumlah Data = ");scanf("%d",&n);scanf("%*c");
    printf("\n");   
    for(i=1;i<=n;i++)
    {

        printf("Nama: ");
        fflush(stdin);scanf("%[^\n]",coba.nama);scanf("%*c");
        printf("No Rekening: ");
        fflush(stdin);scanf("%[^\n]",coba.norek);scanf("%*c");
        printf("TTL: ");
        fflush(stdin);scanf("%[^\n]",coba.ttl);scanf("%*c");
        printf("Jenis Kelamin: ");
        fflush(stdin);scanf("%[^\n]",coba.jk);scanf("%*c");
        printf("Alamat: ");
        fflush(stdin);scanf("%[^\n]",coba.alamat);scanf("%*c");
        printf("Kota: ");
        fflush(stdin);scanf("%[^\n]",coba.kota);scanf("%*c");
        printf("No Hp: ");
        fflush(stdin);scanf("%[^\n]",coba.nohp);scanf("%*c");
        printf("Saldo: ");
        fflush(stdin);scanf("%d",&coba.saldo);scanf("%*c");
        printf("\n");

        fprintf(fp,"%s;",coba.nama);
        fprintf(fp,"%s;",coba.norek);       
        fprintf(fp,"%s;",coba.ttl);
        fprintf(fp,"%s;",coba.jk);
        fprintf(fp,"%s;",coba.alamat);
        fprintf(fp,"%s;",coba.kota);
        fprintf(fp,"%s;",coba.nohp);
        fprintf(fp,"%d\n",coba.saldo);
    }   
}
void read()
{
    //int i=0;
    if ((fp=fopen("nasabah1.txt","r"))==NULL){
        printf("Kesalahan file tidak ditemukan\n");
        exit(1);
    }

    fscanf(fp,"%*c");
    while(fscanf(fp,"%[^;]",bank.nama)!=EOF){
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.norek);
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.ttl);
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.jk);
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.alamat);
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.kota);
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.nohp);
        fscanf(fp,"%*c");
        fscanf(fp,"%d",&bank.saldo);
        fscanf(fp,"%*c");


            printf("nama\t: %s\n",bank.nama);
            printf("no. rekening\t: %s\n",bank.norek);
            printf("ttl\t: %s\n",bank.ttl);
            printf("jenis kelamin\t: %s\n",bank.jk);
            printf("alamat\t: %s\n",bank.alamat);
            printf("kota\t: %s\n",bank.kota);
            printf("no. hp\t: %s\n",bank.nohp);
            printf("saldo\t: %d\n",bank.saldo);
            printf("\n");

        inithashtable();
        inithash();
    }
}


void inithashtable()
{
    int i;
    for(i=0;i<sizeofhash;i++)
    {
        hash[i]=NULL; 
    }
}

void load()
{
    //int i=0;
    if ((fp=fopen("nasabah1.txt","r"))==NULL){
        printf("Kesalahan file tidak ditemukan\n");
        exit(1);
    }

    fscanf(fp,"%*c");
    while(fscanf(fp,"%[^;]",bank.nama)!=EOF){
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.norek);
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.ttl);
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.jk);
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.alamat);
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.kota);
        fscanf(fp,"%*c");
        fscanf(fp,"%[^;]",bank.nohp);
        fscanf(fp,"%*c");
        fscanf(fp,"%d",&bank.saldo);
        fscanf(fp,"%*c");

        inithash();
        }
}       

void inithash()                 /*Function yang berfungsi untuk mengambil nomor rekening
                        kemudian dimasukkan ke hash table sesuai hasil dari hash function
                        dengan sistem tambah belakang linkedlist*/
{                       
    int i;
    i=0;
    i=hashFunction(bank.norek);

    b=hash[i];
    a=malloc(sizeof(bank));     //alokasi memori
    a->next=NULL;

    //strcpy(a->nama,bank.nama);
    strcpy(a->norek,bank.norek);            //mengcopy string
    //strcpy(a->ttl,bank.ttl);
    //strcpy(a->jk,bank.jk);
    //strcpy(a->alamat,bank.alamat);
    //strcpy(a->kota,bank.kota);
    //strcpy(a->nohp,bank.nohp);
    a->saldo;


    if(b == NULL)
    {
        hash[i]=a;
    }
    else
    {
        while(b->next!=NULL)
        {
            b=b->next;
        }
        b->next=a;
        b=a;
        a->next;
    }
}

void display()              //Function yang berfungsi untuk menampilkan isi di hash table
{
    int i;
    for(i=0;i<sizeofhash;i++)
    {
        if(hash[i]==NULL)
        {
            printf("NULL\n");
        }
        else
        {
            printf("\n");
            c=hash[i];
            printf("Index  %d : ",i);

            while(c!=NULL)
            {
                printf("(%s) NEXT-> ",c->norek);
                //printf("\n");
                c=c->next;
            }
        }
    }
}

Recommended Answers

All 2 Replies

Member Avatar for Mouche

The function takes a character array. Each character can be a letter like 'a', but it is actually stored as a number (the ASCII value). 'a' is 97.

On line 105, you store the character into val, which is an int. This converts the number from a char to an int. You then add that to 'nilai', which I'm guessing means sum.

Essentially the hash function adds up the ascii values of a string and then takes the sum mod sizeofhash.

thk u mouche.. btw can you change this program into a program ATM (automatic teller maschine)??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.