Tuesday, 25 March 2014

SHIFT REDUCE PARSER

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-SHIFT REDUCE PARSER*/


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>

char ip_sym[15],stack[15];
int ip_ptr=0,st_ptr=0,len,i;
char temp[2],temp2[2];
char act[15];
void check();
int main()
{
printf("\n\t\t SHIFT REDUCE PARSER\n");
printf("\n GRAMMER\n");
printf("\n E->E+E\n ");
printf("\n E->E*E\n E->a");
printf("\n enter the input symbol:\t");
gets(ip_sym);
printf("\n\t stack implementation table");
printf("\n stack\t\t input symbol\t\t action");
printf("\n______\t\t ____________\t\t ______\n");
printf("\n $\t\t%s$\t\t\t--",ip_sym);
strcpy(act,"shift  ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
len=strlen(ip_sym);
for(i=0;i<=len-1;i++)
{
stack[st_ptr]=ip_sym[ip_ptr];
stack[st_ptr+1]='\0';
ip_sym[ip_ptr]=' ';
ip_ptr++;
printf("\n $%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
check();
st_ptr++;
}
st_ptr++;
check();
}
void check()
{
int flag=0;
temp2[0]=stack[st_ptr];
temp2[1]='\0';
if((!strcmpi(temp2,"a")))
{
stack[st_ptr]='E';
if(!strcmpi(temp2,"a"))
printf("\n $%s\t\t%s$\t\t\tE->a",stack, ip_sym);
flag=1;
}
if((!strcmpi(temp2,"+"))||(strcmpi(temp2,"*")))
{
flag=1;
}
if((!strcmpi(stack,"E*E"))||(!strcmpi(stack,"E+E")))
{
strcpy(stack,"E");
st_ptr=0;
if(!strcmpi(stack,"E+E"))
printf("\n $%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
if(!strcmpi(stack,"E*E"))
printf("\n $%s\t\t%s$\t\t\tE->E*E",stack,ip_sym);
flag=1;
}

if(!strcmpi(stack,"E")&& ip_ptr ==len)
{
 printf("\n $%s\t\t%s$\t\t\tACCEPT",stack,ip_sym);
getch();
 exit(0);
}
if(flag==0)
{
 printf("\n%s\t\t\t%s\t\t reject",stack,ip_sym);
 exit(0);
}
return;
}

THREE ADDRESS CODE

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-TO GENERATE THREE ADDRESS CODE*/

#include<stdio.h>
#include<string.h>
void pm();
void plus();
void div();
int i,ch,j,l,addr=100;
char ex[10],exp[10],exp1[10],exp2[10],id1[5],op[5],id2[5];
void main()
{
clrscr();
while(1)
{
printf("\n1.assignment\n2.arithmetic\n3.relational\n4.Exit\nEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the expression with assignment operator:");
scanf("%s",exp);
l=strlen(exp);
exp2[0]='\0';
i=0;
while(exp[i]!='=')
{
i++;
}
strncat(exp2,exp,i);
strrev(exp);
exp1[0]='\0';
strncat(exp1,exp,l-(i+1));
strrev(exp1);
printf("Three address code:\ntemp=%s\n%s=temp\n",exp1,exp2);
break;

case 2:
printf("\nEnter the expression with arithmetic operator:");
scanf("%s",ex);
strcpy(exp,ex);
l=strlen(exp);
exp1[0]='\0';

for(i=0;i<l;i++)
{
if(exp[i]=='+'||exp[i]=='-')
{
if(exp[i+2]=='/'||exp[i+2]=='*')
{
pm();
break;
}
else
{
plus();
break;
}
}
else if(exp[i]=='/'||exp[i]=='*')
{
div();
break;
}
}
break;

case 3:
printf("Enter the expression with relational operator");
scanf("%s%s%s",&id1,&op,&id2);
if(((strcmp(op,"<")==0)||(strcmp(op,">")==0)||(strcmp(op,"<=")==0)||(strcmp(op,">=")==0)||(strcmp(op,"==")==0)||(strcmp(op,"!=")==0))==0)
printf("Expression is error");
else
{
printf("\n%d\tif %s%s%s goto %d",addr,id1,op,id2,addr+3);
addr++;
printf("\n%d\t T:=0",addr);
addr++;
printf("\n%d\t goto %d",addr,addr+2);
addr++;
printf("\n%d\t T:=1",addr);
}
break;
case 4:
exit(0);
}
}
}
void pm()
{
strrev(exp);
j=l-i-1;
strncat(exp1,exp,j);
strrev(exp1);
printf("Three address code:\ntemp=%s\ntemp1=%c%ctemp\n",exp1,exp[j+1],exp[j]);
}
void div()
{
strncat(exp1,exp,i+2);
printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n",exp1,exp[i+2],exp[i+3]);
}
void plus()
{
strncat(exp1,exp,i+2);
printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n",exp1,exp[i+2],exp[i+3]);
}


/*OUTPUT
Example Generation of Three Address Project Output Result

1. assignment
2. arithmetic
3. relational
4. Exit
Enter the choice:1
Enter the expression with assignment operator:
a=b
Three address code:
temp=b
a=temp

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a+b-c
Three address code:
temp=a+b
temp1=temp-c

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a-b/c
Three address code:
temp=b/c
temp1=a-temp

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a*b-c
Three address code:
temp=a*b
temp1=temp-c

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:a/b*c
Three address code:
temp=a/b
temp1=temp*c
1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:3
Enter the expression with relational operator
a
<=
b

100 if a<=b goto 103
101 T:=0
102 goto 104
103 T:=1

1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:4

*/

Sunday, 23 March 2014

PREDICTIVE PARSER PROGRAM

/*CSEMATTER.BLOGSPOT.IN
PROGRAM NAME-STACK IMPLEMENTATION OF PREDICTIVE PARSER*/

#include<ctype.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define non_term_count 5
#define term_count 6
#define rightHandMarker term_count-1
#define no_of_production 8

char nt[]={'E','A','T','B','F'},ter[]={'i','+','*','(',')','$'};
char arr[20][20][20]={
   {"TA","","","TA","",""},
    {"","+TA","","","@","@"},
    {"FB","","","FB","",""},
    {"","@","*FB","","@","@"},
    {"i","","","(E)","",""}
};
char ipstr[20];
char stack[40],prod[10];
int i=0,top=1,ia,ix;

struct predictive_table
{
 char prod[10];
}table[non_term_count][term_count]={"\0"};

struct
{
 int isNull;
 char term[10];
}first[non_term_count]={{0,"(i"},{1,"+"},{0,"(i"},{1,"*"},{0,"(i"}};
struct
{
 int isMarker;
 char term[10];
}follow[non_term_count]={{1,")"},{1,")"},{1,"+)"},{1,"+)"},{1,"+*)"}};
char terminals[term_count-1][2]={"i","+","*","(",")"};
char nonTerminals[non_term_count][3]={"E","A","T","B","F"};
char grammar[no_of_production][10]={"E T A","A + T A","A @","T F B","B * F B","B @","F ( E )","F i"};
char pgrammar[no_of_production][10]={"E->TA","A->+TA","A->@","T->FB","B->*FB","B->@","F->(E)","F->i"};

int matchTerminals(char term[])
{
 int i=-1;
 for(i=0; i<term_count-1; i++)
if(!strcmp(term,terminals[i]))
break;
 return i;
}

void copy_follow(int index,int loc)
{
 char term[2];
 int j,ind;
 if(follow[index].isMarker==1)
 {
  if(strlen(table[index][rightHandMarker].prod)==0)
   strcpy(table[index][rightHandMarker].prod,pgrammar[loc]);
  else
   printf("\nGiven Grammar is not LL(1)");
 }
 j=0;

 while(follow[index].term[j]!='\0')
 {
  term[0]=follow[index].term[j];
  term[1]='\0';
  ind = matchTerminals(term);
  if(strlen(table[index][ind].prod)==0)
   strcpy(table[index][ind].prod,pgrammar[loc]);
  else
   printf("\nGiven Grammar is not LL(1)");
  j++;
 }
}

int matchNonTerminals(char non_term[])
{
 int i=-1;
 for(i=0; i<non_term_count; i++)
if(!strcmp(non_term,nonTerminals[i]))
break;
 return i;
}



void show_table()
{
 int k,i,j;
 printf("\n\t\t\tPredictive Parser Table \n\n");
 printf("   |");
 for(i=0; i<term_count-1; i++)
  printf("    %s    |",terminals[i]);
 printf("    $    |");
 for(i=0; i<non_term_count; i++)
 {
  printf("\n----------------------------------------------------------------");
  printf("\n%s",nonTerminals[i]);
  for(k=strlen(nonTerminals[i]); k<3; k++)
   printf(" ");
  printf("|");
  for(j=0; j<term_count; j++)
  {
    printf(" %s",table[i][j].prod);
    for(k=strlen(table[i][j].prod); k<8; k++)
     printf(" ");
    printf("|");
  }
 }
}
void fun();
int main()
{
 int i,k,j,t,flag=0,indexTerm,indexNonTerm,index;
 char nextChar[10]="\0",nonterm[10]="\0",term[10]="\0";
 for(i=0; i<no_of_production; i++)
 {
  t=0;
  j=0;
  flag=0;
  while(grammar[i][j]!=' '&&grammar[i][j]!='\0')
   nonterm[t++]=grammar[i][j++];
  nonterm[t]='\0';
  indexNonTerm=matchNonTerminals(nonterm);
X:  j++;
  t=0;
  while(grammar[i][j]!=' '&&grammar[i][j]!='\0')
   nextChar[t++]=grammar[i][j++];
  nextChar[t]='\0';
  if(!strcmp(nextChar,"@"))
   copy_follow(indexNonTerm,i);
  else
  {
   index=matchTerminals(nextChar);
   if(index!=term_count-1)
   {
    if(strlen(table[indexNonTerm][index].prod)==0)
     strcpy(table[indexNonTerm][index].prod,pgrammar[i]);
    else
    {
      printf("\nGiven Grammar is not LL(1)");
      break;
    }
   }
   else
   {
    index=matchNonTerminals(nextChar);
    if(index!=non_term_count)
    {
     k=0;
     while(first[index].term[k]!='\0')
     {
       term[0]=first[index].term[k];
       term[1]='\0';
       indexTerm=matchTerminals(term);
       if(strlen(table[indexNonTerm][indexTerm].prod)==0)
strcpy(table[indexNonTerm][indexTerm].prod,pgrammar[i]);
       else
       {
printf("\n Given Grammar is not LL(1) ");
break;
       }
       k++;
     } //end while
     if(first[index].isNull==1)
     {
       flag=1;
       goto X;
     }
     else
      flag=0;
    }
   }
  }
  if(flag==1)
   copy_follow(indexNonTerm,i);
 }
 show_table();
 fun();
 getch();
}


void fun()
{

       void pop();
void push(char);
int resolve_nt(char);
int resolve_t(char);
void advance();
char a,x;
int len,k;
stack[0]='$';
stack[1]='E';
printf("\nenter the input string:\n");
scanf("%s",ipstr);
printf("I/p string\t\tStack\t\tProduction Used\n");
while(1)
{
a=ipstr[i];
x=stack[top];
for(k=i;ipstr[k]!='$';k++)
printf("%c",ipstr[k]);
printf("$\t\t");
if(x==a)
{
if(x=='$')
{
printf("input is accepted");
break;
}
else
{
pop();
advance();
}
}
else if(isupper(x))
{
ix=resolve_nt(x);
ia=resolve_t(a);
strcpy(prod,arr[ix][ia]);
len=strlen(prod);
pop();
for(k=1;k<=len;k++)
push(prod[len-k]);
if(stack[top]=='@')
pop();
}
else
{
printf("error");
break;
}
for(k=0;k<=top;k++)
printf("%c",stack[k]);
printf("\t\t\t%s\n",prod);
}
}

void push(char t)
{
top+=1;
stack[top]=t;
}
void pop()
{
top--;
}
void advance()
{
i++;
}
int resolve_nt(char t)
{
int k,index;
for(k=0;k<5;k++)
{
if(t==nt[k])
{
index=k;
break;
}
}
return index;
}

int resolve_t(char t)
{
int k,index;
for(k=0;k<6;k++)
{
if(t==ter[k])
{
index=k;
break;
}
}
return index;
}

/*OUTPUT


                        Predictive Parser Table

   |    i    |    +    |    *    |    (    |    )    |    $    |
----------------------------------------------------------------
E  | E->TA   |         |         | E->TA   |         |         |
----------------------------------------------------------------
A  |         | A->+TA  |         |         | A->@    | A->@    |
----------------------------------------------------------------
T  | T->FB   |         |         | T->FB   |         |         |
----------------------------------------------------------------
B  |         | B->@    | B->*FB  |         | B->@    | B->@    |
----------------------------------------------------------------
F  | F->i    |         |         | F->(E)  |         |         |
enter the input string:
i+i*i$
I/p string              Stack           Production Used
i+i*i$          $AT                     TA
i+i*i$          $ABF                    FB
i+i*i$          $ABi                    i
i+i*i$          $AB                     i
+i*i$           $A                      @
+i*i$           $AT+                    +TA
+i*i$           $AT                     +TA
i*i$            $ABF                    FB
i*i$            $ABi                    i
i*i$            $AB                     i
*i$             $ABF*                   *FB
*i$             $ABF                    *FB
i$              $ABi                    i
i$              $AB                     i
$               $A                      @
$               $                       @
$               input is accepted

*/

FIRST AND FOLLOW PROGRAM

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-TO FIND FIRST AND FOLLOW*/

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>

void follow(char c);
void first_1(char c);
void FIRST(char );

int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
int count,n1=0;
char  first[10];

int main()
{
 int i,z,choice;
 char c,ch;
 printf("Enter the no.of productions:");
 scanf("%d",&n);
 printf("Enter the productions(epsilon=$):\n");
 for(i=0;i<n;i++)
  scanf("%s%c",a[i],&ch);
count=n;

do
{
n1=0;
printf("Element :");
scanf("%c",&c);
FIRST(c);
printf("\n FIRST(%c)= { ",c);
for(i=0;i<n1;i++)
printf("%c ",first[i]);
printf("}\n");

printf("press 1 to continue : ");
scanf("%d%c",&choice,&ch);
}
while(choice==1);

 do
 {
  m=0;
  printf("Enter the element whose FOLLOW is to be found:");

  scanf("%c",&c);
  follow(c);
    printf("FOLLOW(%c) = { ",c);
  for(i=0;i<m;i++)
   printf("%c ",f[i]);
  printf(" }\n");
    printf("Do you want to continue(0/1)?");
  scanf("%d%c",&z,&ch);
 }
 while(z==1);
}

void follow(char c)
{

 if(a[0][0]==c)
 f[m++]='$';
 for(i=0;i<n;i++)
 {
  for(j=2;j<strlen(a[i]);j++)
  {
   if(a[i][j]==c)
   {
    if(a[i][j+1]!='\0')
     first_1(a[i][j+1]);

    if(a[i][j+1]=='\0'&&c!=a[i][0])
     follow(a[i][0]);

   }
  }
 }
}
void first_1(char c)
{
      int k;
                 if(!(isupper(c)))
                 f[m++]=c;

                 for(k=0;k<n;k++)
                 {
                 if(a[k][0]==c)
                 {
                 if(a[k][2]=='$')
                  follow(a[i][0]);
                 else if(islower(a[k][2]))
                 f[m++]=a[k][2];

                 else
                  first_1(a[k][2]);
                 }
                 }

}



void FIRST(char c)
{
int j;
if(!(isupper(c)))first[n1++]=c;
for(j=0;j<count;j++)
{
if(a[j][0]==c)
{
if(a[j][2]=='$') first[n1++]='$';
else if(islower(a[j][2]))first[n1++]=a[j][2];
else FIRST(a[j][2]);
}
}
}

/*OUTPUT

Enter the no.of productions:8
Enter the productions(epsilon=$):
E=TD
D=+TD
D=$
T=FS
S=*FS
S=$
F=(E)
F=a
Element :E
FIRST(E)= { ( a }
press 1 to continue : 1

Element :D
FIRST(D)= { + $ }
press 1 to continue : 1

Element :T
FIRST(T)= { ( a }
press 1 to continue : 1

Element :S
FIRST(S)= { * $ }
press 1 to continue : 1

Element :F
FIRST(F)= { ( a }
press 1 to continue : 0

Enter the element whose FOLLOW is to be found:E
FOLLOW(E) = { $ )  }
Do you want to continue(0/1)?1

Enter the element whose FOLLOW is to be found:D
FOLLOW(D) = { $ )  }
Do you want to continue(0/1)?1

Enter the element whose FOLLOW is to be found:T
FOLLOW(T) = { + $ )  }
Do you want to continue(0/1)?1

Enter the element whose FOLLOW is to be found:S
FOLLOW(S) = { + $ )  }
Do you want to continue(0/1)?1

Enter the element whose FOLLOW is to be found:F
FOLLOW(F) = { * + $ )  }
Do you want to continue(0/1)?0 */

Friday, 21 March 2014

LEXICAL TOOL PROGRAM IN C

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-LEXICAL ANALYSIS PROGRAM IN C*/

/*4 FILES ARE NEEDED FOR THIS PROGRAM
1.LEX.C
2.INTER.C-automatically generated on running
3.OPER.C-contains opeartors
4.KEY.C-contains keyword
and one file to perform lexical analysis
NOTE-PUT ALL THREE C FILES IN A SAME FOLDER AND ON RUNNING THE PROGRAM ENTER ANY FILE NAME,THAT SHOULD BE PRESENT IN SAME FOLDER*/

*/


1 LEX.C

#include"stdio.h"
#include"conio.h"
#include<string.h>
#include<ctype.h>

void main()
{
FILE *fi,*fo,*fop,*fk;
int flag=0,i=1;
char c,t,a[15],ch[15],file[20];
clrscr();
printf("Enter the file name : ");
scanf("%s",file);
fi=fopen(file,"r");
fo=fopen("inter.c","w");
fop=fopen("oper.c","r");
fk=fopen("key.c","r");
c=getc(fi);
while(!feof(fi))
{
if(isalpha(c)||isdigit(c)||(c=='['||c==']'||c=='.'==1))
fputc(c,fo);
else
{
if(c=='\n')
fprintf(fo,"\t$\t");
else
fprintf(fo,"\t%c\t",c);
}
c=getc(fi);
}
fclose(fi);
fclose(fo);
fi=fopen("inter.c","r");
printf("\t\tLEXICAL ANALYSIS \n");
fscanf(fi,"%s",a);
printf("\nline : %d\n",i++);
while(!feof(fi))
{
if((strcmp(a,"$")==0))
{
printf("\nline : %d\n",i++);
fscanf(fi,"%s",a);
}
fscanf(fop,"%s",ch);
while(!feof(fop))
{
if(strcmp(ch,a)==0)
{
fscanf(fop,"%s",ch);
printf("\t\t%s\t:\t%s\n",a,ch);
flag=1;
}
fscanf(fop,"%s",ch);
}
rewind(fop);
fscanf(fk,"%s",ch);
while(!feof(fk))
{
if(strcmp(ch,a)==0)
{
fscanf(fk,"%s",ch);
printf("\t\t%s\t:\tkeyword\n",a);
flag=1;
}
fscanf(fk,"%s",ch);
}
rewind(fk);
if(flag==0)
{
if(isdigit(a[0]))
printf("\t\t%s\t:\tconstant\n",a);
else
printf("\t\t%s\t:\tidentifier\n",a);

}
flag=0;
fscanf(fi,"%s",a);
}
getch();
}

------------------------------------------------------------------------------------------------------------

2.key.c

int
void
main
char
if
for
while
else
printf
scanf
FILE
include
stdio.h
conio.h
iostream.h

------------------------------------------------------------------------------------------------------------

3.oper.c

( openpara
) closepara
{ openbrace
} closebrace
<     lesser
> greater
" doublequote
' singlequote
: colon
; semicolon
# preprocessor
= equal
== assign
% percentage
^ bitwise
& reference
* star
+ add
- sub
\ backslash
/ slash


------------------------------------------------------------------------------------------------------------
/*sample file is cd4.c

#include<stdio.h>
#include<conio.h>

int main()
{
int a;
printf("hello");
if(a==1)
{
printf("ok");
}
else
{
printf("notok");
}
}
*/

/*output-

Enter the file name : cd4.c
                LEXICAL ANALYSIS

line : 1
                #       :       preprocessor
                include :       keyword
                <       :       lesser
                stdio.h :       keyword
                >       :       greater

line : 2
                #       :       preprocessor
                include :       keyword
                <       :       lesser
                conio.h :       keyword
                >       :       greater

line : 3
                $       :       identifier
                int     :       keyword
                main    :       keyword
                (       :       openpara
                )       :       closepara

line : 4
                {       :       openbrace

line : 5
                int     :       keyword
                a       :       identifier
                ;       :       semicolon

line : 6
                printf  :       keyword
                (       :       openpara
                "       :       doublequote
                hello   :       identifier
                "       :       doublequote
                )       :       closepara
                ;       :       semicolon

line : 7
                if      :       keyword
                (       :       openpara
                a       :       identifier
                =       :       equal
                =       :       equal
                1       :       constant
                )       :       closepara

line : 8
                {       :       openbrace

line : 9
                printf  :       keyword
                (       :       openpara
                "       :       doublequote
                ok      :       identifier
                "       :       doublequote
                )       :       closepara
                ;       :       semicolon

line : 10
                }       :       closebrace

line : 11
                else    :       keyword

line : 12
                {       :       openbrace

line : 13
                printf  :       keyword
                (       :       openpara
                "       :       doublequote
                notok   :       identifier
                "       :       doublequote
                )       :       closepara
                ;       :       semicolon

line : 14
                }       :       closebrace

line : 15
                }       :       closebrace

line : 16
                $       :       identifier
*/

INFIX TO POSTFIX PROGRAM IN C

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-INFIX TO POSTFIX PROGRAM IN C*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
#define size 10

char stack[size];
int tos=0,element;
void push(int element);
char pop();
void show();
int isempty();
int isfull();
char infix[30],output[30];
int prec(char);

int main()
{
        int i=0,j=0,k=0,length;
        char temp;
        printf("enter the infix expression\n");
        scanf("%s",infix);
        length=strlen(infix);
  for(i=0;i<length;i++)
  {
  if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && infix[i]!='^' && infix[i]!=')' && infix[i]!='(' )
    {
   output[j++]=infix[i];
    }
   else
    {
    if(tos==0)
    {
    push(infix[i]);
    }
    else
    {
    if(infix[i]!=')' && infix[i]!='(')
    {
    if( prec(infix[i]) <= prec(stack[tos-1])  )
    {
    output[j++]=pop();
    push(infix[i]);
    }
    else
    {
    push(infix[i]);
    } }
    else
    {
    if(infix[i]=='(')
    {push(infix[i]);}
    if(infix[i]==')')
    {
    temp=pop();
    while(temp!='(')
    {output[j++]=temp;
    temp=pop();}
    }}}}}
    while(tos!=0)
    {
    output[j++]=pop();
    }
 printf("the postfix expression is: %s\n",output);}

void push(int element)
{
    stack[tos]=element;
    tos++;
}
char pop()
{
    tos--;
    return(stack[tos]);
}

int prec(char symbol)
{
if(symbol== '(')
return 0;
if(symbol== ')')
return 0;
if(symbol=='+' || symbol=='-')
return 1;
if(symbol=='*' || symbol=='/')
return 2;
if(symbol=='^')
return 3;
return 0;
}

Saturday, 8 March 2014

BESSEL METHOD OF INTERPOLATION PROGRAM

/*CSEMATTER.BLOGSPOT.IN
  PROGRAM - BESSEL METHOD OF INTERPOLATION
*/
#include <stdio.h>
#include <conio.h>
 int fact(int i)
 {if(i<2)
 return 1;
 else
 return (i*fact(i-1));}
 void main()
{
float i,p,h,j,x[10],y[10],xy[10][10],val,xs,ys,is,X,Y,y1,y2,y3,y4;
int n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
xy[i][j]=0;
}
for(i=0;i<n;i++)
{
scanf("%f",&x[i]);
scanf("%f",&y[i]);
}
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(j==0)
xy[i][j]=y[i];
if(xy[i+1][j-1]!=0)
xy[i][j]=xy[i+1][j-1]-xy[i][j-1];
}
}
printf("enter the value of X");
scanf("%f",&X);
i=0;
do
{
i++;
}
while(x[i]<X);
is=i;

printf("%f",x[is]);
h=x[1]-x[0];
p=(X-x[is])/h;
y1=p*xy[is][1];
y2=p*(p-1)*(xy[is][2]+xy[is-1][2])/(2*fact(2));
y3=p*(p-1)*(p-0.5)*xy[is-1][3]/fact(3);
y4=(p+1)*p*(p-1)*(p-2)*(xy[i-2][4]+xy[i-1][4])/(fact(4)*2);
Y=y[is]+y1+y2+y3+y4;
printf("\nThe result is  :-  %f",Y);
getch();
}


/*
Output :-
 4
20
2854
24
3162
28
3544
32
3992
25
The result is  :-  3250.875000
*/

NEWTON'S DIVIDEND DIFFERENCE PROGRAM


/*CSEMATTER.BLOGSPOT.IN
Program to implement Newton's dividend difference*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  float x[10],y[10][10],sum,p,u,temp;
  int i,n,j,k=0,f,m;
  float fact(int);
  clrscr();
  printf("\nhow many record you will be enter: ");
  scanf("%d",&n);
  for(i=0; i<n; i++)
  {
   printf("\n\nenter the value of x%d: ",i);
   scanf("%f",&x[i]);
   printf("\n\nenter the value of f(x%d): ",i);
   scanf("%f",&y[k][i]);
  }
  printf("\n\nEnter X for finding f(x): ");
  scanf("%f",&p);

  for(i=1;i<n;i++)
  {
    k=i;
    for(j=0;j<n-i;j++)
    {
     y[i][j]=(y[i-1][j+1]-y[i-1][j])/(x[k]-x[j]);
     k++;
    }
  }
  for(i=0;i<n;i++)
  {
    printf("\n %.3f",x[i]);
    for(j=0;j<n-i;j++)
    {
     printf("   ");
     printf(" %.3f",y[j][i]);
    }
   printf("\n");
  }

  i=0;
  do
  {
   if(x[i]<p && p<x[i+1])
    k=1;
   else
    i++;
  }while(k != 1);
  f=i;

  sum=0;
  for(i=0;i<n-1;i++)
  {
   k=f;
   temp=1;
   for(j=0;j<i;j++)
   {
    temp = temp * (p - x[k]);
    k++;
   }
    sum = sum + temp*(y[i][f]);
  }
  printf("\n\n f(%.2f) = %f ",p,sum);
  getch();
}

 /*  OUT PUT

how many record you will be enter: 5
enter the value of x0: 5
enter the value of f(x0): 150
enter the value of x1: 7
enter the value of f(x1): 392
enter the value of x2: 11
enter the value of f(x2): 1452
enter the value of x3: 13
enter the value of f(x3): 2366
enter the value of x4: 17
enter the value of f(x4): 5202


Enter X for finding f(x): 9



 f(9) = 810
*/

LAGRANGE INTERPOLATION PROGRAM

/*CSEMATTER.BLOGSPOT.IN
Program to implement lagrange interpolation*/

#include<stdio.h>
#include<conio.h>
int main()
{int x,i,j,N,D,n;
int X[20],y[20];
float sum=0.0000;
printf("Enter the no. of terms ");
scanf("%d",&n);
printf("Enter the values of x ");
scanf("%d",&X[20]);
printf("Enter the values of y ");
scanf("%d",&y[20]);
printf("Enter the value of x for which value of y is to be found ");
for(i=0;i<n;i++)
{N=D=1;
for(j=0;j<n;j++)
   if(i!=j)
 {N=N*(x-X[j]);
      D=D*(X[i]-X[j]);
}
     sum=sum+(N/D)*y[i];
}
printf("Sum=%f",sum);
getch();
}


Output:
Enter the no. of terms 5
Enter values of x 5 7 11 13 17
Enter the values of y 150 392 1452 2366 5202
Enter the value of x for which the value of y is to be found 9
Sum=810.000000 

NEWTON RAPHSON METHOD PROGRAM

/*CSEMATTER.BLOGSPOT.IN

program to implement newton raphson method*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x*x*x-x-10)
#define f1(x) (4*x*x*x-1)
void main()
{
int maxitr,itr;
float h,x1,x0;
clrscr();
printf("enter value");
scanf("%f",&x0);
printf("enter value of maxitr");
scanf("%d",&maxitr);
for(itr=1;itr<maxitr;itr++)
{h=f(x0)/f1(x0);
x1=x0-h;
if(fabs(h)<0.0001)
{printf("the root is %fafter %d iteration",x1,itr);
itr=-1;
break;}
else
x0=x1;
}
if(itr!=-1)
printf("no. of iteration are not sufficient");
getch();
}
/*output:enter value 2
enter value of maxitr:6
the root is 1.855585 after 4 iterations*/



ITERATION METHOD PROGRAM

/*CSEMATTER.BLOGSPOT.IN

program to implement iteration method*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

#define f(x) (log10(x)+7)

void main()
{
float x0,x1,DOA;
int i,maxitr;
clrscr();
printf("enter value of xo,DOA,maxitr");
scanf("%f",&x0);
scanf("%f",&DOA);
scanf("%d",&maxitr);

for(i=1;i<=maxitr;i++)
{
  x1=f(x0);
  if(fabs(x1-x0)<DOA)
  {
  printf("root=%f after %d iterations",x1,i);
  i=-1;
  break;
  }
  else
  x0=x1;

}

if(i!=-1)
printf("iterations not enough");
getch();
}
/*output:
enter value of x0,doa,maxitr:3.6  
                                                     .0001    
                                                     7
root is 7.897486 after 5 iteration*/

NUMERICAL DIFFERENTIATION PROGRAM

/*CSEMATTER.BLOGSPOT.IN

PROGRAM TO IMPLEMENT NUMERICAL DIFFERENTIATION*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,e,j,k;
float ax[16],ay[16],x,val,diff[16][16],h,xs,res,sum=0.0;
printf("enter the value of n(1 to 6)\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the value of x:\n");
scanf("%f",&ax[i]);
printf("\nenter the value of y:\n");
scanf("%f",&ay[i]);
}
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(j==0)
diff[i][j]=ay[i];
else if(diff[i+1][j-1]!=NULL)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
printf("\n enter the value of x to find corresponding y:\n");
scanf("%f",&val);
for(i=0;i<n;i++)
{
if(ax[i]>val)
{
break;
}
k=i;
}
h=ax[1]-ax[0];
for(e=0;e<n-1;e++)
{sum=sum + ((pow(-1,e))*(diff[k][e+1]))/(e+1);
res=sum/h;}
printf("the result is %f",res);
}

/*
OUTPUT:
enter the value of n(1 to 6)
5
enter the value of x: 0
enter the value of y: 0
enter the value of x: 5
enter the value of y: 3
enter the value of x: 10
enter the value of y: 14
enter the value of x: 15
enter the value of y: 69
enter the value of x: 20
enter the value of y: 228
enter the value of x to find corresponding y: 0
the result is 1.000000

SIMPSON'S 3/8 PROGRAM

/*CSEMATTER.BOGSPOT.IN
PROGRAM-TO IMPLEMENT SIMPSON'S 3/8 RULE*/


#include<stdio.h>
#include<conio.h>
#define f(x) 1/(1+x*x)
void main()
{
int i,n;
float s1=0.0,s2=0.0,s3=0.0,h,sum;
float x[10];
float y[10];
clrscr();
printf("enter the values of n");
scanf("%d",&n);
printf("enter the values of x");
for(i=0;i<n;i++)
scanf("%f",&x[i]);
h=x[1]-x[0];
for(i=0;i<n;i++)
y[i]=f(x[i]);
s1=(y[0]+y[n-1]);
printf("for simpson's 3/8 rule");
for(i=1;i<n-1;i++)
{
if(i%3!=0)
s2=s2+y[i];
else
s3=s3+y[i];
}
sum=(((s1+(3*s2)+(2*s3))*3*h))/8;
printf("%f/n",sum);
getch();
}

/* OUTPUT
enter the values of n 7
0 1 2 3 4 5 6
for simpson's 3/8 rule
1.357081*/

SIMPSON'S 1/3 RD PROGRAM

/*CSEMATTER.BLOGSPOT.IN
PROGRAM FOR SIMPSON'S 1/3 RD RULE
*/

#include<stdio.h>
#include<conio.h>
#define f(x) 1/(1+x*x)
void main()
{
int i,n;
float s1=0.0,s2=0.0,s3=0.0,h,sum;
float x[10];
float y[10];
clrscr();
printf("enter the values of n");
scanf("%d",&n);
printf("enter the values of x");
for(i=0;i<n;i++)
scanf("%f",&x[i]);
h=x[1]-x[0];
for(i=0;i<n;i++)
y[i]=f(x[i]);
printf("for simpson's 1/3 rd rule");
s1=(y[0]+y[n-1]);
for(i=1;i<n-1;i++)
{
if(i%2!=0)
s2=s2+y[i];
else
s3=s3+y[i]; }
sum=((s1+(4*s2)+(2*s3))*h)/3;
printf("%f/n",sum);
getch();
}


/* OUTPUT
enter the values of n 7
0 1 2 3 4 5 6
for simpson's 1/3 rd rule
1.366174*/

TRAPEZOIDAL PROGRAM

/*CSEMATTER.BLOGSPOT.IN

PROGRAM-PROGRAM FOR TRAPEZOIDAL RULE*/

#include<stdio.h>
#include<conio.h>
#define f(x) 1/(1+x*x)
void main()
{
int i,n;
float s1=0.0,s2=0.0,h,sum;
float x[10];
float y[10];
clrscr();
printf("enter the values of n");
scanf("%d",&n);
printf("enter the values of x");
for(i=0;i<n;i++)
scanf("%f",&x[i]);
h=x[1]-x[0];
for(i=0;i<n;i++)
y[i]=f(x[i]);
printf("for trapezoidal");
s1=(y[0]+y[n-1]);
for(i=1;i<n-1;i++)
{
s2=s2+(2*y[i]);
}
sum=((s1+s2)*h)/2;
printf("%f/n",sum);
getch();
}
/* OUTPUT
enter the values of n 7
0 1 2 3 4 5 6
for trapezoidal
1.410799*/

ROOT USING REGULA FALSI

/*CSEMATTER.BLOGSPOT.IN
program to find root by regula falsi method*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x*x-2*x-5)
void main()
{
int a,b;
float e,f,c,d,itr=0,maxitr=8;
clrscr();
printf("enter the values of intervals");
scanf("%d %d",&a,&b);
e=b-a;
f=f(b)-f(a);
c=a-((e/f) *f(a));
do
{
if(f(a)*f(c)<0)
{ b=c;}
else a=c;
d=a-((e/f) *f(a));
if (fabs(d-c)<0.0001)
{printf("%f is the root",d);
exit(0);}
else
c=d;
itr++;}
while(itr<maxitr);
if(itr>maxitr)
printf("sol doesntconverge no. of iteratn not sufficient");
getch();}
/*output
enter the values of intervals 2 3
2.05882 is the root
*/

GAUSS FORWARD INTERPOLATION

  /*CSEMATTER.BLOGSPOT.IN
 TO IMPLEMENT GAUSS FORWARD INTERPOLATION*/

#include<stdio.h>
#include<conio.h>
int fact(int a)
{
if(a==0)
{
return(1);
}
else
return(a*fact(a-1));
}
void main()
{
int n,i,j,k;
float ax[6],ay[6],x,val,diff[6][6],h,xs,p,res,y1,y2,y3,y4;
clrscr();
printf("enter the value of n(1 to 6)\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the value of x:\n");
scanf("%f",&ax[i]);
printf("\nenter the value of y:\n");
scanf("%f",&ay[i]);
}
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(j==0)
diff[i][j]=ay[i];
else if(diff[i+1][j-1]!=NULL)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
printf("\nenter the value of x to find corresponding y:\n");
scanf("%f",&val);
for(i=0;i<n;i++)
{
if(ax[i]>val)
{
break;
}
xs=ax[i];
k=i;
}
h=ax[1]-ax[0];
p=(val-xs)/h;
y1=p*diff[i][1];
y2=(p*(p-1)*diff[i-1][2])/fact(2);
y3=((p+1)*p*(p-1)*diff[i-2][3])/fact(3);
y4=((p+1)*p*(p-1)*(p-2)*diff[i-3][4])/fact(4);
res=ay[k]+y1+y2+y3+y4;
printf("the result is %f",res);
getch();
}

OUTPUT:
enter the value of n(1 to 6)
5
enter the value of x: 21
enter the value of y: 18.4708
enter the value of x: 25
enter the value of y: 17.8144
enter the value of x: 29
enter the value of y: 17.1070
enter the value of x: 33
enter the value of y: 16.3432
enter the value of x: 37
enter the value of y: 15.5154
enter the value of x to find corresponding y: 30
the result is 16.9216

MULLER'S METHOD PROGRAM

/*CSEMATTER.BLOGSPOT.IN
PROGRAM TO IMPLEMENT MULLER'S METHOD*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define I 2
float Y(float x)
{return cos(x)-x*exp(x);
}
main()
{int i,itr,maxitr;
float x[4],li,di,mu,s,l,doa;
clrscr();
printf("Enter initial approximation");
for(i=I-2;i<3;i++)
{scanf("%f",&x[i]);
}
printf("Enter maxitr & error");
scanf("%d %f",&maxitr,&doa);
for(itr=1;itr<=maxitr;itr++)
{
li=((x[I]-x[I-1])/(x[I-1]-x[I-2]));
di=((x[I]-x[I-2])/(x[I-1]-x[I-2]));
mu=Y(x[I-2])*li*li-Y(x[I-1])*di*di+Y(x[I])*(di+li);
s=sqrt((mu*mu-4*Y(x[I])+di*li*Y(x[I-2])*li-Y(x[I-1])*di+Y(x[I])));
if(mu<0)
l=((2*Y(x[I])*di)/(-mu+s));
else
l=((2*Y(x[I])*di)/(-mu-s));
x[I+1]=x[I]+l*(x[I]-x[I-1]);
if(fabs(x[I+1]-x[I])<doa)
{printf("Solution is %f",x[I+1]);
getch();
exit(0);
}
for(i=I-2;i<3;i++)
x[i]=x[i+1];
}
printf("Solution does not converge");
getch();
}

/* OUTPUT
Enter initial approximation -1 0 1
Enter maxitr & error 10 0.0001
Solution is 0.517823
*/

GAUSS BACKWARD METHOD

/*CSEMATTER.BLOGSPOT.IN
PROGRAM TO IMPLEMENT GAUSS BACKWARD METHOD*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k;
float ax[6],ay[6],x,val,diff[6][6],h,xs,p,res,y1,y2,y3,y4,y5;
printf("enter the value of n(1 to 6)\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the value of x:\n");
scanf("%f",&ax[i]);
printf("\nenter the value of y:\n");
scanf("%f",&ay[i]);}
for(j=0;j<n;j++)
{ for(i=0;i<n;i++)
{if(j==0)
diff[i][j]=ay[i];
else if(diff[i+1][j-1]!=NULL)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];}}
printf("\nenter the value of x to find corresponding y:\n");
scanf("%f",&val);
for(i=0;i<n;i++)
{if(ax[i]>val)
{break;}
xs=ax[i];
k=i; }
h=ax[1]-ax[0];
p=(val-xs)/h;
y1=p*diff[i][1];
y2=(p*(p+1)*diff[i-1][2]);
y3=((p-1)*p*(p+1)*diff[i-2][3]);
y4=((p-1)*p*(p+1)*(p+2)*diff[i-3][4]);
y5=((p-2)*(p-1)*p*(p+1)*(p+2));
res=ay[k]+y1+y2+y3+y4+y5;
printf("the result is %f",res);
}
/*OUTPUT
enter the value of n(1 to 6)
6
enter the value of x: 1939
enter the value of y: 12
enter the value of x: 1949
enter the value of y: 15
enter the value of x: 1959
enter the value of y: 20
enter the value of x: 1969
enter the value of y: 27
enter the value of x: 1979
enter the value of y: 39
enter the value of x: 1989
enter the value of y: 53
enter the value of x to find corresponding y: 30
the result is 32.532318

PROGRAM TO IMPLEMENT CURVE FITTING

/* CSEMATTER.BLOGSPOT.IN
PROGRAM TO IMPLEMENT CURVE FITTING*/

#include<stdio.h>
#include<conio.h>
void main()
{float ag[3][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0}};
float a,b,c,x,y,xsg,t;
int i,j,k,n;
printf("enter the no. of pairs of observed values");
scanf("%d",&n);
ag[0][0]=n;
for(i=0;i<n;i++)
{
scanf("%f %f",&x,&y);
xsg=x+x;
ag[0][1]+=x;
ag[0][2]+=xsg;
ag[1][2]+=x*xsg;
ag[2][2]+=x*xsg*xsg;
ag[0][3]+=y;
ag[1][3]+=x*y;
ag[2][3]+=xsg*y;
ag[1][0]=ag[0][2];
ag[2][1]=ag[1][2];
ag[1][0]=ag[0][1];
ag[2][0]=ag[1][1];
for(j=0;j<3;j++)
for(i=0;i<3;i++)
if(i!=j)
{t=ag[i][j]/ag[j][i];
for(k=0;k<4;k++)
ag[i][k]-=ag[j][k]*t;}
a=ag[0][3]/ag[0][1];
b=ag[1][3]/ag[1][1];
c=ag[2][3]/ag[2][2];
printf("a=%f ,b=%f ,c=%f",a,b,c);
}

/*OUTPUT
Enter the no. of pairs of observed values 7
1.0  1.1
1.5  1.3
2.0  1.6
2.5  2.0
3.0  2.7
3.5  3.4
4.0  4.1
a=1.035712 b=-0.192972 c=0.242981
*/








FLOOD FILL ALGORITHM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM-TO IMPLEMENT FLOOD FILL ALGORITHM*/

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void fill_right(int x,int y)
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);
}
}
void fill_left(int x,int y)
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);
}
}
int main()
{
int x , y ,a[10][10];
int gd=DETECT, gm ,n,i;
initgraph(&gd,&gm,"c:\tc\bgi");
printf("Enter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\nEnter the cordinates of polygon :\n\n ");
for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
printf("\n\nEnter the seed pt. : ");
scanf("%d %d",&x,&y);
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
fill_right(x,y);
fill_left(x-1,y);
getch();
}

BOUNDARY FILL ALGORITHM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM-TO IMPLELMENT BOUNDARY FILL ALGORITHM*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void colouring(int,int,int,int,int);
int main()
{
    int gd=DETECT,gm,x,y,a=0,b=0,c,tempx,tempy,left,right,top,bottom;
    initgraph(&gd,&gm,"C:\TC\BGI");
    printf("Enter the left, top, right & bottom coordinates of rectangle: ");
    scanf("%d %d %d %d",&left,&top,&right,&bottom);
    rectangle(left,top,right,bottom);
    printf("Enter the corresponding code of color to be filled");
    scanf("%d",&c);
    tempx=(right-left)/2;
    tempy=(bottom-top)/2;
    x=tempx+left;
    y=tempy+top;
    printf("%d %d",x,y);
    while(a!=tempx | b!=tempy)
    {printf("%d %d\n",a,b);
        colouring(x,y,a,b,c);
        if(a<=tempx)
        a++;
        if(b<=tempy)
        b++;
    }
    getch();
    closegraph();
}

void colouring(int x,int y,int a,int b,int c)
{
    int i,j;
    for(i=0; i<=a; i++)
    {
        for(j=0; j<=b; j++)
        {
            putpixel(x,y,c);
            putpixel(x-i,y,c);
            putpixel(x+i,y,c);
            putpixel(x,y-j,c);
            putpixel(x,y+j,c);
            putpixel(x-i,y-j,c);
            putpixel(x-i,y+j,c);
            putpixel(x+i,y-j,c);
            putpixel(x+i,y+j,c);
        }
    }
}

3D TRANSFORMATION AND ROTATION

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-3D TRANSFORMATION AND ROTATION*/



#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

#include<graphics.h>




void draw();

void rotate();

int x12,x22,y12,y22,mx,my,depth;

int main()

{


    int gd=DETECT,gm,c;

    initgraph(&gd,&gm,"C:\tc\bgi");

    printf("\n3D Transformation Rotating\n\n");

    printf("\nEnter 1st top value(x12,y12):");

    scanf("%d%d",&x12,&y12);

    printf("Enter right bottom value(x2,y2):");

    scanf("%d%d",&x22,&y22);

    depth=(x22-x12)/4;

    mx=(x12+x22)/2;

    my=(y12+y22)/2;

    draw();

    getch();

    cleardevice();

    rotate();

    getch();

}



void draw()

{

    bar3d(x12,y12,x22,y22,depth,1);

}



void rotate()

{

    float t;

    int a1,b1,a2,b2,dep;

    printf("Enter the angle to rotate=");

    scanf("%f",&t);

    t=t*(3.14/180);

    a1=mx+(x12-mx)*cos(t)-(y12-my)*sin(t);

    a2=mx+(x22-mx)*cos(t)-(y22-my)*sin(t);

    b1=my+(x12-mx)*sin(t)-(y12-my)*cos(t);

    b2=my+(x22-mx)*sin(t)-(y22-my)*cos(t);

    if(a2>a1)

       dep=(a2-a1)/4;

    else

      dep=(a1-a2)/4;

    bar3d(a1,b1,a2,b2,dep,1);

    setcolor(5);

    //draw();

}


COHEN SUTHERLAND LINE CLIPING


/*CSEMATTER.BLOGSPOT.IN
PROGRAM NAME-PROGRAM TO IMPLEMENT COHEN SUTHERLAND LINE CLIPING*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
struct p
{
int x,y;
}p1,p2;
void main()
{
int gd=DETECT,gm;
int i,q=0,x1,y1,x2,y2,l,b,t,r,code1[4],code2[4];
int k=0,counter1=0,counter2=0,temp[5];
clrscr();
initgraph(&gd,&gm,"");
printf("enter the value of left, right ,bottom & top value");
scanf("%d%d%d%d",&l,&r,&b,&t);
rectangle(l,t,r,b);
printf("enter the first point of line");
scanf("%d%d",&p1.x,&p1.y);
printf("enter the end point of line");
scanf("%d%d",&p2.x,&p2.y);
if(p1.x<l)
{
code1[0]=1;
}
else
code1[0]=0;
if(p1.x<r)
code1[1]=0;
else
code1[1]=1;
if(p1.y>b)
code1[2]=1;
else
code1[2]=0;
if(p1.y<t)
code1[3]=1;
else
code1[3]=0;

if(p2.x<l)
{
code2[0]=1;
}
else
code2[0]=0;
if(p2.x<r)
code2[1]=0;
else
code2[1]=1;
if(p2.y>b)
code2[2]=1;
else
code2[2]=0;
if(p2.y<t)
code2[3]=1;
else
code2[3]=0;
for(i=0;i<=3;i++)
{
if(code1[i]==0)
counter1++;
}
for(i=0;i<=3;i++)
{
if(code2[i]==0)
counter2++;
}
if((counter1==4)&&(counter2==4))
{
printf("line is completly visible");
line(p1.x,p1.y,p2.x,p2.y);
}
else
{
for(i=0;i<4;i++)
{
temp[i]=code1[i]&code2[i];
if(temp[i]==1)
{q=0;
printf("line completely invisible");
break;}
else
{
q=1;
}}
if(q==1)
{
if(p1.x<l|| p2.x<l)
{if(p1.x<l)
p1.x=l;
else
p2.x=l;}
if(p1.x>r || p2.x>r)
{if(p1.x>r)
p1.x=r;
else
p2.x=r;}
if(p1.y>b|| p2.y>b)
{if(p1.y>b)
p1.y=b;
else
p2.y=b;}
if(p1.y<t|| p2.y<t)
{if(p1.y<t)
p1.y=t;
else
p2.y=t;}
line(p1.x,p1.y,p2.x,p2.y);
}
}
 getch();
closegraph();}

REFLECTION IN GRPAHICS


/*CSEMATTER.BLOGSPOT.IN
PROGRAM-REFLECTION IN GRAPHICS*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void display(float [3][3]);
void xreflection(float [3][3],float [3][3]);
void yreflection(float [3][3],float [3][3]);
void linereflection(float [3][3],float [3][3]);
void multiply(float [3][3],float [3][3],float [3][3]);
void multiply1(float [3][3],float [3][3],float [3][3],float [3][3],float [3][3],float [3][3],float [3][3]);
int main()
{
    int gd=DETECT,gm,i,j,k,choice;
    float arr2[3][3],arr[3][3],arr_c[3][3];


    initgraph(&gd,&gm,"C:\TC\bgi");
    printf("Enter the values of x & y coordinates for 1st, 2nd, 3rd vertex");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(j==0)
                scanf("%f",&arr2[i][0]);
            if(j==1)
                scanf("%f",&arr2[i][1]);
            else if(j==2)
                arr2[i][2]=1;
        }

    }
    display(arr2);
    printf("enter the choice\n");
    printf("enter the 1 for x-reflection\n");
    printf("enter the 2 for y-reflection\n");
    printf("enter the 3 for y=mx+c reflection \n");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1:
        xreflection(arr2,arr);
        display(arr);
        getch();
        closegraph();
        break;
    case 2:
        yreflection(arr2,arr);
        display(arr);
        getch();
        closegraph();
        break;
    case 3:
        linereflection(arr2,arr_c);
        display(arr_c);
        getch();
        closegraph();
        break;
    }
}
void display(float arr2[3][3])

{
    line(320+arr2[0][0],240-arr2[0][1],320+arr2[1][0],240-arr2[1][1]);
    line(320+arr2[1][0],240-arr2[1][1],320+arr2[2][0],240-arr2[2][1]);
    line(320+arr2[2][0],240-arr2[2][1],320+arr2[0][0],240-arr2[0][1]);
}


void xreflection(float arr2[3][3],float arr[3][3])
{
    int i,j;
    float arr3[3][3];
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(i!=j)
                arr3[i][j]=0;
        }
    }
    arr3[0][0]=1;
    arr3[1][1]=-1;
    arr3[2][2]=1;
    multiply(arr3,arr2,arr);
}




void yreflection(float arr2[3][3],float arr[3][3])
{
    int i,j;
    float arr3[3][3];
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(i!=j)
                arr3[i][j]=0;
        }
    }
    arr3[0][0]=-1;
    arr3[1][1]=1;
    arr3[2][2]=1;
    multiply(arr3,arr2,arr);
}


void linereflection(float arr2[3][3],float arr_c[3][3])
{
    int i,j,k;
    float m,c,arr_t[3][3],arr_r[3][3],arr_it[3][3],arr_ir[3][3],arr_ref[3][3];
    float theta1;
    printf("enter the values of m & c");
    scanf("%f %f",&m,&c);
    theta1=atan(m);
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(i==j)
                arr_t[i][j]=1;
            else
                arr_t[i][j]=0;
            arr_t[2][0]=0;
            arr_t[2][1]=-c;
        }
    }

    arr_r[0][0]=cos(theta1);
    arr_r[0][1]=-sin(theta1);
    arr_r[0][2]=0;
    arr_r[1][0]=sin(theta1);
    arr_r[1][1]=cos(theta1);
    arr_r[1][2]=0;
    arr_r[2][0]=0;
    arr_r[2][1]=0;
    arr_r[2][2]=1;

    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(i!=j)
                arr_ref[i][j]=0;
        }
    }
    arr_ref[0][0]=1;
    arr_ref[1][1]=-1;
    arr_ref[2][2]=1;

    arr_ir[0][0]=cos(theta1);
    arr_ir[0][1]=sin(theta1);
    arr_ir[0][2]=0;
    arr_ir[1][0]=-sin(theta1);
    arr_ir[1][1]=cos(theta1);
    arr_ir[1][2]=0;
    arr_ir[2][0]=0;
    arr_ir[2][1]=0;
    arr_ir[2][2]=1;

    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if(i==j)
                arr_it[i][j]=1;
            else
                arr_it[i][j]=0;
            arr_it[2][0]=0;
            arr_it[2][1]=c;
        }
    }

    multiply1(arr2, arr_t, arr_r, arr_ref, arr_ir, arr_it, arr_c);

}

void multiply(float arr3[3][3],float arr2[3][3],float arr[3][3])
{
    int i,j,k;
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            arr[i][j]=0.0;
        }
    }
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            for(k=0; k<3; k++)
            {
                arr[i][j]=arr[i][j]+arr2[i][k]*arr3[k][j];
            }
        }
    }
}

void multiply1(float arr2[3][3],float arr_t[3][3],float arr_r[3][3],float arr_ref[3][3],float arr_ir[3][3],float arr_it[3][3],float arr_c[3][3])
{
    int i,j,k;
    float arr_a[3][3],arr_b[3][3],arr_d[3][3],arr_e[3][3];
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
                arr_a[i][j]=0;
                arr_b[i][j]=0;
                arr_d[i][j]=0;
                arr_e[i][j]=0;
                arr_c[i][j]=0;
        }
    }
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            for(k=0; k<3; k++)
            {
                arr_a[i][j]=arr_a[i][j]+(arr2[i][k]*arr_t[k][j]);
                arr_b[i][j]=arr_b[i][j]+(arr_r[i][k]*arr_ref[k][j]);
                arr_d[i][j]=arr_d[i][j]+(arr_ir[i][k]*arr_it[k][j]);
            }
        }
    }

    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            for(k=0; k<3; k++)
            {
                arr_e[i][j]=arr_e[i][j]+(arr_a[i][k]*arr_b[k][j]);
            }
        }
    }
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            for(k=0; k<3; k++)
            {
                arr_c[i][j]=arr_c[i][j]+(arr_e[i][k]*arr_d[k][j]);
            }
        }
    }

}

SCALING SHEARING ROTATION TRANSLATION IN GRAPHICS

/*CSEMATTER.BLOGSPOT.IN

PROGRAM-TO PERFORM SCALING,SHEARING,ROTATION,TRANSLATION*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void translation(float [3][3]);
void scaling(float [3][3]);
void display(float [3][3]);
void rotation(float [3][3]);
void shearingx(float [3][3]);
void shearingy(float [3][3]);
void multiply(float [3][3],float [3][3]);
int main()
{
int gd=DETECT,gm,i,j,k,choice;
float arr2[3][3];

initgraph(&gd,&gm,"C:\TC\bgi");
printf("Enter the values of x & y coordinates for 1st, 2nd, 3rd vertex");
for(i=0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
        if(j==0)
        scanf("%f",&arr2[i][0]);
        if(j==1)
        scanf("%f",&arr2[i][1]);
        else
        if(j==2)
        arr2[i][2]=1;
    }

}
display(arr2);
printf("enter the choice\n");
printf("enter the 1 for x-shearing\n");
printf("enter the 2 for y-shearing\n");
printf("enter the 3 for rotation\n");
printf("enter the 4 for translation\n");
printf("enter the 5 for scaling\n");
scanf("%d",&choice);
switch(choice)
{
    case 1:
    shearingx(arr2);
    getch();
closegraph();
    break;
    case 2:
    shearingy(arr2);
    getch();
closegraph();
    break;
    case 3:
    rotation(arr2);
    getch();
closegraph();
    break;
    case 4:
    translation(arr2);
    getch();
closegraph();
    break;
    case 5:
    scaling(arr2);
    getch();
closegraph();
    break;
}

}
void display(float arr2[3][3])

{
line(arr2[0][0],arr2[0][1],arr2[1][0],arr2[1][1]);
line(arr2[1][0],arr2[1][1],arr2[2][0],arr2[2][1]);
line(arr2[2][0],arr2[2][1],arr2[0][0],arr2[0][1]);
}

//SHEARING -X
void shearingx(float arr2[3][3])
{
    int i,j,shx;
float arr3[3][3];
printf("Enter the value of x shearing");
scanf("%d",&shx);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
arr3[i][j]=1;
if(i!=j)
arr3[i][j]=0;
}
}
arr3[1][0]=shx;
multiply(arr3,arr2);
}


//SHEARING -Y
void shearingy(float arr2[3][3])
{
    int i,j,shy;
float arr3[3][3];
printf("Enter the value of y shearing");
scanf("%d",&shy);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
arr3[i][j]=1;
if(i!=j)
arr3[i][j]=0;
}
}
arr3[0][1]=shy;
multiply(arr3,arr2);
}




void rotation(float arr2[3][3])
{float theta,arr3[3][3];
float theta1;
printf("Enter the value of theta");
scanf("%f",&theta);
theta1=(3.141592/180)*theta;
arr3[0][0]=cos(theta1);
arr3[0][1]=sin(theta1);
arr3[0][2]=0;
arr3[1][0]=-sin(theta1);
arr3[1][1]=cos(theta1);
arr3[1][2]=0;
arr3[2][0]=0;
arr3[2][1]=0;
arr3[2][2]=1;
multiply(arr3,arr2);
}


void scaling(float arr2[3][3])
{int sx,sy,i,j,k;
float arr3[3][3];
printf("Enter the value of x & y scaling");
scanf("%d %d ",&sx,&sy);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==0 && j==0)
arr3[i][j]=sx;
if(i==1 && j==1)
arr3[i][j]=sy;
if (i==2 && j==2)
arr3[i][j]=1;
if(i!=j)
arr3[i][j]=0;
}
}
multiply(arr3,arr2);
}
void translation(float arr2[3][3])
{int tx,ty,i,j,k;
float arr3[3][3];
printf("Enter the value of x & y-translation");
scanf("%d %d ",&tx,&ty);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
arr3[i][j]=1;
else
arr3[i][j]=0;
arr3[2][0]=tx;
arr3[2][1]=ty;
}
}
multiply(arr3,arr2);
}
void multiply(float arr3[3][3],float arr2[3][3])
{int i,j,k;
float arr[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr[i][j]=0;
} }
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{arr[i][j]=arr[i][j]+arr2[i][k]*arr3[k][j];
} }}
display(arr);}

PROGRAM FOR CIRCLE


/*CSEMATTER.BLOSPOT.IN
PROGRAM-PROGRAM TO DRAW A CIRCLE IN GRAPHICS*/


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{    void plotpoint(int,int,int,int);
 int x,y,xc,yc,r,gm,gd=DETECT,p=0;
    initgraph(&gd,&gm,"C:\TC\BGI");
      printf("enter the value of centre coordinates");
      scanf("%d %d",&xc,&yc);
      printf("enter the value of x and radius");
      scanf("%d %d",&x,&r);
    p=1-r;
    y=r;
    plotpoint(xc,yc,x,y);
    while(x<=y)
    {
        x++;
        if(p<0)
        {
            p+=2*x+3;
        }
        else
        {
            y--;
            p+=2*(x-y)+5;

        }
        plotpoint(xc,yc,x,y);
    }
    getch();
    closegraph();

}
void plotpoint(int xc,int yc,int x,int y)
{
    putpixel(xc+x,yc+y,15);
    putpixel(xc-x,yc+y,15);
    putpixel(xc+x,yc-y,15);
    putpixel(xc-x,yc-y,15);
    putpixel(xc+y,yc+x,15);
    putpixel(xc-y,yc+x,15);
    putpixel(xc+y,yc-x,15);
    putpixel(xc-y,yc-x,15);
}


Friday, 7 March 2014

LINE USING BRESENHAM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM NAME-PROGRAM TO DRAW A LINE USING BRESENHAM ALGORITHM*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
      int gd = DETECT, gm;
      int dx, dy, p, end;
      float x1, x2, y1, y2, x, y;
      printf("Enter Value of X1: ");
      scanf("%f", &x1);
      printf("Enter Value of Y1: ");
      scanf("%f", &y1);
      printf("Enter Value of X2: ");
      scanf("%f", &x2);
      printf("Enter Value of Y2: ");
      scanf("%f", &y2);
 if ( x1 > x2 )
   {x = x2;
    y = y2;
    end= x1;}
   else
   { x = x1;
y = y1;
end = x2;}
      dx = abs(x1 - x2);
      dy = abs(y1 - y2);
      p = 2 * dy - dx;
    initgraph(&gd, &gm, "c:\tc\bgi");
      putpixel(x, y, 10);
      while(x < end)
      { x = x + 1;
            if(p < 0)
           p = p + 2 * dy;
            else
                {  y = y + 1;
                  p = p + 2 * (dy - dx);}
            putpixel(x, y, 10);}
      getch();
      closegraph();}

LINE IN A SHIFTED ORIGIN

/
*CSEMATTER.BLOGSPOT.IN
PROGRAM NAME-PROGRAM TO DRAW A LINE IN A SHIFTED ORIGIN*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{int gd=DETECT,gm;
float x1,x2,y1,y2;
float m,c,x,y,i;
printf("enter the first coordinatse");
scanf("%f %f",&x1,&y1);
x1=x1+320;
y1=240-y1;
printf("enter the second coordinatse");
scanf("%f %f",&x2,&y2);
x2=x2+320;
y2=240-y2;
m=(y2-y1)/(x2-x1);
c=y1-(m*x1);
initgraph(&gd,&gm,"C:\tc\bgi");
line(320,0,320,480);
line(0,240,640,240);
  if(x2!=x1)
   { x=x1;
     y=y1;
       while(x<=x2)
       { y=(m*x)+c;
        putpixel(x,y,15);
        x++;}
   }
else
printf("ERROR!!!!!!");
   getch();
closegraph();
}

LINE USING SLOPE METHOD


/*CSEMATTER.BLOGSPOT.IN
PROGRAM NAME-PROGRAM TO DRAW A LINE USING SLOPE*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{int gd=DETECT,gm;
float x1,x2,y1,y2;
float m,c,x,y,i;
printf("enter the first coordinatse");
scanf("%f %f",&x1,&y1);
printf("enter the second coordinatse");
scanf("%f %f",&x2,&y2);
m=(y2-y1)/(x2-x1);
c=y1-(m*x1);
initgraph(&gd,&gm,"C:\tc\bgi");
  if(x2!=x1)
   { x=x1;
     y=y1;
       while(x<=x2)
       {y=(m*x)+c;
        putpixel(x,y,15);
        x++;}
    }
else
   printf("ERROR!!!!!!");
   getch();
closegraph();}

LINE USING DDA ALGORITHM

/*CSEMATTER.BLOGSPOT.IN

PROGRAM NAME-PROGRAM TO DRAW A LINE USING DDA ALGORITHM*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{int gd=DETECT,gm;
float x1,x2,y1,y2,dx,dy,steps;
float m,c,y,x,i,deltax,deltay;
printf("enter the first coordinatse");
scanf("%f %f",&x1,&y1);
printf("enter the second coordinatse");
scanf("%f %f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
initgraph(&gd,&gm,"C:\tc\bgi");
  if(abs(dx)>abs(dy))
   steps=dx;
   else
   steps=dy;
deltax=dx/steps;
deltay=dy/steps;
x=x1;
y=y1;
putpixel((int)(x+0.5),(int)(y+0.5),15);
for(i=0;i<steps;i++)
{ x=x+deltax;
 y=y+deltay;
 putpixel((int)(x+0.5),(int)(y+0.5),15);}
getch();
closegraph();}