0% found this document useful (0 votes)
22 views6 pages

Complex No With Constructor & String Class

The document contains C++ code for two classes: 'complex' for performing operations on complex numbers and 'String' for manipulating strings. The 'complex' class includes methods for addition, subtraction, multiplication, division, and conjugate, while the 'String' class provides functionalities for input, display, concatenation, reversal, and length calculation. The main functions demonstrate user interaction for both classes, showcasing their capabilities through various operations.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

Complex No With Constructor & String Class

The document contains C++ code for two classes: 'complex' for performing operations on complex numbers and 'String' for manipulating strings. The 'complex' class includes methods for addition, subtraction, multiplication, division, and conjugate, while the 'String' class provides functionalities for input, display, concatenation, reversal, and length calculation. The main functions demonstrate user interaction for both classes, showcasing their capabilities through various operations.

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 PDF, TXT or read online on Scribd
You are on page 1/ 6

Complex No with Constructor

Input
#include <iostream>
#include <cmath>
using namespace std;

class complex {
private:
float real;
float img;

public:
complex() {}
complex(float r, float i) : real(r), img(i) {}
void display();
complex add(complex);
complex sub(complex);
complex mul(complex);
complex conj();
complex div(complex);
};

int main() {
float r1, i1, r2, i2;
cout << "Enter c1 (real and imaginary): ";
cin >> r1 >> i1;
cout << "Enter c2 (real and imaginary): ";
cin >> r2 >> i2;

complex c1(r1, i1), c2(r2, i2), 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) {


return complex(real + c2.real, img + c2.img);
}
complex complex::sub(complex c2) {
return complex(real - c2.real, img - c2.img);
}

complex complex::mul(complex c2) {


return complex((real * c2.real) - (img * c2.img), (real * c2.img) + (img * c2.real));
}

complex complex::conj() {
return complex(real, -img);
}

complex complex::div(complex c2) {


float d = (pow(c2.real, 2) + pow(c2.img, 2));
complex c3 = c2.conj();
complex c4 = this->mul(c3);
return complex(c4.real / d, c4.img / d);
}

Output
Enter c1 (real and imaginary):
2
4
Enter c2 (real and imaginary):
2
4
1. Display
2. Addition
3. Subtraction
4. Multiplication
5. Conjugate
6. Division
7. Exit
Enter option: 1
c1 is: 2+4i
c2 is: 2+4i
1. Display
2. Addition
3. Subtraction
4. Multiplication
5. Conjugate
6. Division
7. Exit
Enter option: 2
Addition: 4+8i
1. Display
2. Addition
3. Subtraction
4. Multiplication
5. Conjugate
6. Division
7. Exit
Enter option: 3
Subtraction: 0+0i
1. Display
2. Addition
3. Subtraction
4. Multiplication
5. Conjugate
6. Division
7. Exit
Enter option: 4
Multiplication: -12+16i
1. Display
2. Addition
3. Subtraction
4. Multiplication
5. Conjugate
6. Division
7. Exit
Enter option: 5
Conjugate of c1: 2-4i
1. Display
2. Addition
3. Subtraction
4. Multiplication
5. Conjugate
6. Division
7. Exit
Enter option: 6
Division: 1+0i
1. Display
2. Addition
3. Subtraction
4. Multiplication
5. Conjugate
6. Division
7. Exit
Enter option: 7
Exit

=== Code Execution Successful ===


String
#include <iostream>
#include <cstring>
using namespace std;

class String {
public:
char str[100];

void input() {
cin.getline(str, 100);
}

void display() {
cout << str << endl;
}

int getLength() {
return strlen(str);
}

void concat(String s2) {


strcat(str, s2.str);
}

void reverse() {
int len = getLength();
for (int i = 0; i < len / 2; i++)
swap(str[i], str[len - i - 1]);
}
};
int main() {
String s1, s2;

cout << "Enter first string: ";


s1.input();
cout << "Enter second string: ";
s2.input();

cout << "\nConcatenated: ";


s1.concat(s2);
s1.display();

cout << "Reversed First String: ";


s1.reverse();
s1.display();

cout << "Length of First String: " << s1.getLength() << endl;

return 0;
}

Output
Enter first string: Happy
Enter second string: Birthday

Concatenated: Happy Birthday


Reversed First String: yadhtriB yppaH
Length of First String: 14

You might also like