Tuesday 20 September 2011

Lab manuals


                              SYMBOL TABLE WITH SUITABLE HASHING    
AIM:
To write a C program to implement creation of symbol table.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the choice from the user.
Step 3: If the option is CREATE, get the input and if there is a SYMBOL, place it in the SYMBOL TABLE.
Step 4: If the choice is SEARCH, check the SYMBOL TABLE and display the message appropriately.
Step 5: If the choice is MODIFY, search for the label and change the value of input.
Step 6: If the choice is DISPLAY, set a for loop and display the table with the given input.
Step 7: Stop the program.

SOURCE CODE:
/* CREATION OF SYMBOL TABLE */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void create();
void search();
void modify();
void display();
struct symbol
{
char label[10], address[10];
float value;
}s[10];
struct hash
{
char label[10],opcode[10],address[10];
float value,hvalue;
}h[10];
struct instructions
{
char address[10],label[10],opcode[10];
float operand;
}t;
struct hashedsymboltable
{
char label[10];
float hvalue;
}ht[10];
int ch,n=0,i,flag=0;
char a[10];
float val;
void main()
{
char a[10];
int opt;
clrscr();
ch='y';
do
{
printf("\n Enter the choice\n");
printf("1.Create\n2.Search\n3.Modify\n4.Display\n5.Exit\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
create();
break;
case 2:
search();
break;
case 3:
modify();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
}
}
while(opt<5);
getch();
}
void create()
{
printf("\nEnter the \nAddress\tLabel\tOpcode\tOperand\n");
scanf("%s\t%s\t%s\t%f",&t.address,&t.label,&t.opcode,&t.operand);
if(strcmp(t.label,"_")!=0)
{
flag=0;
for(i=0;i<=n;i++)
{
if(strcmp(h[i].label,t.label)==0)
{
flag=1;
break;
}
}
if(flag==0)
{
h[n].value=t.operand;
h[n].hvalue=h[n].value*2;
h[n].hvalue=h[n].hvalue/1000;
printf("\n\nValue od h[n].hvalue is %2.3f and h[n].valueis %2.3f",h[n].hvalue,h[n].value);
strcpy(h[n].label,t.label);
strcpy(h[n].opcode,t.opcode);
strcpy(h[n].address,t.address);
strcpy(ht[n].label,t.label);
ht[n].hvalue=h[n].hvalue;
n++;
}
else
{
printf("\nThe Label is already there");
}
}
}
void search()
{
printf("\nEnter the Label");
scanf("%s",&a);
for(i=0;i<=n;i++)
{
if(strcmp(a,h[i].label)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\t\tLABEL\tADDRESS\tHVALUE\t");
printf("\t\t----------------------\n\n");
printf("\t\t%s\t%s\t%2.3f\n",h[i].label,h[i].address,h[i].hvalue);
}
else
{
printf("Not Found");
}
}
void modify()
{
char a[10];
flag=0;
printf("\nEnter the label\n");
scanf("%s",&a);
for(i=0;i<=n;i++)
{
if(strcmp(a,ht[i].label)==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\nError");
}
else
{
printf("\nEnter the Address, Value\n");
scanf("%s%f",&h[i].address,&h[i].value);
h[i].hvalue=h[i].value*2;
h[i].hvalue=h[i].hvalue/1000;
ht[i].hvalue=h[i].hvalue;
}
}
void display()
{
printf("\t\t HASHED SYMBOL TABLE\n");
printf("\t\t--------------------\n\n");
printf("\t\tLABEL\tHASHED VALUE\n\n");
for(i=0;i<n;i++)
{
printf("\t\t%s\t   %2.3f\n",ht[i].label,ht[i].hvalue);
}
printf("\n\n\t\t HASH TABLE\n");
printf("\t\t---------------------------------------");
printf("\n\n\t\tADDRESS\tH.VALUE\tVALUE\tLABEL\tOPCODE\n\n");
for(i=0;i<n;i++)
{
printf("\t\t%s\t%2.3f\t%2.0f\t%s\t%s\t%s\n",h[i].address,h[i].hvalue,h[i].value,h[i].label,h[i].opcode);
}
}


OUTPUT:

Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
1

Enter the
Address Label   Opcode  Operand
1000    START   COPY    4890

Value od h[n].hvalue is 9.780 and h[n].valueis 4890.000
 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
1

Enter the
Address Label   Opcode  Operand
3000    CLOOP   LDA     5000


Value od h[n].hvalue is 10.000 and h[n].valueis 5000.000
 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
4
                 HASHED SYMBOL TABLE
                --------------------

                LABEL   HASHED VALUE

                START      9.780
                CLOOP      10.000


                 HASH TABLE
                ---------------------------------------

                ADDRESS H.VALUE VALUE   LABEL   OPCODE

                1000    9.780   4890    START   COPY
                3000    10.000  5000    CLOOP   LDA

 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
2

Enter the LabelCLOOP
                LABEL   ADDRESS HVALUE

                CLOOP   3000    10.000

 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
3

Enter the label
CLOOP

Enter the Address, Value
2000    2340

 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
4               
 HASHED SYMBOL TABLE
                --------------------
                LABEL   HASHED VALUE
                START      9.780
                CLOOP      4.680
                 HASH TABLE
                ---------------------------------------
                ADDRESS H.VALUE VALUE   LABEL   OPCODE
                1000    9.780   4890    START   COPY
                2000    4.680   2340    CLOOP   LDA

 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
5

Result
Thus above program executed and output verified.

0 comments:

Post a Comment