Friday 28 September 2012
Symbol table - System Software Lab Program - Anna University - EXPERIMENT 1
EXPT : 2 Symbol table
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct table
{
char var[10];
int value;
};
struct table tb1[20];
int i,j,n;
void create();
void modify();
int search(char[],int);
void display();
void insert();
int sum;
FILE *fp;
void main()
{
int ch;
int result=0;
char v[10];
p:
clrscr();
printf("\t\tmain menu");
printf("\n1.create\n2.insert\n3.modify\n4.search\n5.display\n6.exit");
printf("\n enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
modify();
break;
case 4:
printf("Enter the symbol to be searched for:");
scanf("%s",v);
result=search(v,n);
if(result==0)
printf("the variable doesnot exist");
else
printf("the symbol present\n");
getch();
case 5:
display();
break;
case 6:
exit(1);
break;
default:
break;
}
if(ch<=6)
goto p;
getch();
}
void create()
{
fp=fopen("a.txt","w");
printf("Enter the number of entries:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the symbol and the value");
scanf("%s%d",tb1[i].var,&tb1[i].value);
}
check1:
for(j=1;j<i;j++)
if(strcmp(tb1[i].var,tb1[j].var)==0)
{
fprintf(fp,"the symbol already exists.\nEnter another symbol\n");
scanf("%s%d",tb1[i].var,&tb1[i].value);
goto check1;
}
fprintf(fp,"the table after creation is:\n");
fclose(fp);
display();
}
void insert()
{
fp=fopen("a.txt","a");
if(i>=20)
fprintf(fp,"cannot insert.table is full\n");
else
{
n++;
printf("Enter the variable and the value:");
scanf("%s%d",tb1[n].var,&tb1[n].value);
check1:
for(j=1;j<n;j++)
{
if(strcmp(tb1[j].var,tb1[i].var)==0)
{
printf("The variable already exists\nenter another variable");
scanf("%s%d",tb1[i].var,&tb1[i].value);
goto check1;
}}
fprintf(fp,"the table after insertion is:\n");
fclose(fp);
display();
}
}
void modify()
{
char variable[20];
int result=0;
fp=fopen("a.txt","W");
printf("enter the symbol to be modified");
scanf("%s",variable);
result=search(variable,n);
if(result==0)
fprintf(fp,"%s does not belong to the table",variable);
else
{
printf("the current value f the variable %s is %d \n enter the new variable n
its value",tb1[result].var,tb1[result].value);
scanf("%s%d",tb1[result].var,&tb1[result].value);
}
fprintf(fp,"the table after modification is:\n");
fclose(fp);
display();
}
int search(char variable[],int n)
{
int flag;
for(i=1;i<=n;i++)
if(strcmp(tb1[i].var,variable)==0)
{
flag=1;
break;
}
if(flag==1)
return i;
else
return 0;
}
void display()
{
fp=fopen("a.txt","W");
printf("symbol \t\t value\n");
for(i=1;i<=n;i++)
printf("%s \t\t %d \n",tb1[i].var,tb1[i].value);
getch();
for(i=1;i<=n;i++)
fprintf(fp,"%s \t\t %d \n",tb1[i].var,tb1[i].value);
fclose(fp);
getch();
}
Output:
main menu
1.create
2.insert
3.modify
4.search
5.display
6.exit
enter your choice1
Enter the number of entries:3
Enter the symbol and the value@
1
Enter the symbol and the value#
2
Enter the symbol and the value$
3
symbol
@
#
2
$
3
main menu
1.create
2.insert
3.modify
4.search
5.display
6.exit
enter your choice2
Enter the variable and the value:#
value
1
4
The variable already exists
enter another variable%
5
symbol
@
#
2
$
3
%
5
value
1
main menu
1.create
2.insert
3.modify
4.search
5.display
6.exit
enter your choice3
enter the symbol to be modified$
the current value of the variable $ is 3
enter the new variable n its value^
6
symbol
@
#
2
value
1
^
6
%
5
main menu
1.create
2.insert
3.modify
4.search
5.display
6.exit
enter your choice4
Enter the symbol to be searched for:@
the symbol present
symbol
@
#
2
^
6
%
5
value
1
main menu
1.create
2.insert
3.modify
4.search
5.display
6.exit
enter your choice4
Enter the symbol to be searched for:*
the variable doesnot exist
main menu
1.create
2.insert
3.modify
4.search
5.display
6.exit
Enter your choice5
symbol
@
#
2
^
6
%
5
main menu
1.create
2.insert
3.modify
4.search
5.display
6.exit
Enter your choice6
value
1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment