R
Rock
I'm in the process of writing this program for complex numbers and I
use DevC++. My professor on the other hand compiles on Borland 5.5. So
I ocasionally save and run my work on Borland to see if it caught
anything, it's very picky... Anyway, the code below works on Dev, and
it compiles fine on Borland, but when I run it from borland, there is
no output, no error, it just skips right over the freind ostream call.
HELP! I'm new to this and so far it's been a very trying experience...
any help would be greatly appreciated. Rick
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
using namespace std;
//Class for complex numbers.
class Complex
{
public:
friend Complex operator +(const Complex& left, const Complex&
right);
friend Complex operator -(const Complex& left, const Complex&
right);
friend Complex operator *(const Complex& left, const Complex&
right);
friend bool operator ==(const Complex& left, const Complex&
right);
//overloaded operators
Complex(char in1[], char in2[]);//inputs taken in as Cstrings
Complex(char in1[]);
Complex();
void get_first(char ansr1[]);//returns doubles of first string
void get_secon(char ansr2[]);//returns doubles of second string
void foil(double lr, double li, double rr, double ri);
//Performs multiplication of 2 imaginary numbers
friend ostream& operator <<(ostream& outs, const Complex&
number);
private:
char answer[20];
char l_sign;
double l_real;
double l_imaginary;
char r_sign;
double r_real;
double r_imaginary;
double real_result1;
double imaginary_result1;
double real_result2;
double imaginary_result2;
double real_result3;
double imaginary_result3;
};
int main()
{
system("cls");
char answer1[80], answer2[80];
int x;
do
{
cout << "\n" << "Enter a complex number in the form of a + bi: ";
cin.getline(answer1,80);
cout << "\n" << "Enter the second complex number (Enter for
default): ";
cin.getline(answer2,80);
if (answer2[0] == '\0')
{
break;
}
Complex start(answer1, answer2);
cout << "\n" << start;
return 0;
}while(x==1);
Complex start(answer1);
cout << "\n" << start;
return 0;
}
Complex operator +(const Complex& left, const Complex& right)
{
return(left + right);
}
Complex operator -(const Complex& left, const Complex& right)
{
return(left - right);
}
Complex operator *(const Complex& left, const Complex& right)
{
return(left * right);
}
bool operator ==(const Complex& left, const Complex& right)
{
return(left == right);
}
Complex::Complex(char in1[], char in2[])
{
get_first(in1);
get_secon(in2);
if (!strcmp(in1, in2))
{
strcpy(answer, "equal");
}
else
{
strcpy(answer, "not equal");
}
real_result1 = (l_real + r_real);
imaginary_result1 = (l_imaginary + r_imaginary);
real_result2 = (l_real - r_real);
imaginary_result2 = (l_imaginary - r_imaginary);
foil(l_real, l_imaginary, r_real, r_imaginary);
}
Complex::Complex(char in1[]) : r_real(5), r_imaginary(2), r_sign('+')
{
get_first(in1);
if (l_real == r_real && l_imaginary == r_imaginary)
{
strcpy(answer, "equal");
}
else
{
strcpy(answer, "not equal");
}
real_result1 = (l_real + r_real);
imaginary_result1 = (l_imaginary + r_imaginary);
real_result2 = (l_real - r_real);
imaginary_result2 = (l_imaginary - r_imaginary);
foil(l_real, l_imaginary, r_real, r_imaginary);
}
Complex::Complex()
{
}
void Complex::get_first(char ansr1[])
{
char ans2[80], ans3[2], ans4[80], hold[80];
int x=0, y=0;
while(ansr1[x] != '+' && ansr1[x] != '-' || x == 0)
{
ans2[y] = ansr1[x];
x = x+1;
y = y+1;
}
ans2[y] = '\0';
y=0;
ans3[y] = ansr1[x];
x = x+1;
y=y+1;
ans3[y] = '\0';
y=0;
while(ansr1[x] != 'i')
{
ans4[y] = ansr1[x];
x = x+1;
y = y+1;
}
ans4[y] = '\0';
l_real = atof(ans2);
strcat(ans3, ans4);
l_imaginary = atof(ans3);
l_sign = ans3[0];
}
void Complex::get_secon(char ansr2[])
{
char ans2[80], ans3[2], ans4[80], hold[80];
int x=0, y=0;
while(ansr2[x] != '+' && ansr2[x] != '-' || x == 0)
{
ans2[y] = ansr2[x];
x = x+1;
y = y+1;
}
ans2[y] = '\0';
y=0;
ans3[y] = ansr2[x];
x = x+1;
y=y+1;
ans3[y] = '\0';
y=0;
while(ansr2[x] != 'i')
{
ans4[y] = ansr2[x];
x = x+1;
y = y+1;
}
ans4[y] = '\0';
r_real = atof(ans2);
strcat(ans3, ans4);
r_imaginary = atof(ans3);
r_sign = ans3[0];
}
void Complex::foil(double lr, double li, double rr, double ri)
{
double hold1, hold2;
hold1 = (lr * rr) + ((li * ri)*-1);
hold2 = (li * rr) + (lr * ri);
real_result3 = hold1;
imaginary_result3 = hold2;
}
ostream& operator <<(ostream& outs, const Complex& number)
{
outs << "\n" << "Numbers started with: " << number.l_real <<
number.l_sign
<< abs(number.l_imaginary) << "i" << " and " << number.r_real
<< number.r_sign
<< abs(number.r_imaginary) << "i" << "." << "\n" << "\n" <<
"Added they equal: "
<< number.real_result1 << "+" << number.imaginary_result1 <<
"i" << endl
<< "Subtracted they equal: " << number.real_result2 << "+" <<
number.real_result2
<< "i" << endl << "Multiplied they equal: " <<
number.real_result3 << "+"
<< number.imaginary_result3 << "i" << endl << "They are " <<
number.answer << "."
<< "\n" << endl;
return outs;
}
use DevC++. My professor on the other hand compiles on Borland 5.5. So
I ocasionally save and run my work on Borland to see if it caught
anything, it's very picky... Anyway, the code below works on Dev, and
it compiles fine on Borland, but when I run it from borland, there is
no output, no error, it just skips right over the freind ostream call.
HELP! I'm new to this and so far it's been a very trying experience...
any help would be greatly appreciated. Rick
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
using namespace std;
//Class for complex numbers.
class Complex
{
public:
friend Complex operator +(const Complex& left, const Complex&
right);
friend Complex operator -(const Complex& left, const Complex&
right);
friend Complex operator *(const Complex& left, const Complex&
right);
friend bool operator ==(const Complex& left, const Complex&
right);
//overloaded operators
Complex(char in1[], char in2[]);//inputs taken in as Cstrings
Complex(char in1[]);
Complex();
void get_first(char ansr1[]);//returns doubles of first string
void get_secon(char ansr2[]);//returns doubles of second string
void foil(double lr, double li, double rr, double ri);
//Performs multiplication of 2 imaginary numbers
friend ostream& operator <<(ostream& outs, const Complex&
number);
private:
char answer[20];
char l_sign;
double l_real;
double l_imaginary;
char r_sign;
double r_real;
double r_imaginary;
double real_result1;
double imaginary_result1;
double real_result2;
double imaginary_result2;
double real_result3;
double imaginary_result3;
};
int main()
{
system("cls");
char answer1[80], answer2[80];
int x;
do
{
cout << "\n" << "Enter a complex number in the form of a + bi: ";
cin.getline(answer1,80);
cout << "\n" << "Enter the second complex number (Enter for
default): ";
cin.getline(answer2,80);
if (answer2[0] == '\0')
{
break;
}
Complex start(answer1, answer2);
cout << "\n" << start;
return 0;
}while(x==1);
Complex start(answer1);
cout << "\n" << start;
return 0;
}
Complex operator +(const Complex& left, const Complex& right)
{
return(left + right);
}
Complex operator -(const Complex& left, const Complex& right)
{
return(left - right);
}
Complex operator *(const Complex& left, const Complex& right)
{
return(left * right);
}
bool operator ==(const Complex& left, const Complex& right)
{
return(left == right);
}
Complex::Complex(char in1[], char in2[])
{
get_first(in1);
get_secon(in2);
if (!strcmp(in1, in2))
{
strcpy(answer, "equal");
}
else
{
strcpy(answer, "not equal");
}
real_result1 = (l_real + r_real);
imaginary_result1 = (l_imaginary + r_imaginary);
real_result2 = (l_real - r_real);
imaginary_result2 = (l_imaginary - r_imaginary);
foil(l_real, l_imaginary, r_real, r_imaginary);
}
Complex::Complex(char in1[]) : r_real(5), r_imaginary(2), r_sign('+')
{
get_first(in1);
if (l_real == r_real && l_imaginary == r_imaginary)
{
strcpy(answer, "equal");
}
else
{
strcpy(answer, "not equal");
}
real_result1 = (l_real + r_real);
imaginary_result1 = (l_imaginary + r_imaginary);
real_result2 = (l_real - r_real);
imaginary_result2 = (l_imaginary - r_imaginary);
foil(l_real, l_imaginary, r_real, r_imaginary);
}
Complex::Complex()
{
}
void Complex::get_first(char ansr1[])
{
char ans2[80], ans3[2], ans4[80], hold[80];
int x=0, y=0;
while(ansr1[x] != '+' && ansr1[x] != '-' || x == 0)
{
ans2[y] = ansr1[x];
x = x+1;
y = y+1;
}
ans2[y] = '\0';
y=0;
ans3[y] = ansr1[x];
x = x+1;
y=y+1;
ans3[y] = '\0';
y=0;
while(ansr1[x] != 'i')
{
ans4[y] = ansr1[x];
x = x+1;
y = y+1;
}
ans4[y] = '\0';
l_real = atof(ans2);
strcat(ans3, ans4);
l_imaginary = atof(ans3);
l_sign = ans3[0];
}
void Complex::get_secon(char ansr2[])
{
char ans2[80], ans3[2], ans4[80], hold[80];
int x=0, y=0;
while(ansr2[x] != '+' && ansr2[x] != '-' || x == 0)
{
ans2[y] = ansr2[x];
x = x+1;
y = y+1;
}
ans2[y] = '\0';
y=0;
ans3[y] = ansr2[x];
x = x+1;
y=y+1;
ans3[y] = '\0';
y=0;
while(ansr2[x] != 'i')
{
ans4[y] = ansr2[x];
x = x+1;
y = y+1;
}
ans4[y] = '\0';
r_real = atof(ans2);
strcat(ans3, ans4);
r_imaginary = atof(ans3);
r_sign = ans3[0];
}
void Complex::foil(double lr, double li, double rr, double ri)
{
double hold1, hold2;
hold1 = (lr * rr) + ((li * ri)*-1);
hold2 = (li * rr) + (lr * ri);
real_result3 = hold1;
imaginary_result3 = hold2;
}
ostream& operator <<(ostream& outs, const Complex& number)
{
outs << "\n" << "Numbers started with: " << number.l_real <<
number.l_sign
<< abs(number.l_imaginary) << "i" << " and " << number.r_real
<< number.r_sign
<< abs(number.r_imaginary) << "i" << "." << "\n" << "\n" <<
"Added they equal: "
<< number.real_result1 << "+" << number.imaginary_result1 <<
"i" << endl
<< "Subtracted they equal: " << number.real_result2 << "+" <<
number.real_result2
<< "i" << endl << "Multiplied they equal: " <<
number.real_result3 << "+"
<< number.imaginary_result3 << "i" << endl << "They are " <<
number.answer << "."
<< "\n" << endl;
return outs;
}