C++: Input output operations - Applications solved


1) Make a program that performs formatting using the formatting indicators.
#include <iostream>
#include <fstream>
#include <math.h>
#include<conio.h>
using namespace std;
 
void main()
{
	int n=123;
 
	//1. 00123 displays
	cout.width(5);
	cout.setf(ios::right,ios::adjustfield);
	cout.fill('0');
	cout<<n;
	cout<<"\nAct to continue.\n";
	getch();
 
	//2. +1230 displays
	cout.width(5);
	cout.fill('0');
	cout.setf(ios::left,ios::adjustfield);
	cout.setf(ios::showpos);
	cout<<n;
	cout<<"\nAct to continue.\n";
	getch();
 
	//3. +0123 displays (plus sign remains activated)
	cout.width(5);
	cout.fill('0');
	cout.setf(ios::internal,ios::adjustfield);
	cout<<n;
	cout<<"\nAct to continue.\n";
	getch();
 
	//4. displays the hexadecimal value of the number "n"
	cout.setf(ios::hex,ios::basefield); //oct for display in octal
	cout<<n;
	cout<<"\nAct to continue.\n";
	getch();
	double d=123.456;
 
	//5. number is displayed in scientific form "d"
	cout.unsetf(ios::showpos); //turn off the display with+
	cout.setf(ios::scientific,ios::floatfield);
	cout<<d;
	cout<<"\nAct to continue.\n";
	getch();
 
	//6. is displayed with a precision of 2 digits
	cout.setf(ios::fixed,ios::floatfield);
	cout.precision(2);
	cout<<d;
	cout<<"\nAct to continue.\n";
	getch();
 
	int g;
	cin >>g;
}
2) Make a program that performs formatting using format manipulators.
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
 
void main()
{
	int n=123;
 
	//1. dispaly 00123
	cout<<setw(5)<<setiosflags(ios::right)<<setfill('0')<<n;
	cout<<"\nAct a key to continue.\n";
	getch();
 
	//2. dispaly +1230
	cout<<setw(5)<<setiosflags(ios::left)<<setfill('0')<<
	setiosflags(ios::showpos)<<n;
	cout<<"\nAct a key to continue.\n";
	getch();
 
	//3. dispaly +0123 (plus sign remains activated)
	cout<<setw(5)<<setiosflags(ios::internal)<<setfill('0')<<n;
	cout<<"\nAct a key to continue.\n";
	getch();
 
	//4. number is displayed in hexadecimal
	cout<<hex<<n;
	cout<<"\nAct a key to continue.\n";
	getch();
	double d=123.456;
 
	//5. is displayed in scientific form
	cout<<resetiosflags(ios::fixed)<<
	setiosflags(ios::scientific)<<d;
	cout<<"\nAct a key to continue.\n";
	getch();
 
	//6. is displayed with a precision of 2 digits
	cout<<setprecision(2)<<d;
	cout<<"\nAct a key to continue.\n";
	getch();
 
	int g;
	cin >>g;
}
3) Make a program via a constructor populate data into a phone book and by overloading operator << makes the output data on the screen.
#include <iostream>
#include<string.h>
#include<conio.h>
using namespace std;
 
class phonebook {
	public:
	char name[20];
	int codezone;
	int prefix;
	unsigned long int number;
 
	phonebook(char *n,int a,int p,unsigned long int no)
	{
		strcpy(name,n);
		codezone=a;
		prefix=p;
		number=no;
	}
};
 
//Displays the name and phone number
ostream &operator<<(ostream &stream, phonebook a)
{
	stream<<a.name;
	stream<<"("<<a.codezone<<")";
	stream<<a.prefix<<"-"<<a.number<<"\n";
	return stream;
}
 
void main()
{
	phonebook a("John",40,269,244573);
	phonebook b("Helen",40,264,789045);
	cout<<a<<b;
	getch();
 
	int g;
	cin >>g;
}
4) Make a program via a constructor populate data into a phone book and by overloading operator << data makes output on the screen so that you can work with protected data.
#include <iostream>
#include<string.h>
#include<conio.h>
using namespace std;
 
class phonebook  {
	char name[20];
	int codezone;
	int prefix;
	unsigned long int number;
	public:
	phonebook (char *n,int a,int p,unsigned long int no)
	{
		strcpy(name,n);
		codezone=a;
		prefix=p;
		number=no;
	}
 
	//Afiseaza namele si numberul de telefon
	friend ostream &operator<<(ostream &stream, phonebook  a)
	{
		stream<<a.name;
		stream<<"("<<a.codezone<<")";
		stream<<a.prefix<<"-"<<a.number<<"\n";
		return stream;
	}
};
 
void main()
{
	phonebook  a("John",40,269,244573);
	phonebook  b("Helen",40,264,789045);
	cout<<a<<b;
	getch();
 
	int g;
	cin >>g;
}
5) Rebuild the previous example so that the data will not be entered via a constructor, but by a function overloaded operator >>.
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
 
class phonebook {
	char nume[20];
	int codezone;
	int prefix;
	unsigned long int number;
	public:
	phonebook()
	{
	}
 
	//Afiseaza numele si numberul de telefon
	friend ostream &operator<<(ostream &stream, phonebook a);
 
	//Introduce numele si numberul de telefon
	friend istream &operator>>(istream &stream, phonebook &a);
};
 
//Afiseaza numele si numberul de telefon
ostream &operator<<(ostream &stream, phonebook a)
{
	stream<<strupr(a.nume)<<'\t';
	stream<<"("<<a.codezone<<")";
	stream<<a.prefix<<"-"<<a.number<<"\n";
	return stream;
}
 
//Introduce numele si numberul de telefon
istream &operator>>(istream &stream, phonebook &a)
{
	cout<<"Enter the name: ";
	stream>>a.nume;
	cout<<"Enter codezone: ";
	stream>>a.codezone;
	cout<<"Enter the prefix: ";
	stream>>a.prefix;
	cout<<"Enter the number: ";
	stream>>a.number;
	cout<<"\n";
	return stream;
}
void main()
{
	phonebook a;
	cin>>a;
	cout<<a;
	getch();
 
	int g;
	cin >>g;
}
6) Describe an abstract data type for storing a complex number and main operations with these numbers by overloading operators.
#include <iostream>
#include<string.h>
#include<conio.h>
#include<math.h>
#include<iomanip> /*library used manipulators
 with parameter, if this application: function setprecision */
using namespace std;
 
class complex
{
	float a,b; //in 'a' memorize real part in 'b' memorize the imaginary
	public:
	complex(float=0,float=0); //constructor initialization parameter
	float& retre(); //returns the real part
	float& retim(); //returns the imaginary
	complex operator+(complex&); //adding two complex numbers
	complex operator-(); //negative (opposite) of a complex number
	complex operator-(complex&); //difference between two complex numbers
	complex operator~(); //conjugate of a complex number
	complex operator*(complex&); //product of two complex numbers
	double operator!(); //module of a complex number
	complex operator *(float); //multiply a complex number by a scalar
	friend complex operator*(float,complex&); /*reverse the previous operation,
	that is, multiplied by the complex scalar*/
	friend istream& operator>>(istream&,complex&); /*Overloading the stream 
	extraction operator to read a complex number*/
	friend ostream& operator<<(ostream&,complex); /*overload
	stream insertion operator to display a complex number*/
};
 
inline complex::complex(float x,float y)
{
	a=x;
	b=y;
}
 
inline float& complex::retre()
{
	return a;
}
 
inline float& complex::retim()
{
	return b;
}
complex complex::operator+(complex& z)
{
	complex r;
	r.a=a+z.a;
	r.b=b+z.b;
	return r;
}
 
complex complex::operator-()
{
	complex r;
	r.a=-a;
	r.b=-b;
	return r;
}
 
complex complex::operator-(complex& z)
{
	return (*this)+(-z); /*relation between the current object and one passed 
	as a parameter, relation operators are overloaded by the previous functions*/
}
 
complex complex::operator~()
{
	complex r;
	r.a=a;
	r.b=-b;
	return r;
}
 
complex complex::operator*(complex& z)
{
	complex r;
	r.a=a*z.a-b*z.b;
	r.b=a*z.b+b*z.a;
	return r;
}
 
double complex::operator!()
{
	return sqrt(pow(a,2)+pow(b,2));
}
 
complex complex::operator*(float x)
{
	complex r;
	r.a=x*a;
	r.b=x*b;
	return r;
}
 
complex operator*(float x, complex& z)
{
	return z*x; //previously overloaded operator
}
 
istream& operator>>(istream& c, complex& z)
{
	cout<<"\tEnter part real: ";
	c>>z.a;
	cout<<"\tEnter part imaginary: ";
	c>>z.b;
	return c;
}
 
ostream& operator<<(ostream& c, complex z)
{
	c<<"no. complex is: "<<setprecision(2)<<z.a; /*will display 2 decimal 
	places after the decimal point, only where appropriate*/
	cout.setf(ios::showpos);
	c<<' '<<z.b<<"*i"<<endl;
	cout.unsetf(ios::showpos);
	return c;
}
 
void main()
{
	complex z1,z2; /*the real and imaginary ones are filled with 0*/
	cout<<"Read z1."<<endl;
	cin>>z1; //extraction operator call from overloaded flux
	cout<<"Save data directly in z2."<<endl;
	z2.retre()=4;
	z2.retim()=-1;
	cout<<endl;
	cout<<"No complex z1 - "<<z1; /*call the function that overloads 
	operator insertion in flux*/
	cout<<"No complex z2 - "<<z2;
	cout<<endl;
	cout<<"Sum z1+z2 - "<<z1+z2; /*call of overloaded functions
	operators '+' and insertion in flux*/
	cout<<"Product z1*z2 - "<<z1*z2; /*call of overloaded functions
	operators '*' and insertion in flux*/
	cout<<"Module sum is: "<<!(z1+z2)<<endl; /*call the functions
	overload operators '+' and '!'*/
	complex z3;
	z3=(~z1)+(2*(-z2)*(!z1)); //are used more operators overload list
	cout<<"The expression (~z1)+(2*(-z2)*(!z1)) - "<<z3;
 
	int g;
	cin >>g;
}
7) Make an application for working with rational numbers (rational numbers call any number that can be written as a fraction). It will use overloaded operators.
#include<iostream>
#include<fstream>
#include<string.h>
#include<conio.h>
#include<math.h>

using namespace std;
 
class rational
{
	int a; //numerator of the fraction
	int b; //denominator of the fraction
int gcd(int,int); /*the gcd of two integers used to determine the irreducible fractions*/
	void irreducible(); //determination of irreducible fractions
	public:
	rational(int=0,int=1); //initialization constructor with default parameters
	int& retnumerator(); //function that returns the numerator of the fraction
	int& retdenominator(); //function that returns the denominator of the fraction
friend istream& operator>>(istream&, rational&); /*reading a rational number*/
friend ostream& operator<<(ostream&, rational); /*displaying a rational number*/
	rational operator+(rational&); //gathering two rational numbers
	rational operator-(); //opposite of a rational number
	rational operator-(rational&); //difference of two rational numbers
	rational operator*(rational&); //multiplication of two rational numbers
	rational operator*(int); //multiplying a complex number by a scalar
friend rational operator*(int, rational&); /*a scalar multiplication of complex numbers*/
	rational operator!(); //inverse of a rational number
	rational operator/(rational&); //dividing two rational numbers
};
 
/*the gcd of two integers used to determine the irreducible fractions 
					 I chose iterative*/
 
int rational::gcd(int x,int y)
{
	x=abs(x);
	y=abs(y);
	while(x!=y)
	if(x>y)
		x-=y; //x=x-y;
	else
		y-=x; //y=y-x;
	return x;
}
 
/*determination of irreducible fractions*/
void rational::irreducible()
{
	if(a==0) b=1;
	else
		{
			if((abs(a)!=1)&&(abs(b)!=1))
			{
				int d;
				d=gcd(a,b);
				a/=d; //a=a/d;
				b/=d; //b=b/d;
			}
		if(b<0)
		{
			a=-a;
			b=-b;
		}
	}
}
 
/*constructor initialization parameter*/
inline rational::rational(int x,int y)
{
	a=x;
	b=y;
	irreducible();
}
 
/*function that returns the numerator of the fraction*/
inline int& rational::retnumerator()
{
	return a;
}
 
/*function that returns the denominator of the fraction*/
inline int& rational::retdenominator()
{
	return b;
}
 
/*extraction operator overloading
from flux to read a rational number*/
 
istream& operator>>(istream& in, rational& r)
{
	cout<<"\tEnter numerator: ";
	in>>r.a;
	do{
		cout<<"\tEnter denominator: ";
		in>>r.b;
	}while(r.b==0); //denominator different from 0
	r.irreducible();
	return in;
}
 
/*overloading flux insertion operator
 for display of a rational number*/
 
ostream& operator<<(ostream& out, rational r)
{
	out<<r.a<<"/"<<r.b;
	return out;
}
 
/* overloading operator + for adding two rational numbers */
rational rational::operator+(rational& r)
{
	rational p;
	p.a=a*r.b+b*r.a;
	p.b=b*r.b;
	p.irreducible();
	return p;
}
 
/*overloading operator '-' to determine the opposite of rational numbers*/
rational rational::operator-()
{
	rational p;
	p.a=-a;
	p.b=-b;
	p.irreducible(); /*At first sight there seems useless of this function, but
 I chose not know that this counting operation of popular
 data a rational number, if through them we used
 procedure for determining an irreducible fractions*/
	return p;
}
 
/*overloading operator '-' for the difference of two rational numbers*/
rational rational::operator-(rational& r)
{
	rational p;
	p=(*this)+(-r); //call the two functions defined above
	p.irreducible();
	return p;
}
 
/*overloading operator '*' for multiplication of two rational numbers*/
rational rational::operator*(rational& r)
{
	rational p;
	p.a=a*r.a;
	p.b=b*r.b;
	p.irreducible();
	return p;
}
 
/*overloading operator '*' for multiplication of no. rational
  with an integer*/
rational rational::operator*(int x)
{
	rational p;
	p.a = a*x; //or: x*a;
	p.b = b*x; //or: x*b;
	p.irreducible();
	return p;
}
 
/*overloading operator '*' for multiplying an integer
with a rational*/
rational operator*(int x, rational r)
{
	return r*x; //call the function previously developed
}
 
/*overloading operator '!' the inverse of a rational number*/
rational rational::operator!()
{
	rational p;
	p.a=b;
	p.b=a;
	p.irreducible();
	return p;
}
 
/*overloading operator '/' for division of two rational numbers*/
rational rational::operator/(rational& r)
{
	rational p;
	p=(*this)*(!r);
	p.irreducible();
	return p;
}
 
void main()
{
	rational a(4,-2);
	cout<<"The first number is rational is introduced by constructor function."<<endl;
	rational b;
	cout<<"Enter data for the second no. rational:"<<endl;
	cin>>b;
	cout<<"The first rational number is: "<<a<<endl;
	cout<<"The second rational number is: "<<b<<endl;
	cout<<endl<<"-----------------------------------"<<endl;
	cout<<"Addition of the two numbers is: "<<a+b<<endl;
	cout<<"Multiplying the two numbers is: "<<a*b<<endl;
	cout<<"Dividing the two numbers is: "<<a/b<<endl;
 
	int g;
	cin >>g;
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.