Categories
Uncategorised

Classes – Time Data

/* Copyright © 2007 Ankur Banerjee. Special thanks to Naman Bagga for source code verification. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. http://www.fsf.org/licensing/licenses/lgpl.html /

/ Classes – Program to perform functions on time data /

#include <iostream.h>
#include <conio.h>

class time
{
int hours, minutes, second;
public:
void readtime(int h, int m, int s)
{
hours = h;
minutes = m;
second = s;
}
void showtime()
{
cout<<hours<<” : “<<minutes<<” : “<<second;
}
time addtime(time t1, time t2)
{
time t3;
t3.second = (t1.second + t2.second) % 60;
t3.minutes = (t1.minutes + t2.minutes) % 60;
t3.minutes += (t1.second + t2.second) / 60;
t3.hours = t1.hours + t2.hours;
t3.hours += (t1.minutes + t2.minutes) / 60;
return t3;
}
} time1, time2, time3;

void main()
{
clrscr();
int hh, mm, ss;
cout<<endl<<“Program to perform functions on time”
<<endl<<“Note : Use hours minutes seconds time format”;
cout<<endl<<“Enter first time duration : “;
cin>>hh>>mm>>ss;
time1.readtime(hh, mm, ss);
cout<<endl<<“Enter second time duration : “;
cin>>hh>>mm>>ss;
time2.readtime(hh, mm, ss);
time3 = time3.addtime(time1, time2);
cout<<endl<<“Added time duration is “;
time3.showtime();
cout<<endl;
getch();
}

/ Output */

Program to perform functions on time
Note : Use hours minutes seconds time format

Enter first time duration : 18 42 42
Enter second time duration : 9 6 19
Added time duration is 27 : 49 : 1

Categories
Uncategorised

Classes – Complex Numbers

/* Copyright © 2007 Ankur Banerjee. Special thanks to Naman Bagga for source code verification. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. http://www.fsf.org/licensing/licenses/lgpl.html /

/ Classes – Program to perform operations on complex numbers /

#include <iostream.h>
#include <conio.h>

class complexnum
{
int real, imaginary;
public:
void readdata()
{
cout<<endl<<“Enter real part of complex number : “;
cin>>real;
cout<<“Enter imaginary part of complex number : “;
cin>>imaginary;
}
void display()
{
cout<<real<<” + “<<imaginary<<“i”;
}
complexnum addcomplex(complexnum n1, complexnum n2)
{
complexnum n3;
n3.real = n1.real + n2.real;
n3.imaginary = n1.imaginary + n2.imaginary;
return n3;
}
complexnum subcomplex(complexnum n1, complexnum n2)
{
complexnum n3;
n3.real = n1.real – n2.real;
n3.imaginary = n1.imaginary – n2.imaginary;
return n3;
}
} num1, num2, num3;

void main()
{
clrscr();
int ch;
char choice;
cout<<endl<<“Program to carry out operations on complex numbers”
<<“\n\nEnter first complex number : “;
num1.readdata();
cout<<endl<<“Enter second complex number : “;
num2.readdata();
do
{
cout<<endl<<“Program Menu”<<endl
<<“1. Add the two complex numbers”<<endl
<<“2. Subtract the two complex numbers”<<endl
<<“Enter your choice : “;
cin>>ch;
if (ch == 1)
{
num3 = num3.addcomplex(num1, num2);
cout<<endl<<“Result after addition : “;
num3.display();
}
else if (ch == 2)
{
num3 = num3.subcomplex(num1, num2);
cout<<endl<<“Result after subtraction : “;
num3.display();
}
else cout<<endl<<“You entered an incorrect choice”;
cout<<endl<<“Do you want to continue – y / n : “;
cin>>choice;
} while (choice == ‘y’ || choice == ‘Y’);
cout<<endl;
getch();
}

/ Output */

Program to carry out operations on complex numbers

Enter first complex number :
Enter real part of complex number : 8
Enter imaginary part of complex number : 5

Enter second complex number :
Enter real part of complex number : 9
Enter imaginary part of complex number : 6

Program Menu
1. Add the two complex numbers
2. Subtract the two complex numbers
Enter your choice : 1

Result after addition : 17 + 11i

Program Menu
1. Add the two complex numbers
2. Subtract the two complex numbers
Enter your choice : 2

Result after subtraction : -1 + -1i