convert the following c++ code in c   #include #include using namespace std; class Complex { public: Complex(double r = 0.0, double i = 0.0); // constructor void setReal(double real); void setImag(double imag); double getReal(); double getImag(); Complex operator+(const Complex&)const ; // operator+() Complex operator-(const Complex&)const ; // operator-()    /** * read the complex number to an input stream */ friend std::istream & operator >>(std::istream &din, Complex &); /** * Friend function declarations * Output complex number to an output stream, Example 3+4i */ friend std::ostream & operator <<(std::ostream &dout, const Complex&); operator double(); private: //Declaring variables double real, imaginary; }; //Parameterized constructor Complex::Complex(double r,double i) { this->real=r; this->imaginary=i; } //Function implementation which adds two complex numbers by using the operator overloading '+' Complex Complex::operator +(const Complex &c)const { Complex temp; temp.real=real+c.real; temp.imaginary=imaginary+c.imaginary; return temp; } //Function implementation which subtract two complex numbers by using the operator overloading '-' Complex Complex::operator -(const Complex &c)const { Complex temp; temp.real=real-c.real; temp.imaginary=imaginary-c.imaginary; return temp; } //Function implementation which read complex numbers by using the operator overloading '>>' istream & operator >> (istream &din, Complex &c) { char op,i; din>>c.real; din>>op; din>>c.imaginary; din>>i; if(op=='-') c.imaginary=-(c.imaginary);    return din;    } //Function implementation which display complex numbers by using the operator overloading '<<' ostream & operator << (ostream &dout, const Complex &c) { if(c.imaginary<0) dout<>X; cin>>Y;    cout<<"Complex Number#1:"<

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

convert the following c++ code in c

 

#include <iostream>
#include <iomanip>
using namespace std;

class Complex
{
public:
Complex(double r = 0.0, double i = 0.0); // constructor
void setReal(double real);
void setImag(double imag);
double getReal();
double getImag();
Complex operator+(const Complex&)const ; // operator+()
Complex operator-(const Complex&)const ; // operator-()
  
/**
* read the complex number to an input stream
*/
friend std::istream & operator >>(std::istream &din, Complex &);
/**
* Friend function declarations
* Output complex number to an output stream, Example 3+4i
*/
friend std::ostream & operator <<(std::ostream &dout, const Complex&);

operator double();
private:
//Declaring variables
double real, imaginary;
};

//Parameterized constructor
Complex::Complex(double r,double i)
{
this->real=r;
this->imaginary=i;
}
//Function implementation which adds two complex numbers by using the operator overloading '+'
Complex Complex::operator +(const Complex &c)const
{
Complex temp;
temp.real=real+c.real;
temp.imaginary=imaginary+c.imaginary;
return temp;
}
//Function implementation which subtract two complex numbers by using the operator overloading '-'
Complex Complex::operator -(const Complex &c)const
{
Complex temp;
temp.real=real-c.real;
temp.imaginary=imaginary-c.imaginary;
return temp;
}
//Function implementation which read complex numbers by using the operator overloading '>>'
istream & operator >> (istream &din, Complex &c)
{
char op,i;
din>>c.real;
din>>op;
din>>c.imaginary;
din>>i;
if(op=='-')
c.imaginary=-(c.imaginary);
  
return din;   
}
//Function implementation which display complex numbers by using the operator overloading '<<'
ostream & operator << (ostream &dout, const Complex &c)
{
if(c.imaginary<0)
dout<<c.real<<" - "<<(-c.imaginary)<<" i ";
else
dout<<c.real<<" + "<<c.imaginary<<" i ";
return dout;
}
// conversion operator: return float value of fraction
Complex::operator double() {
return double(real) / double(imaginary);
}

int main()
{
Complex X,Y,Z;
cout<<"Enter two complex numbers (in form 7.1+8.3i):"<<endl;
cin>>X;
cin>>Y;
  
cout<<"Complex Number#1:"<<X<<endl;
cout<<"Complex Number#2:"<<Y<<endl;
  
Z=X+Y;
cout<<X<<" + "<<Y<<" = "<<Z<<endl;

system("pause");
return 0;

}

#include <iostream>
#include <iomanip>
using namespace std;

class Complex
{
public:
Complex(double r = 0.0, double i = 0.0); // constructor
void setReal(double real);
void setImag(double imag);
double getReal();
double getImag();
Complex operator+(const Complex&)const ; // operator+()
Complex operator-(const Complex&)const ; // operator-()
  
/**
* read the complex number to an input stream
*/
friend std::istream & operator >>(std::istream &din, Complex &);
/**
* Friend function declarations
* Output complex number to an output stream, Example 3+4i
*/
friend std::ostream & operator <<(std::ostream &dout, const Complex&);

operator double();
private:
//Declaring variables
double real, imaginary;
};

//Parameterized constructor
Complex::Complex(double r,double i)
{
this->real=r;
this->imaginary=i;
}
//Function implementation which adds two complex numbers by using the operator overloading '+'
Complex Complex::operator +(const Complex &c)const
{
Complex temp;
temp.real=real+c.real;
temp.imaginary=imaginary+c.imaginary;
return temp;
}
//Function implementation which subtract two complex numbers by using the operator overloading '-'
Complex Complex::operator -(const Complex &c)const
{
Complex temp;
temp.real=real-c.real;
temp.imaginary=imaginary-c.imaginary;
return temp;
}
//Function implementation which read complex numbers by using the operator overloading '>>'
istream & operator >> (istream &din, Complex &c)
{
char op,i;
din>>c.real;
din>>op;
din>>c.imaginary;
din>>i;
if(op=='-')
c.imaginary=-(c.imaginary);
  
return din;   
}
//Function implementation which display complex numbers by using the operator overloading '<<'
ostream & operator << (ostream &dout, const Complex &c)
{
if(c.imaginary<0)
dout<<c.real<<" - "<<(-c.imaginary)<<" i ";
else
dout<<c.real<<" + "<<c.imaginary<<" i ";
return dout;
}
// conversion operator: return float value of fraction
Complex::operator double() {
return double(real) / double(imaginary);
}

int main()
{
Complex X,Y,Z;
cout<<"Enter two complex numbers (in form 7.1+8.3i):"<<endl;
cin>>X;
cin>>Y;
  
cout<<"Complex Number#1:"<<X<<endl;
cout<<"Complex Number#2:"<<Y<<endl;
  
Z=X+Y;
cout<<X<<" + "<<Y<<" = "<<Z<<endl;

system("pause");
return 0;

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY