Wednesday, 24 August 2016

Create a class Rational Number (fractions) with the following capabilities: a) Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators. b) Overload the addition, subtraction, multiplication and division operators for this class. c) Overload the relational and equality operators for this class.

#include<iostream>
using namespace std;
class a
{
    int i,n,d,sn,sd,cond;
public:
    void getdata()
    {
        cout<<"Enter the numerator"<<endl;
        cin>>n;
        cout<<"Enter the denominator"<<endl;
        cin>>d;
    }
    void operator+(a a1)
    {
        if(a1.d!=d)
        {
        sn=(a1.n*d)+(n*a1.d);
        sd=(a1.d)*(d);
        }
        else
        {
            sn=a1.n+n;
            sd=a1.d;
        }
        if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
    }
    void sum()
    {

        for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
            }
        }

        cout<<"Sum is "<<sn<<"/"<<sd<<endl;
    }

    a operator-(a a1)
    {
        a a3;
         if(a1.d!=d)
        {
        a3.sn=(a1.n*d)-(n*a1.d);
        a3.sd=(a1.d)*(d);
        }
        else
        {
            a3.sn=a1.n-n;
            a3.sd=a1.d;
        }
        return a3;


    }
    void sub()
    {
        if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
         for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
          }
        }
        cout<<"Subtraction is "<<sn<<"/"<<sd<<endl;
    }
    a operator /(a a1)
    {
        a a4;
        a4.sn=a1.n*d;
        a4.sd=a1.d*n;
        return a4;
    }
    void div()
    {
         if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
        else if(sn==sd)
        {
            sn=1;
            sd=1;
        }
         for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
            }

    }

    cout<<"Division is "<<sn<<"/"<<sd<<endl;
    }

    a operator*(a a1)
    {
        a a5;
        a5.sn=a1.n*n;
        a5.sd=a1.d*d;
        return a5;

    }

    void mul()
    {
        if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
         for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
          }
        }
        cout<<"Multiplication - "<<sn<<"/"<<sd<<endl;
    }

};
int main()
{
    a a1,a2,a3,a4,a5,a6;
    a1.getdata();
    a2.getdata();

    a2+(a1);
    a2.sum();

    a3=a2-(a1);
    a3.sub();

    a4=a2/(a1);
    a4.div();

    a5=a2*(a1);
    a5.mul();

    return 0;
}

Create a class template to represent a generic vector. Include following member functions:  To create the vector.  To modify the value of a given element  To multiply by a scalar value  To display the vector in the form (10,20,30,…)

#include<iostream>
#include<vector>
using namespace std;
void display(vector<int> &v)
{
    for(int i=0;i<v.size();i++)
    {
        cout<<v[i]<<" ";
    }
    cout<<"\n";
}

int main()
{
    vector<int> v;
    cout<<"\n initial size="<<v.size()<<" \n ";

    int x;
    cout<<"\n enter values ";
    for(int i=0;i<5;i++)
    {
        cin>>x;
        v.push_back(x);
    }
    cout<<"\n\n size after inserting 5 values  "<<v.size();

    cout<<"\n\n current contents ";
    display(v);

    v.push_back(2);

    cout<<"\n\n size after push_back()= "<<v.size()<<"\n";
    cout<<"\n\n now the contents of vector after push_back()";
    display(v);

    cout<<"\n\n iterator created and inserted element at 3rd position ";
    vector<int>::iterator itr1=v.begin();
    itr1=itr1+2;
    v.insert(itr1,1,6);

    cout<<"\n\n contents of vector after insrting ";
    display(v);
    cout<<"\n\n removing elements 3rd and 4th position";
    //v.erase(v.begin()+2,v.begin()+6);
    v.erase(v.begin()+2,v.begin()+4);
    cout<<"\n\n contents after deletion ";
    display(v);
    cout<<"\n\n";
    return 0;

   }


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 licence 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<string.h>
#include<iomanip>

using namespace std;
class db
{
int roll;
char name[20];
char Class[10];
char Div[10];
char dob[10];
  char bg[3],contact[10];
  char phone[10],license[12];

  public:
  static int stdno;
  static void count()
  {
    cout<<"\nNo. of objects created: "<<stdno;
  }
void fin(){cout<<"\nInline Function!";}

  db()
  {
   roll=0;
   strcpy(name,"Sachin");
   strcpy(Class,"I");
   strcpy(Div,"A");
    strcpy(dob,"11/11/1111");
    strcpy(bg,"A");
    strcpy(contact,"city");
    strcpy(phone,"9000000000");
    strcpy(license,"A0101010");
    ++stdno;
  }
  db(db *ob)
  {
            strcpy(name,ob->name);
    strcpy(dob,ob->dob);
    strcpy(Class,ob->Class);
   strcpy(Div,ob->Div);
    strcpy(bg,ob->bg);
    strcpy(contact,ob->contact);
    strcpy(phone,ob->phone);
    strcpy(license,ob->license);
    ++stdno;
  }
void getdata()
  {
     cout<<"\n\nEnter:name,roll,Class,Div,Dob,bg,contact,phone,license \n\n\n";
     cin>>name>>roll>>Class>>Div>>dob>>bg>>contact>>phone>>license;
  }
  friend void display(db d);
  ~db()
  {
    cout<<"\n\n"<<this->name<<"(Object) is destroyed!";
  }
};

void display(db d)
{
cout<<"\n"<<setw(12)<<d.name<<setw(5)<<d.roll<<setw(4)<<d.Class<<setw(3)<<d.Div<<setw(12)<<d.dob<<setw(4)<<d.bg<<setw(12)<<d.contact<<" "<<setw(12)<<d.phone<<" "<<setw(12)<<" "<<d.license;
}
int db::stdno;

int main()
{
    int n,i;
    db d1,*ptr[5];
    cout<<"\nDefault values:";
    display(d1);

    d1.getdata();
    display(d1);

    db d2(&d1);
    cout<<"\n\nUse of copy constructor :\n";
    display(d2);


    cout<<"\nHow many objects u want to create?:";
    cin>>n;
    for(i=0;i<n;i++)
    {
    ptr[i]=new db();
    ptr[i]->getdata();
    }
    cout<<"\n"<<setw(12)<<"name"<<setw(5)<<"roll"<<setw(4)<<"Class"<<setw(4)<<"Div"<<setw(12)<<"dob"<<setw(4)<<"bg"<<setw(12)<<"contact"<<setw(12)<<"phone"<<setw(12)<<"license";
   for(i=0;i<n;i++)
     display(*ptr[i]);
   db::count();
   for(i=0;i<n;i++)
   {
     delete(ptr[i]);
   }
   cout<<"\nObjects deleted!" ;
   return 0;
}

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.

#include <iostream>
using namespace std;
int main()
{
    char o,ch;
    float num1,num2;
    do
    {
    cout << "Enter operator either + or - or * or /: ";
    cin >> o;
    cout << "Enter two operands: ";
    cin >> num1 >> num2;
    switch(o)
    {
        case '+':
            cout << num1+num2;
            break;
        case '-':
            cout << num1-num2;
            break;
        case '*':
            cout << num1*num2;
            break;
        case '/':
            cout << num1/num2;
            break;
        default:
            /* If operator is other than +, -, * or /, error message is shown */
            cout << "Error! operator is not correct";
            break;
    }
    cout<<"\n\n do you want to Continue?";
    cin>>ch;
    }while(ch!='n' && ch!='N');
    return 0;
}

Write a function template selection Sort. Write a program that inputs, sorts and outputs an int array and a float array.

#include<iostream>
using namespace std;
template<class T>
void selection(T a[], int n)
{
int i, j;
for (j = 0; j < n-1; j++)
{
    int iMin = j;
    for ( i = j+1; i < n; i++)
{
     if (a[i] < a[iMin])
     {
              iMin = i;
          }
    }
    if(iMin != j) {
        swap(a[j], a[iMin]);
    }
}
}
int main()
{
int a[6]={1,2,6,9,4,7};
char b[4]={'s','b','d','e'};
float c[6]={10.2,5.0,6.3,5.2,1.5,1.56};
selection(a,6);
cout<<"\nSorted Order Integers: ";
for(int i=0;i<6;i++)
cout<<"\n"<<a[i]<<"\t";
selection(b,4);
cout<<"\nSorted Order Characters: ";
for(int j=0;j<4;j++)
    cout<<"\n"<<b[j]<<"\t";
cout<<"\nSorted Order float: ";
selection(c,6);
for(int j=0;j<6;j++)
cout<<"\n"<<c[j]<<"\t";
return 0;
}

Using standard template library (STL) list container implement following member functions of list class: empty, insert, merge, reverse, sort, Unique, using iterator

#include<iostream>
#include<list>
#include<cstdlib>
using namespace std;
void display(list <int> &lst)
{
list<int> :: iterator p;
for(p=lst.begin();p!=lst.end();++p)
cout<<*p<<",";
cout<<"\n\n";
}
int main()
{
    list<int> list1;
list<int> list2(5);
for(int i=0;i<3;i++)
list1.push_back(rand()/100);
list<int>::iterator p;
    for(p=list2.begin();p!=list2.end();++p)
*p=rand()/100;
cout<<"list1\n";
display(list1);

cout<<"list2\n";
display(list2);

list1.push_front(100);
list1.push_back(200);

list2.pop_front();

cout<<" Modified list1\n";
display(list1);

cout<<"Modified list2\n";
display(list2);

list<int>listA,listB;
listA=list1;
listB=list2;

list1.merge(list2);

cout<<"merged unsorted list";
display(list1);


listA.sort();
listB.sort();
listA.merge(listB);
cout<<"Merged sorted list\n";
display(listA);


listA.reverse();
cout<<"Reversed sorted list\n";
display(listA);
return 0;
}

Crete User defined exception to check the following conditions and throw the exception if the criterion does not met.

Crete User defined exception to check the following conditions and throw the exception if the criterion does not met. a. User has age between 18 and 55 b. User stays has income between Rs. 50,000 – Rs. 1,00,000 per month c. User stays in Pune/ Mumbai/ Bangalore / Chennai d. User has 4-wheeler Accept age, Income, City, Vehicle from the user and check for the conditions mentioned above. If any of the condition not met then throw the exception.

/*
#include<iostream>
#include<string.h>
using namespace std;
class exception_handling
{
    int age,income;
    char city[20];
    char Isvehicle;
    public:
    friend istream &operator >>(istream &din,exception_handling &e)
    {
        cout<<"\n\n Enter age of person::";
        cin>>e.age;
        cout<<"\n\n Enter Income::";
        cin>>e.income;
        cout<<"\n\n Enter City ::";
        cin>>e.city;
        cout<<"\n is Vehicle(Y/N)";
        cin>>e.Isvehicle;
        return(din);
    }
    friend ostream &operator <<(ostream &dout,exception_handling &e)
    {
        try
        {
            if(e.age<18 || e.age>55)
            throw(1);
            if(e.income<50000 || e.income>100000)
            throw(2);
            if((e.city!="PUNE" || e.city!="Pune" || e.city!="pune") && (e.city!="MUMBAI" || e.city!="Mumbai" || e.city!="mumbai") && (e.city!="CHENNAI" || e.city!="Chennai" || e.city!="chennai")&& (e.city!="Bangelore" || e.city!="BANGELORE" || e.city!="bangelore"))
            throw(3);
            if(e.Isvehicle=='n' || e.Isvehicle=='N')
            throw(4);

        }
            catch(int ex)
            {
                switch(ex)
                {
                    case 1:
                        cout<<"\n Exception Caught";
                        cout<<"\n User's age should be between 18 and 55!!!";
                    break;
                    case 2:
                        cout<<"\n Exception Caught";
                        cout<<"\n User should have income between Rs.50,000-Rs.1,00,000 per month!!!";
                    break;
                    case 3:
                        cout<<"\n Exception Caught";
                        cout<<"\n User should be from Pune/ Mumbai/ Bangalore / Chennai!!!";
                    break;
                    case 4:
                        cout<<"\n Exception Cauht";
                        cout<<"\n User should have 4-wheeler!!!";
                    break;
                }
            }
            return(dout);
        }

};
int main()
{
    int ch;
    exception_handling e1;
    cout<<"\n\t============Exception Handling===============\n";
    do
    {
        cout<<"\n\n\t 1.Insert data\n\t Display Data \n\t 3.Exit\n";
        cout<<"Enter your choice";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cin>>e1;
            break;
            case 2:
                cout<<e1;
            break;
            case 3:
            break;
        default:
            cout<<"Enter correct choice";
        }
    }while(ch!=3);
    return 0;
}
*/