1

2

#include<iostream>
#include<math.h>
using namespace std;
float n1,n2,result;
char op,ch;
int main()
{
 do
 {
  cout<<"Enter first number:\n";
  cin>>n1;
  cout<<"Enter second number:\n";
  cin>>n2;
  cout<<"Enter operator:\n";
  cin>>op;
  switch(op)
  {
    case '+':
          { 
           result=n1+n2;
           cout<<"The result based on given operator\n"<<result;
           break;
         }
    case '-':
          {
           result=n1-n2;
           cout<<"The result based on given operator\n"<<result;
           break;
         }
    case '*':
          {
           result=n1*n2;
           cout<<"The result based on given operator\n"<<result;
           break;
         }
    case '/':
          {
           result=n1/n2;
           cout<<"The result based on given operator\n"<<result;
           break;
         }
    default:
             cout<<"The operator is invalid\n";
             break;
   }
   cout<<"\nDo you want another(y/n)";
   cin>>ch;
 }
 while(ch=='y');
 return 0;
}

3

#include<iostream>
using namespace std;
class Complex
{
	int real,img;
	public:
	Complex();
	Complex operator+(Complex m); 
	Complex operator*(Complex m);
	friend ostream & operator<<(ostream &out,Complex &);
	friend istream & operator>>(istream &in,Complex &); 
};
Complex::Complex()
{
	real=img=0;
}
Complex Complex::operator+(Complex m)
{
	Complex r;
	r.real=real+m.real;
	r.img=img+m.img;
	return(r);
}
Complex Complex::operator*(Complex m)
{
	Complex r;
	r.real=real*m.real;
	r.img=img*m.img;
	return(r);
}
istream & operator>>(istream &in,Complex &m)
{
	cout<<"\nEnter real & imaginary part";
	in>>m.real>>m.img;
	return in;
}
ostream & operator<<(ostream &out,Complex &m)
{
	if(m.img>=0)
	{
		out<<m.real<<"+"<<m.img<<"i";
	}
	else
	{
		out<<m.real<<m.img<<"i";
	}
	return out;
}
int main()
{
	Complex c1,c2,c3;
	int op;
	cout<<"\nEnter the value of 1st & 2nd Complex nos:-";
	cin>>c1>>c2;
	cout<<"\nEnter 1 for Addition:-";
	cout<<"\nEnter 2 for Multiplication:-";
	cin>>op;
	switch(op)
	{
		case 1:
		{
			c3=c1+c2;
			cout<<"\nAddition of two complex nos:-"<<c3;
			break;	
		}
		case 2:
		{
			c3=c1*c2;
			cout<<"\nMultiplication of two complex nos:-"<<c3;
			break;	
		}
		default:
		{
			cout<<"Invalid option";
			break;
		}
	}
	return(0);
}

4

#include<iostream>
#include<math.h>
using namespace std;
class quadratic
{
int a,b,c;
public:
quadratic();
quadratic operator+(quadratic q1);
double eval(double x);
void root(void);
friend istream& operator>>(istream&,quadratic &);
friend ostream& operator<<(ostream&,quadratic &);
};
quadratic::quadratic()
{
  a=b=c=0;
}
quadratic quadratic::operator+(quadratic q1)
{
 quadratic q2;
 q2.a=a+q1.a;
 q2.b=b+q1.b;
 q2.c=c+q1.c;
 return(q2);
}
double quadratic::eval(double x)
{
 double y;
 y=a*x*x+b*x+c;
 return(y);
}
void quadratic::root(void)
{
 double p,r1,r2;
 p=(b*b)-4*a*c;
 if(p<0)
 {
  cout<<"the roots of equation are imaginary";
}
 else
 {
  r1=((-b)-sqrt((b*b)-4*a*c))/2*a;
  r2=((-b)+sqrt((b*b)-4*a*c))/2*a;
 cout<<"the roots of equation are:";
 cout<<r1<<""<<r2;
}
}
istream & operator>>(istream &in,quadratic &q1)
{
 cout<<"enter coefficient of equation:";
 in>>q1.a>>q1.b>>q1.c;
 return in ;
}
ostream & operator<<(ostream &out,quadratic & q1)
{
 out<<q1.a<<"x*x"<<"+"<<q1.b<<"x"<<"+"<<q1.c;
 return out ;
}

int main()
{
 quadratic q1,q2,q3;
 int opt;
 double c;
 cout<<"enter coefficient of 1st quadratic number\n";
 cin>>q1;
 cout<<"enter coefficient of 2nd quadratic number\n";
 cin>>q2;
 cout<<"enter 1 for additon,2 for evaluation of number and 3 for root calculation:\n"; 
 cin>>opt;
 switch(opt)
{
 case 1:
{
 q3=q1+q2;
 cout<<"sum of two equations:"<<q3;
 break;
}
 case 2:
{
 cout<<"enter the value of x:";
 cin>>c;
 cout<<"the value of function:"<<q1.eval(c);
 break;
}
  case 3:
{
 q1.root();
q2.root();
 break;
}
}
return(0);
}

5

#include <iostream>
using namespace std;
class Cpparray
{
int *a;
int len;
public:
Cpparray()
{
len=0;
}
Cpparray(int x)
{
len=x;
a= new int[len];
}
void operator=(Cpparray m)
{
len=m.len;
a=new int[len];
for(int i=0;i<len;i++)
{
a[i]=m.a[i];
}
}
int getsize()
{
return(len);
}
int operator[](int i)
{
return (a[i]);
}
friend istream & operator>>(istream &in,Cpparray &m);
friend ostream & operator<<(ostream &out,Cpparray &m);
void range(int beg,int end,int value);
};
void Cpparray::range(int beg,int end,int value)
{
int count=0;
int c=value;
if(beg<0 || end>=len)
{
cout<<"the range is invalid";
}
else
{
for(int i=beg;i<=end;i++)
{
if(a[i]==c)
{
cout<<"data is found";
break;
}
else
{
++count;
}
}
if(count==len)
{
  cout<<"data is not found";
}
}

}
istream& operator>>(istream &in,Cpparray &m)
{
cout<<"enter the value of array=";
for(int i=0;i<m.len;i++)
{
in>>m.a[i];
}
return in;
}
ostream& operator<<(ostream &out,Cpparray &m)
{
cout<<"the array is=";
for(int i=0;i<m.len;i++)
{
out<<m[i];
}
return out;
}
int main()
{
int length;
int start,end,number;
cout<<"enter the length of array=";
cin>>length;
Cpparray c1(length);
cout<<"\n The size of array formed is="<<c1.getsize();
cout<<"\n enter value in array=\n";
cin>>c1;
cout<<"enter the range of array=\n";
cin>>start>>end;
cout<<"enter the search value=\n";
cin>>number;
c1.range(start,end,number);
Cpparray c2;
cout<<"\n";
cout<<"copying old array into new array..........\n";
c2=c1;
cout<<"The size of array formed after copying is="<<c2.getsize(); 
cout<<"\n";
cout<<"Displaying the values of both the arrays \n";
cout<<"Before copying"<<c1;
cout<<"\n";
cout<<"After copying"<<c2;
cout<<"\n";
return(0);
}

6

#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class personal_record
{
char name[30];
int age;
public:
void input()
{
cout<<"Enter your name:\n ";
fflush(stdin);
cin.getline(name,30);
cout<<"Enter your age: \n ";
cin>>age;
}
void output()
{
cout<<"Name of the employee: \n"<<name;
cout<<"Age of the employee:\n "<<age;
}
};
class professional
{
char job[30];
int exp;
public:
void input()
{
cout<<"Enter your job profile:\n ";
cin.ignore(1);
fflush(stdin);
cin.getline(job,30);
cout<<"Enter your work experience: \n";
cin>>exp;
}
void output()
{
cout<<"Job profile of employee:\n "<<job;
cout<<"Experience of the employee:\n "<<exp;
}
};
class academic
{
char quali[30];
char board[30];
public:
void input()
{
cout<<"Enter your qualification:\n";
cin.ignore(1);
fflush(stdin);
cin.getline(quali,30);
cout<<"Enter your university or board:\n";
fflush(stdin);
cin.getline(board,30);
}
void output()
{
cout<<"The qualification of employee: \n"<<quali;
cout<<"The university of employee: \n"<<board;
}
};
class biodata:public personal_record,public professional,public academic
{
public:
void input()
{
personal_record::input();
professional::input();
academic::input();
}
void output()
{
personal_record::output();
professional::output();
academic::output();
}
};
int main()
{
biodata b1;
cout<<"Enter the details of biodata: \n";
b1.input();
cout<<"\nThe biodata of employee is as follows: ";
b1.output();
return 0;
}

7

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class udexp
{
int expno;
public:
udexp(int a)
{
expno=a;
}
void printerror()
{
if(expno==1)
{
cout<<"Your age is invalid";
}
if(expno==2)
{
cout<<"Your income is invalid";
}
if(expno==3)
{
cout<<"Your city is invalid";
}
if(expno==4)
{
cout<<"\nYour vehicle is invalid";
}
}
};
int main()
{
char name[30];
int age,income,cityno,vehno;
cout<<"Enter your name: ";
fflush(stdin);
cin.getline(name,30);
cout<<"Enter your age: ";
cin>>age;
cout<<"Enter your income: ";
cin>>income;
cout<<"enter city:1.pune,2.mumbai,3.chennai,4.banglore,5.others:  ";
cin>>cityno;
cout<<"Enter your vehicle:1.four wheeler,2.others: ";
cin>>vehno;
try
{
if(age<18||age>55)
{
udexp e1(1);
throw e1;
}
if(income<50000||income>100000)
{
udexp e1(2);
throw e1;
}
if(cityno==5)
{
udexp e1(3);
throw e1;
}
if(vehno==2)
{
udexp e1(4);
throw e1;
}
else
{
cout<<"Congrats "<<name;
cout<<" you are a valid user:\n";
}
}
catch(udexp e)
{
e.printerror();
}
cout<<"\nThank you user for comming!!!";
return(0);
}


8

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
{
int n,i;
char name[30];
ofstream fout;
fout.open("prog.txt");
cout<<"Enter the count of words to be entered:";
cin>>n;
for(i=1;i<=n;i++)
{
fflush(stdin);
cin.getline(name,30);
fout.write(name,sizeof(name));
}
fout.close();
cout<<"Data is written and file is closed:\n";
ifstream fin;
fin.open("prog.txt");
cout<<"File is reopen:";
for(i=1;i<=n;i++)
{
fin.read(name,sizeof(name));
cout<<name;
}
fin.close();
return 0;
}

9

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main(int argc,char *argv[])
{
char filename[30];
char oldname[30];
char newname[30];
char temp[30];
int i;
if (argc>=4)
{
strcpy(filename,argv[3]);
strcpy(oldname,argv[1]);
strcpy(newname,argv[2]);
int len1=strlen(oldname);
int len2=strlen(newname);
ifstream fin;
fin.open(filename);
ofstream fout;
fout.open("temp.txt");
if(!fin)
{
cout<<"file doesnot exist";
cout<<"run program: progname oldword newword filename";

}
else
{
while(fin)
{
fin>>temp;
if(strcmp(temp,oldname))
{
fout<<newname;
i++;
}
else
{
fout<<temp;
}
}
fin.close();
fout.close();
remove(filename);
rename("temp.txt",filename);
remove("temp.txt");
if(i>0)
{
cout<<"no of word changed="<<i;
}
else
{
cout<<"no word are changed";
}
}
}
else
{
cout<<"run program: progname oldword newword filename";
}
return(0);
}

o/p:
./a.out prg9 rose lotus t1.txt
no of words changed =10

10

#include<iostream>using namespace std;
template
void sort()
{
int i,j;
T temp;
T a[5];
cout<<“Enter five numbers:”; for(i=0;i<5;i++) { cin>>a[i];
}
for(i=0;i<4;i++) { for(j=i;j<5;j++) { if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<“The sorted array is”<<endl;
for(i=0;i<5;i++)
{
cout<<a[i]<<endl;
}
}

int main()
{
int choice;
char ans;
do
{
cout<<“1.Integer sort”<>choice;
switch(choice)
{
case 1:sort();
break;
case 2:sort();
break;
case 3:cout<<“Invalid choice”;
break;

        }
        cout<<"You want to continue or not:";
        cin>>ans;
    }while(ans=='Y'||ans=='y');
return 0;
}

OUTPUT:

/output/ [email protected]:~$ g++ sele.cpp [email protected]:~$ ./a.out 1.Integer sort 2.float sort Enter your choice: 1 Enter five numbers:5 9 6 2 8 The sorted array is 2 5 6 8 9 You want to continue or not:y 1.Integer sort 2.float sort Enter your choice: 2 Enter five numbers:2.5 6.3 3.7 4.4 8.9 The sorted array is 2.5 3.7 4.4 6.3 8.9 You want to continue or not:n [email protected]:~$ /*/

11

#include<iostream>
#include<cctype>
#include<deque>
using namespace std;
int main()
{
int i,opt1,opt2;
float val;
char ch,ch1;
deque<float>dq;
deque<float>::iterator itr;
do
{
cout<<"Enter values for deque=";
cin>>val;
dq.push_back(val);
cout<<"Add more values(y/n)=";
cin>>ch;
}while(ch=='y');
do
{
cout<<"your option=";
cout<<"1.Add value";
cout<<"2.remove value";
cout<<"3.Print deque";
cout<<"Enter your choice=";
cin>>opt1;
switch(opt1)
{
case 1 :
{
cout<<"Add at end:1 or Add at front:2=";
cin>>opt2;
cout<<"Enter value=";
cin>>val;
if(opt2==1)
{
dq.push_back(val);
cout<<"After addition at end the deque is=";
itr=dq.begin();
while(itr!=dq.end())
{
cout<<*itr++;
}
}
else
{
dq.push_front(val);
cout<<"After addition at front the deque is=";
itr=dq.begin();
while(itr!=dq.end())
{
cout<<*itr++;
}
}
break;
}
case 2 :
{
cout<<"remove from end:1 or remove from front:2=";
cin>>opt2;

if(opt2==1)
{
dq.pop_back();
cout<<"After removal at end the deque is=";
itr=dq.begin();
while(itr!=dq.end())
{
cout<<*itr++;
}
}
else
{
dq.pop_front();
itr=dq.begin();
cout<<"After removal at front the deque is=";
while(itr!=dq.end())
{
cout<<*itr++;
}
}
break;
}
case 3 :
{
cout<<"The size of deque="<<dq.size();
cout<<"The deque contains following values=";
itr=dq.begin();
while(itr!=dq.end())
{
cout<<*itr++;
}
break;
}
default :
{
cout<<"Invalid option";
break;
}
}
cout<<"do you want another operation(y/n)=";
cin>>ch1;
}while(ch1=='y');
return (0);
}

12

#include<iostream>
#include<cctype>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
void printStack(stack<int> stk)
{
while(!stk.empty())
{
cout<<'\t'<<stk.top();
stk.pop();
}
cout<<"\n";
}
void printQueue(queue<int> q1)
{
queue<int> q2 = q1;

while(!q2.empty())
{
cout<<'\t'<<q2.front();
q2.pop();
}
cout<<"\n";
}
int main()
{
stack<int> s1;
queue<int> q;
int choice, choice1, choice2,n,val,i;
char ch,ans;
do
{
cout<<"Enter the choice of operation: \n";
cout<<"1. Stack Operations"<<"\t"<<"2. Queue operations";
cin>>choice, choice1, choice2;
if(choice==1)
{
cout<<"Choose the stack operation you want to perform";
cout<<"1.Add Values in stack\n";
cout<<"2.Display size of stack\n";
cout<<"3.Display top of stack\n";
cout<<"4.Delete values from stack\n";
cout<<"5.Print stack\n";
cin>>choice1;
switch(choice1)
{
case 1:
{
cout<<"How many values you want to enter??";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"Enter value to be added";
cin>>val;
s1.push(val);
}
cout<<"After Addition the Stack is=\n";
printStack(s1);
break;
}
case 2:
{
if(s1.empty())
{
cout<<"Stack is empty";
}
else
{
cout<<"The size of stack is="<<s1.size();
}
break;
}
case 3:
{
if(s1.empty())
{
cout<<"Stack is empty";
}
else
{
cout<<"The top of stack is="<<s1.top();
}
break;
}
case 4:
{
if(!s1.empty())
{
cout<<"One value is deleted";
s1.pop();
cout<<"After Deletion the stack is=\n";
printStack(s1);
}
else
{
cout<<"stack is empty";
}
break;
}
case 5:
{
if(s1.empty())
{
cout<<"Stack is empty";
}
else
{
cout<<"The data present in stack is as follows:\n";
printStack(s1);
}
break;
}
default:
{
cout<<"Invalid option";
break;
}
}
}
else if(choice==2)
{
cout<<"Choose the Queue operation you want to perform";
cout<<"1.Add Values in Queue\n";
cout<<"2.Display size of Queue\n";
cout<<"3.Display front of Queue\n";
cout<<"4.Display back of Queue\n";
cout<<"5.Delete values from Queue\n";
cout<<"6. Print Queue";
cin>>choice2;
switch(choice2)
{
case 1:
{
cout<<"How many values you want to enter??";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"Enter value to be added";
cin>>val;
q.push(val);
}
cout<<"After Addition the Queue is=\n";
printQueue(q);
break;
}
case 2:
{
if(q.empty())
{
cout<<"Queue is empty";
}
else
{
cout<<"The size of Queue is="<<q.size();
}
break;
}
case 3:
{
if(q.empty())
{
cout<<"Queue is empty";
}
else
{
cout<<"The front of queue is="<<q.front();
}
break;
}
case 4:
{
if(q.empty())
{
cout<<"Queue is empty";
}
else
{
cout<<"The back of Queue is="<<q.back();
}
break;
}
case 5:
{
if(!q.empty())
{
cout<<"One value is deleted";
q.pop();
cout<<"After Deletion the Queue is=\n";
printQueue(q);
}
else
{
cout<<"Queue is empty";
}
break;
}
case 6:
{
if(q.empty())
{
cout<<"Queue is empty";
}
else
{
cout<<"The data present in Queue is as follows:\n";
printQueue(q);
}
break;
}
default:
{
cout<<"Invalid option";
break;
}
}
}
else
{
cout<<"Invalid choice for operation";
}
cout<<"Do you want another operation(y/n)??";
cin>>ch;
}while(ch=='y');
return(0);
}

13

#include<iostream>
#include<cctype>
#include<list>
#include<math.h>

using namespace std;

void AddNode(list<int> &a, int v)
{
a.push_back(v);
list<int> :: iterator itr;
cout<<"After addition List contains:\n";
itr = a.begin();
while(itr!=a.end())
{
cout<<*itr++<<" ";
}
cout<<"\n.............\n";
}


void Insert(list<int> &a, int v, int p)
{

list<int> :: iterator itr = a.begin();;
advance(itr, p-1);
a.insert(itr,v);
cout<<"After Insert List contains:\n";
itr = a.begin();
while(itr!=a.end())
{
cout<<*itr++<<" ";
}
cout<<"\n.............\n";

}



void Display(list<int> a)
{
if(!a.empty())
{
list<int> :: iterator itr;
cout<<"List contains:\n";
itr = a.begin();
while(itr!=a.end())
{
cout<<*itr++<<" ";
}
cout<<"\n.............\n";
}
else
{
cout<<"List is empty";
}
}


int main()
{
cout<<"Linked List Using STL.....\n";
list<int> l1;

int n,i,opt,val,pos;
char ch;

cout<<"Enter how many numbers:";
cin>>n;
cout<<"Enter"<<n<<"Values\n";

for(i=1;i<=n;i++)
{
cin>>val;
l1.push_back(val);
}

do
{
cout<<"Enter your options";
cout<<"1.Add Values\n";
cout<<"2.Display List\n";
cout<<"3.Delete Values\n";
cout<<"4.Insert Values\n";
cout<<"Enter Your Option:";
cin>>opt;
switch(opt)
{
case 1:
{
cout<<"Enter Values to add:";
cin>>val;
AddNode(l1,val);
break;
}

case 2:
{
Display(l1);
break;
}

case 3:
{
cout<<"Enter Values to delete:";
cin>>val;
if(!l1.empty())
{
list<int> :: iterator itr;
itr= l1.begin();
while(itr!=l1.end())
{
if(*itr==val)
{
l1.erase(itr);
break;
}
else
{
itr++;
}
}
cout<<"After deletion List contains:\n";
itr = l1.begin();
while(itr!=l1.end())
{
cout<<*itr++<<" ";
}
cout<<"\n.............\n";
}
else
{
cout<<"\n\nList is empty";
}

break;
}

case 4:
{
cout<<"Enter Value:";
cin>>val;
cout<<"Enter Position where to insert:";
cin>>pos;
Insert(l1,val,pos);

break;
}
default:
cout<<"\n\nInvalid Option......\n";
break;
}
cout<<"Do you want another operation:(y/n)";
cin>>ch;
}
while(ch=='y');
return(0);
}