Tuesday, November 6, 2007

Symbol Table

Program : Symbol Table



#include<stdio.h>

#include<conio.h>

#include<process.h>


struct table

{

char tab[2];

char label[20];

char ins[20];

char exp[20];

}obj[20];


struct symtab //structure for storing symtab variables

{

char name[9];

int add;

}sym[25];


char array[9],num[9];

int temp,sign=0,ln,tmp,loc;

int z,i,j,t,c,k,no,s,value,pos;


void main() //main function

{

char ch,str[20];

FILE *ptr1;

ptr1=fopen("code.txt","r");

clrscr();


if(!ptr1)

{

printf("FIle not found:");

getch();

exit(0);

}

c=i=0;


while((ch=getc(ptr1))!=EOF)

{

if(ch=='\t')

{

obj[c].tab[0]=NULL;

ch=getc(ptr1);

}


while(ch!='\t')

{

obj[c].tab[i]=ch;

ch=getc(ptr1);

i++;

}

i=0;


while(ch!='\t')

{

obj[c].label[i]=ch;

ch=getc(ptr1);

i++;

}

ch=getc(ptr1);

j=0;


while(ch!='\t')

{

obj[c].ins[j]=ch;

j++;

ch=getc(ptr1);

}

k=0,t=0;

ch=getc(ptr1);


while(ch!='\n')

{

if(isdigit(ch))

{

obj[c].exp[t]=ch;

ch=getc(ptr1);

t++;

}

else

{

obj[c].exp[t]=ch;

ch=getc(ptr1);

t++;

}

}

c++;

}



printf("\n\tSOURCE PROGRAM\n\t*---------------*\n");


for(t=0;t<=c;t++)

{

printf("\n%s\t%s\t%s\n",obj[t].tab,obj[t].ins,obj[t].exp);


}


printf("\n\t SYMBOL TABLE \n\t*-------------------*\n");

value=c;

s=0;



while(s<=value)

{

if((strcmp(obj[s].ins,"START"))==0)

{

sym[s+1].add=atoi(obj[s].exp);

sym[s].add=sym[s+1].add;

loc=sym[s].add;

}



else if((strcmp(obj[s].ins,"STOP"))==0)

{

sym[s+1].add=loc+1;

loc=loc+1;

}



else if((strcmp(obj[s].ins,"DS"))==0)

{

sym[s+1].add=loc+atoi(obj[s].exp);

loc=loc+atoi(obj[s].exp);

}



else if((strcmp(obj[s].ins,"MOVER"))==0)

{

sym[s+1].add=loc+1;

loc=loc+1;

}



else if((strcmp(obj[s].ins,"DC"))==0)

{

sym[s+1].add=loc+1;

loc=loc+1;

}



else if((strcmp(obj[s].ins,"ADD"))==0)

{

sym[s+1].add=loc+1;

loc=loc+1;

}


else if((strcmp(obj[s].ins,"MOVEM"))==0)

{

sym[s+1].add=loc+1;

loc=loc+1;

}


else if((strcmp(obj[s].ins,"PRINT"))==0)

{

sym[s+1].add=loc+1;

loc=loc+1;

}


else if((strcmp(obj[s].ins,"EQU"))==0)

{


for(z=0;z<strlen(obj[s].exp);z++)

{

if(obj[s].exp[z]=='+')

{

pos=z;

sign=1;

ln=s;

goto lab;

}

else if(obj[s].exp[z]=='-')

{

pos=z;

sign=2;

ln=s;

goto lab;

}

} //end of for



lab:


if(sign==1)

{

t=0;


for(i=0;i<pos;i++)

{

array[i]=obj[ln].exp[i];

}


for(j=pos+1;j<strlen(obj[ln].exp);j++)

{

num[t]=obj[ln].exp[j];

t++;


}

tmp=atoi(num);


for(i=0;i<=c;i++)

{

if(strcmp(array,obj[i].tab)==0)

{


sym[s].add=sym[i].add+tmp;

sym[s+1].add=sym[s-1].add+1;

break;

}


} //end of for

} //end of else if sign =1



else if(sign==2)

{

t=0;

for(i=0;i<pos;i++)

{

array[i]=obj[ln].exp[i];

}

for(j=pos+1;j<strlen(obj[ln].exp);j++)

{

num[t]=obj[ln].exp[j];

}

tmp=atoi(num);

for(i=0;i<=c;i++)

{

if(strcmp(array,obj[i].tab)==0)

{

if(obj[s].ins[0]=='O')

{

sym[s+1].add=sym[s].add+1;

}

else

{

sym[s].add=sym[i].add-tmp;

sym[s+1].add=sym[s-1].add+1;

}

break;

}


} //end of for

} //end of else if sign =2

}

else if((strcmp(obj[s].ins,"ORIGIN"))==0)

{

for(z=0;z<strlen(obj[s].exp);z++)

{

if(obj[s].exp[z]=='+')

{

pos=z;

sign=1;

ln=s;

goto lab1;

}



else if(obj[s].exp[z]=='-')

{

pos=z;

sign=2;

ln=s;

goto lab1;

}

} //end of for

lab1:

if(sign==1)

{

t=0;

for(i=0;i<pos;i++)

{

array[i]=obj[ln].exp[i];

}



for(j=pos+1;j<strlen(obj[ln].exp);j++)

{

num[t]=obj[ln].exp[j];

t++;


}

tmp=atoi(num);


for(i=0;i<=c;i++)

{

if(strcmp(array,obj[i].tab)==0)

{

loc=sym[i].add+tmp;

sym[s+1].add=loc;

break;

}

} //end of for

} //end of else if sign =1

else if(sign==2)

{

t=0;



for(i=0;i<pos;i++)

{

array[i]=obj[ln].exp[i];

}

for(j=pos+1;j<strlen(obj[ln].exp);j++)

{

num[t]=obj[ln].exp[j];

}

tmp=atoi(num);

for(i=0;i<=c;i++)

{

if(strcmp(array,obj[i].tab)==0)

{

loc=sym[s].add=sym[i].add-tmp;

sym[s+1].add=loc;

break;

}


} //end of for

} //end of else if sign =2

}



if(strcmp(obj[s].tab,NULL)!=0)

printf("\n\t%s\t%d",obj[s].tab,sym[s].add);

s=s+1;

} //end of for s

fclose(ptr1);

getch();


} //end of main function


String Checking

Program : String Checking



#include<stdio.h>

#include<conio.h>


void main()

{

int i,y,x,l;

char a[20]; //the input string is stored in this character array

clrscr();

printf("String to be checked\n");

gets(a); //get the input string

l=strlen(a); //calculate the length of the string



if(a[l-3]=='a'&&a[l-2]=='b'&&a[l-1]=='c')

x=1;



else

x=0;



for(i=1;i<=l-3;i++)

{

if(a[i]=='a'||a[i]=='b')

y=1;

else

break;

}



if((x==1)&&(y==1))

printf("String accepted"); //output statement



else

printf("String not accepted\n");

getch();

}


S R Parser

Program : S R Parser



#include<stdio.h>

#include<process.h>

#include<conio.h>

#include<string.h>


void main() //main function

{

int i,j,len;

char str[20],sta[20];

int k;

clrscr();

printf("\nEnter the string:\t");

gets(str);

len=strlen(str); //get string length

j=0;



for(i=0;i<len;i++)

{

printf("\n%s",sta);



if(str[i]=='a')

{

sta[j]='E';

j++; //increment stack pointer

printf("\n%s",sta);


if((sta[j-2]=='+')||(sta[j-2]=='-')||(sta[j-2]=='*')||(sta[j-2]=='/'))

{

if(sta[j-3]=='E')

{

sta[j-2]=' ';

sta[j-1]=' ';

j=j-2; //decrement stack pointer by 2

}


else

{

if(j>2)

{

printf("\nSTRING NOT ACCEPTED");

getch();

exit(0);

}

}

}


else if(sta[j-2]=='E')

{

printf("\nSTRING NOT ACCEPTED");

getch();

exit(0);

}


else

{

if(j>2)

{

printf("\nSTRING NOT ACCEPTED");

getch();

exit(0);


}

}


} //end of if(id)


else if(str[i]=='+')

{

sta[j]='+';

j++; //increment stack pointer


}


else if(str[i]=='-')

{

sta[j]='-';

j++; //increment stack pointer


}


else if(str[i]=='/')

{

sta[j]='/';

j++; //increment stack pointer


}


else if(str[i]=='*')

{

sta[j]='*';

j++; //increment stack pointer


}

} //end of for loop


printf("\nE\nSTRING ACCEPTED");

getch();

} //end of main function

Operator Precedence Parser

Program : Operator Precedence Parser



#include<stdio.h>

#include<conio.h>

#include<string.h>


void main() //main function

{

int i,j=0,count=0,n,s,k,d=0,f,loc=0,stlen=0,mj=0;

char st[10],in[20],stack[10];

clrscr();

printf("ENTER THE LANGUAGE TO BE REDUCED\n");

gets(in); //get the input language

n=strlen(in); //get string length


for(i=0;i<n;i++) //until end of string

{

if(in[i]==' ')

break;

else if(in[i]=='+'||in[i]=='-'||in[i]=='*'||in[i]=='/')

{

stack[j]=in[i];

st[d]=in[i];

j++;

d++;

}


else if(in[i]=='i')

{

stack[j]='E';

j++;

}

}

st[d]='\0';

stlen=d;

stack[j]='\0';

puts(stack);

puts(st);

i=0;

j=0;


while(st[i]!=' ')

{

count=0;

if(st[i]=='+'||st[i]=='-')

{

while(st[j]!=' ')

{

if(st[j]=='*'||st[j]=='/')

{

count++;

loc=j;

break;

}



else

{

j++;

}

}



if(count!=0)

{

st[j]=' ';

j=(2*(loc+1))-1;



for(mj=loc;mj<stlen;mj++)

{

st[mj]=st[mj+1];

}

stlen--;



for(mj=j;mj<strlen(stack);mj++)

{

stack[mj]=stack[mj+2];

stack[mj+1]=stack[mj+3];

}



puts(stack);

puts(st);

}

else

{

st[i]=' ';

j=((2*(i+1))-1);



for(mj=i;mj<stlen;mj++)

{

st[mj]=st[mj+1];

}

stlen--;


for(mj=j;mj<strlen(stack);mj++)

{

stack[mj]=stack[mj+2];

stack[mj+1]=stack[mj+3];

}

puts(stack);

}

}



else if(st[i]=='*'||st[i]=='/')

{


while(st[j]!=' ')

{

if(st[j]=='*'||st[j]=='/')

{

j++;

}

else break;

}

st[j]=' ';

j=0;


while(st[j]!=' ')

{

if(st[j]=='*'||st[j]=='/')

{

j++;

}

else break;

}


stack[j]=stack[j+2];

stack[j+1]=stack[j+3];

puts(stack);

}

}

puts(stack);

getch();

} //end of main function

Left Recursion

Program : Left Recursion



#include<stdio.h>

#include<conio.h>

#include<string.h>


void main() //main function

{

char pro[20],alph[10],beta[10];

int i,len,u,pos;

clrscr();

printf("Enter the production\t");

gets(pro); //get the input production

len=strlen(pro); //find length of production

pos=3;

u=0;



if(pro[0]!=pro[2]) //check production is non left recursive

{

printf("\nProduction is not left recursive\n");


}



else //if production is left recursive

{

printf("\nProduction is left recursive\n");



for(i=3;i<=len;i++) //converting to non left recursive production

{

if(pro[i]!='|')

{

alph[u]=pro[i];

u++;

pos++;

}

else

{

break;

}

alph[u]='\0';

}

pos++;

u=0;



for(i=pos;i<=len;i++)

{

beta[u]=pro[i];

u++;

}





beta[u]='\0';


printf("\n\nEquivalent non left recursive grammar is:\n");


printf("\n%c=%s%c'\n",pro[0],beta,pro[0]);


printf("\n%c'=%s%c'|e\n",pro[0],alph,pro[0]);


}



getch();

}


Lexical Analyzer

Program : Lexical Analyzer



#include<stdio.h>

#include<conio.h>

#include<stdlib.h>


void main() //main function

{

FILE *p;

int i=0,j=0,k=0,flag;

char x,temp[100];


char digit[10]={'0','1','2','3','4','5','6','7','8','9'};



char key[25][10]={"auto","break","case","char","const","continue",

"default","do","double","else","enum","for",

"goto","if","int","return","signed","static",

"struct","union","while","switch","void","float"};


char opera[10][17]={"+","=","*","/","-",">","<"};


clrscr();

p=fopen("text.txt","r"); //open file

if(p==NULL) //if file not present

printf("cannot open the file");

else

{

printf("\tKEYWORD\tIDENTIFIER\tOPERATOR\n");

while((x=fgetc(p))!=EOF)

{

if((x!=' ')&&(x!='\t')&&(x!='\n')) //get keyword

{

temp[i]=x;

i++;

temp[i]='\0';

}

else

{

for(j=0;j<i;j++)

temp[j]=' ';

i=0;

}

for(k=0;k<25;k++) //compare with key

{

if(strcmp(temp,key[k])==0)

{

printf("\n\t%s",temp);

for(j=0;j<i;j++)

temp[j]=' ';

i=0;

}

}

for(j=0;j<strlen(temp);j++) //check identifiers

{

for(k=0;k<11;k++)

{

flag=0;

if(temp[j]==digit[k])

{

flag=1;

break;

}

}

}

if(flag!=0)

{

printf("\n\t\t%s",temp);

for(j=0;j<i;j++)

temp[j]=' ';

i=0;

}

for(k=0;k<10;k++) //check if operator

{

if(strcmp(temp,opera[k])==0)

{

printf("\n\t\t\t\t%s",temp);

for(j=0;j<i;j++)

temp[j]=' ';

i=0;

}

}

}

}

fclose(p); //close the file

getch();

}


Intermediate Code

Program : Intermediate Code



#include<stdio.h>

#include<conio.h>


struct //structure for generating symbol table

{

char label[15];

char opcode[15];

char operand[15];

}pgm[20];


struct //structure for printing symbol table

{

char label[25];

int addr;

}symtab[20];


struct //structure for temporary usage

{

char label[15];

}samplesym[20];


static char is[40][40]={"stop","add","sub","mult","mover","movem","comp",

"bc","div","read","print"};

static char dl[40][40]={"dc","ds"};

static char ad[40][40]={"start","end","origin","equ","ltorg"};

static char re[40][40]={"areg","breg","creg","dreg"};


void main() //main function

{

FILE *p1;

int n=0,loc=0,j=0,k=0,i=0,s=0,q=0,z1=0,fl=0,i1,ll,z=0,u=0,u1=0,ee,m=0,

int a1=0,w,temp;

char c,f[5],t[5],ff[5],tt[5],mm,q1;

clrscr();

p1=fopen("codes.txt","r");

if(p1==NULL)

printf("\n FILE CANNOT BE OPENED");

else

{

while((c=fgetc(p1))!=EOF)

{

if(c=='\n')

{

n++;

c=fgetc(p1);

}

}

}

fclose(p1);

p1=fopen("codes.txt","r");



if(p1==NULL)

printf("\n FILE CANNOT BE OPENED");



else

{

for(i=0;i<n;i++)

{

j=0;

while((c=fgetc(p1))!='\t')

pgm[i].label[j++]=c;

j=0;

while((c=fgetc(p1))!='\t')

pgm[i].opcode[j++]=c;

j=0;

while((c=fgetc(p1))!='\n')

pgm[i].operand[j++]=c;

}

fclose(p1);

printf("\n INPUT FILE IS:\n\n");

for(i=0;i<n;i++)

{


printf(" %s\t",pgm[i].label);

printf("%s\t",pgm[i].opcode);

printf("%s\t",pgm[i].operand);

printf("\n\n");

}



for(i=0;i<n;i++) //comparing symtab variables

{

if(strcmp(pgm[i].opcode,"start")==0)

loc=atoi(pgm[i].operand);

else if(strcmp(pgm[i].opcode,"ds")==0)

{

ll=atoi(pgm[i].operand);

if(strcmp(pgm[i].label,"no")!=0)

{

strcpy(symtab[k].label,pgm[i].label);

symtab[k].addr=loc;

k++;

}

loc=loc+ll;

}

else if(strcmp(pgm[i].opcode,"equ")==0)

{

s=0;

while(!u1)

{

if(pgm[i].operand[z]=='-')

{

u1=1;

}

else if(pgm[i].operand[z]=='+')

{

u1=1;

}

else

{

u1=0;

f[u]=pgm[i].operand[z];

u++;

}

z++;

}

z--;

f[u]='\0';

q=pgm[i].operand[z];

z++;

u=0;

while(z<strlen(pgm[i].operand))

{

t[u]=pgm[i].operand[z];

z++;u++;

}

t[u]='\0';

ee=atoi(t);

for(j=0;j<k;j++)

{

if(strcmp(symtab[j].label,f)==0)

{

s=symtab[j].addr;

if(q=='+')

s=s+ee;

else

s=s-ee;

break;


}


}

if(strcmp(pgm[i].label,"no")!=0)

{

strcpy(symtab[k].label,pgm[i].label);

symtab[k].addr=s;

k++;

}

}

else if(strcmp(pgm[i].opcode,"origin")==0)

{

s=0,u1=0,z=0,u=0;

while(!u1)

{

if(pgm[i].operand[z]=='-')

{

u1=1;

}

else if(pgm[i].operand[z]=='+')

{

u1=1;

}

else

{

u1=0;

ff[u]=pgm[i].operand[z];

u++;

}

z++;

}

z--;

ff[u]='\0';

q=pgm[i].operand[z];

z++; u=0;

while(z<strlen(pgm[i].operand))

{

tt[u]=pgm[i].operand[z];

z++;u++;

}

tt[u]='\0';

ee=atoi(t);

for(j=0;j<k;j++)

{

if(strcmp(symtab[j].label,ff)==0)

{

s=symtab[j].addr;

if(q=='+')

s=s+ee;

else

s=s-ee;

break;

}

}

loc=s;

}

else

{

if(strcmp(pgm[i].label,"no")!=0)

{

strcpy(symtab[k].label,pgm[i].label);

symtab[k].addr=loc;

k++;

loc++;

}

}

}

}


printf("INTERMEDIATE CODE IS\n");


//generation of intermediate code

z=0;

for(j=0;j<=k;j++)

{

if(strcmp(symtab[j].label,"\0")!=0)

{

strcpy(samplesym[z].label,symtab[j].label);

z++; }

}

for(i=0;i<n;i++)

{

if(strcmp(pgm[i].opcode,"start")==0)

{

loc=atoi(pgm[i].operand);

z=0;

for(i1=0;i1<=10;i1++)

{

if(strcmp(ad[i1],"start")==0)

{

z=i1;

break;

}

}

z++;

printf("(ad,0%d) (c,%d)\n",z,loc);

m=m+1;

}

else if(strcmp(pgm[i].opcode,"ds")==0)

{

ll=atoi(pgm[i].operand);

z=0;

for(i1=0;i1<=1;i1++)

{

if(strcmp(dl[i1],"ds")==0)

{

z=i1;

break;

}

}

z=z++;

ll=atoi(pgm[i].operand);

printf("(dl,0%d) (c,%d)\n",z,ll);

m=m+1;

loc=loc+ll;

}

else if(strcmp(pgm[i].opcode,"dc")==0)

{

z=0;

for(i1=0;i1<=1;i1++)

{

if(strcmp(dl[i1],"dc")==0)

{

z=i1;

break;

}

}

z=z++;

printf("(dl,0%d) (%s)\n",z,pgm[i].operand);

}

else if(strcmp(pgm[i].opcode,"equ")==0)

{

s=0,z=0,u1=0;

while(!u1)

{

if(pgm[i].operand[z]=='-')

{

u1=1;

}

else if(pgm[i].operand[z]=='+')

{

u1=1;

}

else

{

u1=0;

f[u]=pgm[i].operand[z];

u++;

}

z++;

}

z--;

f[u]='\0';

q1=pgm[i].operand[z];

z++;

u=0;

while(z<strlen(pgm[i].operand))

{

t[u]=pgm[i].operand[z];

z++;u++;

}

t[u]='\0';

ee=atoi(t);

for(j=0;j<k;j++)

{

if(strcmp(symtab[j].label,f)==0)

{

s=symtab[j].addr;

if(q1=='+')

s=s+ee;

else

s=s-ee;

break;


}

}

z=0;

for(i1=0;i1<=4;i1++)

{

if(strcmp(ad[i1],"equ")==0)

{

z=i1;

break;

}

}

z=z+1;

w=0;

for(q=0;q<=k;q++)

{

if(strcmp(samplesym[q].label,f)==0)

{

w=q;

break;

}

}

w=w+1;

printf("(ad,0%d) (s,%d)%c%s\n",z,w,q1,t);

m=m+1;

}

else if(strcmp(pgm[i].opcode,"origin")==0)

{

s=0,u1=0,z=0,u=0;

while(!u1)

{

if(pgm[i].operand[z]=='-')

{

u1=1;

}

else if(pgm[i].operand[z]=='+')

{

u1=1;

}

else

{

u1=0;

ff[u]=pgm[i].operand[z];

u++;

}

z++;

}

z--;

ff[u]='\0';

q1=pgm[i].operand[z];

z++;

u=0;

while(z<strlen(pgm[i].operand))

{

tt[u]=pgm[i].operand[z];

z++;u++;

}

tt[u]='\0';

ee=atoi(t);

for(j=0;j<k;j++)

{

if(strcmp(symtab[j].label,ff)==0)

{

s=symtab[j].addr;

if(q=='+')

s=s+ee;

else

s=s-ee;

break;


}


}

loc=s;

z=0;

for(i1=0;i1<=4;i1++)

{

if(strcmp(ad[i1],"origin")==0)

{

z=i1;

break;

}

}

z=z+1;

w=0;

for(q=0;q<=k;q++)

{

if(strcmp(samplesym[q].label,ff)==0)

{

w=q;

break;

}

}

w=w+1;

printf("(ad,0%d) (s,%d)%c%s\n",z,w,q1,tt);

m=m+1;


}

else if(strcmp(pgm[i].opcode,"mover")==0)

{

z=0;

for(i1=0;i1<=10;i1++)

{

if(strcmp(is[i1],"mover")==0)

{

z=i1;

break;

}

}

temp=z;

w=0,z=0,u1=0,u=0;

while(!u1)

{

if(pgm[i].operand[z]==',')

{

u1=1;

}

else

{

u1=0;

f[u]=pgm[i].operand[z];

u++;

}

z++;

}

z--;

f[u]='\0';

q1=pgm[i].operand[z];

z++;

u=0;

while(z<strlen(pgm[i].operand))

{

t[u]=pgm[i].operand[z];

z++;u++;

}

t[u]='\0';

for(q=0;q<=k;q++)

{

if(strcmp(re[q],f)==0)

{

fl=q;

break;

}

}

fl++;

a1=0;

for(q=0;q<=k;q++)

{

if(strcmp(samplesym[q].label,t)==0)

{

a1=q;

break;

}

}

a1++;

printf("(is,0%d) (%d)(s,%d)\n",temp,fl,a1);

m=m+1;

loc=loc+1;

}

else if(strcmp(pgm[i].opcode,"add")==0)

{

z1=0;

for(i1=0;i1<=10;i1++)

{

if(strcmp(is[i1],"add")==0)

{

z1=i1;

break;

}

}

z1++;

w=0,z=0,u1=0;

while(!u1)

{

if(pgm[i].operand[z]==',')

{

u1=1;

}

else

{

u1=0;

f[u]=pgm[i].operand[z];

u++;

}

z++;

}

z--;

f[u]='\0';

//splitting opernds of mover

q1=pgm[i].operand[z];

z++;

u=0;

while(z<strlen(pgm[i].operand))

{

t[u]=pgm[i].operand[z];

z++;u++;

}

t[u]='\0';

z=0;

for(q=0;q<=k;q++)

{

if(strcmp(re[q],f)==0)

{

z=q;

break;

}

}

z=z+1;

for(q=0;q<=k;q++)

{

if(strcmp(samplesym[q].label,t)==0)

{

a1=q;

break;

}

}

a1++;

printf("(is,0%d) (%d)(s,%d)\n",z1,z,a1);

m=m+1;

loc=loc+1;

}

else if(strcmp(pgm[i].opcode,"movem")==0)

{

z=0;

for(i1=0;i1<=10;i1++)

{

if(strcmp(is[i1],"movem")==0)

{

z=i1;

break;

}

}

temp=z;

//splitting opernds of movem

w=0,z=0,u1=0,u=0;

while(!u1)

{

if(pgm[i].operand[z]==',')

{

u1=1;

}

else

{

u1=0;

f[u]=pgm[i].operand[z];

u++;

}

z++;

}

z--;

f[u]='\0';

q1=pgm[i].operand[z];

z++;

u=0;

while(z<strlen(pgm[i].operand))

{

t[u]=pgm[i].operand[z];

z++;u++;

}

t[u]='\0';

for(q=0;q<=k;q++)

{

if(strcmp(re[q],f)==0)

{

fl=q;

break;

}

}

fl++;

for(q=0;q<=k;q++)

{

if(strcmp(samplesym[q].label,t)==0)

{

a1=q;

break;

}

}

a1++;

printf("(is,0%d) (%d)(s,%d)\n",temp,fl,a1);

m=m+1;

loc=loc+1;

}

else if(strcmp(pgm[i].opcode,"print")==0)

{

z=0;

for(i1=0;i1<=10;i1++)

{

if(strcmp(is[i1],"print")==0)

{

z=i1;

break;

}

}

w=0;

for(q=0;q<=k;q++)

{

if(strcmp(samplesym[q].label,pgm[i].operand)==0)

{

w=q;

break;

}

}

w=w+1;

printf("(is,%d) (s,%d)\n",z,w);

m=m+1;

loc=loc+1;

}

else if(strcmp(pgm[i].opcode,"stop")==0)

{

z=0;

for(i1=0;i1<=10;i1++)

{

if(strcmp(ad[i1],"stop")==0)

{

z=i1;

break;

}

}

z++;

printf("(is,0%d)\n",z);

m=m+1;

loc=loc+1;

}

else if(strcmp(pgm[i].opcode,"end")==0)

{

z=0;

for(i1=0;i1<=4;i1++)

{

if(strcmp(ad[i1],"end")==0)

{

z=i1;

break;

}

}

z=z+1;

printf("(ad,0%d)\n",z);

m=m+1;

loc=loc+1;

}

}

getch();

}


First Parser

Program : First Parser



#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<string.h>



void main() //main function

{

char gramar[10][10];

int ch=1,n=0;

char temp[10];

char first[23];

int m;

int j=0,i=0,k=3,flag=0;


char small[30]={'a','b','c','d','e','f','g','h',

'i','j','k','l','m','n','o','p',

'q','r','s','t','u','v','w','x',

'y','z','*','+','/','-','(',')'}; //check terminals



int p,h=1;

char sa[10];

char pa[10];

char temp1[10];

clrscr();


do

{

printf("\nenter grammar");

scanf("%s",gramar[n]); //get the grammar

printf("\ncontinue( 0 or 1 )");

scanf("\n%d",&ch);

n++;

}




while(ch==1);


printf("\n\nTHE ENTERED GRAMMAR IS");

for(i=0;i<n;i++)

printf("\n%s",gramar[i]);

do

{

printf("\nenter the grammar symbol whose first has to be found");

scanf("%s",sa); //accept symbol

label:


for(i=0;i<n;i++)

{

strcpy(temp,gramar[i]);

if(sa[0]==temp[0])

{

strcpy(temp1,temp); //copy to temp1

break;

}

}


k=2;

j=0;


for(i=0;i<35;i++) //check if terminal

{

if(temp1[2]==small[i])

{

flag=1;

first[j]=temp1[k];

}

}

j++;


while(k<strlen(temp1))

{

if(temp1[k]=='|')

{

for(i=0;i<35;i++) //check if terminal

{

if(temp1[k+1]==small[i])

{

flag=1;

first[j]=temp1[k+1];

}

}

j++;

goto l1;

}

k++;

}

l1:

if(flag==0) //if no terminal is found

{

sa[0]=temp1[2];

goto label;

}

first[j]='\0';

printf("\n { ");

for(i=0;i<strlen(first);i++) //print the first of symbol

{

if(i+1==strlen(first))

printf("%c",first[i]);

else

printf("%c , ",first[i]);

}

printf(" } ");

printf("\n do u want to continue ?? yes(1) or no(0) ");

scanf("\n%d",&h);

}

while(h!=0);


getch();

}


3 Address Conversion

Program : 3 Address Conversion



# include<stdio.h>

# include<conio.h>

# include<string.h>


void main() //main function

{



int i,j,ctr,n,k,l,f,z,m1;

char exp[30];

char temp[8];

char m[10]={'P','Q','R','S','T','U','V','W','X','Y'};

clrscr();

ctr=n=k=l=f=z=m1=0; //initialize variables as 0

printf("Enter the expression");

gets(exp); //get input expression



while(strlen(exp)!=5)

{

for(i=2;i<strlen(exp);i++)

{



if(exp[i]=='(') //if scanned character is (

{

k=i;

j=0;



while(exp[k+1]!=')') //until a ) is reached

{

temp[j]=exp[k+1];

j++; //increment j and k

k++;

}



exp[i]=m1[m];

temp[j]='\0';

printf("\n%c=%s",m1[m],temp);

m1++;

ctr=l;

n=i+1;



while(exp[ctr+n-1]!=')')

{

ctr++;

}



for(z=n,f=n+ctr;f<strlen(exp);f++,z++)

{

exp[z]=exp[f];

}



exp[z]='\0';

}

}



for(i=2;i<strlen(exp);i++)

{



if((exp[i]=='+')||(exp[i]=='-')) //if character is a + or - operator

{

k=i;

j=0;



for(k=i-1;k<i+2;k++)

{

temp[j]=exp[k];

j++;

}



exp[i-1]=m1[m];

temp[j]='\0';

printf("\n%c=%s",m1[m],temp);

m1++;



for(z=i;z<strlen(exp);z++)

{

exp[z]=exp[z+2];

}





exp[z]='\0';

}



for(i=2;i<strlen(exp);i++)

{

//if character is a * or / operator

if((exp[i]=='*')||(exp[i]=='/'))


{

k=i;

j=0;



for(k=i-1;k<i+2;k++)

{

temp[j]=exp[k];

j++;

}



exp[i-1]=m1[m];

temp[j]='\0';

printf("\n%c=%s",m1[m],temp);

m1++;



for(z=i;z<strlen(exp);z++)

{

exp[z]=exp[z+2];

}

exp[z]='\0'; //initialize final character as \0

}

}

}


}

printf("\n%s",exp);

getch();

}

Stack Using Linked List

Program : Stack Using Linked List



import java.io.*; //To implement I/O operations


class link

{

int data; //data item

link next; //next link in list



link(int id) //constructor

{

data=id; //initialize data

}



void displaylink() //To display the list

{

System.out.println("{"+data+"}");

}

} //end of class link




class stack

{

link first; //ref to first link on list

stack() //constructor

{

first=null; //no links on list yet

}



public void insert(int dd) //To insert into the list

{

link op=new link(dd); //make new link

op.next=first;

first=op;

} //End of function create



void delete() //To delete node from the list

{

if (first==null) //Check if stack is empty

{

System.out.println("STACK EMPTY");

//Print stack Empty

}

else

{

first=first.next; //point to next node

System.out.println("THE TOP ELEMENT IS DELETED FROM STACK");

}

} //End of function delete



void display() //To display the stack

{

link current=first; //start at the beginning of the list

System.out.println("THE ELEMENTS OF STACK ARE");

while (current!=null) //until end of the list

{

current.displaylink();

//print data

current=current.next;

//move to next link

}

} //end of function display

} //End of class stack



public class Stacklink //The main class linkstack

{

public static void main(String args[])throws IOException

//main function

{

stack m=new stack(); //make new linklist

char cc;

try

{

do

{

BufferedReader f=new BufferedReader(new InputStreamReader(System.in));

System.out.println("MENU");

//Print menu

System.out.println("1.PUSH");

System.out.println("2.POP");

System.out.println("3.DISPLAY");

System.out.println("ENTER YOUR CHOICE");

int y=Integer.parseInt(f.readLine());

//Accepts choice

if (y==1)

{

System.out.println("ENTER THE ELEMENT TO BE INSERTED");

int a=Integer.parseInt(f.readLine());

m.insert(a); //Assign values to links



}

if(y==2)

m.delete(); //Call the function delete

if (y==3)

m.display(); //Call the function display

System.out.println();

System.out.println("DO YOU WANT TO CONTINUE(Y/N)");

cc=(char)f.read();

}

while((cc=='y')||(cc=='Y'));

} //End of try statement

catch(Exception e) //To catch any exceptions

{

}

} //End of main function

} //End of main class linkstack


Stack Using Array

Program : Stack Using Array



import java.io.*; // importing input/output package


class stacks // class definition


{

int data[]=new int[10]; // initializing array

int top,i=0;

int n=0; // initializing variables


stacks() // constructor


{

top=-1; // initializing top variable

n=10;

}


void create(int item) // create function


{

if (top>n) // conditional statement

{

System.out.print("OVERFLOW"); // print statement

return;

}

else

{

top=top+1; // incrementing top

data[top]=item;

}

}


void pop() // print statement


{

if (top==-1)

{


System.out.print("EMPTY"); // print statement

return;

}

else

{

System.out.print(data[top]);

top=top-1; // decrementing top

}

}


void display() // display function


{

for(i=top;i>-1;i--) // looping structure

{

System.out.println("|_\t"+data[i]+"\t_|");

}

if (top==-1)

{

System.out.println("EMPTY"); // print statement

}

}

}






class stack


{

public static void main(String args[]) throws IOException

// main function

{

int x,y,z,d,p;

DataInputStream a=new DataInputStream(System.in);

// invoking input class

stacks t=new stacks(); // object decleration

int exit=0;

while (exit==0)

{

System.out.println("\t\t\nMENU\n1.PUSH\n2.POP\n3.DISPLAY\n4.QUIT");

System.out.println("Enter your choice:");

x=Integer.parseInt(a.readLine()); // converting input received to integer


switch (x) // switching cases

{


case 1:

{

System.out.println("ENTER ELEMENT:");

p=Integer.parseInt(a.readLine());

t.create(p); // calling create function

}

break;





case 2:


{

y=0;

t.pop(); // calling pop function

}

break;




case 3:


{

t.display(); // calling display function

}

break;




case 4:


{

exit=1; // exit program

}

break;

}

}

}

}

Sparse Matrix

Program : Sparse Matrix



import java.io.*; // importing input/output package

class Sparse // class definition


{

int a[][]=new int[10][10],i,j,n,m,flag=0; // initializing array

int c[][]=new int[10][10],k,l,s=1; // initializing array

int t[][]=new int[10][10],q=1; // initializing array

BufferedReader f=new BufferedReader(new InputStreamReader(System.in));

// invoking input class


public void data() // void function


{

try // Exception handling

{

System.out.println("Enter the no. of rows");

// print statement

n=Integer.parseInt(f.readLine());

System.out.println("Enter the no. of columns");

m=Integer.parseInt(f.readLine()); // converting input received to integer

System.out.println("Enter the elements");

for(i=0;i<n;i++) // looping structure

for(j=0;j<m;j++)

{

a[i][j]=Integer.parseInt(f.readLine());

}

}


catch(Exception e) // catching the user defined exception



{

System.out.println("Error"); // print statement

}

}


public void work() // void function

{

c[0][0]=n; // assignment

c[0][1]=m;

for(i=0;i<n;i++) // looping structure

for(j=0;j<m;j++)

{

if (a[i][j]!=0) // conditional statement

{

c[s][0]=i;

c[s][1]=j;

c[s][2]=a[i][j];

flag=flag+1; // incrementing flag

s=s+1; // incrementing s

}

}

c[0][2]=flag;

System.out.println("The tuple form is:"); // print statement

for(i=0;i<=flag;i++)

{

for(j=0;j<3;j++)

{

System.out.print(" "+c[i][j]); // print statement

}

System.out.println(" "); // print statement

}

}


public void transpose() // void function



{

t[0][0]=c[0][1];

t[0][1]=c[0][0];

t[0][2]=c[0][2];

if (t[0][2]<=0) // conditional statement

{

for(i=0;i<3;i++)

System.out.println(" "+t[0][i]); // print statement

System.exit(0); // exit program

}

else

{

for(k=0;k<m;k++) // looping structure

{

for(l=1;l<=c[0][2];l++) // looping structure

{

if(c[l][2]==k) // conditional statement

{

t[q][0]=c[l][1];

t[q][1]=c[l][0];

t[q][2]=c[l][2];

q=q+1; // incrementing q

}

}

}

System.out.println("The transposed matrix is:");

// print statement

for(i=0;i<=flag;i++)

{

for(j=0;j<3;j++) // looping structure

{

System.out.print(" "+t[i][j]);

}

System.out.println(" "); // print statement

}

}

}


public static void main(String args[]) // main function

{

Sparse ob=new Sparse(); // object decleration

ob.data(); // calling function

ob.work(); // calling function

ob.transpose(); // calling function

}

}

Sorting Numbers

Program : Sorting Numbers




import java.io.*; // importing input/output package


class Sort // class definition


{


public static void main(String args[]) // main function


{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

// invoking input class

int a[]=new int[10]; // initializing array

int i=0;

int j=0; // initializing variables

int n=0;

int t=0;


try // Exception handling


{

System.out.println("Enter no: of elements");

// print statement

n=Integer.parseInt(in.readLine()); // converting input received to integer

System.out.println("Enter the elements"); // print statement

for(i=0;i<n;i++)

{

a[i]=Integer.parseInt(in.readLine()); // converting input received to integer

}

for(i=0;i<n-1;i++)

{

for(j=i+1;j<n;j++)

{

if (a[i]>a[j]) // conditional statement

{

t=a[i];

a[i]=a[j]; // swapping values

a[j]=t;

}

}

}

System.out.println("Sorted array is:"); // print statement

for(i=0;i<n;i++)

{

System.out.println(+a[i]); // print statement

}

}

catch(Exception e) // catching the user defined exception

{ }

}

}

Singly Linked List

Program : Singly Linked List



import java.io.*; //To implement I/O operations


class link //The class link

{

int data; //data item

link next; //next link in list



link(int id) //constructor

{

data=id; //initialize data

}



void displaylink() //To display the list

{

System.out.println("{"+data+"}");

}

} //end of class link


class linklist //The class linklist

{

link first; //ref to first link on list





linklist() //constructor

{

first=null; //no links on list yet

}



void create(int id) //To insert into the list

{

link l=new link(id); //make new link

l.next=first;

first=l;

} //End of function create



public void delete(int dt) //To delete from the list

{

link ct=first; //point to the beginning of the list

link pre=first; //point to the beginning of the list

if(first==null) //Check if list is empty

System.out.println("EMPTY LIST");

else if (first.data==dt)

first=first.next; //move to next link

else

{

while (ct.data!=dt)

{

pre=ct;

ct=ct.next;

}

pre.next=ct.next;

}

} //End of function delete



public void insert(int el,int da)

{

link ct=first; //point to the beginning of the list

link l=new link(el);

if (first==null) //Check if list is empty

{

first=l;

}

else

{

while (ct.data!=da)

{

ct=ct.next;

}

l.next=ct.next;

ct.next=l;

}

}

public void search(int ele) //Function to search for an element

{

int i=0;

link ct=first; //point to the beginning of the list

while (ct!=null)

{

if(ct.data==ele)

i=1;

ct=ct.next;

}

if(i==1)

System.out.println("THE SEARCHED ITEM IS EXIST IN THE LIST");

else

System.out.println("THE SEARCHED ITEM IS NOT EXIST IN THE LIST");

}



void displaylist() //Function to display the linkedlist

{

link current=first; //point to the beginning of the list

if (current==null) //Check if list is empty

{

System.out.println("EMPTY LIST");

}

while (current!=null)

{

current.displaylink();

current=current.next; //move to next link

}

}

}


class singlylinkedlist //The main class singlylinkedlist

{

//The main function

public static void main(String args[])throws IOException

{

char cc;

int ch=0;

linklist list=new linklist();

try

{

do

{

BufferedReader f=new BufferedReader(new InputStreamReader(System.in));

//Print Menu

System.out.println("1.CREATE THE LIST");

System.out.println("2.DELETE AN ELEMENT");

System.out.println("3.DISPLAY THE LIST");

System.out.println("4.INSERT AN ELEMENT");

System.out.println("5.SEARCH AN ELEMENT");

System.out.println("ENTER YOUR CHOICE");

ch=Integer.parseInt(f.readLine());

//Accepts choice

if (ch==1)

{

System.out.println("ENTER THE NUMBER OF ELEMENTS TO BE INSERTED");

int n=Integer.parseInt(f.readLine());

for(int i=0;i<n;i++)

{

System.out.println("ENTER THE NUMBER");

int x=Integer.parseInt(f.readLine());

list.create(x);

}

}

if (ch==2)

{

System.out.println("ENTER THE NUMBER TO BE DELETED");

int d=Integer.parseInt(f.readLine());

list.delete(d);

}

else if (ch==3)

{

System.out.println("ELEMENTS IN THE LIST ARE");

list.displaylist();

}

else if (ch==4)

{

System.out.println("ENTER THE NUMBER AND POSITION TO BE INSERTED");

int d1=Integer.parseInt(f.readLine());

int d2=Integer.parseInt(f.readLine());

list.insert(d1,d2);

}

if (ch==5)

{

System.out.println("ENTER THE NUMBER TO BE SEARCHED");

int se=Integer.parseInt(f.readLine());

list.search(se);

}

System.out.println();

System.out.println("DO YOU WANT TO CONTINUE(Y/N)");

cc=(char) f.read();

}

while(cc=='y');

}

catch(Exception e) //To catch any exceptions

{

}

} //End of main function

} //End of main class SinglyLinkedlist



Queue Using Linked List

Program : Queue Using Linked List



import java.io.*; //To implement I/O operations


class Link

{

int data; //data item

Link next; //next link in list



Link(int d) //constructor

{

data=d; //initialize data

}



void displayLink() //To display the list

{

System.out.print(" "+data);

}

} //end of class link


class list

{

Link first; //ref to first link on list

Link last; //ref to last link on list



list() //constructor

{

first=null; //no links on list yet

last=null; //no links on list yet

}



void insert(int d) //To insert into the list

{

Link l=new Link(d); //make new link

if (first==null) //to check if queue is empty

{

first=l;

last=l;

}

else

{

last.next=l;

last=l;

}

} //End of function insert



void delete() //To delete node from the list

{

if (first==null) //Check if queue is empty

{

System.out.println("Queue is empty:");

//Print queue Empty

}

else

{

first=first.next;

}

} //End of function delete



void display() //To display the queue

{

Link current=first; //start at the beginning of the list

while (current!=null) //until end of the list

{

current.displayLink(); //print data

current=current.next; //move to next link

}

System.out.println(" ");

} //end of function display

} //End of class list



public class Queuelink //The main class LinkQueue

{

public static void main(String args[]) throws IOException

//Main function

{

list q=new list(); //make new linklist

int d,c=0;

char f;

do

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

System.out.println("\t\t\nQueue Operations");

//Print Menu

System.out.println("\n1.Push into Queue");

System.out.println("\n2.POP from Queue");

System.out.println("\n3.Display Queue\n");

System.out.print("Enter your Choice:");

c=Integer.parseInt(in.readLine());

//Accepts choice

switch (c)

{

case 1:

{

int p,flag=0;

{

System.out.println("Enter number of elements:");

d=Integer.parseInt(in.readLine());

while (flag<d)

{

System.out.println("Enter Element:");

p=Integer.parseInt(in.readLine());

q.insert(p);

//Assign values to links

flag++;

}

}

break;

}

case 2:

{

q.delete(); //Call the function delete

break;

}

case 3:

{

System.out.println("CURRENT LIST IS:");

q.display(); //Call the function display

break;

}

}

System.out.print("DO YOU WANT TO CONTINUE(y/n):");

f=(char)in.read();

}

while((f=='y')||(f=='Y'));

}

} //End of main function

Translation

Program : Translation



#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h> //header file inclusion

#include<dos.h>

#include<math.h>

int x1,x2,y1,y2,midx,midy,r; //variable initializations

int i,xt,yt;


void input() //function for input

{

printf("Enter the coordinates of the line:");

scanf("%d%d",&x1,&y1);

scanf("%d%d",&x2,&y2); //Inputting line endpoints

printf("Enter the centre of circle:");

scanf("%d%d",&midx,&midy); //Inputting center coordinates

printf("Enter the radius:");

scanf("%d",&r); //Inputting circle radius

printf("Enter the translation coordinates:");

scanf("%d%d",&xt,&yt); //Inputting translation values

}


void trans() //function for translation

{

input(); //calling input() function

setcolor(RED);

line(x1,y1,x2,y2); //draw line

circle(midx,midy,r); //draw circle

setcolor(GREEN);

x1+=xt; x2+=xt; //translate x coordinate

y1+=yt; y2+=yt; //translate y coordinate

midx+=xt;

midy+=yt; //translating circle center

line(x1,y1,x2,y2);

circle(midx,midy,r); //draw line and circle

getch();

}

void main(void) //main function

{

int gdriver = DETECT, gmode, errorcode;

initgraph(&gdriver, &gmode, "E:\\TC\\BGI"); //initializing graphics window

trans(); //calling trans() function

getch();

}

Scaling and Rotation

Program : Rotation



#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h> //header file inclusion

#include<dos.h>

#include<math.h>

int x1,x2,x3,y1,y2,y3;

int xs,ys; //variable initializations


void input() //function for input

{

printf("Enter the coordinates of the triangle:");

scanf("%d%d",&x1,&y1);

scanf("%d%d",&x2,&y2); //Inputting triangle coordinates

scanf("%d%d",&x3,&y3);

printf("Enter the scaling factor:");

scanf("%d%d",&xs,&ys); //Inputting scaling factor

}



void scale() //function to scale

{

input(); //calling input()

setcolor(RED);

line(x1,y1,x2,y2);

line(x2,y2,x3,y3); //drawing triangle

line(x3,y3,x1,y1);

x1*=xs; x2*=xs; x3*=xs; //scaling x values

y1*=ys; y2*=ys; y3*=ys; //scaling y values

setcolor(GREEN);

line(x1,y1,x2,y2);

line(x2,y2,x3,y3); //drawing scaled triangle

line(x3,y3,x1,y1);

getch();

}


void main(void) //main function

{

int gdriver = DETECT, gmode, errorcode;

initgraph(&gdriver, &gmode, "D:\\TC\\BGI"); //initializing graphics window

scale(); //calling scale() function

getch();

}

Rotating Wheel on Inclined Plane

Program : Rotating Wheel on Inclined Plane



#include <graphics.h>

#include <stdlib.h>

#include <stdio.h> //header file inclusion

#include <conio.h>

#include <math.h>

int main(void) //main function

{

int gdriver = DETECT, gmode, errorcode,x1,y1,x2,y2,m,x,y; //variable initializations

int stepx,stepy, no_of_steps,i;

initgraph(&gdriver, &gmode, "D:\\TC\\"); //initializing graphics window

errorcode = graphresult();

if (errorcode != grOk) //an error occurred

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1); //return with error code

}

x1=10;

y1=300;

x2=300; //initializing inclined plane

y2=10;

m=(float)(y2-y1)/(x2-x1); //computing slope

setcolor(BLUE);

line(x1+140,x2,y1+140,y2); //draw line for plane

x=x1+140;y=y1+140;

i=0;

no_of_steps=abs(y2-y1);

while(i<no_of_steps) //while not bottom

{

y1=y1+m;

x1=x1+1; //updating coordinates

setcolor(RED);

line(x1,y1,x1+100*cos(3.14*i/180),y1+100*sin(3.14*i/180));

line(x1,y1,x1-100*cos(3.14*i/180),y1-100*sin(3.14*i/180));

circle(x1,y1,100); //draw wheel

delay(20);

setcolor(0);

line(x1,y1,x1+100*cos(3.14*i/180),y1+100*sin(3.14*i/180));

line(x1,y1,x1-100*cos(3.14*i/180),y1-100*sin(3.14*i/180));

circle(x1,y1,100); //rolling wheel

delay(20);

i++;

setcolor(RED);

line(x,x2,y,y2); //draw line

}

closegraph();

return 0; //terminate

}

Rotating Wheel

Program : Rotating Wheel



#include <graphics.h>

#include <stdlib.h>

#include <stdio.h> //header file inclusion

#include <conio.h>

#include<math.h>

int main(void) //main function

{

int gdriver = DETECT, gmode, errorcode,i,x1,y1;

initgraph(&gdriver, &gmode, "D:\\TC\\"); //initializing graphics window

errorcode = graphresult();

if (errorcode != grOk) //an error occurred

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:"); //error recovery

getch();

exit(1); //return with error code

}

printf("\n\nenter the centre coordinates of the circle");

scanf("%d%d",&x1,&y1); //Inputting circle center



for(i=0;i<360;i++)

{

setcolor(RED);

line(x1,y1,x1+100*cos(3.14*i/180),y1+100*sin(3.14*i/180)); //draw lines for wheel

line(x1,y1,x1-100*cos(3.14*i/180),y1-100*sin(3.14*i/180)); //draw lines for wheel

circle(x1,y1,100); //draw circle

delay(20);

setcolor(0);

line(x1,y1,x1+100*cos(3.14*i/180),y1+100*sin(3.14*i/180)); //draw lines for wheel

line(x1,y1,x1-100*cos(3.14*i/180),y1-100*sin(3.14*i/180)); //draw lines for wheel

circle(x1,y1,100); //draw circle

delay(20);

x1++; //translate circle

y1++;

}

closegraph();

return 0; //terminate

}

Floodfill Polygon Filling

Program : Floodfill Polygon Filling



#include<stdio.h>

#include<conio.h>

#include<math.h> //header file inclusion

#include<graphics.h>


void main() //main function

{

int gdriver=DETECT,gmode,errorcode;

int x,y,i,r;

int xc,yc,t; //variable initializations

clrscr();

initgraph(&gdriver,&gmode,"D:\\TC"); //initializing graphics window

printf("\nEnter the co-ordinates of the centre of the circle(xc,yc): \n");

scanf("%d%d",&xc,&yc); //Inputting center coordinates

printf("\nEnter the radius of the circle ,r:");

scanf("%d",&r); //Inputting circle radius

for(i=0;i<360;i++)

{

x=r*cos(i); //polar conversion

y=r*sin(i);

circleplot(xc,yc,x,y); //calling circleplot() function

}

Midpoint Ellipse Drawing

Program : Midpoint Ellipse Drawing



#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#define ROUND(a)((int)(a+0.5))

#include<math.h>

void ellipsemid(int xc,int yc,int rx,int ry)

{

int rx2=rx*rx;

int ry2=ry*ry;

int tworx2=2*rx2;

int twory2=2*ry2;

int p;

int x=0;

int y=ry;

int px=0;

int py=tworx2*y;

ellipsepixelplot(xc,yc,x,y);

p=ROUND(ry2-(rx2*ry)+(.25*rx2));

while(px<py)

{

x++;

px+=twory2;

if(p<0)

p+=ry2+px;

else

{

y--;

py-=tworx2;

p+=ry2+px-py;

}

ellipsepixelplot(xc,yc,x,y);

}

p=ROUND(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);

while(y>0)

{

y--;

py-=tworx2;

if(p>0)

p+=rx2-py;

else

{

x++;

px+=twory2;

p+=rx2-py+px;

}

ellipsepixelplot(xc,yc,x,y);

}

}


ellipsepixelplot(int xa,int ya,int x,int y)

{

putpixel((xa+x),(ya+y),1);

putpixel((xa-x),(ya+y),2);

putpixel((xa+x),(ya-y),3);

putpixel((xa-x),(ya-y),4);

// putpixel((xa+y),(ya+x),5);

// putpixel((xa-y),(ya+x),6);

// putpixel((xa+y),(ya-x),7);

// putpixel((xa-y),(ya-x),8);

return(0);

}


void main()

{

int gdriver=DETECT,gmode,errorcode;

int xc=0,yc=0;

int x,y;

int rx,ry;

clrscr();

initgraph(&gdriver,&gmode,"D:\\TC");

printf("\nEnter the centre co-ordinates(xc,yc): \n");

scanf("%d%d",&xc,&yc);

printf("\nEnter the semimajor and semiminor axis :");

scanf("%d%d",&rx,&ry);

x=rx;y=ry;

ellipsemid(xc,yc,x,y);

getch();

}


Digital Clock

Program : Digital Clock



#include<stdio.h>

#include<conio.h>

#include<graphics.h> //header file inclusion

#include<dos.h>

void main()

{

int gdriver=DETECT,gmode,errorcode;

int font,i,direction, charsize; //variable initializations

struct time t; char a[60];

clrscr();

initgraph(&gdriver,&gmode,"D:\\TC\\BGI"); //initializing graphics window

while(!kbhit())

{ gettime(&t); //get system time

settextstyle(4,0,8); setcolor(6); //set output text style

sprintf(a,"%2d:%02d:%02d",t.ti_hour, t.ti_min, t.ti_sec);

outtextxy(200,200,a); //print digital time

delay(1000);

setcolor(BLACK); outtextxy(200,200,a); //set output text style

}

getch();

}

DDA Line Drawing

Program : DDA Line Drawing



#include<stdio.h>

#include<conio.h>

#include <graphics.h>

#include<math.h>

#include<dos.h>

void main()

{

float a,b,c,d,e,x,y,x1,y1,x2,y2;

int x3,y3,i;

int gdriver=DETECT,gmode,errorcode;

initgraph(&gdriver,&gmode,"c:\\turboc3" );

cleardevice();

printf("\nEnter x1:");

scanf("%f",&x1);

printf("\nEnter y1:");

scanf("%f",&y1);

printf("\nEnter x2:");

scanf("%f",&x2);

printf("\nEnter y2:");

scanf("%f",&y2);

clrscr();

a=x2-x1; b=y2-y1;

if(abs(a)<abs(b))

c=abs(b);

else

c=abs(a);

d=a/c; e=b/c;

x=x1; y=y1;

x3=x; y3=y;

putpixel(x3,y3,15);

for(i=0;i<c;i++)

{

//printf("(%d,%d)\n",x3,y3);

putpixel(x3,y3,0);

delay(20);

x=x+d; y=y+e;

x3=x; y3=y;

if((x-x3)>=.5)

x3++;

if((y-y3)>=.5)

y3++;

}

x3=x; y3=y;

putpixel(x3,y3,4);

getch();

closegraph();

}

Midpoint Circle Drawing

Program : Midpoint Circle Drawing



#include<stdio.h>

#include<conio.h>

#include<graphics.h>

cirmid(int,int,int,int,int);

circleplot(int,int,int,int);

void main()

{

int gdriver=DETECT,gmode,errorcode;

int x=0,y=0;

int d,t,r;

int xc,yc,xc1,yc1;

clrscr();

initgraph(&gdriver,&gmode,"c:\\TurboC3");

printf("\nEnter the co-ordinates of the centre of the circle(xc,yc): \n");

scanf("%d%d",&xc,&yc);

printf("\nEnter the radius of the circle ,r:");

scanf("%d",&r);

x=0;y=r;

d=1-r;

circleplot(xc,yc,x,y);

cirmid(xc,yc,x,y,d);

getch();

}


cirmid(int xc,int yc,int x,int y,int d)

{

int t;

while(x<=y)

{

x++;

if(d<0)

d=d+(2*x)+1;

else

{

t=2*(x-y);

d=d+t+1;

y--;

}

circleplot(xc,yc,x,y);

}

return(0);

}

circleplot(int xa,int ya,int x,int y)

{

putpixel((xa+x),(ya+y),1);

putpixel((xa-x),(ya+y),2);

putpixel((xa+x),(ya-y),3);

putpixel((xa-x),(ya-y),4);

putpixel((xa+y),(ya+x),5);

putpixel((xa-y),(ya+x),6);

putpixel((xa+y),(ya-x),7);

putpixel((xa-y),(ya-x),8);

return(0);

}

Bresenham's Circle Drawing

Program : Bresenham's Circle Drawing



#include<stdio.h>

#include<conio.h>

#include<graphics.h>


void main()

{

int gdriver=DETECT,gmode,errorcode;

int x=0,y=0;

int d,t,r;

int xc,yc,xc1,yc1;

clrscr();

initgraph(&gdriver,&gmode,"c:\\TurboC3");

printf("\nEnter the co-ordinates of the centre of the circle(xc,yc): \n");

scanf("%d%d",&xc,&yc);

printf("\nEnter the radius of the circle ,r:");

scanf("%d",&r);

x=0;y=r;

d=3-(2*r);

circleplot(xc,yc,x,y);

cirbres(xc,yc,x,y,d);

getch();

}


cirbres(int xc,int yc,int x,int y,int d)

{

int t;

while(x<=y)

{

x++;

if(d<0)

d=d+(4*x)+6;

else

{

t=4*(x-y);

d=d+t+10;

y--;

}

circleplot(xc,yc,x,y);

}

return(0);

}

circleplot(int xa,int ya,int x,int y)

{

putpixel((xa+x),(ya+y),12);

putpixel((xa-x),(ya+y),12);

putpixel((xa+x),(ya-y),12);

putpixel((xa-x),(ya-y),12);

putpixel((xa+y),(ya+x),12);

putpixel((xa-y),(ya+x),12);

putpixel((xa+y),(ya-x),12);

putpixel((xa-y),(ya-x),12);

return(0);

}


Bresenham's Line Drawing

Program : Bresenham's Line Drawing


#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gdriver=DETECT,gmode,errorcode;

int x1,y1,x2,y2,l=0;

clrscr();

initgraph(&gdriver,&gmode,"c:\\TurboC3");

printf("enter the first endpoint:");

scanf("%d%d",&x1,&y1);

printf("\nenter the second endpoint:");

scanf("%d%d",&x2,&y2);


if((x1>x2)&&(y2>y1))

l=1;

else if((x2>x1)&&(y1>y2))

l=1;

else

l=0;

linebres(x1,y1,x2,y2,l);

getch();

closegraph();

}

linebres(int xa,int ya,int xb,int yb,int l)

{

int dx=abs(xa-xb);

int dy=abs(ya-yb);

int p=dy-dx;

int twody=2*dy;

int twodydx=2*(dy-dx);

int x,y,xend;

if(xa>xb)

{

x=xb;

y=yb;

xend=xa;

}

else

{

x=xa;

y=ya;

xend=xb;

}

putpixel(x,y,10);

while(x<xend)

{

x++;

if(p<0)

p+=twody;

else

{

if(l==0)

y++;

else

y--;

p+=twodydx;

}

putpixel(x,y,10) ;

}

return(0);

}

Boundaryfill Polygon Filling

Program : Boundaryfill Polygon Filling


#include<stdio.h>

#include<conio.h>

#include<math.h> //header file inclusion

#include<graphics.h>


void main() //main function

{

int gdriver=DETECT,gmode,errorcode;

int x,y,i,r;

int xc,yc,t; //variable initializations

clrscr();

initgraph(&gdriver,&gmode,"D:\\TC\\BGI"); //initializing graphics window

printf("\nEnter the co-ordinates of the centre of the circle(xc,yc): \n");

scanf("%d%d",&xc,&yc); //Inputting center coordinates

printf("\nEnter the radius of the circle ,r:");

scanf("%d",&r); //Inputting circle radius

for(i=0;i<360;i++)

{

x=r*cos(i); //polar conversion

y=r*sin(i);

circleplot(xc,yc,x,y); //calling circleplot() function

}

boundaryfill4(xc,yc,1,12); //calling boundaryfill4() function

getch();

}

circleplot(int xa,int ya,int x,int y) //function to plot circle

{

putpixel((xa+x),(ya+y),12); //plotting the point

return(0);

}

boundaryfill4(int x,int y,int fillcolor,int bdycolor) //function for filling polygon

{

int ct=getpixel(x,y); //get pixel color

if((ct!=bdycolor)&&(ct!=fillcolor)) //if not already filled

{

putpixel(x,y,fillcolor); //plotting the point

boundaryfill4(x+1,y,fillcolor,bdycolor);

boundaryfill4(x-1,y,fillcolor,bdycolor);

boundaryfill4(x,y+1,fillcolor,bdycolor); //filling neighboring pixels

boundaryfill4(x,y-1,fillcolor,bdycolor);

}

return(0);

}

Sunday, November 4, 2007

Queue Using Array

Program : Queue Using Array

import java.io.*; // importing input/output package

class queues // class definition

{
int data[]=new int[10]; // initializing array
int top,i=0;
int n=0,k=0; // initializing variables

queues() // constructor
{
top=-1;
n=10;
}

void create(int item) // create function
{
if (top>n) // conditional statement
{
System.out.print("OVERFLOW"); // print statement
return;
}
else
{
top=top+1; // incrementing top
data[top]=item;
}
}

void pop() // pop function

{

if (top==-1)
{
System.out.print("EMPTY"); // print statement
return;
}
else
{
data[k]=0;
k++;
}
}

void display() // display function

{
for(i=k;i<=top;i++) // looping structure

{
System.out.print("_\t"+data[i]+"\t_");
}
if (top==-1)
{
System.out.println("EMPTY");
}
}
}

class queue

{
public static void main(String args[]) throws IOException
// main function
{
int x,y,z,d,p;
DataInputStream a=new DataInputStream(System.in);
// invoking input class
queues t=new queues(); // object decleration
int exit=0;
while (exit==0)
{
System.out.println("\t\t\nMENU\n1.PUSH\n2.POP\n3.DISPLAY\n4.QUIT");
System.out.println("Enter your choice:");
x=Integer.parseInt(a.readLine()); // converting input received to integer
switch (x) // switching cases
{

case 1:

{
System.out.println("ENTER ELEMENT:");
p=Integer.parseInt(a.readLine());
t.create(p); // calling create function
}
break;


case 2:

{
y=0;
t.pop(); // calling pop function
}
break;

case 3:

{
t.display(); // calling display function
}
break;

case 4:

{
exit=1; // exit program
}
break;
}
}
}
}

Pattern Matching

Program : Pattern Matching

import java.io.*; //To implement I/O operations

class link
{
char data;
link next; //next link in list
link (char d) //constructor
{
data=d; //initialize data
}
}

class linklist
{
link first; //ref to first link on list
linklist() //constructor
{
first=null; //no links on list yet
}
void insert(char d) //To insert into the list
{
link node=new link(d); //make new link
node.next=first;
first=node;
} //End of function create
void find(linklist m,linklist pat) //To check for matching pattern
{
link c,mstr,substr;
c=m.first;
while (c!=null)
{
mstr=c;
substr=pat.first;
while ((mstr!=null)&&(substr!=null))
{
if ((mstr.data==substr.data))
{
mstr=mstr.next;
substr=substr.next;
}
else break;
}
if(substr!=null)
c=c.next;
else
{
System.out.println("GIVEN PATTERN EXIST IN THE MAIN STRING");
break;
}
}
if(c==null)
System.out.println("GIVEN PATTERN NOT EXIST IN THE MAIN STRING");
} //end of function find
} //end of class linklist

public class Patmatch //The main class circularqueue
{
public static void main(String args[]) //main function
{
try //the try block
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String st;
int i=0;
char a;
linklist mainlist=new linklist(); //make new main string
linklist pattern=new linklist(); //make new sub string
linklist o=new linklist(); //for find function
System.out.println("ENTER THE MAIN STRING");
st=in.readLine();
int cnt=st.length();
for(i=0;i<cnt;i++)
{
mainlist.insert(st.charAt(i)); //Assign values to string
}
System.out.println("enter the substring:");
st=in.readLine();
int cn=st.length();
for(i=0;i<cn;i++)
{
pattern.insert(st.charAt(i)); //Assign values to string
}
o.find(mainlist,pattern);
}
catch (Exception e) //To catch I/O Exception
{
}
} //End of main function
} //End of main class patmatch

Polynomial Addition

Program : Polynomial Addition

import java.io.*; //To implement I/O operations

class link
{
int coef,exp; //data part for the link list
link next; //next link in list

link(int a,int b) //constructor
{
coef=a; //initialize data
exp=b; //initialize data
}

void display() //To display the list
{
System.out.print(" "+coef+"xe"+exp);
}
} //end of class link

class linklist
{
link p,q,d;
link first; //ref to first link on list
linklist() //constructor
{
first=null; //no links on list yet
}

void create(int a,int b) //To insert into the list
{
link node=new link(a,b); //make new link
node.next=first;
first=node;
} //End of function create

void padd(linklist A,linklist B) //To add the polynomials
{
int x; //Temporary variable for storing coef
p=A.first;
q=B.first;
d=first;
while ((p!=null) && (q!=null))
{
if (p.exp==q.exp)
{
x=p.coef+q.coef;
if (x!=0)
{
link node=new link(x,p.exp);
//make new link
node.next=d;
d=node;
}
p=p.next; //move to next node of 'A'
q=q.next; //move to next node of 'B'
}
else if (p.exp>q.exp)
{
link node=new link(p.coef,p.exp);
node.next=d;
d=node;
p=p.next;
}
else
{
link node=new link(q.coef,q.exp);
node.next=d;
d=node;
q=q.next;
}
}
while (p!=null)
{
link node=new link(p.coef,p.exp);
node.next=d;
d=node;
p=p.next;
}
while (q!=null)
{
link node=new link(q.coef,q.exp);
node.next=d;
d=node;
q=q.next;
}
first=d;
} //end of function padd

void disp() //To display the resultant polynomial
{
link current=first; //start at the beginning of the list
while (current!=null) //until end of the list
{
current.display();
if(current.next!=null)
{
System.out.print("+");
}
else
{
System.out.print(" ");
//print data
}
current=current.next;
//move to next link
}
System.out.println(" ");
} //end of function disp
} //end of class linklist

public class Polyadd //The main class add
{
public static void main(String args[])
//main function
{ try //to catch any exceptions
{
int r=0,n,x,y;
System.out.println("/* POLYNOMIAL ADDITION */");
linklist A=new linklist(); //make new linklist 'A'
linklist B=new linklist(); //make new linklist 'B'
linklist C=new linklist(); //make new linklist 'C'
BufferedReader f=new BufferedReader(new InputStreamReader(System.in));
for(int j=1;j<=2;j++)
{
//To insert the polynomials
System.out.println("Enter the "+j+" polynomial:");
System.out.println("Enter the no. of terms:");
n=Integer.parseInt(f.readLine());
for(int i=n;i>0;i--)
{
System.out.println("Enter the coeff & exponent of "+i+" term");
x=Integer.parseInt(f.readLine());
y=Integer.parseInt(f.readLine());
if(j==1)
A.create(x,y);
//Assign values to links
else
B.create(x,y);
//Assign values to links
}
}
System.out.println("FIRST POLYNOMIAL IS:");
A.disp(); //Display the first plynomial
System.out.println("SECOND POLYNOMIAL IS:");
B.disp(); //Display the second plynomial
C.padd(A,B); //Calling the function padd System.out.println("THE SUM OF POLYNOMIALS IS:");
C.disp(); //Display the resultant plynomial
}
catch(IOException e) //To catch I/O Exception
{
}
} //End of main function
}

Doubly Linked List

Program : Doubly Linked List

import java.io.*; //To implement I/O operations

class link
{
int data; //data item
link r,l; //next link in list

link(int d) //constructor
{
data=d; //initialize data
}

void displaylink() //To display the list
{
System.out.print(" "+data);
}
} //end of class link


class linklist
{
link first,last; //ref to first & last links on list
int n=0,i=0;

linklist() //constructor
{
first=null; //no links on list yet
last=null; //no links on list yet
}

void create(int d) //To insert into the list
{
link node=new link(d); //make new link
i++;
node.r=first;
if(n!=0)
(node.r).l=node;
if(n==0)
last=node;
first=node;
n++;
}

void insertright(int d,int e) //To insert to the right of a node
{
link node=new link(d); //make new link
i++;
link current=first;
while (current.data!=e)
{
current=current.r;
}
if (current==last)
{
node.l=current;
node.r=null;
current.r=node;
last=node;
}
else
{
node.l=current;
node.r=current.r;
(current.r).l=node;
current.r=node;
}
}

void insertleft(int d,int e) //To insert to the left of a node
{
link node=new link(d); //make new link
i++;
link current=last;
while (current.data!=e)
{
current=current.l;
}
if (current==first)
{
node.l=null;
node.r=current;
current.l=node;
first=node;
}
else
{
node.l=current.l;
node.r=current;
(current.l).r=node;
current.l=node;
}
}

void delete(int x) //To delete a node
{
link current=first;
while (current.data!=x)
{
if(current==last) //Check if list is empty
{
System.out.println("data not present");
return;
}
current=current.r;
}
if (current==first)
{
current=current.r;
first=current;
}
else if(current==last)
{
current=current.l;
last=current;
}
else
{
(current.l).r=current.r;
(current.r).l=current.l;
}
i--;
}

void displayright() //to display from right to left
{
link current=first; //start at the beginning of the list
int j=i;
if(i==0) //Check if list is empty
System.out.println("No items to display");
else
{
while (j>0)
{
current.displaylink(); //print data
current=current.r; //move to next link
j--;
}
}
}

void displayleft() //To display from left to right
{
link current=last; //start at the end of the list
int j=i;
if (i==0) //Check if list is empty
System.out.println("NO items for display");
else
{
while (j>0)
{
current.displaylink(); //print data
current=current.l; //move to next link
j--;
}
}
}
} //End of class linklist



public class Doubly //The main class doubleLink
{
public static void main(String s[]) //Main function
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int choice=0,ch=0,a,b;
linklist list=new linklist();
System.out.println("MENU:\n1.CREATE/MODIFY");
//Print Menu
System.out.println("\n2.INSERT");
System.out.println("\n3.DELETE");
System.out.println("\n4.DISPLAY");
System.out.println("\n5.EXIT");
do
{
System.out.print("Enter ur choice:");
choice=Integer.parseInt(in.readLine());
//Accepts choice
switch (choice)
{
case 1:
System.out.print("Enter the data to be stored:");
a=Integer .parseInt(in.readLine());
list.create(a);
break;
case 2:
System.out.print("1.Insert left");
System.out.print("\n2-Insert Right");
System.out.print("\nSpecify ur choice:");
ch=Integer.parseInt(in.readLine());
System.out.print("Enter the data to be inserted:");
a=Integer.parseInt(in.readLine());
if (ch==1)
{
System.out.print("Enter the data to be inserted:");
b=Integer.parseInt(in.readLine());
list.insertleft(a,b);
}
else if (ch==2)
{
System.out.print("Enter the data to be inserted:");
b=Integer.parseInt(in.readLine());
list.insertright(a,b);
}
break;
case 3:
System.out.print("Enter the number to be deleted:");
a=Integer.parseInt(in.readLine());
list.delete(a);
break;
case 4:
System.out.print("1.LEFT TO RIGHT");
System.out.print("\n2.RIGHT TO LEFT");
System.out.print("\nSpecify ur choice:");
ch=Integer.parseInt(in.readLine());
if(ch==1)
list.displayright();
else
list.displayleft();
System.out.println(" ");
break;
}
}
while(choice!=5);
}
catch(IOException e) //To catch any exceptions
{
}
} //End of main function
} //End of main class LinkQueue

Pattern Matching

Program :


import java.io.*; //To implement I/O operations

class link
{
char data;
link next; //next link in list
link (char d) //constructor
{
data=d; //initialize data
}
}



class linklist
{
link first; //ref to first link on list
linklist() //constructor
{
first=null; //no links on list yet
}
void insert(char d) //To insert into the list
{
link node=new link(d); //make new link
node.next=first;
first=node;
} //End of function create
void find(linklist m,linklist pat) //To check for matching pattern
{
link c,mstr,substr;
c=m.first;
while (c!=null)
{
mstr=c;
substr=pat.first;
while ((mstr!=null)&&(substr!=null))
{
if ((mstr.data==substr.data))
{
mstr=mstr.next;
substr=substr.next;
}
else break;
}
if(substr!=null)
c=c.next;
else
{
System.out.println("GIVEN PATTERN EXIST IN THE MAIN STRING");
break;
}
}
if(c==null)
System.out.println("GIVEN PATTERN NOT EXIST IN THE MAIN STRING");
} //end of function find
} //end of class linklist



public class Patmatch //The main class circularqueue
{
public static void main(String args[]) //main function
{
try //the try block
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String st;
int i=0;
char a;
linklist mainlist=new linklist(); //make new main string
linklist pattern=new linklist(); //make new sub string
linklist o=new linklist(); //for find function
System.out.println("ENTER THE MAIN STRING");
st=in.readLine();
int cnt=st.length();
for(i=0;i{
mainlist.insert(st.charAt(i)); //Assign values to string
}
System.out.println("enter the substring:");
st=in.readLine();
int cn=st.length();
for(i=0;i{
pattern.insert(st.charAt(i)); //Assign values to string
}
o.find(mainlist,pattern);
}
catch (Exception e) //To catch I/O Exception
{
}
} //End of main function
} //End of main class patmatch