Tuesday, 23 February 2016

Implementation of boundary fill algorithm

Q)Boundary fill algorithm.
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void bounfill(int x,int y)
    { 
int a =getpixel(x,y);
    setcolor(3);
    if(a!=2&&a!=3)
{
    putpixel(x,y,3);
    bounfill(x+1,y);
    bounfill(x-1,y);
    bounfill(x,y+1);
    bounfill(x,y-1);
    }
    }

int main()
{

   int gdriver = DETECT, gmode;
   initgraph(&gdriver, &gmode, "C:\\TC\\bgi");
   setcolor(2);
   circle(250, 250, 50);
   bounfill(250,250);
   getch();
   closegraph();
   return 0;
}

Sunday, 21 February 2016

Filehandeling in c++

Q) Program in C++ to read a text file and convert all letters into uppercase and save on another text file.

#include<iostream>
#include<stdio.h>
using namespace std;

void uppercase()
{char p;
char chi;
    FILE *fp;
    FILE *rp;

    fp=fopen("rem.txt","r");
    rp=fopen("mem.txt","w");
    char ch =getc(fp);

    while(ch!=EOF)
    {

        if(97<=ch&&ch<=122)
        {p=ch;
           p=p-32;
           cout<<p;
        putc(p,rp);
        }

      else
        {  chi=ch;
        cout<<ch;
        putc(chi,rp);
      }
 ch=getc(fp);

    }}
int main()
{
  uppercase();
   
return 0;
}

Filehandeling :Reading a text file

Q) Program to  read a text file and calculate number of tabs,new lines,spaces,characters in it.

#include <iostream>
#include<stdio.h>
using namespace std;

int main()

{
int tab=0,lines=0,pa=0,charec=0;
FILE *fp;
char ch;
fp=fopen("mim.txt","r");
ch=getc(fp);
    while(ch!=EOF)
    {   if(ch=='\t')
       tab++;
       if(ch=='\n')
       lines++;
       if(ch==' ')
       pa++;
       charec++;
       ch=getc(fp);
    }

cout<<"number of tabs ="<<tab<<endl;
cout<<"number of new lines ="<<lines<<endl;
cout<<"number of spaces ="<<pa<<endl;
cout<<"number of characters ="<<charec<<endl;
fclose(fp);
return 0;
}

Friday, 19 February 2016

Scenery using graphics in C++

    #include <graphics.h>
   #include <stdlib.h>
   #include <stdio.h>
   #include<conio.h>
   int main(void)
   {
      int gdriver = DETECT, gmode;
      initgraph(&gdriver, &gmode, "C:\\TC\\bgi");
      circle(150, 150, 20);
      circle(141,146,5);
      circle(160,146,5);
      arc(150,156,215,340,6);
      line(150,170,150,240);
      line(150,240,110,300) ;
      line(150,240,190,300);
      line(150,180,100,180);
      line(150,180,200,180);
     line(300,170,2000,170);
    line(300,170,400,100);
    line(400,100,500,170);
    line(500,170,800,50);
    arc(500,200,45,135,70);
   getch();
   closegraph();
   return 0;
}

Friday, 12 February 2016

Conversion of NFA to DFA

#include<iostream>
#include<string>
using namespace std;

string statear[40];

int find(string nift)
{
for(int i=0;i<40;i++)
{

      if(nift==statear[i])
   {  return 0;
  }
  }
  return 1;
}

int main()
{ int jk=2;
string stra[30];
string no0,no1;
statear[0]="a";
statear[1]="b";
statear[2]="c";
for(int e=0;e<6;e++)
{ cout<<"enter the state when ";

if(e%2==0)
  cout<<"0";
else
  cout<<"1";
  cout<<"is entered to";
  if(e==0||e==1)
  cout<<"a:  ";
  if(e==2||e==3)
  cout<<"b:  ";
  if(e==4||e==5)
  cout<<"c:  ";
  cin>>stra[e];
}
  cout<<"The NFA is"<<endl;
cout<<"    0   1"<<endl;
cout<<"a"<<"   "<<stra[0]<<"   "<<stra[1]<<endl;
cout<<"b"<<"   "<<stra[2]<<"   "<<stra[3]<<endl;
cout<<"c"<<"   "<<stra[4]<<"   "<<stra[5]<<endl;
cout<<"The equivalent DFA is"<<endl;
  cout<<    "           0            1"<<endl;
for(int pl=0;pl<3;pl++)
  {
if(pl==0)
{
  cout<<"a         ";
  cout<<stra[0]<<"       "<<stra[1]<<endl;

}
if(pl==1)
{
cout<<"b        ";
cout<<stra[2]<<"       "<<stra[3]<<endl;
}
if(pl==2)
{
  cout<<"c        ";

cout<<stra[4]<<"      "<<stra[5]<<endl;}
for(int bk=0;bk<13;bk++)

{

int l=find(stra[bk]);
if(l==1&&stra[bk]!="0")
{
jk++;

no0="";
no1="";

for(int j=0;j<3;j++)
{
statear[jk]=stra[bk];
char decide=stra[bk][j];
switch (decide)
{case 'a':

           if(stra[0]!="0") {
   no0.append(stra[0]);
 
 
 
      }
          if(stra[1]!="0")  no1.append(stra[1]);
            break;
            case 'b':
if(stra[2]!="0")   no0.append(stra[2]);
           if(stra[3]!="0") no1.append(stra[3]);
            break;
            case 'c':
if(stra[4]!="0")no0.append(stra[4]);
         if(stra[5]!="0")   no1.append(stra[5]);
            break;
}
}

string mr="0";
if(no0.length()==0)
no0="0";
if(no0.length()>=4)
no0="abc";
if(no1.length()==0)
no1="0";
if(no1.length()>=4)
no1="abc";
cout<<statear[jk]<<"      "<<no0<<"       "<<no1<<endl;
}

}
}

}




Friday, 22 January 2016

DDA algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include<iostream.h>
int main(void)
{   int gdriver = DETECT, gmode;
   initgraph(&gdriver, &gmode, "C:\\TC\\bgi");

  float x1,y1,x2,y2,dx,dy,length,xinr,yinr,x,y;
cout<<"enter the x coordinate of the first point";
cin>>x1;
cout<<"enter the y coordinate of the first point ";
cin>>y1;
cout<<"enter the x coordinate of the second point ";
cin>>x2;
cout<<"enter the y coordinate of the second point ";
cin>>y2;
     dx=x2-x1;
     dy=y2-y1;
     if(abs(dy)>abs(dx))
     length=abs(dy);
     else
     length=abs(dx);
       xinr=dx/length;
       yinr=dy/length;
       x=x1;
       y=y1;
for(int i=0 ;i<length;i++)
{   putpixel ( x,y,2);
x+=xinr;
y+=yinr;

}




   getch();
   closegraph();
   return 0;
}

Bresenham algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include<iostream.h>
int main(void)
{   int gdriver = DETECT, gmode;
   initgraph(&gdriver, &gmode, "C:\\TC\\bgi");

  float x1,y1,x2,y2,dx,dy,length,xinr,yinr,x,y,e;
cout<<"enter the x coordinate of the first point";
cin>>x1;
cout<<"enter the y coordinate of the first point ";
cin>>y1;
cout<<"enter the x coordinate of the second point ";
cin>>x2;
cout<<"enter the y coordinate of the second point ";
cin>>y2;
     dx=x2-x1;
     dy=y2-y1;
       if(abs(dy)>abs(dx))
     length=abs(dy);
     else
     length=abs(dx);
e=2*dy-dx;
      x=x1;
      y=y1;
      for(int i=0;i<length;i++)
      { while(e>0)
      {
y+=1;
e-= 2*dx;

      }
x+=1;
e=e+2*dy;
    putpixel(x,y,3);
      }

   getch();
   closegraph();
   return 0;
}