Converting a string literal to a Complex Number.

Hi, I am new to programming and I've been trying to create my own complex number class in C++ it can do things like z = x + y, z = x/y, z = conj(x), Re(z) etc. but I also want it to be able to initialise ComplexNumber objects from these strings:

[+|-]realNumber // a real number e.g "-7"
[+|-][realNumber]i // a pure imaginary number e.g. "2i"
[+|-]realNumber(+|-)[realNumber]i // a generic complex number e.g. "1-i"

and then to convert the strings to Complex Number objects. It works for everything, except for pure imaginary numbers where it is initialising the real and the imaginary parts of the ComplexNumber object to zero. If my approach is wrong please tell me.

Any help appreciated, Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<iostream>

using namespace std;

class ComplexNumber {

    public:
        ComplexNumber( double real = 0, double imag = 0 );
        double Re() const;
        double Im() const;
        
        friend ostream& operator<<( ostream& in, const ComplexNumber& b );
        friend istream& operator>>( istream& in, ComplexNumber& c );

    private:
        double re;
        double im;
};

ComplexNumber::ComplexNumber( double real, double imag ) {

    re = real;
    im = imag;
}

double ComplexNumber::Re() const {

	return re;
}

double ComplexNumber::Im() const {

	return im;
}


// Pretty prints ComplexNumbers

ostream& operator<<( ostream &out, const ComplexNumber& b ) {

    bool realPrinted = false;

    if (b.re != 0 || (b.re == 0 && b.im == 0)) {
        out << b.re;
        realPrinted = true;
    }

    if (b.im > 0) {
        if (realPrinted) {
            out << "+";
        }
        if (b.im != 1) {
            out << b.im;
        }
        out << "i";
    }

    else if (b.im < 0) {
        if (b.im == -1) {
            out << "-";
        }
        else {
            out << b.im;
        }
        out << "i";
    }

    return out;
}

// natural extraction operator for ComplexNumbers

istream& operator>> ( istream& in, ComplexNumber& z ) {

    string complexText;
    int plusPos, minusPos, i_Pos;
    double coefficient1;
    bool real, imag;

    in >> coefficient1; // fails for pure imag nums converts "bi" to zero.
    getline(cin,complexText);

    plusPos = complexText.find('+');
    minusPos = complexText.find('-');
    i_Pos = complexText.find('i');

    real = i_Pos < 0; // real number won't contain the character i
    imag = minusPos < 0 && plusPos < 0; // pure imag number if no + or -

    if (real) {
        z.re = coefficient1;
        z.im = 0.0;
    }

    else if (imag) {
        z.re = 0.0;
        z.im = coefficient1;
    }

    else {
        z.re = coefficient1;
        
        complexText.replace(i_Pos,1," ");

        if (plusPos >= 0) 
            complexText.replace(plusPos,1," ");

        if (minusPos >= 0) 
            complexText.replace(minusPos,1," ");
 
        z.im = atof(complexText.c_str());

        if (z.im == 0)
            z.im = 1; // 1 + i, 1 - i

        if (minusPos >= 0) 
            z.im = -z.im;
    }

    return in;
}

int main()
{
	ComplexNumber z;
        cout << "Input a Complex Number: ";
        cin >> z;
        cout << z.Re() << "\n" << z.Im() << "\n" << z << "\n";

	return 0;
}

Last edited on
It works for me. Could you give us an example where it fails?
Try a string non literal...always works
It works for me. Could you give us an example where it fails?

That's weird, it doesn't seem to ever work for me. (I'm using g++ on a mac)
Input a Complex Number: 2i
0 
0
0

But I noticed that if I change line 85 to i_Pos = complexText.find('j'), using j as the imaginary unit, it works correctly for all complex numbers .
Input a Complex Number: 2j
0
2
2i


1
2
3
4
5
6
7
8
int main() {

    double d;

    cout << "Input a double followed by a char (eg. '1h') ";
    cin >> d;
    cout << "The double was " << d << "\n";
} 



If you run this code and input the strings "1a", "1b" etcetera what is your output? Because for me if the char is in the set {a b c d e f i n p x} it returns zero but if the char is in the set {g h j k l m o q r s t u v w y z} it returns 1.
Last edited on
Topic archived. No new replies allowed.