Question

Coding in C++

Additional files are:-

complexInput.txt   (3+4i) * (5-6i)

complexOutput.txt 39+2i

complexInputWrong.txt (3+4i) - dummy

USE COMPLEX.H, COMPLEX.CPP AND MAIN.CPP

What you will learn Implementing classes .File I/O More operator overloading .Error checking Coding exercise Create a class called complexNumber that stores a complex number of the form a+bi, where i is v-I. a is the real and b is the imaginary part of the number1. You should be able to get the real and imaginary parts of the number. a and b can be negative, e.g., 3+4i, -3+4i, 3-41, and -3- 4i 1. 2. Implement the ability to add, subtract, and multiply two complexNumber objects and save the 3. Overload the operator>and< to input and output string of the form a+bi from the 4. Read a file called complexInput.txt (sample input file attached) containing numbers of the 5. Perform the operations on the numbers you read and store the result in a new result in another complexNumber object by overloading operators +,-, and* complexNumber object respectively form (a+bi) followed by an operator followed by another complex number of the form (a+b). complexNumber. Output the number in a new output file called complexoutput.txt (sample output file included) [Bonus- optional] Implement error checking on the input and reject/ignore values that are not in the format a+bi. Log this information in the output file. A sample wrong input file called complexInputwrong.txt is attached. 6. What to turn in A zip file containing the files cmpe126-lab2.cpp, complex.cpp and complex.h files A file called Notes explaining your design

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

//Complex.h

#pragma once

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

class Complex

{

double real;

double imaginary;

public:

Complex();

Complex(string s);

Complex operator+(Complex &obj);

Complex operator-(Complex &obj);

Complex operator*(Complex &obj);

friend ostream& operator<<(ostream& out, Complex &obj);

friend ifstream& operator>>(ifstream& out, Complex &obj);

};

--------------------------------------------------

//Complex.cpp

#include"Sep_5_Complex.h"

Complex::Complex()

{

real = 0;

imaginary = 0;

}

Complex::Complex(string s)

{

string a, b;

bool isOp = false;

for (int i = 0; i < s.length(); i++)

{

if (s[i] != '+' || s[i] != '-')

{

if (!isOp)

a += s[i];

else

b += s[i];

}

else

{

isOp = true;

}

//convert a and b to double and store in real and imaginary respectively

real = stod(a);

imaginary = stod(b);

}

}

Complex Complex::operator+(Complex &obj)

{

Complex result;

result.real = real+obj.real;

result.imaginary = imaginary+obj.imaginary;

return result;

}

Complex Complex::operator-(Complex &obj)

{

Complex result;

result.real = real - obj.real;

result.imaginary = imaginary - obj.imaginary;

return result;

}

Complex Complex::operator*(Complex &obj)

{

Complex result;

result.real = real * obj.real;

result.imaginary = real * obj.imaginary;

result.imaginary += imaginary * obj.real;

result.imaginary += imaginary * obj.imaginary;

return result;

}

ostream& operator<<(ostream& out, Complex &obj)

{

//out << "Complex number is: ";

if(obj.imaginary > 0)

out << obj.real << "+" << obj.imaginary << "i";

else

out << obj.real << obj.imaginary << "i";

return out;

}

ifstream& operator>>(ifstream& in, Complex &obj)

{

string s;

string a, b;

bool isOp = false;

in >> s;

for (int i = 0; i < s.length(); i++)

{

if (s[i] != '+' && s[i] != '-' )

{

if (!isOp)

{

if(isdigit(s[i]))

a += s[i];

}

else

{

if (isdigit(s[i]))

b += s[i];

}

}

else

{

isOp = true;

}

}

//convert a and b to double and store in real and imaginary respectively

obj.real = stod(a);

obj.imaginary = stod(b);

return in;

}

-------------------------------------------------

//Main.cpp

#include<iostream>

#include<string>

#include<fstream>

#include"Sep_5_Complex.h"

using namespace std;

int main()

{

ifstream in;

ofstream out;

string a, b;

//open file for reading

in.open("complexInput.txt");

if (!in)

{

cout << "Nit able to open input file " << endl;

return -1;

}

//opern file for writing output

out.open("ComplexOutput.txt");

//check output file is open

if (!out)

{

cout << "Not able to open output file" << endl;

return -1;

}

//declare two objects of complex ttpe

Complex num1, num2,result;

char op;

while (!in.eof())

{

in >> num1;

in >> op;

in>>num2;

switch (op)

{

case '+':

result = num1 + num2;

out << "Addition of two complex numbers " << num1 << " and " << num2 << " is : " << result << endl;

break;

case '-':

result = num1 - num2;

out << "Subtraction of two complex numbers " << num1 << " and " << num2 << " is : " << result << endl;

break;

case '*':

result = num1 * num2;

out << "Multiplication of two complex numbers " << num1 << " and " << num2 << " is : " << result << endl;

break;

default:

cout << "Operator not implemented\n";

}

}

}

/*input file Complexinput.txt has below content

3+4i + 5+8i
12+14i - 8+20i
12+14i * 8+10i

---------------------------------------------------

output in file called ComplexOutput.txt

Addition of two complex numbers 3+4i and 5+8i is : 8+12i

Subtraction of two complex numbers 12+14i and 8+20i is : 4-6i

Multiplication of two complex numbers 12+14i and 8+10i is : 96+372i

*/

Add a comment
Know the answer?
Add Answer to:
Coding in C++ Additional files are:- complexInput.txt   (3+4i) * (5-6i) complexOutput.txt 39+2i complexInputWrong.txt (3+4i) - dummy...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT