Below is a code that is used to calculate complex numbers (a+bi, where i = sqroot (-1)) through multiplication and addition. However, on my output file, no Header is being printed; the only thing that is being printed is "8 + 7i + = "
Is anyone able to tell me what is wrong with it? "complex.h" is included at the end of the code, if you need a look.
Code:
// Trey Brumley// CMPS
// Dr. Tina Johnson
// March 1, 2013
// Program 2: Classes
// This program will demonstrate the use of classes by using a custom "complex-number" (a+bi) class.
// =================================================================================================
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include "complex.h"
using namespace std;
void Header();
void PrintComplex (complex cn);
int main()
{
ofstream outfile;
outfile.open("output.txt");
Header;
complex c1, c2, c3, c4(3.0, 4.0);
double real, imag;
c2.SetReal(5.0);
c2.SetImag(3.0);
real = c4.GetReal();
imag = c4.GetImag();
c3 = c2.multiply(c4);
c1 = c2.add(c4);
PrintComplex(c2);
outfile << " * ";
PrintComplex(c4);
outfile << " = ";
PrintComplex(c3);
outfile << endl << endl;
PrintComplex(c2);
outfile << " + ";
PrintComplex(c4);
outfile << " = ";
PrintComplex(c1);
outfile << endl;
system("pause");
return 0;
}
//This function will print a header on the outfile.
void Header()
{
ofstream outfile;
outfile.open("output.txt");
outfile << "Trey Brumley" << endl;
outfile << "Johnson - 1063-201" << endl;
outfile << "March 1, 2013" << endl;
outfile << "Program 2: Classes" << endl;
outfile << "===================" << endl << endl;
outfile << "This program will demonstrate the use of classes by using a custom complex-number (a+bi) class." << endl << endl;
return;
}
void PrintComplex (complex c)
{
ofstream outfile;
outfile.open("output.txt");
if (c.GetImag() < 0)
outfile << c.GetReal() << " - " << (c.GetImag() * -1) << "i ";
else
outfile << c.GetReal() << " + " << c.GetImag() << "i ";
}
complex::complex(void)
{
}
complex::~complex(void)
{
}
complex::complex(double r, double im)
{
real = r;
imag = im;
}
void complex::SetReal(double r)
{
real = r;
}
void complex::SetImag(double im)
{
imag = im;
}
double complex::GetReal()
{
return real;
}
double complex::GetImag()
{
return imag;
}
complex complex::multiply(complex comp)
{
complex c;
c.real = (real * comp.real) + (imag * comp.imag * -1);
c.imag = (imag * comp.real) + (real * comp.imag);
return c;
}
complex complex::add(complex comp)
{
complex c;
c.real = real + comp.real;
c.imag = imag + comp.imag;
return c;
}
#pragma once
class complex
{
private:
double real;
double imag;
public:
complex(void);
complex(double r, double im);
~complex(void);
void SetReal(double r);
void SetImag(double im);
double GetReal();
double GetImag();
complex add(complex c);
complex multiply(complex c);
};