Coding in C++ Additional files are:- complexInput.txt (3+4i) * (5-6i) complexOut
ID: 3744631 • Letter: C
Question
Coding in C++
Additional files are:-
complexInput.txt (3+4i) * (5-6i)
complexOutput.txt 39+2i
complexInputWrong.txt (3+4i) - dummy
What you will learn . Implementing templates Improve grasp on file I/O Coding exercise This exercise extends Lab 2, so make sure you start this after you finish Lab 2 before starting this assignment 1. Create a new C++ project with the same complexNumber as lab 2 implemented as a template. Create a class called complexNumber that stores a complex number of the form a+bi. a is the real and b is the imaginary part of the number. The type of the numbers a and b should be generic. Implement the ability to add, subtract, and multiply two complexNumber objects to create another complexNumber object by overloading operators ,, and * a. b. c. Overload the operatorsandExplanation / Answer
#include<iostream>
#include <fstream>
#include<conio.h>
#include<math.h>
struct complexNumber
{
float rel;
float img;
}s1,s2;
int main()
{
clrscr();
fstream file; //object of fstream class
//opening file "complexInput.txt" in out(write) mode
file.open("complexInput.txt",ios::in);
if(!file)
{
cout<<"Error in creating file";
return 0;
}
cout<<"File created successfully";
float a,b;
cout<<"Enter real and imaginary part of 1st complex number:";
cin>>s1.rel>>s1.img;
cout<<"Enter real and imaginary part of 2nd complex number:";
cin>>s2.rel>>s2.img;
//Addition
a=(s1.rel)+(s2.rel);
b=(s1.img)+(s2.img);
cout<<"nAddition: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
//Subtraction
a=(s1.rel)-(s2.rel);
b=(s1.img)-(s2.img);
cout<<"nSubtraction: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
//Multiplication
a=((s1.rel)*(s2.rel))-((s1.img)*(s2.img));
b=((s1.rel)*(s2.img))+((s2.rel)*(s1.img));
cout<<"nMultiplication: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
freopen ("complexInput.txt","w",stdout);
sOutFile << stdout;
sOutFile("complexOutput.txt" );
file.close();
fclose (stdout);
return 0;
getch();
}