Saturday, June 22, 2019

STRING OPERATIONS || CPP

STRING OPERATIONS USING CPP


                                                                                                                                                                                           

CODE :-


#include
#include
#include
using namespace std;
class operation
{
private:
char a[15];
public:
void getdata()
{
cout<<"\nENTER A STRING: ";
cin>>a;
}
void copy()
{
char b[15];
strcpy(b,a);
cout<<"\nTHE STRING IS COPIED FROM a WHICH IS "<}
void length()
{
int l;
l=strlen(a);
cout<<"\nLENGHT OF STRING: "<
}
void cmp()
{
char b[15];
int l;
cout<<"\n ENTER A STRING YOU WANT TO COMPARE: ";
cin>>b;
l=strcmp(a,b);
if(l==0)
{
cout<<"\nEQUAL";
}
else
{
cout<<"\n NOT EQUAL!";
}
}
void cat ()
{
char b[15], a[15];
cout<<"\n ENTER A STRING YOU WANT TO JOIN: ";
cin>>a;
cout<<"\nENTER ANOTHER STRING YOU WANT TO JOIN: ";
cin>>b;
strcat(a,b);
cout<<"\nTHE STRING WILL BE: "<
}
void count()
{
int count =0, i=0;
char x;
cout<<"\nENTER ANY CHARACTER: ";
cin>>x;
while(a[i]!='\0')
{
if(a[i]==x)
{
count++;
}
i++;
}
cout<<"\nOCCURENCE OF "<}
};
int main()
{
operation s;
int ch;
cout<<"\n\t\n\t*********STRING OPERATIONS************";
s.getdata();
while(1)
{
cout<<"\n\t\nSELECT AN OPERATION YOU WANT TO PERFORM ON THE STRING:"
" \n1.COPY A STRING IN ANOTHER VARRIABLE"<<
"\n2.MEASURE THE LENGHT OF STRING\n"
"3.COMPARE A STRING WITH ANOTHER \n"
"4.JOIN TWO STRING\n"
"5. COUNT A LETTER IN THE STRING \n"
"6. EXIT: ";
cin>>ch;
switch(ch)
{
case 1:
s.copy();
break;
case 2:
s.length();
break;
case 3:
s.cmp();
break;
case 4:
s.cat();
break;
case 5:
s.count();
break;
case 6:
exit(0);
break;
default:
cout<<"\nINVALID";
break;
}
}
return 0;
}



OUTPUT :-

        *********STRING OPERATIONS************
ENTER A STRING: SANTOSH


SELECT AN OPERATION YOU WANT TO PERFORM ON THE STRING:
1.COPY A STRING IN ANOTHER VARRIABLE
2.MEASURE THE LENGHT OF STRING
3.COMPARE A STRING WITH ANOTHER
4.JOIN TWO STRING
5. COUNT A LETTER IN THE STRING
6. EXIT:



FILE HANDLING || TELIPHONE DIERY || C++

CODE :


#include
#include
#include
#include
#include
using namespace std;
class phonebook
{
char name[20],phno[15];
public:
void getdata()
{
cout<<"\n enter name : ";
cin>>name;
cout<<"\n enter number : ";
cin>>phno;
}
void display()
{
cout<
cout<
cout<
}
char *getname()
{
return name;
}
char *getphno()
{
return phno;
}
void update(char *nm,char *telno)
{
strcpy(name,nm);
strcpy(phno,telno);
}
};
int main()
{
phonebook rec;
fstream file;
file.open("/home/student/Desktopphone.data",ios::ate | ios::in | ios::binary);
char ch,nm[30],telno[14];
int choice,found=0;
int cnt=0;
while(1)
{
cout<<"\n\n\n\n********** #Phone Book# ***********\n";
cout<<"1. Add New Record \n";
cout<<"2. Display All Record \n";
cout<<"3. Search Telephone No. \n";
cout<<"4. Search Person name \n";
cout<<"5. Update Telephone No. \n";
cout<<"6. Exit \n";
cout<<"Enter choice : ";
cin>>choice;
switch(choice)
{
case 1:
rec.getdata();
cin.get(ch);
file.write((char *) &rec, sizeof(rec));
break;
case 2:
file.seekg(0,ios::beg);
cout<<"\n\n REcords in Phone Book \n";
while(file)
{
file.read((char *) &rec, sizeof(rec));
if(!file.eof())
rec.display();
}
file.clear();
getchar();
break;
case 3:
cout<<"\n\n Enter 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.display();
}
}
file.clear();
if(found==0)
cout<<"\n\n -----Record Not Found----- \n";
getchar();
break;
case 4:
cout<<"\n\n Enter Telephone No. : ";
cin>telno;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getphno())==0)
{
found=1;
rec.display();
}
}
file.clear();
if(found==0)
cout<<"\n\n -----Record Not Found----- \n";
getchar();
break;
case 5:
cout<<"\n\n Enter 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:
exit (0);
default:
cerr<<"********** Please Enter Valid Choice **********";
}
}
return 0;

}

OUTPUT :-



addition of complex no in cpp

     addition and multiplication on complex no 


code :
#include
using namespace std;
class complex
{
private:
float a,b;
public:
complex()
{
a = 0.0;
b = 0.0;
}
void getdata()
{
cout<<"Enter real part of complex number :";
cin>>a;
cout<<"Enter imaginary part of complex number :";
cin>>b;
}
void putdata()
{
if (b>0)
{
cout<}
else
{
cout<
}
}
complex operator +(complex c)
{
complex x;
x.a = a + c.a;
x.b = b + c.b;
return x;
}
complex operator *(complex c)
{
complex x;
x.a = (a*c.a) - (b*c.b);
x.b = (a*c.b) + (c.a*b);
return x;
}
};
int main()
{
complex c1,c2,c3,c4;
cout<<"For first complex number : ";
c1.getdata();
cout<<"For second complex number : ";
c2.getdata();
c3 = c1 + c2;
c4 = c1 * c2;
cout<<" First complex number = ";
c1.putdata();
cout<<" Second complex number = ";
c2.putdata();
cout<<"_________________________________________________"<cout<<" Addition of the complex number = ";
c3.putdata();
cout<<"_________________________________________________"<cout<<"Multiplication of the complex number = ";
c4.putdata();
return 0;
}

OOP Binary addition -STL

         C++       ||       Binary Addition   ||   oop assignment



code :



#include
#include
using namespace std;
stack read()
{
stack s;
int x,n,i;
cout<<"\nEnter the no. of bits in the no. :";
cin>>n;
cout<<"\nEnter the binary number : ";
for(i=0;i
{
cin>>x;
s.push(x);
}
return s;
}
void display(stack &s)
{
cout<<" ";
while(!s.empty())
{
cout<
s.pop();
}
}
stack add(stack &s1,stack &s2)
{
stack 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 s1,s2,s3;
int ch;
cout<<"\n\t\t\t********** MENU **********\n";
cout<<"\n1........Read first number"
<<"\n2........Read second number"
<<"\n3........Display addition of two numbers"
<<"\n4........Exit";
do
{
cout<<"\n Enter your choice..: ";
cin>>ch;
switch(ch)
{
case 1:
s1=read();
break;
case 2:
s2=read();
break;
case 3:
cout<<"\n The result of addition is :";
s3=add(s1,s2);
display(s3);
break;
}
}while(ch!=4);
return 0;
}

calculetor program in C++

C++ Program of  Calculetor


#include
using namespace std;
class Calculator
{
private:
float a;
public:
void getdata()
{
cout<<"Enter any number";
cin>>a;
}
void operator +(Calculator p)
{
Calculator q;
q.a=a+p.a;
cout<<"Addition ="<
}
void operator -(Calculator p)
{
Calculator q;
q.a=a-p.a;
cout<<"Subtraction ="<
}
void operator *(Calculator p)
{
Calculator q;
q.a=a*p.a;
cout<<"Multiplication ="<
}
void operator /(Calculator p)
{
Calculator q;
q.a=a/p.a;
cout<<"Division ="<
}
};
int main()
{
Calculator c1,c2;
while(1)
{
c1.getdata();
c2.getdata();
int ch;
cout<<"1.Addition"<
cout<<"Enter your choice :";
cin>>ch;
switch(ch)
{
case 1:
c1+c2;
break;
case 2:
c1-c2;
break;
case 3:
c1*c2;
break;
case 4:
c1/c2;
break;
case 5:
return 0;
default :
cout<<"Incorrect choice "<
}
}
}
output:




C++ Hallo Word program

C++ Hallo word
_______________________________________
program:

#include <iostream> 
 
using namespace std; 
 
int main()
{

cout<<"hallo word"
return 0;
}



output :

hallo word

STRING OPERATIONS || CPP

STRING OPERATIONS USING CPP                                                                                                             ...