Experiment 10
Aim
To develop C++ programs that randomly generate complex
numbers using the previously designed Complex class, write two complex numbers
per line in a file along with an arithmetic operator, and read the file line by
line to perform the specified operation.
Objectives
- To
use the previously designed Complex class.
- To
generate random complex numbers.
- To
perform file output and input operations.
- To
store arithmetic operators along with complex numbers.
- To
read and process data line by line.
- To
perform arithmetic operations on Complex objects.
Theory
File handling allows data to be stored permanently rather
than only in main memory. C++ provides:
- ofstream
for writing to files.
- ifstream
for reading from files.
In this experiment, each line contains two complex numbers
and an arithmetic operator.
For example:
(4 + 3i) (2 + 1i) +
The second program reads each line and calculates:
(4 + 3i) + (2 + 1i)
Program 1: Generate and Store Complex Numbers
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Complex {
int r, i;
public:
Complex(int x = 0,
int y = 0) {
r = x;
i = y;
}
void
write(ofstream &file) {
file <<
"(" << r << " + " << i <<
"i)";
}
};
int main() {
ofstream
file("complex.txt");
srand(time(0));
char op[] = {'+',
'-', '*', '/'};
for (int n = 0; n
< 5; n++) {
Complex
a(rand() % 10, rand() % 10);
Complex
b(rand() % 10, rand() % 10);
a.write(file);
file << " " <<
op[rand() % 4] << " ";
b.write(file);
file <<
endl;
}
file.close();
cout <<
"Complex numbers written to file.\n";
return 0;
}
Program 2: Read File and Perform Operation
#include <iostream>
#include <fstream>
using namespace std;
class Complex {
double r, i;
public:
Complex(double x =
0, double y = 0) {
r = x;
i = y;
}
Complex
operator+(Complex c) {
return
Complex(r + c.r, i + c.i);
}
Complex
operator-(Complex c) {
return
Complex(r - c.r, i - c.i);
}
Complex
operator*(Complex c) {
return
Complex(r*c.r - i*c.i,
r*c.i + i*c.r);
}
Complex
operator/(Complex c) {
double d =
c.r*c.r + c.i*c.i;
if (d == 0)
throw
"Division by zero";
return
Complex(
(r*c.r +
i*c.i) / d,
(i*c.r -
r*c.i) / d
);
}
void display() {
cout <<
"(" << r << " + " << i <<
"i)";
}
};
int main() {
ifstream
file("complex.txt");
double r1, i1, r2,
i2;
char ch, op;
Complex result;
while (file
>> ch >> r1 >> ch >> i1 >> ch
>> ch >> r2 >> ch >> i2 >> ch >> op)
{
Complex a(r1,
i1), b(r2, i2);
try {
switch
(op) {
case
'+':
result = a + b;
break;
case
'-':
result = a - b;
break;
case
'*':
result = a * b;
break;
case
'/':
result = a / b;
break;
}
a.display();
cout
<< " " << op << " ";
b.display();
cout
<< " = ";
result.display();
cout
<< endl;
}
catch (const
char *msg) {
cout
<< "Exception: " << msg << endl;
}
}
file.close();
return 0;
}
Sample Output
Complex numbers written to file.
(4 + 3i) + (2 + 1i) = (6 + 4i)
(7 + 2i) - (3 + 5i) = (4 + -3i)
(2 + 4i) * (3 + 1i) = (2 + 14i)
(8 + 6i) / (2 + 2i) = (3.5 + -0.5i)
Result
Thus, complex numbers were successfully generated
randomly, stored in a file along with arithmetic operators, read from the file,
and processed using overloaded Complex class operators.
No comments:
Post a Comment