Tuesday, 18 October 2016

5exceptionB1_1476775079740


#include <iostream>
#include <string.h>
using namespace std;
class data
{
    int age;
    string city;
    double income;
    char fw;
    int c=0;
public:
    void getage()
    {
        try
        {
            cout<<"Enter age:"<<endl;
            cin>>age;
            if(age<18||age>55)
                throw(age);
            else
                c++;
        }
        catch(int i)
        {
            cout<<"Exception caught: Age should be between 18-55 years"<<endl;
        }
    }
    void getcity()
    {
        try
        {
            cout<<"Enter the city you live in:"<<endl;
            cin>>city;
            if(city=="Pune"||city=="Mumbai"||city=="Bangalore"||city=="Chennai")
                c++;
            else
                throw(city);
        }
        catch(string s)
        {
            cout<<"Exception caught: City should be either Pune, Mumbai, Bangalore or Chennai"<<endl;
        }
    }
    void getincome()
    {
        try
        {
            cout<<"Enter monthly income:"<<endl;
            cin>>income;
            if(income<50000||income>100000)
                throw(income);
            else
                c++;
        }
        catch(double d)
        {
            cout<<"Exception caught:Income should be between Rs. 50,000- Rs. 1,00,000"<<endl;
        }
    }
    void fourw()
    {
        try
        {
            cout<<"Do you have a four wheeler? Y or N"<<endl;
            cin>>fw;
            if(fw=='y'||fw=='Y')
                c++;
            else
                throw(fw);
        }
        catch(char w)
        {
            cout<<"Exception caught: 4 wheeler not present"<<endl;
        }
    }
    void print()
    {
        if(c==4)
            cout<<"No incorrect data provided, thenks";
    }
};
int main()
{
    data obj;
    obj.getage();
    obj.getincome();
    obj.getcity();
    obj.fourw();
    obj.print();
    return 0;
}

/*Output:
[student@localhost ~]$ g++ age.cpp
[student@localhost ~]$ ./a.out
Enter age:
22
Enter monthly income:
15000
Exception caught:Income should be between Rs. 50,000- Rs. 1,00,000
Enter the city you live in:
Pune
Do you have a four wheeler? Y or N
N
Exception caught: 4 wheeler not present
[student@localhost ~]$ ./a.out
Enter age:
22
Enter monthly income:
15000
Exception caught:Income should be between Rs. 50,000- Rs. 1,00,000
Enter the city you live in:
Pune
Do you have a four wheeler? Y or N
Y
[student@localhost ~]$ /*

searching and sorting of detail_1476775079695



/*Write C++ program using STL for Sorting and searching with user-defined records such as Person Record (Name, birth date, telephone no), item record (item code, item name, quantity and cost)*/    


#include <iostream>
#include<list>
using namespace std;
class record
{
    list<string>name,dob,phone,ni;
    list<string>::iterator it1,it2,it3,j,k,l,c,n;
    list<string>code;
    list<int>number;
    list<float>cost;
    list<int>::iterator no,j1;
    list<float>::iterator f,i;
public:
    void getp();
    void display();
    void searchp(string);
    void sortp();
    void checkempty();
    void getlist();
    void displayit();
    void searchlist();
    void sortitem();

};
void record::getp()
{
    int count;
    string n,d,p;
    cout<<"Enter the number of members in record:"<<endl;
    cin>>count;
    for(int i=1;i<=count;i++)
    {
        cout<<"Enter name:"<<endl;
        cin>>n;
        name.push_back(n);
        cout<<"Enter date of birth:"<<endl;
        cin>>d;
        dob.push_back(d);
        cout<<"Enter phone number:"<<endl;
        cin>>p;
        phone.push_back(p);
    }
}
void record::searchp(string data)
{
    int flag=0;
    it1=name.begin();
    it2=dob.begin();
    it3=phone.begin();
    while(it1!=name.end()&&it2!=dob.end()&&it3!=phone.end())
    {
        if(*it1==data)
        {
            cout<<"Record found!"<<endl;
            cout<<"Corresponding D.O.B: "<<*it2<<endl;
            cout<<"Corresponding phone number: "<<*it3<<endl;
            flag=1;
            break;
        }
        if(*it2==data)
        {
            cout<<"Record found!"<<endl;
            cout<<"Corresponding name "<<*it1<<endl;
            cout<<"Corresponding phone number: "<<*it3<<endl;
            flag=1;
            break;
        }
        if(*it3==data)
        {
            cout<<"Record found!"<<endl;
            cout<<"Corresponding name: "<<*it1<<endl;
            cout<<"Corresponding D.O.B: "<<*it2<<endl;
            flag=1;
            break;
        }
        it1++;
        it2++;
        it3++;
    }
    if(flag==0)
        cout<<"Record not found."<<endl;
}
void record:: display()
{
    it1=name.begin();
    it2=dob.begin();
    it3=phone.begin();
    while(it1!=name.end())
    {
        cout<<*it1<<"\t"<<*it2<<"\t"<<*it3<<endl;
        it1++;
        it2++;
        it3++;
    }
}
void record::sortp()
{
    string temp;
    it1=name.begin();
    it2=dob.begin();
    it3=phone.begin();
    j=it1;
    k=it2;
    l=it3;
    j++;
    k++;
    l++;
    while(it1!=name.end())
    {
        while(j!=name.end())
        {
            if(*it1>*j)
            {
                temp=*it1;
                *it1=*j;
                *j=temp;
                temp=*it2;
                *it2=*k;
                *k=temp;
                temp=*it3;
                *it3=*l;
                *l=temp;
            }
            j++;
            k++;
            l++;
        }
        it1++;
        it2++;
        it3++;
    }
}
void record::getlist()
{
    cout<<"Enter the number of items:"<<endl;
    int c,no;
    string n;
    float f;
    cin>>c;
    for(int i=1;i<=c;i++)
    {
        cout<<"Enter item name:"<<endl;
        cin>>n;
        ni.push_back(n);
        cout<<"Enter item code:"<<endl;
        cin>>n;
        code.push_back(n);
        cout<<"Enter cost:"<<endl;
        cin>>f;
        cost.push_back(f);
        cout<<"Enter the quantity:"<<endl;
        cin>>no;
        number.push_back(no);
    }
}
void record::displayit()
{
    c=code.begin();
    n=ni.begin();
    no=number.begin();
    f=cost.begin();
    while(c!=code.end())
    {
        cout<<*c<<"\t"<<*n<<"\t"<<*no<<"\t"<<*f<<endl;
        c++;
        n++;
        no++;
        f++;
    }
}
void record::sortitem()
{
    string temp;
    int tempno;
    float tempf;
    c=code.begin();
    n=ni.begin();
    no=number.begin();
    f=cost.begin();
    i=f;
    j1=no;
    k=c;
    l=n;
    i++;
    j1++;
    k++;
    l++;
    while(f!=cost.end())
    {
        while(i!=cost.end())
        {
            if(*f>*i)
            {
               tempf=*f;
               *f=*i;
               *i=tempf;

               temp=*n;
               *n=*l;
               *l=temp;

               temp=*c;
               *c=*k;
               *k=temp;

               tempno=*no;
               *no=*j1;
               *j1=tempno;
            }
            i++;
            j1++;
            k++;
            l++;
        }
        f++;
        n++;
        no++;
        c++;
    }
}
void record::searchlist()
{
    string key;
    cout<<"Enter the item code:"<<endl;
    cin>>key;
    c=code.begin();
    n=ni.begin();
    no=number.begin();
    f=cost.begin();
    while(c!=code.end())
    {
        if(key==*c)
        {
            cout<<"Item available!"<<endl;
            cout<<"Item name: "<<*n<<endl;
            cout<<"Item quantity: "<<*no<<endl;
            cout<<"Item cost: "<<*f<<endl;
        }
        c++;
        n++;
        no++;
        f++;
    }
}
int main()
{
    record obj;
    string key;
    int ch,chr;
    char x='y';
    do
    {
    cout<<"1. Personal record\n2. Item record\nEnter choice:\n";
    cin>>ch;
    do
    {
    if(ch==1)
    {
        cout<<"1. Enter details\n2. Display\n3. Search entry\n4. Sort records\nEnter choice\n";
        cin>>chr;
        switch(chr)
        {
        case 1:
            obj.getp();
            obj.display();
            break;
        case 2:
            obj.display();
            break;
        case 3:
            cout<<"Enter either name, d.o.b or phone number you want to find\n";
            cin>>key;
            obj.searchp(key);
            break;
        case 4:
            obj.sortp();
            obj.display();
            break;
        default:
            cout<<"Wrong choice"<<endl;
        }
    }
    else if(ch==2)
    {
        cout<<"1. Enter details\n2. Display\n3. Search entry\n4. Sort records\nEnter choice\n";
        cin>>chr;
        switch(chr)
        {
        case 1:
            obj.getlist();
            obj.displayit();
            break;
        case 2:
            obj.displayit();
            break;
        case 3:
            obj.searchlist();
            break;
        case 4:
            obj.sortitem();
            obj.displayit();
            break;
        default:
            cout<<"Wrong choice"<<endl;
        }
    }
    else
    {
        cout<<"Wrong choice"<<endl;
        break;
    }
    cout<<"Do you wish to continue? Y or N\n";
    cin>>x;
    }while(x=='y'||x=='Y');
    cout<<"Do you wish to select another type of record? Y or N\n";
    cin>>x;
    }while(x=='y'||x=='Y');
    return 0;
}

/*Output:
[student@localhost ~]$ g++ C3.cpp
[student@localhost ~]$ ./a.out
1. Personal record
2. Item record
Enter choice:
1
1. Enter details
2. Display
3. Search entry
4. Sort records
Enter choice
1
Enter the number of members in record:
2
Enter name:
abc
Enter date of birth:
5/6/1993
Enter phone number:
22556
Enter name:
pqr
Enter date of birth:
9/7/1993
Enter phone number:
22076
abc    5/6/1993    22556
pqr    9/7/1993    22076
Do you wish to continue? Y or N
y
1. Enter details
2. Display
3. Search entry
4. Sort records
Enter choice
2
abc    5/6/1993    22556
pqr    9/7/1993    22076
Do you wish to continue? Y or N
y
1. Enter details
2. Display
3. Search entry
4. Sort records
Enter choice
3
Enter either name, d.o.b or phone number you want to find
abc
Record found!
Corresponding D.O.B: 5/6/1993
Corresponding phone number: 22556
Do you wish to continue? Y or N
y
1. Enter details
2. Display
3. Search entry
4. Sort records
Enter choice
4
abc    5/6/1993    22556
pqr    9/7/1993    22076
Do you wish to continue? Y or N
n
Do you wish to select another type of record? Y or N
n
[student@localhost ~]$ */

calculatorA3_1476775079738


\\Program for Calculator for arithmatic operation.
#include<iostream>
using namespace std;
class Calculator
{
   private:
   float num1,num2,result;
   char op;
   public:
   void get();
   void calculate();
};
void Calculator::get()
{
   cout<<"\n Enter first number,operator & second number";
   cin>>num1;
   cin>>op;
   cin>>num2;
}
void Calculator::calculate()
{
   switch(op)
   {
      case '+':
      result=num1+num2;
      break;  
      case '-':
      result=num1-num2;
      break;  
      case '*':
      result=num1*num2;
      break;  
      case '/':
      if(num2==0)
      cout<<"\n Error. Not valid.";
      result=num1/num2;
      break;
   }
   cout<<" "<<num1<<" "<<op<<" "<<num2<<" = "<<result;
}
int main()
{
   char ag;
   Calculator obj;
 x:obj.get();
   obj.calculate();
   cout<<"\n Do you want to perform another operation? y/n";
   cin>>ag;
   if(ag=='y'||ag=='Y')
   goto x;
   return 0;
}      
   
Output:
[student@localhost ~]$ g++ Calculator1.cpp
[student@localhost ~]$ ./a.out

 Enter first number,operator & second number 5+2
 5 + 2 = 7
 Do you want to perform another operation? y/n y

 Enter first number,operator & second number 7-3
 7 - 3 = 4
 Do you want to perform another operation? y/n y

 Enter first number,operator & second number 90*5
 90 * 5 = 450
 Do you want to perform another operation? y/n y

 Enter first number,operator & second number 60/20
 60 / 20 = 3
 Do you want to perform another operation? y/n n
[student@localhost ~]$
 

11dqueuestl_1476775079724

//  Write C++ program using STL for Dqueue (Double ended queue)

#include<iostream>
#include<string.h>
#include<deque>
using namespace std;

class Dequeue
{
    public :
        int a;
        deque <int> s;
        deque <int> :: iterator itr;


        void push_front()
        {
            cout<<"\n Enter a number : ";
                cin>>a;
                s.push_front(a);
        }

        void push_back()
        {
             cout<<"\n Enter a number : ";
             cin>>a;
             s.push_back(a);
        }

        void pop_back()
        {
            itr=s.end();
            itr--;
            s.pop_back();
            cout<<"\n The element popped out of the queue is "<<*itr;
        }

        void pop_front()
        {
            itr=s.begin();
            s.pop_front();
            cout<<"\n The element popped out of the queue is "<<*itr;
        }

        void displayqueue()
        {
                cout<<"\n The elements in the queue are : "<<"\n";
                for(itr=s.begin() ; itr!=s.end() ; itr++)
            {
                cout<<*itr<<"\t";
            }
        }

};

int main()
{
    Dequeue p;
    int choice;
    char ans;
    do
    {
        cout<<"\n 1. Add element from front \n 2. Add element from behind \n 3. Delete element front front \n 4. Delete element from behind \n 5. Display dequeue elements";
        cout<<"\n Enter the operation you want to perform : ";
        cin>>choice;
        switch(choice)
        {
            case 1 : p.push_front();
            break;
            case 2 : p.push_back();
            break;
            case 3 : p.pop_front();
            break;
            case 4 : p.pop_back();
            break;
            case 5 : p.displayqueue();
            break;
        }
        cout<<"\n Do you want to perform any other operation ?";
        cin>>ans;
    }while(ans=='Y' || ans=='y');
}

/*Output:
[student@localhost ~]$ g++ C2deque.cpp
[student@localhost ~]$ ./a.out

 1. Add element from front
 2. Add element from behind
 3. Delete element front front
 4. Delete element from behind
 5. Display queue elements
 Enter the operation you want to perform : 1

 Enter a number : 5

 Do you want to perform any other operation ? y

 1. Add element from front
 2. Add element from behind
 3. Delete element front front
 4. Delete element from behind
 5. Display queue elements
 Enter the operation you want to perform : 5

 The elements in the queue are :
5   
 Do you want to perform any other operation ? y

 1. Add element from front
 2. Add element from behind
 3. Delete element front front
 4. Delete element from behind
 5. Display queue elements
 Enter the operation you want to perform : 2

 Enter a number : 6

 Do you want to perform any other operation ? y

 1. Add element from front
 2. Add element from behind
 3. Delete element front front
 4. Delete element from behind
 5. Display queue elements
 Enter the operation you want to perform : 5

 The elements in the queue are :
5    6   
 Do you want to perform any other operation ? y

 1. Add element from front
 2. Add element from behind
 3. Delete element front front
 4. Delete element from behind
 5. Display queue elements
 Enter the operation you want to perform : 3

 The element popped out of the queue is 5
 Do you want to perform any other operation ? y

 1. Add element from front
 2. Add element from behind
 3. Delete element front front
 4. Delete element from behind
 5. Display queue elements
 Enter the operation you want to perform : 4

 The element popped out of the queue is 6
 Do you want to perform any other operation ?n
[student@localhost ~]$ */

10binaryadditionc2_1476775079730

/*write c++ program using STL to add binary numbers(assume one bit as one number);use STL stack*/
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int i;
stack<int> A;
stack<int> B;
stack<int> C;
char First_number[5],Second_number[5];
int a=0,b=0;
cout<<"\n Enter the First binary number";
cin>>First_number;
for(i=0;First_number[i]!='\0';i++)
{
if(First_number[i]=='0')
A.push(0);
else
A.push(1);
}
//A stack stores first binary number
cout<<"\nEnter the Second binary number";
cin>>Second_number;
for(i=0;Second_number[i]!='\0';i++)
{
if(Second_number[i]=='0')
B.push(0);
else
B.push(1);
}
//B satck stores second binary number
int carry=0;
while(!A.empty()||!B.empty())
{
a=0;b=0;
if(!A.empty())//length of first number is larger than second
{
a=A.top();
A.pop();
}
if(!B.empty())//length of second number is larger than first
{
b=B.top();
B.pop();
}
int sum=carry+a+b;//performing addition
C.push(sum%2);//pushing the result onto the third stack
carry=sum/2;//setting carry
}
if(carry==1)//if carry is there then
C.push(carry);//it is pushed at the end onto the stack
cout<<"\nAddition of two binary numbers is...";
while(!C.empty())//C stack is popped to display result
{
cout<<C.top();
C.pop();
}
cout<<"\n";
return 0;
}



/*output
[student@localhost ~]$ g++ assignment10.cpp
[student@localhost ~]$ ./a.out

 Enter the First binary number1101

Enter the Second binary number1100

Addition of two binary numbers is...11001 */



9stlstackc1_1476775079741


//  Write C++ program using STL for implementation of stack using SLL

#include<iostream>
#include<string.h>
#include<list>
using namespace std;

class Stack
{
    public :
        int a;
        list <int> s;
        list <int> :: iterator itr;


        void push()
        {
            cout<<"\n Enter a number : ";
                cin>>a;
                s.push_back(a);
        }

        void displaystack()
        {
            cout<<"\n The elements in the stack are : "<<"\n";
            for(itr=s.begin() ; itr!=s.end() ; itr++)
            {
                cout<<*itr<<"\t";
            }
        }

        void pop()
        {
            itr=s.end();
            itr--;
            s.pop_back();
            cout<<"\n The element popped out of the stack is "<<*itr;
        }

};

int main()
{
    Stack p;
    int choice;
    char ans;
    do
    {
        cout<<"\n 1. Add element \n 2. Delete element \n 3. Display stack elements";
        cout<<"\n Enter the operation you want to perform : ";
        cin>>choice;
        switch(choice)
        {
            case 1 : p.push();
            break;
            case 2 : p.pop();
            break;
            case 3 : p.displaystack();
            break;
        }
        cout<<"\n Do you want to perform any other operation ?";
        cin>>ans;
    }while(ans=='Y' || ans=='y');
}

/*Output:
[student@localhost ~]$ g++ C1stack.cpp
[student@localhost ~]$ ./a.out

 1. Add element
 2. Delete element
 3. Display stack elements
 Enter the operation you want to perform : 1

 Enter a number : 9

 Do you want to perform any other operation ? y

 1. Add element
 2. Delete element
 3. Display stack elements
 Enter the operation you want to perform : 1

 Enter a number : 70

 Do you want to perform any other operation ? y

 1. Add element
 2. Delete element
 3. Display stack elements
 Enter the operation you want to perform : 3

 The elements in the stack are :
9    70   
 Do you want to perform any other operation ?y

 1. Add element
 2. Delete element
 3. Display stack elements
 Enter the operation you want to perform : 2

 The element popped out of the stack is 70
 Do you want to perform any other operation ?n
[student@localhost ~]$  */

9stlqueuec1_1476775079750

// Write C++ program using STL for implementation of queue using SLL

#include<iostream>
#include<string.h>
#include<list>
using namespace std;

class Queue
{
    public :
        int a;
        list <int> s;
        list <int> :: iterator itr;


        void push()
        {
            cout<<"\n Enter a number : ";
                cin>>a;
                s.push_back(a);
        }

        void displaystack()
        {
            cout<<"\n The elements in the queue are : "<<"\n";
            for(itr=s.begin() ; itr!=s.end() ; itr++)
            {
                cout<<*itr<<"\t";
            }
        }

        void pop()
        {
            itr=s.begin();
            s.pop_front();
            cout<<"\n The element popped out of the queue is "<<*itr;
        }

};

int main()
{
    Queue p;
    int choice;
    char ans;
    do
    {
        cout<<"\n 1. Add element \n 2. Delete element \n 3. Display queue elements";
        cout<<"\n Enter the operation you want to perform : ";
        cin>>choice;
        switch(choice)
        {
            case 1 : p.push();
            break;
            case 2 : p.pop();
            break;
            case 3 : p.displaystack();
            break;
        }
        cout<<"\n Do you want to perform any other operation ?";
        cin>>ans;
    }while(ans=='Y' || ans=='y');
}

/*Output:
[student@localhost ~]$ g++ C1.cpp
[student@localhost ~]$ ./a.out

 1. Add element
 2. Delete element
 3. Display queue elements
 Enter the operation you want to perform : 1

 Enter a number : 6

 Do you want to perform any other operation ? y

 1. Add element
 2. Delete element
 3. Display queue elements
 Enter the operation you want to perform : 1

 Enter a number : 8

 Do you want to perform any other operation ? y

 1. Add element
 2. Delete element
 3. Display queue elements
 Enter the operation you want to perform : 3

 The elements in the queue are :
6    8   
 Do you want to perform any other operation ? y

 1. Add element
 2. Delete element
 3. Display queue elements
 Enter the operation you want to perform : 2

 The element popped out of the queue is 6
 Do you want to perform any other operation ?n
[student@localhost ~]$ */