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
  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

  • Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator...

    Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator Overloading – Chapter 14 Design a class Complex for representing complex numbers and the write overloaded operators for adding, subtracting, multiplying and dividing 2 complex numbers. Also create a function that will print a complex number on the screen. Provide appropriate constructors...

  • Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 file...

    Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator Overloading – Chapter 14 Design a class Complex for representing complex numbers and the write overloaded operators for adding, subtracting, multiplying and dividing 2 complex numbers. Also create a function that will print a complex number on the screen. Provide appropriate constructors...

  • 5. A complex number consists of two components: the real component and the imaginary component. A...

    C++ //add as many comments as possible 5. A complex number consists of two components: the real component and the imaginary component. An example of a complex number is 2+3i, where 2 is the real component and 3 is the imaginary component of the data. Define a class MyComplexClass. It has two data values of float type: real and imaginary This class has the following member functions A default constructor that assigns 0.0 to both its real and imaginary data...

  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • C language huffman This exercise will familiarize you with linked lists, which you will need for...

    C language huffman This exercise will familiarize you with linked lists, which you will need for a subsequent programming Getting Started assignment Overview Requirements Getting Started Submit Start by getting the files. Type 264get hw13 and then cd hw13 from bash. Pre-tester You will get the following files: Q&A Updates 1. huffman.h: An empty header file, you have to define your own functions in this homework. 2. huffman.c: An empty c file, you have to define your own functions in...

  • C++ requirements The store number must be of type unsigned int. The sales value must be...

    C++ requirements The store number must be of type unsigned int. The sales value must be of of type long long int. Your program must properly check for end of file. See the section Reading in files below and also see your Gaddis text book for details on reading in file and checking for end of file. Your program must properly open and close all files. Failure to follow the C++ requirements could reduce the points received from passing the...

  • The files must be called Proper coding conventions required the first letter of the class start...

    The files must be called Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter. Only submit the .java file needed to make the program run. Do not submit the .class file or any other file. 5% Style Components Include properly formatted prologue, comments, indenting, and other style elements as shown in Chapter 2 starting page 64 and Appendix 5 page 881-892....

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data...

    need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data file – It has: player ID, player first name, middle initial, last name soccer.txt – Player information file – It has: player ID, goals scored, number of penalties, jersey number output file - formatted according to the example provided later in this assignment a) Define a struct to hold the information for a person storing first name, middle initial, and   last name). b) Define...

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