Jumat, 11 Juni 2010

Codingan Praktikum Mod 4

#include
#include
#include
#include

#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4

typedef struct node_s
{
char isi;
struct node_s *left,*right,*up, *down;
}*NODEPTR;

NODEPTR getnode()
{
NODEPTR p;
p = (NODEPTR) malloc(sizeof(struct node_s));
p->left = NULL;
p->right = NULL;
p->up = NULL;
return(p);
}

int cekbenar(NODEPTR temp,char cari[],int now, int last,int move);
void findfirst(NODEPTR tree,char cari[]);

int main()
{
int n,i,j;
int nn;
int z,y,x,a;
NODEPTR tree,first,now,news,atas;
char input[100][100];
char cari[100][100];
NODEPTR node = getnode();
printf("Jumlah = ");
scanf("%d",&n);
for(a=0;a {
printf("X Y =");
scanf("%d %d",&i, &j);
for(y = 0;y < i;y++)
{
fflush(stdin);
printf("Kalimat = ");
gets(input[y]);
for(x = strlen(input[y]);x < j;x++)
{
input[y][x] = '\0';
}
}
tree = getnode();
tree->isi = input[0][0];
first = tree;
now = tree;
for(y = 1;y < j;y++)
{
news = getnode();
news->isi = input[0][y];
news->left = now;
now->right = news;
now = now->right;
}
for(y = 1;y < i;y++)
{
news = getnode();
news->isi = input[y][0];
news->up = first;
first->down = news;
first = first->down;
now = first;
for(x = 1;x < j;x++)
{
news = getnode();
news->isi = input[y][x];
news->left = now;
atas = first->up;
for(z = 1;z <= x;z++)
{
atas = atas->right;
}
atas->down = news;
news->up = atas;
now->right = news;
now = now->right;
}
}
printf("Jumlah = ");
scanf("%d", &nn);
for(z = 0;z < nn;z++)
{
printf("Cari = ");
fflush(stdin);
gets(cari[z]);
}
for(z = 0;z < nn;z++)
{
findfirst(tree,cari[z]);
}
getch();
}
}

int cekbenar(NODEPTR temp,char cari[],int now, int last,int move)
{
if(now > last)
{
return 1;
}
else
{
if(cari[now] == temp->isi)
{
now++;
switch(move)
{
case UP :
temp = temp->up;
break;
case DOWN :
temp = temp->down;
break;
case LEFT :
temp = temp->left;
break;
case RIGHT :
temp = temp->right;
break;
}
return 1 * cekbenar(temp, cari,now,last,move);
}
else
{
return 0;
}
}
}

void findfirst(NODEPTR tree,char cari[])
{
NODEPTR first,temp;
int x = 0;
int y = 0;
int cek;
first = tree;
temp = tree;
while(temp->right != NULL || temp->down != NULL)
{
if(temp->isi == cari[0])
{
if(temp->up != NULL)
{
cek = cekbenar(temp->up, cari,1,strlen(cari)-1,UP);
if(cek == 1)
{
printf("Cintaku di %d,%d\n", y,x);
return;
}
}
if(temp->down != NULL)
{
cek = cekbenar(temp->down, cari,1,strlen(cari)-1,DOWN);
if(cek == 1)
{
printf("Cintaku di %d,%d\n", y,x);
return;
}
}
if(temp->left != NULL)
{
cek = cekbenar(temp->left, cari,1,strlen(cari)-1,LEFT);
if(cek == 1)
{
printf("Cintaku di %d,%d\n", y,x);
return;
}
}
if(temp->right != NULL)
{
cek = cekbenar(temp->right, cari,1,strlen(cari)-1,RIGHT);
if(cek == 1)
{
printf("Cintaku di %d,%d\n", y,x);
return;
}
}
}
if(temp->right != NULL)
{
temp = temp->right;
x++;
}
else
{
temp = first->down;
first = temp;
y++;
x = 0;
}
}
printf("Di Mana Cintaku??\n");
return;
}


1 komentar:

  1. Baik,, hmmm... Lengkap,, tp.. hmmm.. Yawes okelah. :)

    N mohon maaf y klo selama asistensi akeh salahe, sekian asistensi pasd 2010, semoga sukses, wassalamualaikum wr wb. :)

    BalasHapus