Here is a list of useful programs compiled by bOtskOOl which might be relevant for you:
1. Complex numbers in C++
#include <cmath>
#include <iostream>
#include <iomanip.h>
using namespace std;
class complex
{
private:
float real; // Real Part
float imag; // Imaginary Part
public:
complex(float, float);
complex(const complex & );
complex operator + (complex);
complex operator - (complex);
complex operator * (complex);
complex operator / (complex);
complex getconjugate();
complex getreciprocal();
float getmodulus();
void setdata(float, float);
void getdata();
float getreal();
float getimaginary();
bool operator == (complex);
void operator = (complex);
friend ostream & operator << (ostream & s, complex & c);
};
// CONSTRUCTOR
complex::complex(float r = 0.0 f, float im = 0.0 f)
{
real = r;
imag = im;
}
// COPY CONSTRUCTOR
complex::complex(const complex & c)
{
this - > real = c.real;
this - > imag = c.imag;
}
void complex::operator = (complex c)
{
real = c.real;
imag = c.imag;
}
complex complex::operator + (complex c)
{
complex tmp;
tmp.real = this - > real + c.real;
tmp.imag = this - > imag + c.imag;
return tmp;
}
complex complex::operator - (complex c)
{
complex tmp;
tmp.real = this - > real - c.real;
tmp.imag = this - > imag - c.imag;
return tmp;
}
complex complex::operator * (complex c)
{
complex tmp;
tmp.real = (real * c.real) - (imag * c.imag);
tmp.imag = (real * c.imag) + (imag * c.real);
return tmp;
}
complex complex::operator / (complex c)
{
float div = (c.real * c.real) + (c.imag * c.imag);
complex tmp;
tmp.real = (real * c.real) + (imag * c.imag);
tmp.real /= div;
tmp.imag = (imag * c.real) - (real * c.imag);
tmp.imag /= div;
return tmp;
}
complex complex::getconjugate()
{
complex tmp;
tmp.real = this - > real;
tmp.imag = this - > imag * -1;
return tmp;
}
complex complex::getreciprocal()
{
complex t;
t.real = real;
t.imag = imag * -1;
float div;
div = (real * real) + (imag * imag);
t.real /= div;
t.imag /= div;
return t;
}
float complex::getmodulus()
{
float z;
z = (real * real) + (imag * imag);
z = sqrt(z);
return z;
}
void complex::setdata(float r, float i)
{
real = r;
imag = i;
}
void complex::getdata()
{
cout << "Enter Real:";
cin >> this - > real;
cout << "Enter Imaginary:";
cin >> this - > imag;
}
float complex::getreal()
{
return real;
}
float complex::getimaginary()
{
return imag;
}
bool complex::operator == (complex c)
{
return (real == c.real) && (imag == c.imag) ? 1 : 0;
}
ostream & operator << (ostream & s, complex & c)
{
s << "Real Part = " << c.real << endl << "Imaginary Part = " << c.imag << endl;
s << "z = " << c.real << setiosflags(ios::showpos) << c.imag << "i" << endl << resetiosflags(ios::showpos);
return s;
}
int main()
{
complex a(10.0 f, -2. f); // Calls Constructor
cout << a << endl; // Calls the overloaded << operator
complex b(-2); // Calls Constructor
complex c = b; // Calls Copy Constructor
c = a; // calls overloaded = operator
b.getdata(); // Calls Getdata()
c.getdata();
if (b == c) // calls overloaded == operator
cout << "\nb == c";
else
cout << "\nb != c";
cout << endl << c.getmodulus() << endl; // calls getmodulus function()
complex d;
d = a + b;
cout << d << endl;
d = a - b;
cout << d << endl;
d = a * b;
cout << d << endl;
d = a / b;
cout << d << endl;
return 0;
}
2. Sort an array
#include<iostream.h>
#include<string.h>
template < class t >
int sort(t * a, int n)
{
int flag = 1, i, j;
t temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < (n - 1 - i); j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
flag = 0;
}
}
if (flag)
break;
}
}
int main()
{
int a[5], n, i;
cout << "\nenter the size of the array";
cin >> n;
cout << "\nenter the elements of integer array ";
for (i = 0; i < n; i++)
cin >> a[i];
cout << "\nelements of the unsorted array are:" << endl;
for (i = 0; i < n; i++)
cout << a[i] << "\n";
sort(a, n);
cout << "elements of the sorted array array:" << endl;
for (i = 0; i < n; i++)
cout << a[i] << "\n";
}
Area and perimeter of rectangle
#include<iostream.h>
#include<string.h>
class shape
{
protected:
float inp_perimeter;
float inp_area;
public:
void area();
void perimeter();
void show();
};
class rectangle: public shape
{
float length;
float breadth;
public:
rectangle()
{
cout << "\nenter the length";
cin >> length;
cout << "\nenter the breadth";
cin >> breadth;
}
void area()
{
inp_area = length * breadth;
}
void perimeter()
{
inp_perimeter = 2 * (length + breadth);
}
void show()
{
cout << "\narea of rectangle = " << inp_area;
cout << "\nperimeter of rectangle = " << inp_perimeter;
}
};
int main()
{
rectangle r;
rectangle * ptr[3] = {
& r,
& r,
& r
};
int i;
for (i = 0; i < 3; i++)
ptr[i] - > area();
for (i = 0; i < 3; i++)
ptr[i] - > perimeter();
for (i = 0; i < 3; i++)
ptr[i] - > show();
}
4. Program to search an element in a Dynamic Array
int key;
cout << "Enter a value to search for:" << endl;
cin >> key;
int location = search(a, arraySize, key); //function "location" is called
if (location == -1)
cout << key << " is not in the array.";
else
cout << key << " is element " << location << " in the array.";
delete[] a;
cin.ignore(255, '\n');
cin.get();
return 0;
}
int search(int a[], int size, int key)
{
int i = 0;
while ((a[i] != key) && (i < size))
i++;
if (i == size) //if target is not in a
i = -1;
return i;
}
5. Program to get address of an integer
#include <iostream.h>
using namespace std;
int main()
{
int xyz;
cout << & xyz;
}