-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_2.cpp
More file actions
79 lines (63 loc) · 1.8 KB
/
5_2.cpp
File metadata and controls
79 lines (63 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <vector>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex() : real(0), imag(0) {}
Complex(double r, double i) : real(r), imag(i) {}
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
Complex operator-(const Complex& other) const {
return Complex(real - other.real, imag - other.imag);
}
friend ostream& operator<<(ostream& out, const Complex& c) {
out << c.real;
if (c.imag >= 0)
out << " + " << c.imag << "i";
else
out << " - " << -c.imag << "i";
return out;
}
friend istream& operator>>(istream& in, Complex& c) {
cout << "Enter real part: ";
in >> c.real;
cout << "Enter imaginary part: ";
in >> c.imag;
return in;
}
};
Complex Add(const vector<Complex>& list) {
Complex sum;
for (const auto& c : list) {
sum = sum + c;
}
return sum;
}
Complex Subtract(const vector<Complex>& list) {
if (list.empty()) return Complex();
Complex result = list[0];
for (size_t i = 1; i < list.size(); ++i) {
result = result - list[i];
}
return result;
}
int main() {
int n;
cout << "Enter number of complex numbers: ";
cin >> n;
vector<Complex> numbers(n);
for (int i = 0; i < n; ++i) {
cout << "\nComplex number " << i + 1 << ":\n";
cin >> numbers[i];
}
Complex sum = Add(numbers);
Complex diff =Subtract(numbers);
cout << "\nSum of all complex numbers: " << sum << endl;
cout << "\nSubtraction result: " << diff << endl;
cout<<"\n24ce052_Pushti kansara";
return 0;
}