[go: up one dir, main page]

0% found this document useful (0 votes)
12 views8 pages

PBL With Constructor

The document is a C++ program that defines a 'complex' class for performing operations on complex numbers, including addition, subtraction, multiplication, conjugation, and division. The main function provides a menu-driven interface for users to interact with two complex numbers, allowing them to choose operations and display results. The program continues to prompt the user until they choose to exit.

Uploaded by

ishapandit1603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

PBL With Constructor

The document is a C++ program that defines a 'complex' class for performing operations on complex numbers, including addition, subtraction, multiplication, conjugation, and division. The main function provides a menu-driven interface for users to interact with two complex numbers, allowing them to choose operations and display results. The program continues to prompt the user until they choose to exit.

Uploaded by

ishapandit1603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

Input

#include <iostream>

#include <cmath>

using namespace std;

class complex {

private:

float real;

float img;

public:

complex(float a, float b) : real(a), img(b) {}

complex() : real(0), img(0) {}

void display();

complex add(complex);

complex sub(complex);

complex mul(complex);

complex conj();

complex div(complex);

};

int main() {

complex c1(3, 5), c2(2.4, 5), result, difference, product, conjugate, division;

int op;
do {

cout << " 1. Display \n2. Addition \n3. Subtraction \n4. Multiplication \n5. Conjugate \n6. Division \
n7. Exit \n";

cout << "Enter option: ";

cin >> op;

switch (op) {

case 1:

cout << "c1 is: ";

c1.display();

cout << "c2 is: ";

c2.display();

break;

case 2:

cout << "Addition: ";

result = c1.add(c2);

result.display();

break;

case 3:

cout << "Subtraction: ";

difference = c1.sub(c2);

difference.display();

break;

case 4:

cout << "Multiplication: ";

product = c1.mul(c2);
product.display();

break;

case 5:

cout << "Conjugate of c1: ";

conjugate = c1.conj();

conjugate.display();

break;

case 6:

cout << "Division: ";

division = c1.div(c2);

division.display();

break;

case 7:

cout << "Exit\n";

break;

default:

cout << "Invalid option. Please try again.\n";

} while (op != 7);

return 0;

void complex::display() {

if (img < 0)
cout << real << "" << img << "i" << endl;

else

cout << real << "+" << img << "i" << endl;

complex complex::add(complex c2) {

complex s1;

s1.real = real + c2.real;

s1.img = img + c2.img;

return s1;

complex complex::sub(complex c2) {

complex x;

x.real = real - c2.real;

x.img = img - c2.img;

return x;

complex complex::mul(complex c2) {

complex p;

p.real = (real * c2.real) - (img * c2.img);

p.img = (real * c2.img) + (img * c2.real);

return p;

}
complex complex::conj() {

complex c;

c.real = real;

c.img = -img;

return c;

complex complex::div(complex c2) {

float d = (pow(c2.real, 2) + pow(c2.img, 2));

if (d == 0) {

cout << "Error: Division by zero!" << endl;

return complex();

complex c3 = c2.conj();

complex c4 = this->mul(c3);

complex division;

division.real = c4.real / d;

division.img = c4.img / d;

return division;

Output

1. Display

2. Addition

3. Subtraction
4. Multiplication

5. Conjugate

6. Division

7. Exit

Enter option: 1

c1 is: 3+5i

c2 is: 2.4+5i

1. Display

2. Addition

3. Subtraction

4. Multiplication

5. Conjugate

6. Division

7. Exit

Enter option: 2

Addition: 5.4+10i

1. Display

2. Addition

3. Subtraction

4. Multiplication

5. Conjugate

6. Division

7. Exit

Enter option: 3

Subtraction: 0.6+0i
1. Display

2. Addition

3. Subtraction

4. Multiplication

5. Conjugate

6. Division

7. Exit

Enter option: 4

Multiplication: -17.8+27i

1. Display

2. Addition

3. Subtraction

4. Multiplication

5. Conjugate

6. Division

7. Exit

Enter option: 5

Conjugate of c1: 3-5i

1. Display

2. Addition

3. Subtraction

4. Multiplication

5. Conjugate

6. Division

7. Exit
Enter option: 6

Division: 1.04681-0.0975293i

1. Display

2. Addition

3. Subtraction

4. Multiplication

5. Conjugate

6. Division

7. Exit

Enter option: 7

Exit

You might also like