Pages

Monday, September 1, 2014

Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space

#include<fstream.h>
#include<iostream.h>
#include<process.h>
#include<conio.h>
void main()
{
 clrscr();
 ifstream file1;
 ofstream file2;
 char sfile[13],tfile[13];
  cout<<"\nEnter Source file name-";
   cin>>sfile;
  cout<<"\nEnter Target file name-";
   cin>>tfile;
  file2.open(sfile);
  file2<<"\nMy Name      is RAHUL KUMAR"<<endl;
   file2.close();
  file1.open(sfile);
  file2.open(tfile);
   if(!file1)
   {
    cerr<<" Some Error..";
exit(1);
   }
  char varch,p=file1.get();
   while(!file1.eof())
   {
    file2.put(p);
    varch=file1.get();

     if(p==' ' && varch==' ')
     {
       p=varch;
       varch=file1.get();
     }
     p=varch;
   
   }
   cout<<"\ncopied......";
   file1.close();
   file2.close();
   cout<<"\nTarget File Contents....\n";
   file1.open(tfile);
   while(!file1.eof())
    {
     file1.get(varch);
     cout<<varch;
    }
   file1.close();
 getch();
 }

/* OUTPUT

Enter Source file name-SOURCE

Enter Target file name-TARGET

copied......
Target File Contents....

My Name is RAHUL KUMAR */

Write a C program for Polygon Filling using Boundaryfill,Flodfill and Scanline Algorithm

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

int n,i,j,k,gd,gm,dy,dx;
static int x,y,temp,xavg=0,yavg=0;
int a[20][2],xi[20],ch;
float slope[20];

void ffill(int sx,int sy,int bc,int fc)
{
  if(getpixel(sx,sy)==bc && getpixel(sx,sy)!=fc)
  {
putpixel(sx,sy,fc);
delay(1);
ffill(sx+1,sy,bc,fc);
ffill(sx,sy-1,bc,fc);
ffill(sx-1,sy,bc,fc);
ffill(sx,sy+1,bc,fc);
  }
  return;
}

 void bfill(int sx,int sy,int bc,int fc)
 {
if(getpixel(sx,sy)==bc)
{
putpixel(sx,sy,fc);
delay(1);
bfill(sx+1,sy,bc,fc);
bfill(sx,sy-1,bc,fc);
bfill(sx-1,sy,bc,fc);
bfill(sx,sy+1,bc,fc);
  }
  return;
 }

void scanfill(int a[20][2])
{
for(i=0;i<n;i++)
{
dy=a[i+1][1]-a[i][1];
dx=a[i+1][0]-a[i][0];
if(dy==0) slope[i]=1.0;
if(dx==0) slope[i]=0.0;
if((dy!=0)&&(dx!=0))            /*- calculate inverse slope -*/
{
slope[i]=(float) dx/dy;
}
}
for(y=0;y< 480;y++)
{
k=0;
for(i=0;i<n;i++)
{
if(((a[i][1]<=y)&&(a[i+1][1]>y))||
((a[i][1]>y)&&(a[i+1][1]<=y)))
{
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k++;
}
}

for(j=0;j<k-1;j++)               /*- Arrange x-intersections in order -*/
for(i=0;i<k-1;i++)
{
if(xi[i]>xi[i+1])
{
temp=xi[i];
xi[i]=xi[i+1];
xi[i+1]=temp;
}
}
setcolor(35);
delay(1);
for(i=0;i<k;i+=2)
{
line(xi[i],y,xi[i+1]+1,y);
}
}
getch();
}

void drawp()
{
printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

for(i=0;i<n;i++)
{
printf("\tx%d y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
xavg+=a[i][0];
yavg+=a[i][1];
}

a[n][0]=a[0][0];
a[n][1]=a[0][1];
xavg=xavg/n;
yavg=yavg/n;
/*- draw polygon -*/
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}

//switch for color
printf("\n0.Exit\t1.Bfill\t2.Ffill\t3.Scanfill\tEnter Choice: ");
scanf("%d",&ch);
switch(ch)
 {
case 0: break;
case 2: ffill(xavg+1,yavg+1,0,15); break;
case 1: bfill(xavg+1,yavg+1,0,15); break;
case 3: scanfill(a); break;x
default: printf("\nSomething goes wromg...!!\n");
 }



}//function close

void main()
{
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
 drawp();
 getch();
}


/* output


*/