SYMBOL TABLE CREATION in C | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

Wednesday, May 7, 2014

SYMBOL TABLE CREATION in C

SYMBOL TABLE CREATION
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct symbol
{
            char name[15],addr[5];
            int value;
}s[25];
void display();
void insertion();
void updation();
void deletion();
int i,n;
void main()
{
            int ch,op;
            clrscr();
            printf("\nSymbol Table Creation:\n");
            printf("----------------------\n\n");
            printf("Enter the no.of.symbols: ");
            scanf("%d",&n);
            for(i=0;i<n;i++)
            {
              printf("\nEnter the details of symbol %d",i+1);
              printf("\nEnter Symbol name, address & value:\n");
              scanf("%s%s%d",&s[i].name,&s[i].addr,&s[i].value);
            }
            display();
            label:
            printf("\nMENUS:\n\n1.Insert\n2.Update\n3.Delete\n4.Quit\n\n");
            printf("Enter your Choice: ");
            scanf("%d",&ch);
            switch(ch)
            {
              case 1:
                insertion(); break;
              case 2:
                updation(); break;
              case 3:
                deletion(); break;
              case 4:
                exit(0); break;
              default:
                printf("Invalid option...Try Again...\n");
            }
            printf("\nDo U want to continue (1/0): ");
            scanf("%d",&op);
            if(op==1)  goto label;
            else   exit(0);
            getch();
}
void display()
{
            printf("\n-----------------------------------------------\n\n");
            printf("Sym.Name      Sym.Address       Value\n");
            printf("\n-----------------------------------------------\n\n");
            for(i=0;i<n;i++)
             printf("%s            %s               %d\n",s[i].name,s[i].addr,s[i].value);
            printf("\nTotal Number of Symbols available in Symbol Table is...%d\n",n);
}
void insertion()
{
            int t,k; clrscr();
            printf("\nEnter the no.of.symbols to be inserted: ");
            scanf("%d",&t);
            for(i=n;i<(n+t);i++)
            {
              printf("\nEnter the details of symbol %d",i+1);
              printf("\nEnter Symbol name, address & value:\n");
              scanf("%s%s%d",&s[i].name,&s[i].addr,&s[i].value);
              for(k=0;k<(i-1);k++)
                if(strcmp(s[i].name,s[k].name)==0)
                {
                  printf("\nSymbol already exist...\n"); n=i; goto disp;
                }
            }
            n=n+t;
            disp:
            printf("\nSymbol Table after insertion...\n");
            display();
}
void deletion()
{
            int a; char na[10];  clrscr();
            printf("\nSymbol Table before deletion...\n\n");
            display();
            printf("\nEnter the sym.name to be deleted: ");
            scanf("%s",na);
            for(i=0;i<n;i++)
            {
              if(strcmp(na,s[i].name)==0)
              {
                a=1;
                do
                {
                  strcpy(s[i].name,s[i+1].name); strcpy(s[i].addr,s[i+1].addr); s[i].value=s[i+1].value;
                  i++;
                }while(i<n);
              }
            }
            if(a==1)
            {
              printf("\nSymbol Table after deletion...\n\n");
              n=n-1;
              display();
            }
            else printf("\nSymbol not exist...\n\n");
}
void updation()
{
            int a;  char na[10]; clrscr();
            printf("\nSymbol Table before Updation...\n\n");
            display();
            printf("\nEnter the Sym.name which is to be updated: ");
            scanf("%s",na);
            for(i=0;i<n;i++)
            {
              if(strcmp(na,s[i].name)==0)
              {
                a=1;
                printf("\nEnter the new name, addr & value: ");
                scanf("%s%s%d",&s[i].name,&s[i].addr,&s[i].value);
              }
            }
            if(a!=1) printf("\nSymbol not exist...\n\n");
            printf("\nSymbol Table after Updation...\n\n");
            display();
}





OUTPUT:


Symbol Table Creation:
------------------------------

Enter the no.of.symbols: 2

Enter the details of symbol 1
Enter Symbol name, address & value:
VAR1   1001   23

Enter the details of symbol 2
Enter Symbol name, address & value:
VAR2   1003   54
-----------------------------------------------
Sym.Name      Sym.Address       Value
-----------------------------------------------
VAR1               1001                    23
VAR2               1003                    54

Total Number of Symbols available in Symbol Table is...2

MENUS:
1.Insert
2.Update
3.Delete
4.Quit

Enter your Choice: 1
Enter the no.of.symbols to be inserted: 2

Enter the details of symbol 3
Enter Symbol name, address & value:
VAR3   100A  30

Enter the details of symbol 4
Enter Symbol name, address & value:
VAR4   100F   67

Symbol Table after insertion...
-----------------------------------------------
Sym.Name      Sym.Address       Value
-----------------------------------------------
VAR1                1001                  23
VAR2                1003                  54
VAR3               100A               30
VAR4               100F               67

Total Number of Symbols available in Symbol Table is...4

Do U want to continue (1/0): 1

MENUS:
1.Insert
2.Update
3.Delete
4.Quit

Enter your Choice: 2

Symbol Table before Updation...
-----------------------------------------------
Sym.Name      Sym.Address       Value
-----------------------------------------------
VAR1                 1001                 23
VAR2                 1003                 54
VAR3                 100A                30
VAR4                 100F                67

Total Number of Symbols available in Symbol Table is...4

Enter the Sym.name which is to be updated: VAR3
Enter the new name, addr & value: VAR5  1009  45

Symbol Table after Updation...
-----------------------------------------------
Sym.Name      Sym.Address       Value
-----------------------------------------------
VAR1               1001                   23
VAR2               1003                   54
VAR5               1009                   45
VAR4               100F                   67

Total Number of Symbols available in Symbol Table is...4
Do U want to continue (1/0): 1

MENUS:
1.Insert
2.Update
3.Delete
4.Quit

Enter your Choice: 3

Symbol Table before deletion...
-----------------------------------------------
Sym.Name      Sym.Address       Value
-----------------------------------------------
VAR1                1001                  23
VAR2                1003                  54
VAR5                1009                  45
VAR4                100F                  67

Total Number of Symbols available in Symbol Table is...4

Enter the sym.name to be deleted: VAR2

Symbol Table after deletion...
-----------------------------------------------
Sym.Name      Sym.Address       Value
-----------------------------------------------
VAR1               1001                   23
VAR5               1009                   45
VAR4               100F                   67

Total Number of Symbols available in Symbol Table is...3

Do U want to continue (1/0): 1

MENUS:
1.Insert
2.Update
3.Delete
4.Quit


Enter your Choice: 4

No comments:

Post a Comment

Thanks for your valuable comments

Followers