Showing posts with label Object oriented Programming. Show all posts
Showing posts with label Object oriented Programming. Show all posts

Sunday, 23 October 2016

Generic Vector Using Template

/* AIM : Create a class template to represent a generic vector. Include following
member functions:
1]To create the vector.
2]To modify the value of a given element
3]To multiply by a scalar value
4]To display the vector in the form (10,20,30)
*/

#include<iostream>
using namespace std;

template<class T>
class vector
{
T *v;
int size;
public:
vector(int m) // parameterized constructor to create NULL vector
{
v=new T[size=m];
for(int i=0;i<size;i++)
v[i]=0;
}

void create()
{
for(int i=0;i<size;i++)
{
cout<<"v["<<i<<"] = ";
cin>>v[i];
}
}

void modify()
{
int pos;
cout<<"enter position to make changes :";
cin>>pos;
cout<<"Enter new Value :";
cin>>v[pos];
}

void multiply()
{
T sc;
cout<<"Enter scaler Number to multiply with vector : ";
cin>>sc;
for(int i=0;i<size;i++)
v[i]=v[i]*sc;
}

void display()
{
int i;
cout<<"(";
for(i=0;i<size-1;i++)
{
cout<<v[i]<<",";
}
cout<<v[i]<<")\n";
}


};

int main()
{
int size;
cout<<"enter size of vector: ";
cin>>size;
vector<int> vi(size); //creates int vector
vi.create();
vi.display();
vi.modify();
vi.display();
vi.multiply();
vi.display();

}

Selection Sort Function Template

/*
TITLE:
Write a function template selection Sort. Write a program that inputs, sorts and outputs an integer array and a float array.
*/
#include<iostream>
using namespace std;
template<class T>
class sort
{
public:
T a[50];
int n;
void accept()
{
cout<<"Enter no. of elements:\n";
cin>>n;
cout<<"enter a elements\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void selection_sort()
{
T temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void display()
{
for(int i=0;i<n;i++)
{cout<<a[i]<<"\n";
}
}
};

int main()
{
int v;char ch;
sort<int>s1;
sort<float>s2;

cout<<"*****SELECTION SORT*******\n";
do
{
cout<<"sorting of integer and float array"<<"\n";
cout<<"1.for int array\n";
cout<<"2.for float array\n";
cout<<"enter your choice\n";
cin>>v;
switch(v)
{
case 1:
s1.accept();
cout<<"\nElements are:\n";
s1.display();
s1.selection_sort();
cout<<"\nSorted elements are:\n";
s1.display();
break;
case 2:
s2.accept();
cout<<"\nElements are:\n";
s2.display();
s2.selection_sort();
cout<<"\nSorted elements are:\n";
s2.display();
break;
}
cout<<"\n do you want to continue";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;
}

Addition of Binary numbers using Standard Template Library

/*
TITLE:
Write C++ program using STL to add binary numbers ; use STL stack.
*/
#include<iostream>
#include<stack>
using namespace std;
stack<int> read()
{
stack<int> s;
int x,n,i;
n=4;
cout<<"\nEnter 4 bit binary number : ";
for(i=0;i<n;i++)
{
cin>>x;
s.push(x);
}
return s;
}

void display(stack<int> &s)
{
cout<<" ";
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
}

stack<int> add(stack<int> &s1,stack<int> &s2)
{
stack<int> s;
int sum,carry=0,b1,b2;
while(!s1.empty()||!s2.empty())
{
b1=b2=0;
if(!s1.empty())
{
b1=s1.top();
s1.pop();
}
if(!s2.empty())
{
b2=s2.top();
s2.pop();
}
sum=(b1+b2+carry)%2;
carry=(b1+b2+carry)/2;
s.push(sum);
}
if(carry==1)
s.push(1);
return s;
}

int main()
{
stack<int> s1,s2,s3;
int c;
char ch;
do
{
cout<<"Binary Addition\n";
cout<<"1.accept a numbers(give space after each bit)\n";
cout<<"2.Display addtion of two numbers\n";
cout<<"Enter your choice..:\n";
cin>>c;
switch(c)
{
case 1:
s1=read();
s2=read();
break;
case 2:
cout<<"\nThe result of addition is : ";
s3=add(s1,s2);
display(s3);
break;
}
cout<<"\ndo you want to continue?\n";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;
}

Dequeue Using Standard Template Library

/*
TITLE:Write C++ program using STL for Dequeue (Double ended queue)
*/

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

int main()
{
int v,a;
deque<int>dq;
deque<int>::iterator it;
cout<<"1.insert from front\n";
cout<<"2.insert from back\n";
cout<<"3.delete from front\n";
cout<<"4.delete from back\n";
cout<<"5.front element\n";
cout<<"6.last element\n";
cout<<"7.size of dequeue\n";
cout<<"8.display\n";
cout<<"9.exit\n";
while(1)
{
cout<<"\nenter choice\n";
cin>>v;
switch(v)
{
case 1: cout<<"enter element\n";
cin>>a;
dq.push_front(a);
break;
case 2: cout<<"enter element\n";
cin>>a;
dq.push_back(a);
break;
case 3: a=dq.front();
dq.pop_front();
cout<<"\n"<<a<<" is deleted";
break;
case 4: a=dq.back();
dq.pop_back();
cout<<"\n"<<a<<" is deleted";
break;
case 5: a=dq.front();
cout<<"front element is "<<a<<"\n";
break;
case 6: a=dq.back();
cout<<"last element is "<<a<<"\n";
break;
case 7: cout<<"size of deque is "<<dq.size();
break;
case 8:
for(it=dq.begin();it!=dq.end();it++)
{
cout<<*it<<"\t";
}
break;
case 9:
return 0;
}
}
}

Phonebook using File Handling

****To run this code Succesfully****
create phone.txt file in home directory of ubuntu  and then run this code

#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;

class phoneBook
{
char name[20],phno[15];
public:
void getdata();
void showdata();
 char *getname(){ return name; }
    char *getphno(){ return phno; }
    void update(char *nm,char *telno){
        strcpy(name,nm);
        strcpy(phno,telno);
    }
};

void phoneBook :: getdata(){
    cout<<"\nEnter Name : ";
    cin>>name;
    cout<<"Enter Phone No. : ";
    cin>>phno;
}

void phoneBook :: showdata(){
    cout<<"\n";
    cout<<setw(20)<<name;
    cout<<setw(15)<<phno;
}


int main(){
    phoneBook rec;
    fstream file;
    file.open("phone.txt", ios::ate | ios::in | ios::out | ios::binary );
    char c,ch,nm[20],telno[6];
    int choice,cnt,found=0;
    do{
        cout<<"\n*****Phone Book*****\n";
        cout<<"1) Add New Record\n";
        cout<<"2) Display All Records\n";
        cout<<"3) Search Telephone No.\n";
        cout<<"4) Search Person Name\n";
        cout<<"5) Update Telephone No.\n";
        cout<<"6) Exit\n";
        cout<<"Choose your choice : ";
        cin>>choice;
        switch(choice){
            case 1 : //New Record
                 rec.getdata();
                 file.write((char *) &rec, sizeof(rec));
cout<<"Record Added Succesfully\n";
                 break;

            case 2 : //Display All Records
                 file.seekg(0,ios::beg);
                 cout<<"\n\nRecords in Phone Book\n";
                 while(file){
                    file.read((char *) &rec, sizeof(rec));
                    if(!file.eof())
                        rec.showdata();
                 }
                 file.clear();
break;

            case 3 : //Search Tel. no. when person name is known.
                 cout<<"\n\nEnter Name : ";
                 cin>>nm;
                 file.seekg(0,ios::beg);
                 found=0;
                 while(file.read((char *) &rec, sizeof(rec)))
                 {
                    if(strcmp(nm,rec.getname())==0)
                    {
                        found=1;
                        rec.showdata();
                    }
                 }
                 file.clear();
                 if(found==0)
                    cout<<"\n\n---Record Not found---\n";
                    break;

            case 4 : //Search name on basis of tel. no
                 cout<<"\n\nEnter Telephone No : ";
                 cin>>telno;
                 file.seekg(0,ios::beg);
                 found=0;
                 while(file.read((char *) &rec, sizeof(rec)))
                 {
                    if(strcmp(telno,rec.getphno())==0)
                    {
                        found=1;
                        rec.showdata();
                    }
                 }
                 file.clear();
                 if(found==0)
                    cout<<"\n\n---Record Not found---\n";
                    break;

            case 5 : //Update Telephone No.
                 cout<<"\n\nEnter Name : ";
                 cin>>nm;
                 file.seekg(0,ios::beg);
                 found=0;
                 cnt=0;
                 while(file.read((char *) &rec, sizeof(rec)))
                 {
                    cnt++;
                    if(strcmp(nm,rec.getname())==0)
                    {
                        found=1;
                        break;
                    }
                 }
                 file.clear();
                 if(found==0)
                    cout<<"\n\n---Record Not found---\n";
                 else
                 {
                    int location = (cnt-1) * sizeof(rec);
                    cin.get(ch);
                    if(file.eof())
                        file.clear();

                    cout<<"Enter New Telephone No : ";
                    cin>>telno;
                    file.seekp(location);
                    rec.update(nm,telno);
                    file.write((char *) &rec, sizeof(rec));
                    file.flush();
                 }
                 break;
case 6:
break;
              }
cout<<"do you want to exit(y/n): ";
cin>>c;
    }while(c=='n'||c=='N');

file.close();
}

Friday, 21 October 2016

Book shop (simple)

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//author:rushikesh sanjay palke
//class : SE comp
//roll no: 237
//subject: object oriented programming
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
/*
A book shop maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether it is available or not. If it is not, an appropriate message is displayed. If it is, then the system displays the book details and requests for the number of copies required. If the requested copies book details and requests for the number of copies required. If the   requested   copies   are   available,   the   total   cost   of   the   requested   copies   is displayed; otherwise the message  ― Required copies not in stock ‖  is displayed.
Design a system using a class called books with suitable member functions and Constructors.   Use   new   operator   in   constructors   to   allocate   memory   space required. Implement C++ program for the system.
*/
#include <iostream>
#include<string>
#include<cstring>
#include <iomanip>
using namespace std;
typedef struct b_shop
{
char author[20],title[20],pub[20];
int price,copies;
}b_shop;

class bookshop
{
int sum,count;
b_shop *B;

public:

bookshop()
{
B=new b_shop[1000];
sum=0;count=0;
}

void initial()
{
char ch;

      cout<<"\n\nEnter Author of Book:-";
      cin>>B[count].author;
      cout<<"Enter Title of Book:-";
      cin>>B[count].title;
      cout<<"Enter Publication of Book:-";
      cin>>B[count].pub;
      cout<<"Enter Price of Book:-";
      cin>>B[count].price;
      cout<<"Enter no. of copies of Book:-";
      cin>>B[count].copies;
      sum=sum+B[count].copies*B[count].price;
      count++;
   
   
}



int search()
{
char author[20],title[20];
cout<<"Enter Book title and its Author name respectively to Search in stock\n";
cin>>title>>author;
   for(int i=0;i<count;i++)
   {
    int j=strcmp(B[i].title,title);
    int k=strcmp(B[i].author,author);  
     if(!j&&!k)
      {
            cout<<"\n\nBook is In Stock\n";
            cout<<"It Cost Rs  "<<B[i].price<<endl;
            return i;
       
      }
   
    }
cout<<"\n\nSEARCH IS NOT IN STOCK\n";
return 0;
}

void display()
{
cout<<setw(10)<<"Book Title"<<setw(25)<<"Author of Book"<<setw(25)<<"Publication"<<setw(20)<<"Price"<<setw(10)<<"Copies"<<endl<<endl;
for(int i=0;i<45;i++)
cout<<"=*";
cout<<endl;
for(int i=0;i<count;i++)
{
cout<<setw(10)<<B[i].title<<setw(25)<<B[i].author<<setw(25)<<B[i].pub<<setw(20)<<B[i].price<<setw(10)<<B[i].copies<<endl;
}
}

void stock()
{
cout<<"\n\n\n\tTotal Number of Books in Stock is  "<<count<<endl;
cout<<"\tPurchase Price of Total Stock is  "<<sum<<endl;
}



void purchase()
{
int copies;
 int i=search();
if(i!=0)
{
cout<<"enter no of copies you want to purcase: ";
cin>>copies;
if(B[i].copies>copies)
{
cout<<"copies are available\n";
B[i].copies=B[i].copies-copies;
}
else
cout<<copies<<" are not available\n";
}
}



};


int main()
{
int c;
char ch;
bookshop o;
do
{
cout<<"^^^^^^^^^^^BookStore^^^^^^^^^^^^\n";
cout<<"1]New entry\n";
cout<<"2]Display\n";
cout<<"3]search book\n";
cout<<"4]Purchase book\n";
cout<<"5]stock\n";
cout<<"6]Exit\n";
cin>>c;
switch(c)
{
case 1:
o.initial();
break;
case 2:
o.display();
break;
case 3:
o.search();
break;
case 4:
o.purchase();
break;
case 5:
o.stock();
break;
case 6:
break;
}
cout<<"do you want to continue(y/n): ";
cin>>ch;
}while(ch=='y'||ch=='Y');

return 0;
}

Thursday, 20 October 2016

Exception Handling

/*
CREATE A USER DEFINED EXEPTION TO CHECK THE EXEPTION AND CHECK THE FOLLOWING CONDITIONS AND THROW EXEPTION IF CRITERIA DOES NOT MATCH
A] USER HAS AGE BETWEEN 18 AND 55
B]USER STAYS IN PUNE ,MUMBAI ,BANGLORE ,CHENNAI.
C]USER HAS INCOME BETWEEN 50000 AND 100000
D] USER HAS 4 WHEELER
*/



#include<iostream>
using namespace std;


class sample
{
public:
int age,income;
char ch;
string city;
public:
void getage()
{
cout<<"enter age\n";
cin>>age;
}
void getincome()
{
cout<<"enter income\n";
cin>>income;
}
void getcity()
{
cout<<"enter city\n";
cin>>city;
}
void getveh()
{
cout<<"do you have a 4 wheeler ? (y/n)\n";
cin>>ch;
}
};


int main()
{
sample p;
p.getage();
p.getcity();
p.getincome();
p.getveh();


try
{
if((p.age<18) || (p.age>55))
{
throw p;
}
else
{
cout<<"the age is "<<p.age<<endl;
}
}
catch(sample p)
{
cout<<" age exception caught"<<endl;
}


try
{
if(p.city=="mumbai"||p.city=="pune"||p.city=="delhi"||p.city=="banglore"||p.city=="chennai")
{
cout<<"user stays in the given city"<<endl;
}
else
{
throw p;
}
}
catch(sample p)
{
cout<<"exception for city occured"<<endl;
}


try
{
if((p.income<50000)||(p.income>100000))
{
throw p;
}
else
{
cout<<"the income is "<<p.income<<endl;
}
}
catch(sample p)
{
cout<<"exception for income caught"<<endl;
}


try
{
if(p.ch=='y')
{
cout<<"the user has four wheeler"<<endl;
}
else
{
throw p;
}
}
catch(sample p)
{
cout<<"exception for vehicle cuaght"<<endl;
}
}

student database using Dynamic Memory Allocation

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//author:rushikesh sanjay palke
//class : SE comp
//roll no: 237
//date: 05 july 2016
//subject: object oriented programming
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
/*                          

Develop an object oriented program in C++ to create a database of student information
system containing the following information: Name, Roll number, Class, division, Date of
Birth, Blood group, Contact address, telephone number, driving license no. etc Construct the
database with suitable member functions for initializing and destroying the data viz
constructor, default constructor, Copy constructor, destructor, static member functions, friend
class, this pointer, inline code and dynamic memory allocation operators-new and delete.*/


#include<iostream>
#include<unistd.h>
#include<cstring>
#include<string>
using namespace std;
int cnt=0;
class student
{
public:
int roll_no,age;
long int mob_no;
string b_g,clas,div;
string name,adr;
student()
{
name=" ";
roll_no=0;
clas="SE";
div="A";
adr="PCCOE HOSTEL";
b_g="A+";
mob_no=7777777777;
age=19;
}

void enter(student list[20])
{
char temp[30];
student *ptr=new student();
cout<<"enter name:\n";
cin.clear();
cin.ignore();
cin.getline(temp,30);
ptr->name=temp;
cout<<"enter roll no:\n";
cin>>ptr->roll_no;
cout<<"enter class :\n";
cin>>ptr->clas;
cout<<"enter division:\n";
cin>>ptr->div;
cout<<"enter address:\n";
cin.clear(); //used to clear input buffer
cin.ignore(); //used to clear input buffer
cin.getline(temp,30);
ptr->adr=temp;
cout<<"enter mobile number:\n";
cin>>ptr->mob_no;
cout<<"enter blood group:\n";
cin>>ptr->b_g;
cout<<"enter age:\n";
cin>>ptr->age;
list[cnt]=*ptr;
cnt++;
}
void update(student list[20])
{int r;
cout<<"enter roll no for updation";
cin>>r;
for(int i=0;i<20;i++)
{
if(r==list[i].roll_no)
{cout<<"OLD DATA\n";
cout<<"name: "<<list[i].name<<"\n";
cout<<"age: "<<list[i].age<<"\n";
cout<<"ENTER NEW DATA\n";
cout<<"Name:\n";
cin>>list[i].name;
cout<<"class :\n";
cin>>list[i].clas;
cout<<"division:\n";
cin>>list[i].div;
cout<<"address:\n";
cin>>list[i].adr;
cout<<"mobile number:\n";
cin>>list[i].mob_no;
cout<<"blood group:\n";
cin>>list[i].b_g;
cout<<"age:\n";
cin>>list[i].age;
usleep(30000);
cout<<"data updated succesfully";
}
else
cout<<"No data found\n";
break;
}

}

void display(student list[20])
{int r;
cout<<"enter students roll no";
cin>>r;
for(int i=0;i<20;i++)
{
if(r==list[i].roll_no)
{
cout<<"name: "<<list[i].name<<"\n";
cout<<"age: "<<list[i].age<<"\n";
cout<<"class:"<<list[i].clas<<endl;
cout<<"division:"<<list[i].div<<endl;
cout<<"address:"<<list[i].adr<<endl;
cout<<"mobile number:"<<list[i].mob_no<<endl;
cout<<"blood group:"<<list[i].b_g<<endl;
}
}

}


~student(){};

};

int main()
{
int ch;
student s;
student list[20];
char c;
do
{
cout<<"############STUDENT DATABASE#############\n";
cout<<"1] enter data\n";
cout<<"2] update data\n";
cout<<"3] display data\n";
cout<<"4] delete data\n";
cout<<"5] Exit student database\n";
cout<<"enter your choise:";
cin>>ch;
switch (ch)
{
case 1:
   
      s.enter(list);
      break;
   
case 2:
      s.update(list);
      break;
case 3:
      s.display(list);
      break;
case 4:
      cout<<"deleting memory..............\n";
      s.~student();
      usleep(300000);
      cout<<"memory cleaned\n";
      break;                

}
if(ch==5)
c='n';
else{
cout<<"do you want to continue (y/n)";
cin>>c;
}
}while(c=='y'||c=='Y');

cout<<"Googby Have a nice day.....\n";


}


/*
output:
############STUDENT DATABASE#############
1] enter data
2] update data
3] display data
4] delete data
5] Exit student database
enter your choise:1
enter name:
rushikesh phalke
enter roll no:
32
enter class :
SE
enter division:
B
enter address:
PCCOE Hostel
enter mobile number:
7775910607
enter blood group:
A+
enter age:
19
do you want to continue (y/n)y
############STUDENT DATABASE#############
1] enter data
2] update data
3] display data
4] delete data
5] Exit student database
enter your choise:3
enter students roll no32
name: rushikesh phalke
age: 19
class:SE
division:B
address:PCCOE Hostel
mobile number:7775910607
blood group:A+
do you want to continue (y/n)
*/

calculator using Switch Case

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//developer:rushikesh sanjay palke
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

/*Write a C++ program create a calculator for an arithmetic operator (+,-,*,/).The program should take two operands from user and performs the operation on those two operands  depending upon the operator entered by user.
Use a switch statement to select the operation. Finally, display the result.
Some sample interaction with the program might look like this:
Enter first number, operator, second number: 10 / 3
Answer = 3.333333
Do another (y/n)? y
Enter first number, operator, second number: 12 + 100
Answer = 112
Do another (y/n)? n
*/

# include <iostream>
using namespace std;
class calci
{
char o;
float num1,num2;
public:
void calculator()
{
cout << "Enter first number, operator, second number: ";
cin >>num1>>o>>num2;
cout<<"Answer =";
switch(o) {
case '+':
cout << num1+num2<<endl;
break;
case '-':
cout << num1-num2<<endl;
break;
case '*':
cout << num1*num2<<endl;
break;
case '/':
cout << num1/num2<<endl;
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
cout << "Error! operator is not correct\n";
break;
}
}
};
int main()
{
calci o;
char ch;
do
{
cout<<">>>>>>>>CALCULATOR<<<<<<<<<\n";
o.calculator();
cout<<"do you want to run calculator again (y/n) ";
cin>>ch;
}while(ch=='y');
return 0;
}


/*output:

>>>>>>>>CALCULATOR<<<<<<<<<
Enter first number, operator, second number: 10/3
Answer =3.33333
do you want to run calculator again (y/n) y
>>>>>>>>CALCULATOR<<<<<<<<<
Enter first number, operator, second number: 10+2
Answer =12
do you want to run calculator again (y/n) y
>>>>>>>>CALCULATOR<<<<<<<<<
Enter first number, operator, second number: 1-1
Answer =0
do you want to run calculator again (y/n) y
>>>>>>>>CALCULATOR<<<<<<<<<
Enter first number, operator, second number: 4*1.2
Answer =4.8
do you want to run calculator again (y/n) y
>>>>>>>>CALCULATOR<<<<<<<<<
Enter first number, operator, second number: 4&4
Answer =Error! operator is not correct
do you want to run calculator again (y/n) ^C

*/

Polynomial Operations using Operator overloading

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//developer:rushikesh sanjay palke
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
/*Implement a class Quadratic that represents    two degree polynomials i.e.,  polynomials of type ax2+bx+c.
Your class will require three data member corresponding to a, b and c.
Implement the following operations:
1.A constructor(including a default constructor which creates the 0 polynomial).
2.Overloaded operator+ to add two polynomials of degree 2.
3.Overloaded << and >> to print and read polynomials.
To do this, you will need to decide what you want your input and output format to look like.
4.A function eval that computes the value of a polynomial for a given value of x.
5.A function that computes the two solutions of the equation ax 2 +bx+c=0
*/


#include<iostream>
#include<cmath>
using namespace std;
class polynomial
{ int a,b,c;
public:
polynomial() //default constructor
{
a=0;
b=0;
c=0;

        }
        polynomial(int x,int y,int z) //parameterized constructor
        { a=x;b=y;c=z;    
     
        }


friend istream &operator >> (istream &IN, polynomial &T) //operator >> overloaded
{
IN>>T.a>>T.b>>T.c;
return IN;
}
friend ostream &operator << (ostream &OUT, polynomial &T) //operator << overloaded
{
OUT<<T.a<<"x^2+"<<T.b<<"x+"<<T.c;
return OUT;
}

polynomial operator +(polynomial T) //opeartor + overloaded
{
polynomial R;
R.a=a+T.a;
R.b=b+T.b;
R.c=c+T.c;
return R;
}
void eval(polynomial T,int x) //eval fn to evaluate the polynomial for given value of x
       {int z;
        z=T.a*x*x+T.b*x+T.c;
        cout<<" for x ="<<x<<" is :"<<z<<"\n";
       }
     
       void compute(polynomial T) //compute fn to find roots of polynomial
       {
       float x,y1,y2;
       x= T.b*T.b-4*T.a*T.c;
       if(x>0)
       {
       cout<<"Roots are real & not equal\n";
       y1 = (-T.b+sqrt(x))/(2*T.a);
       y2 = (-T.b-sqrt(x))/(2*T.a);
       cout<<y1<<"\n";
       cout<<y2<<"\n";
       }
       else if(x==0)
       {cout<<"roots are real & equal\n";
       y1= -T.a/(2*T.a);
       cout<<y1<<"\n";
       }
       else if(x<0)
       {
       cout<<"complex Roots\n";

       }


       }
};
int main()
{
int x,ch;
char p;
polynomial s1(5,6,10),s2,s3;
                        do
                        {
                        cout<<"######################MENU##################\n";
                        cout<<"1]display default polynomial\n";
                        cout<<"2]accept & display polynomial\n";
                        cout<<"3]add two polynomial\n";
                        cout<<"4]find f(x) for given x\n";
                        cout<<"5]find roots of give polynomial\n";
                        cout<<"enter your choise\n";
                        cin>>ch;
                        switch(ch)
                        {
                        case 1:
                        cout<<"display stored polynomial\n";
cout<<s1<<endl;
                        break;
                     
                        case 2:
cout<<"accept & display\n";
cout<<"enter the  polynomial :";
cin>>s2;
cout<<s2<<endl;
                        break;
                     
                        case 3:
                        cout<<"addition of two polynomials\n";
                        cout<<"enter the 1st polynomial :";
cin>>s1;
                        cout<<s1<<endl;
                        cout<<"enter the 2nd polynoimial:";
                        cin>>s2;
                        cout<<s2<<endl;
                        s3=s1+s2;
                        cout<<"addition is:";
cout<<s3<<endl;
                        break;
                 
                        case 4:
                        cout<<"calculate f(X) fo hiven x\n";
                        cout<<"enter the polynomial :";
cin>>s1;
cout<<"enter the value for x = ";
cin>>x;
s2.eval(s1,x);
                        break;

                        case 5:
                        cout<<"find roots of polynomial\n";
                        cout<<"enter the polynomial :";
cin>>s2;
                        s2.compute(s2);
                        break;
                        }
                        cout<<"do you want to continue(y/n)";
                        cin>>p;

                       }while(p=='y'||P=='Y');
                cout<<"goodby have a nice day\n";
               return 0;
}

/*
output:

######################MENU##################
1]display default polynomial
2]accept & display polynomial
3]add two polynomial
4]find f(x) for given x
5]find roots of give polynomial
enter your choise
1
display stored polynomial
5x^2+6x+10
do you want to continue(y/n)y
######################MENU##################
1]display default polynomial
2]accept & display polynomial
3]add two polynomial
4]find f(x) for given x
5]find roots of give polynomial
enter your choise
2
accept & display
enter the  polynomial :1 2 3
1x^2+2x+3
do you want to continue(y/n)y
######################MENU##################
1]display default polynomial
2]accept & display polynomial
3]add two polynomial
4]find f(x) for given x
5]find roots of give polynomial
enter your choise
3
addition of two polynomials
enter the 1st polynomial :1 2 3
1x^2+2x+3
enter the 2nd polynoimial:10 20 30
10x^2+20x+30
addition is:11x^2+22x+33do you want to continue(y/n)y
######################MENU##################
1]display default polynomial
2]accept & display polynomial
3]add two polynomial
4]find f(x) for given x
5]find roots of give polynomial
enter your choise
4
calculate f(X) fo hiven x
enter the polynomial :1 2 3
enter thevalue for x = 1
 for x =1 is :6
do you want to continue(y/n)y
######################MENU##################
1]display default polynomial
2]accept & display polynomial
3]add two polynomial
4]find f(x) for given x
5]find roots of give polynomial
enter your choise
5
find roots of polynomial
enter the polynomial :1 2 3
complex Roots
do you want to continue(y/n)n

*/

Complex number Operations using Operator Overloading

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//author:rushikesh sanjay palke
//subject: object oriented programming
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
/*                                        
Implement a class Complex which represents the Complex Number data type. Implement the following operations:
1. A constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers.
To do this, you will need to decide what you want your input and output format to look like.
*/

#include<iostream>
using namespace std;
class complex
{
float  real,imag;
public:
complex(float x1,float x2)//parametrrised consructor
{real=x1;
imag=x2;}

complex()//default consructor
{real=imag=0;}

friend istream &operator>>(istream &in,complex &t) //operator >> overloaded
{
in>>t.real>>t.imag;
return in;
}

complex operator+(complex t) //operator + overloaded
{
complex z;
z.real=this->real+t.real;
z.imag=this->imag+t.imag;
return z;
}

complex operator*(complex t) //operator * overloaded
{
complex z;
z.real=this->real*t.real-this->imag*t.imag;
z.imag=this->imag*t.real+this->real*t.imag;
return z;
}


friend ostream &operator<<(ostream &op,complex &t) //operator << overloaded
{
op<<t.real<<"+i"<<t.imag;
return op;
}

};
int main()
{complex c1/*creates complex no 0+i0(default)*/,c2(3,5)/*creates complex no 3+i5(parameterized)*/,c3,c4;
int c;
char ch;
do
{
cout<<"%%%%%%%%%%%%%%polynimial operations%%%%%%%%%%%%%\n";
cout<<"1]addition of two complex no\n";
cout<<"2]multiplication of two complex no\n";
cout<<"enter your choise :";
cin>>c;
switch(c)
{
case 1:
cout<<"enter real & img  part of complex no. 1\n";
cin>>c1;
cout<<"enter real & img  part of complex no. 2\n";
cin>>c2;
c3=c1+c2;
cout<<"addition is : ";
cout<<c3<<"\n";
break;

case 2:
cout<<"enter real & img  part of complex no. 1\n";
cin>>c1;
cout<<"enter real & img  part of complex no. 2\n";
cin>>c2;
cout<<"multiplication is :";
c4=c1*c2;
cout<<c4<<"\n";
break;
}
cout<<"do you want to continue";
cin>>ch;
}while(ch=='y'||ch=='Y');

}

/*output :
*%%%%%%%%%%%%%%polynimial operations%%%%%%%%%%%%%
1]addition of two complex no
2]multiplication of two complex no
enter your choise :1
enter real & img  part of complex no. 1
1
2
enter real & img  part of complex no. 2
4
5
addition is : 5+i7
do you want to continue: y
%%%%%%%%%%%%%%polynimial operations%%%%%%%%%%%%%
1]addition of two complex no
2]multiplication of two complex no
enter your choise :2
enter real & img  part of complex no. 1
1
2
enter real & img  part of complex no. 2
4 5
multiplication is :-6+i13
do you want to continue: n
rushikeshsp25@rushikeshsp25-Aspire-E1-571:~$
/