Monday, June 7, 2010

file handling

#include"stdio.h"
#include"conio.h"
#include"fstream.h"
#include"stdlib.h"
#include"string.h"
#include"iomanip.h"

class book    //class defination for book
{
public:
    char name[20];     //all data member and member fun are public
    char author[20];
    int a;
    void getdata();
    void show();
};
//takes the details of book
void book::getdata()
{
 cout<<"\nENTER DETAILS ABOUT BOOK U WANT TO PURCHASE :\"";
 cout<<"\n\n\n\t\tEnter Name Of Book  :\t";
 cin>>name;
 cout<<"\n\t\tEnter Name Of Author    :\t";
 cin>>author;
 fflush(stdin);
 cout<<"\n\t\tEnter No. Of Copies  :\t";
 cin>>a;
}
void book::show()  //shows the details of book
{
    cout<<"\nBOOK      :"<
    cout<<"\nAUTHOR  :"<
    cout<<"\nCOPIES  :"<
}
void main()
{
    clrscr();
    int ch;
    book b1;//create a object of class book
do
{
    fstream f; //create object of fstream class
    f.open("book",ios::in|ios::out|ios::app);//open file in i/p,o/p and append mode
    cout<<"\nWHAT DO U WANT TO DO:"; //case
    cout<<"\n1 . TO ADD BOOK IN SHOP";
    cout<<"\n2 . TO SHOW ALL BOOKS";
    cout<<"\n3 . TO CHECK AVAILABILITY";
    cout<<"\n4 . TO MODIFY";
    cout<<"\n5 . TO EXIT";
    int a;
    cout<< "\nENTER UR CHOICE:";
     f.seekg(0);//move the file pointer to 0
    cin>>a;
    char x;
    switch (a)
    {
    case 1:
         clrscr();
         fstream f;
         f.open("book",ios::app|ios::out|ios::binary);
         char ans;
         b1.getdata();//call to getdata fun.
         f.write((char *)&b1,sizeof(b1));//write a record into file which is accept by b1 object
         getch();
         break;
 case 2:
        cout<<"\n\n";
        fstream f1;
        f1.open("book",ios::in);
        f1.seekg(0);
        while(f1.read((char *)&b1,sizeof(b1)) )
        {
        b1.show();
        if(f1.eof()==1)
        {
         break;
        }
        }
        f.close();
        break;
    case 3:
        {
        ifstream f;
         book b1;
        char name[20];
        char author[20];
        f.open("book",ios::in|ios::binary);
        cout<<"\n\n\n          Enter book name whose record to be seen  :";
        cin>>name;
     do
    {
    f.read((char *)&b1,sizeof(b1));
    if(f.eof()==1)
    {
     break;
    }
    if(strcmp(b1.name,name)==0)
    {
    cout<<"\n                  Name     :"<
    cout<<"\n                 author   :"<
    cout<<"\n                 copies   :"<
    getchar();
    }
    }while(f);
     f.close();
     }
    break;

case 4:
    {
    fstream f;
    book b1;
    char name[20];
    char author[20];
    int a;
    f.open("book",ios::in|ios::binary);
     cout<<"\n";
    cout<<"\n Enter book name whose record to be changed  :";
     cin>>name;
    do
   {
    f.read((char *)&b1,sizeof(b1));
    if(f.eof()==1)
    {
    break;
    }
     if(strcmp(b1.name,name)==0)
    {
    cout<<"\n        Name     :"<
    cout<<"\n        Author   :"<
    cout<<"\n        Copies   :" <
    getchar();
    cout<<"\n                 Enter New Values" ;
    cout<<"\n\n      Enter the book name  :";
    cin>>name;
    cout<<"\n        Enter author name    :";
    cin>>author;
    cout<<"\n        Enter no. of copies  :";
    cin>>a;
    strcpy(b1.name,name);
    strcpy(b1.author,author);
    b1.a=a;
    int l=f.tellg();
    f.close();
    f.open("book",ios::out|ios::binary|ios::ate);
    f.seekg(l-sizeof(b1));
    f.write((char *)&b1,sizeof(b1));
    }
    }while(f);
        f.close();
    }
        break;

 case 5:exit(1);
   }
}while(ch!=5);
getch();
}

graphics

//------------------------------------------------------------------------------------------------
//Aim:Write a prg to create a graphics class hierarchy.create abstract base class called
//figure and derive two classes close and open from that .decare two more classes
//called shape and ellipse using close class .create a derive clases polyline and line from open //cass
//(Run time polymorphism)
//------------------------------------------------------------------------------------------------

#include
#include
#include
class Figure            //abstract base class
    {
       public:        //all base class fun are virtual fun.
        virtual void draw()
         {
         }
        virtual void draw(int,int,int,int)
         {
         }
        virtual void draw(int,int,int)
         {
         }
        virtual void draw(int,int,int,int,int,int)
         {
         }
    };

class Close1 : public Figure             //deriving the close1 class from figure base class
    {
    public:
        void draw()           //draw fun with no argu.
         {
         }
        void draw(int,int,int) //draw fun with 3 argu.
         {
         }
        void draw(int,int,int,int)
         {
         }
        void draw(int,int,int,int,int,int)
         {
         }
    };

class Open : public Figure            //deriving the open class from figur base class
    {
    public:
        void draw()                 //multilines
         {
         }
        void draw(int,int,int,int)//to draw single line
         {
         }

    };
//deriving the shape calss from close1 base class
class shape : public Close1
    {
    public:
        void draw()                //fun to triangle
            {
              line(0, 0, 100,100);
              line(100, 100, 50, 150);
              line(50, 150, 0, 0);
            }
        void draw(int l,int t,int r,int b) //fun to draw rectangle
            {
            //rectangle(left,top,right,bottom);
               rectangle(l,t,r,b);
            }
        void draw(int x,int y,int r)       //fun to draw circle
            {
               circle(x,y,r);
            }

    };

//deriving the ellipse calss from close1 base class
class Ellipse : public Close1
    {
    void draw(int x,int y,int s,int e,int xrad,int yrad)//fun. to draw ellipse
        {
        //ellipse(midx, midy, stangle, endangle,xradius, yradius);
        ellipse(x, y, s, e,xrad, yrad);
        }
    };

class Line : public Open    //line class derived from open class
    {
    public:
    void draw(int x1,int y1,int x2, int y2)        //fun to draw line
        {
        /* draw a diagonal line*/
        line(x1, y1, x2, y2);
        }
    };

class Polyline : public Open     //polyline calss derived from open base class
    {
    public:
    void draw()         //fun. to draw polyline
        {
          line(20,150,100,100);
          line(150,140,20,150);
          line(250,50,50,150);
        }
    };

void main()
{
    Close1 *c[2];    //create pointer type array of object of close1 base class
    Open *o[2];    //create pointer type array of object of open base class
    shape p;     //create object of sahpe
    Polyline pl;    //create object of polyline
    Ellipse e;     //create a object of ellipse class
    Line l;     //create a object of line class
    int ch,x1,y1,r,x2,y2;
    clrscr();
    c[0]=&p;      //triangle rectangle circle
    c[1]=&e;     //ellipse
    o[0]=&pl;     //polyline
    o[1]=&l;      //line
    int gd = DETECT, gm;
    /* initialize graphics and local variables */
    initgraph(&gd, &gm, "d:\\tc\\bgi");

    do
    {
    cleardevice();
    cout<<"\nMENU";
    cout<<"\n1.Line\n2.Rectangle\n3.Ellipse\n4.Triangle\n5.PolyLine\n6.Circle\n7.Exit\n";
    cout<<"\nEnter ur Choice :";
    cin>>ch;
    switch(ch)
    {
    case 1:
        cout<<"\nEnter the coordinates(x1,y1,x2,y2) : ";
        cin>>x1>>y1>>x2>>y2;
        o[1]->draw(x1,y1,x2,y2);     //line
        break;
    case 2:
        cout<<"Enter the top(x1,y1) co-ordinates and bottom(x2,y2) :";
        cin>>x1>>y1>>x2>>y2;
        c[0]->draw(x1,y1,x2,y2);    //rectangle
        break;
    case 3:
        c[1]->draw(100,100,0,360,100,50);//ellipse
        break;
    case 4:
        c[0]->draw();            //triangle
        break;
    case 5:

        o[0]->draw();            //polyline
        break;
    case 6:
        cout<<"\nEnter the coordinates(x,y) and radius : ";
        cin>>x1>>y1>>r;
        c[0]->draw(x1,y1,r);        //circle
        break;
    }
    getch();
}while(ch!=7);                  //execute a loop till choice !=7;

}

manipulator

/*===================================================================
PRACTICAL NO:-
AIM:-WRITE A PROGRAM IN C++ FOR DISPLAYING TELEPHONE BILL
     OF BSNL (Bharat Sanchar Nigam limited) USING MANIPULATOR
ROLL NO:-
====================================================================*/
#include
#include
#include
#include
#include //exit
#include //toupper
#include
#include
#include
// CLASS HAVING THE RECORD OF CUSTOMER
class customer//CREAT A CUSTOMER CLASS
{
      char ph[10];
      char name[30];
      char add[40];
      char type;
     public :
      void append();
      void display();

}cust;//Declare the class variable

// MAIN FUNCTION HAVING THE MAIN MENU
void main()
{
     char ch,ch1;
     while(1)
     {
      clrscr();
      cout<<<<<<<<<<
      cout<<<<"MAHASANCHAR TELEPHONE NIGA LTD.";
      cout<<<<" "<<<

      cout<<<<"D-Display a Bill"<<<
      cout<<<"A-Append a record"<<<
      cout<<<"Q-Quit"<<<
      cout<<<"Select your choice:-";
      ch =getch();
      ch = toupper(ch);
      switch(ch)
      {
      case ('Q') :
               clrscr();
               exit(1);
      case ('D') : cust.display();
               break;

      case ('A') :cust.append();
              break;
        }
      }
     }
// FUNCTION TO ADD THE CUSTOMER RECORD IN THE FILE
void customer :: append()
{
     char choice;
     fstream fp;
     fp.open("tphone.dat", ios::app);
     if (!fp)
     {
      cout<<"Unable to open FILE.";
      getch();
      return;
     }
     while(1)
     {
      clrscr();
      cout<<<<<<<
      cout<<<<"ENTER  THE  CUSTOMER  RECORD";
      cout<<<<" "<<<

      while(1)
      {
           cout<<<<"Enter the name:-";
           gets(name);
           cout<<<
           if(strlen(name)==0)
           {
        cout<<<"Name cannot be left blank\a"<<
        }
           else
            break;
      }
      while(1)
      {
           cout<<<"Enter the address:-";
           gets(add);
           cout<<<
           if (strlen(add) != 0)
           break;
      }

      while(1)
      {
           cout<<<"Assigned Phone. No.:";
           cin>>ph;
           cout<<<
           if (ph != 0 )
            break;

      }
      cout<<<"Enter O for Office and R for residential phone"<
      cout<
      cout<<<"Catagory ( O/R ) :";
      cin>>type;
      fp.write((char *) this,sizeof(cust));
      return;
      fp.close();
    }
}
// FUNCTION TO DISPLAY THE CUSTOMER RECORD AND CALCULATE THE BILL
void customer :: display()
{
     char p[10];
     char choice;
     int found=0;
     int no;
     float bill, tax, fine, bbill, abill;
     fstream fp;
     fp.open("tphone.dat", ios::in);
     if (!fp)
     {
      cout<<"Unable to open a file";
      getch();
      fp.close();
      return;
     }
     while(choice != '0')
     {
      clrscr();
      cout<<<<<<<<<<
      cout<<<"Please enter the Phone No:- ";
      cin>>p;
      if (!strcmp(p,"0"))
      return;
      found = 0;
      fp.seekg(0);
      while (fp.read((char *)this ,sizeof(cust)))
      {
           if (found==1)
            break;
           if (!strcmp(ph,p))
           {
            clrscr();
            cout<
            cout<<<<" ";;
            cout<<
            cout<<<<"MASANCHAR TELEPHONE BILL"<<
            cout<<<<(" ");
            cout<<
            cout<<<"Name:";
            cout<
            cout.width(57);
            cout<<"Assigned Ph. No.:";
            cout<<<
            cout<<"Catagory ( O/R ):";
            cout<
            cout<<<"Address:";
            cout<<<

            cout<<<"No. of calls:- ";
            cout<<"____";
            cin>>no;
            if ( no <= 150 )
             bill = 0 ;
            else
            {
             no = no - 150;
             if (toupper(type) == 'O')
                  bill = no * 1.00 ;
             else
                  bill = no * .80;
            }
             cout<<
            cout<<<<(" ");
            cout<<
             cout<<<<"Bill:-";
            cout<<<

            tax = (5*bill)/100;
            cout<<
            cout<<<"5% Tax:-";
            cout<<<
            cout<<

            cout<<<"Duties:-";
            cout<<<"100";
            int dd,mm,yy;
            struct date d;   // Getting system Date
            getdate(&d);
            dd = d.da_day;
            mm = d.da_mon;
            yy = d.da_year;
            cout<<<<<

            cout<<<"TOTAL BILL before " ;
            cout<

<<"/"<<<"/"<<<":";
            bbill = bill+tax+100;

            cout<<<
            cout<<

            cout<<<"Late Fine:-";
            fine = (bbill*5)/100;
            cout<<<

            cout<<
            cout<<<"TOTAL BILL after ";
            cout<
<<"/"<<<"/"<<<":-";
            abill = bbill+fine;
            cout<<<
            found = 1;

            cout<<
            cout<<<<(" ");
            cout<<
            cout<<<<"Press a Key";
            getch();
           }
         return;

    }
   }
     fp.close();
}
//end of program