Sunday, 24 April 2016

Book shop management program by c++

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
//Structure used to Store values of bill in file
struct bill
{
char date[20];
int a_price,s_price;
};
//Union used in search of name in book
union search
{
char n[20];
char a[20];
}s;
//Program main class
class book
{
private:
char name[20],author[20],date[20];
int price,copies,a_price;
public:
//Function for input
void input()
{
cout<<"\t\t\t\tEnter author of book :";
fflush(stdin);
cin.getline(author,20);
inputdate: cout<<"\t\t\t\tEnter today's date (dd/mm/yyyy) :";
fflush(stdin);
cin.getline(date,20);
cout<<"\t\t\t\tEnter actual price of the book :";
fflush(stdin);
cin>>a_price;
cout<<"\t\t\t\tEnter selling price of the book :";
cin>>price;
cout<<"\t\t\t\tEnter no. of copies :";
cin>>copies;
}
//Function to initialize book name if it is not already present in file or we wants to modify other things
void ini_name (char bookname[20])
{
strcpy(name,bookname);
}
//Function to display for admin
void admin_display()
{
cout<<"\n\t\t\t\t---------------------------------------\n\t\t\t\tName of book :"<<name<<"\n\t\t\t\tAuthor name :"<<author;
cout<<"\n\t\t\t\tDate when record was added :"<<date;
cout<<"\n\t\t\t\tActual price of book :"<<a_price<<"\n\t\t\t\tSelling price of book :"<<price<<"\n\t\t\t\tNo. of copies available :"<<copies;
cout<<"\n\t\t\t\t---------------------------------------\n\n";
}
//Function to display for buyer
void display()
{
cout<<"\n\t\t\t\t---------------------------------------\n\t\t\t\tName of book :"<<name<<"\n\t\t\t\tAuthor name :"<<author;
cout<<"\n\t\t\t\tPrice of book :"<<price<<"\n\t\t\t\t---------------------------------------\n\n";
}
//Fuction to search for admin
int admin_check(search s)
{
if(strcmp(s.n,name)==0)
{
admin_display();
return 1;
}
else if(strcmp(s.a,author)==0)
{
admin_display();
return 1;
}
else
return 0;
}
//Function to search for customer
int check(search s)
{
if(strcmp(s.n,name)==0)
{
display();
return 1;
}
else if(strcmp(s.a,author)==0)
{
display();
return 1;
}
else
return 0;
}
//Funtion to change the no. of copies in case any customer buy a book
void change_copies(int n)
{
copies-=n;
}
//Function to return price
int getprice()
{
return price;
}
int get_actualprice()
{
return a_price;
}
//Function to reeturn no of copies
int getcopies()
{
return copies;
}
//Functon to return date
string getdate()
{
return date;
}
//Function to return name of book
string getname()
{
return name;
}
//Function to return book's author name
string geta_name()
{
return author;
}
};
//Function to modify in case record is already present when someone is trying to add same name of record
void modify(string name)
{
book b1;
char c;
fstream f2("G:\\book.dat",ios ::in|ios::out|ios::binary);
f2.read((char *)&b1,sizeof(b1));
while(!f2.eof())
{
if(b1.getname()==name)
{
f2.seekp(f2.tellg()-sizeof(b1),ios::beg);
b1.input();
cout<<"\t\t\t\tAre you sure to modify previous record with this record (y/n)? ";
fflush(stdin);
cin>>c;
if(c=='y'||c=='Y')
f2.write((char*)&b1,sizeof(b1));
else
{
cout<<"\n\t\t\t\tRecord not modified";
break;
}
}
f2.read((char *)&b1,sizeof(b1));
}
f2.close();

}
//Function to get value of name to search
void getvalue(int j)
{
switch(j)
{
  case 1:
{
cout<<"\t\t\t\tEnter the name of book :";
fflush(stdin);
cin.getline(s.n,20);
break;
}
case 2:
{
cout<<"\t\t\t\tEnter the author's name :";
fflush(stdin);
cin.getline(s.a,20);
break;
}
}
}
  int main()
  {
  int a;
  while(1)
  {
  start: cout<<"\n\n\t\t\t\tEnter \n\t\t\t\t1 to purchase a book\n\t\t\t\t2 to add a new book\n";
  cout<<"\t\t\t\t3 to check record of book\n\t\t\t\t4 to modify a record of book\n\t\t\t\t5 to display all records\n";
  cout<<"\t\t\t\t6 to check profit or loss of any day\n\t\t\t\t7 to exit :";
  cin>>a;
  cout<<"\n";
  //Code to purchase a book
  if(a==1)
  {
  book b1;
    int j,s_price=0,ts_price=0,cpy,a_price,ta_price;
      char c;
string name;
char date[20];
  z: cout<<"\t\t\t\tPress 1 to purchase via book name and\n\t\t\t\t2 to purchase via author name :";
  cin>>j;
  fflush(stdin);
getvalue(j);
fstream f2("G:\\book.dat",ios::in | ios::binary);
int x=0,y;
while(f2.read((char *)&b1,sizeof(b1)))
{
if(x==0)
{
x=b1.check(s);
if(x==1)
{
cout<<"\t\t\t\tEnter today's date (dd/mm/yyyy) :";
cin.getline(date,20);
   s_price=b1.getprice();
   a_price=b1.get_actualprice();
   cpy=b1.getcopies();
   name=b1.getname();
   f2.close();
   break;
    }
    else
    {
    y=b1.check(s);
}
}
else
{
y=b1.check(s);
}
}
if(x==0)
{
cout<<"\t\t\t\tBook not found\n";
goto z;
}
a: cout<<"\t\t\t\tEnter the no. of copies you wants to purchase :";
fflush(stdin);
cin>>j;
if(j>cpy)
{
cout<<"\t\t\t\tSorry we are out of stock\n";
goto a;
}
//Code to reduce no. of copies if a person buy from record
else if(j<cpy)
{
book b1;
fstream f2("G:\\book.dat",ios::in|ios::out|ios::binary);
while(f2.read((char *)&b1,sizeof(b1)))
{
if(b1.getname()==name)
{
    f2.seekp(f2.tellg()-sizeof(b1),ios::beg);
    b1.change_copies(j);
f2.write((char*)&b1,sizeof(b1));
}
}
}
ts_price+=s_price*j;
ta_price+=a_price*j;
cout<<"\t\t\t\tDo you wants to purchase more books (y/n)? :";
cin>>c;
if(c=='y'||c=='Y')
goto z;
else
{
    cout<<"\n\t\t\t\t------------------------------------\n";
cout<<"\t\t\t\tTotal amount to be paid is :"<<ts_price<<"\n";
cout<<"\t\t\t\t------------------------------------\n";
//Code to make a bill
{
   bill b;
   b.a_price=ta_price;
   strcpy(b.date,date);
   b.s_price=ts_price;
   ofstream fout("G:\\bill2.dat",ios::app | ios::binary);
   fout.write((char*)&b,sizeof(b));
   fout.close();
}

}

}
//Code to add a new book
  else if(a==2)
  {
  int pswrd;
  book b1;
  while(1)
  {
  cout<<"\t\t\t\tEnter password : ";
  cin>>pswrd;
  if(pswrd==3572)
  break;
  else
  cout<<"\t\t\t\tWrong password try again\n\n";
}
  fflush(stdin);
  while(1)
  {
  int i=0;
  char c;
  char name[20];
         cout<<"\t\t\t\tEnter name of book:";
fflush(stdin);
cin.getline(name,20);
//Code to check if record is already present

ifstream f2("G:\\book.dat",ios::in | ios::binary);
while(f2.read((char *)&b1,sizeof(b1)))
{
if(b1.getname()==name)
i=1;
}
f2.close();
if(i==1)
{
cout<<"\t\t\t\tRecord of this book is already present.\n\t\t\t\tDo you wants to modify it (y/n)? ";
fflush(stdin);
cin>>c;
if(c=='y'||c=='Y')
{
modify(name);
break;
}
else
goto start;
}
b1.ini_name(name);
  b1.input();
  ofstream f1("G:\\book.dat",ios::app | ios::binary);
  f1.write((char*)&b1,sizeof(b1));
  f1.close();
  cout<<"\t\t\t\tEntry saved\n";
  cout<<"\t\t\t\tDo you want to add more books (y/n)? ";
  fflush(stdin);
  cin>>c;
  if(c=='y'||c=='Y')
  continue;
  else
  break;
     }
  }
  //Code to check record of a book
  else if(a==3)
  {
  int pswrd;
  book b1;
  while(1)
  {
  cout<<"\t\t\t\tEnter password : ";
  cin>>pswrd;
  if(pswrd==3572)
  break;
  else
  cout<<"\t\t\t\tWrong password try again\n\n";
}
  fflush(stdin);
  int j;
  cout<<"\t\t\t\tPress 1 to check record via book name and\n\t\t\t\t2 to check record via author name :";
  cin>>j;
  fflush(stdin);
  getvalue(j);
ifstream f2("G:\\book.dat",ios::in | ios::binary);
int x=0,y;
while(f2.read((char *)&b1,sizeof(b1) ))
{
if(x==0)
{
x=b1.admin_check(s);
}
else
{
y=b1.admin_check(s);
}
}
f2.close();
if(x==0)
{
cout<<"\t\t\t\tNothing found\n";
}
  }
  //Code to modify a record in a file
  else if(a==4)
  {
  book b1;
  int pswrd;
  while(1)
  {
  cout<<"\t\t\t\tEnter password : ";
  cin>>pswrd;
  if(pswrd==3572)
  break;
  else
  cout<<"\t\t\t\tWrong password try again\n\n";
}
  t: cout<<"\t\t\t\tPress 1 to modify a record via book name and\n\t\t\t\t2 to modify via author name :";
  int j=1;
  fflush(stdin);
cin>>j;
  getvalue(j);
fstream f2("G:\\book.dat",ios::in|ios::out|ios::binary);
int x=0;
f2.read((char *)&b1,sizeof(b1));
while(!f2.eof())
{
if(j==1)
{
if(b1.getname()==s.n)
{
char c;
f2.seekp(f2.tellg()-sizeof(b1),ios::beg);
cout<<"\n\t\t\t\tThe record is :-\n";
    b1.admin_display();
b1.input();
cout<<"\t\t\t\tAre you sure to modify previous record with this record (y/n)? ";
fflush(stdin);
cin>>c;
if(c=='y'||c=='Y')
{
f2.write((char*)&b1,sizeof(b1));
x=1;

}
else
{
cout<<"\n\t\t\t\tRecord not modified";
goto start;
}
}
}
else if(j==2)
{
if(b1.geta_name()==s.a)
{
char c;
    f2.seekp(f2.tellg()-sizeof(b1),ios::beg);
    cout<<"\n\t\t\t\tThe record is :-\n";
    b1.admin_display();
b1.input();
cout<<"\t\t\t\tAre you sure to modify previous record with this record (y/n)? ";
fflush(stdin);
cin>>c;
if(c=='y'||c=='Y')
{
f2.write((char*)&b1,sizeof(b1));
x=1;

}
else
{
cout<<"\n\t\t\t\tRecord not modified";
goto start;
}
}
}
f2.read((char *)&b1,sizeof(b1));

}
if(x==0)
{
cout<<"\t\t\t\tRecord not found\n\n";
goto t;
}
f2.close();
}
//Code to display all records
else if(a==5)
        {
            int p;
            cout<<"\t\t\t\tEnter password :";
            cin>>p;
            if(p==3572)
            {
                book b1;
                ifstream f2("G:\\book.dat",ios::in | ios::binary);
                while(f2.read((char *)&b1,sizeof(b1) ))
{
    b1.admin_display();
}
f2.close();
            }
            else
            {
                cout<<"Wrong password try again...\n";
            }
        }
//Code to check profit of any day
else if(a==6)
{
bill b;
char date[20];
int pswrd,pl=0,a_price=0,s_price=0;
while(1)
  {
  cout<<"\t\t\t\tEnter password : ";
  cin>>pswrd;
  if(pswrd==3572)
  break;
  else
  cout<<"\t\t\t\tWrong password try again\n\n";
}
cout<<"\t\t\t\tEnter date (dd/mm/yyyy) :";
fflush(stdin);
cin.getline(date,20);
ifstream fin("G:\\bill2.dat",ios::in | ios::binary);
if(fin.fail())
{
cout<<"\n\t\t\t\tNo record found\n";
goto start;
}
fin.read((char*)&b,sizeof(b));
while(!fin.eof())
{
if(strcmp(b.date,date)==0)
{
a_price+=b.a_price;
s_price+=b.s_price;
}
fin.read((char*)&b,sizeof(b));
}
fin.close();
pl=s_price-a_price;
if(pl<0)
{
pl*=-1;
cout<<"\t\t\t\tLoss : Rs."<<pl;
}
else if(pl>0)
{
cout<<"\t\t\t\tProfit : Rs."<<pl;
}
else if(pl==0)
cout<<"\t\t\t\tNo profit no loss";
}
//Code to exit from program
  else if(a==7)
  break;
  else
  {
  cout<<"\t\t\t\tWrong input\n";
  goto start;
    }
      }
  }

Thursday, 7 April 2016

Swapping by friend function in c++

#include<iostream>
using namespace std;
class alpha;
class beta
{
int a;
public :
void getvalue()
{
cin>>a;
}
void show()
{
cout<< a;
}
friend void swap (alpha &,beta&);
};
class alpha
{
int a;
public:
void getvalue()
{
cin>>a;
}
void show()
{
cout<< a;
}
friend void swap (alpha &,beta&);


};
void swap(alpha &x,beta &y)
{
int temp;
temp=x.a;
x.a=y.a;
y.a=temp;
}
int main()
{
alpha p;
beta q;
p.getvalue();
q.getvalue();
cout<<"Before swapping: ";
p.show();
cout<<" ";
q.show();
swap(p,q);
cout<<"\nAfter swapping: ";
p.show();
cout<<" ";
q.show();
}

Monday, 4 April 2016

Fraction class in c++ with operator overloading


#include<iostream>
using namespace std;
class fraction
{
int num,den,quo;
public:
void input()
{
cin>>num>>den;
}
void display()
{

cout<<num<<"/"<<den<<"\n";
}
void output();

fraction add(fraction);
void simple();
fraction mul(fraction);
fraction sub(fraction);
fraction div(fraction);
fraction large(fraction &);
bool equal(fraction);
};
bool fraction :: equal(fraction n)
{
if(num==n.num&&den==n.den)
return true;
else
return false;
}
fraction fraction :: large (fraction &m)
{
float temp,temp1;
temp=(float)num/den;
temp1=(float)m.num/m.den;
if(temp1>temp)
return m;
else
return *this;
}
void fraction :: output()
{
for(int i=den;i>=2;i--)
{
if(num%i==0 && den%i==0)
{
num=num/i;
den=den/i;
}
}
 
if(den==1)
cout<<num;
else if(num==0)
cout<<"0";
else if(quo!=0)
cout<<quo;
   if(num!=0&&den!=0&&(num/den)!=1&&!(num>den))
cout<<"("<<num<<"/"<<den<<")";
}
fraction  fraction :: add(fraction l)
{
fraction temp;
temp.num=(den*l.num)+(num*l.den);
temp.den=den*l.den;
if(temp.num==temp.den)
{
temp.num=1;
temp.den=1;
}
return(temp);
}
fraction  fraction :: sub(fraction l)
{
fraction temp;
temp.num=(den*l.num)-(num*l.den);
temp.den=den*l.den;
return(temp);
}
fraction fraction :: mul(fraction l)
{
fraction temp;
temp.num=num*l.num;
temp.den=den*l.den;
return temp;
}
fraction fraction :: div(fraction l)
{
fraction temp;
temp.den=num*l.den;
temp.num=den*l.num;
return temp;
}
void fraction :: simple()
{
quo=num/den;
if(num%den!=0)
num=num%den;
if(num==0&&den==0)
{
quo=0;
den=0;
num=0;
}
}
int main()
{
fraction f1,f2,f3,f4,f5,f6,f7;
bool a;
cout<<"Enter 1st fraction :\n";
f1.input();
cout<<"Enter 2nd fraction :\n";
f2.input();
cout<<"The no.'s are :\n";
f1.display();
f2.display();
cout<<"The addition is: ";
f3=f2.add(f1);
f3.simple();
f3.output();
cout<<"\nThe sub. is: ";
f5=f2.sub(f1);
f5.simple();
f5.output();
cout<<"\nThe mul. is: ";
f4=f2.mul(f1);
f4.simple();
f4.output();
cout<<"\nThe div. is: ";
f6=f2.div(f1);
f6.simple();
f6.output();
a=f1.equal(f2);
if(a)
{
cout<<"\nThe fractions are same." ;
}
else
{
f7=f2.large(f1);
cout<<"\nThe larger fraction is :";
f7.display();
}
return 0;
}

Coordinate system conversion by classes

#include<iostream>
#include<math.h>
using namespace std;
class rect;
class polar
{
float r, theta;
public:
polar()
{
}
polar(float a,float b)
{
r=a;
theta=b;
}
void input()
{
cout<<"Enter the polar coordinates :\n";
cout<<"r :";
cin>>r;
cout<<"Theta :";
cin>>theta;
}
void output()
{
cout<<"Polar coordinates are :\n"<<"r :"<<r<<" theta :"<<theta<<" (apxx.)";
}
float getr()
{
return r;
}
float gettheta()
{
return theta;
}
};

class rect
{
float x,y;
public:
rect()
{
}
rect(polar a)
{
  float temp=a.gettheta()/57.3;       //To convert degree to radians
  x=a.getr()*cos(temp);
  y=a.getr()*sin(temp);
}
operator polar()
{
float r,t;
r=sqrt(pow(x,2)+pow(y,2));
t=atan(y/x);
t*=57.3;    //To convert radians into degree as we know 1 radian=57.3 degree
if(x<0&&y<0)  //For 3rd quadrant
t+=180;
if(x<0&&y>0)  //For 2nd quadrant
{
t*=-1;
t+=90;
}
if(y<0&&x>0)  //For 4th quqdrant
{
t*=-1;
t+=270;
}
return polar(r,t);
}
void input()
{
cout<<"Enter the rectangular coordintes :\n";
cout<<"x :";
cin>>x;
cout<<"y :";
cin>>y;
}

void output()
{
cout<<"Rectangular coordinates are :\n"<<"x :"<<x<<" y :"<<y<<" (apxx.)";
}
};
int main()
{
polar a;
rect b;
int c;
cout<<"Enter \n1 Polar to rectangular coordinates\n2 Rectangular to polar coordinates :";
cin>>c;
if(c==1)
{
a.input();
b=a;
b.output();
}
else if(c==2)
{
b.input();
a=b;
a.output();
}
return 0;
}