C++ 如何仅在对象存在/特定情况下输出对象的特定部分?

C++ 如何仅在对象存在/特定情况下输出对象的特定部分?,c++,C++,抱歉,如果标题不清楚,我遇到的问题是我必须导入一个包含复数的txt文件,但是其中一些文件没有虚部或实部,并且我不知道如何仅输出虚部或实部(如果另一部分缺失) 这是我的密码: .h头文件: #ifndef COMPLEXOBJ_H #define COMPLEXOBJ_H #include <iostream> class complexType { friend std::ostream& operator<<(std::ostream& os, co

抱歉,如果标题不清楚,我遇到的问题是我必须导入一个包含复数的txt文件,但是其中一些文件没有虚部或实部,并且我不知道如何仅输出虚部或实部(如果另一部分缺失)

这是我的密码:

.h头文件:

#ifndef COMPLEXOBJ_H
#define COMPLEXOBJ_H
#include <iostream>


class complexType
{
friend std::ostream& operator<<(std::ostream& os, const complexType& obj);
friend double getreal(const complexType& sample1);
friend char getsign(const complexType& sample2);
public:
  complexType();
  complexType(double r, double i, char signin);
  double getreal() const;
private:
    double real;
    double imagine;
    char sign;
};

#endif // COMPLEXOBJ_H
它不正确地导出它,而是导出:

Object 1: -8-5.6i
Object 2: -7-5.6i
Object 3: -6-5.6i
Object 4: -5.5-5.6i
Object 5: -4.4-4.5i
Object 6: 1+1i
Object 7: 2+2i
Object 8: 3.3+3.4i
对于5.6,我处于开始阶段,因为它不知道如何将它们分开


我知道问题出在我的输出过载或者当我的main读取复杂对象时,但我不确定如何修复它。

首先,你应该从类中删除sign char字段。它很容易出错,因为类的用户可以调用complexType1.0、1.0、x等等。但它还存储了重复的信息——真实部分和想象部分的字段已经存储了关于符号的信息。 之后,您的输出运算符可以如下所示:

ostream& operator<<(ostream& os, const complexType& obj)
{
    if(obj.real == 0.0)
    {
        if(obj.imagine == 0.0)
            os << 0.0;
        else
            os << obj.imagine << "i";
    }
    else
    {
        if(obj.imagine == 0.0)
            os << obj.real;
        else
            if(obj.imagine >= 0.0)
                os << obj.real << "+" << obj.imagine << "i";
            else
                os << obj.real << obj.imagine << "i";
    }

    return os;
}
最后,您只需将输入文件拆分为字符串并输出:

ofstream outputfile;
outputfile.open("126expotr.txt", std::ofstream::out);

ifstream inputfile;
inputfile.open("126import.txt", std::ifstream::in);

string str;
while(!inputfile.eof())
{
    double r, i;
    getline(inputfile, str);
    parseComplexTypeString(str, r, i);
            complexType value(r, i);
            outputfile << value << "\r\n";
}

inputfile.close();
outputfile.close();
Object 1: -8-5.6i
Object 2: -7-5.6i
Object 3: -6-5.6i
Object 4: -5.5-5.6i
Object 5: -4.4-4.5i
Object 6: 1+1i
Object 7: 2+2i
Object 8: 3.3+3.4i
ostream& operator<<(ostream& os, const complexType& obj)
{
    if(obj.real == 0.0)
    {
        if(obj.imagine == 0.0)
            os << 0.0;
        else
            os << obj.imagine << "i";
    }
    else
    {
        if(obj.imagine == 0.0)
            os << obj.real;
        else
            if(obj.imagine >= 0.0)
                os << obj.real << "+" << obj.imagine << "i";
            else
                os << obj.real << obj.imagine << "i";
    }

    return os;
}
void parseComplexTypeString(string str, double & r, double & i)
{
    bool firstMinus = false;
    if(str[0] == '-')
    {
        firstMinus = true;
        str = str.substr(1);
    }

    int plusPos = str.find('+');
    int minusPos = str.find('-');
    if(plusPos > 0 || minusPos > 0)
    {
        str = str.substr(0, str.size() - 1);
        int dividerPos = plusPos > minusPos ? plusPos : minusPos;

        string rStr = str.substr(0, dividerPos);
        string iStr = str.substr(dividerPos + 1);

        if(firstMinus)
            rStr.insert(rStr.begin(), '-');

        r = atof(rStr.c_str());

        if(dividerPos == minusPos)
            iStr.insert(iStr.begin(), '-');

        i = atof(iStr.c_str());
    }
    else
        if(str.find('i') != -1)
        {
            str = str.substr(0, str.size() - 1);
            r = 0.0;
            if(firstMinus)
                str.insert(str.begin(), '-');
            i = atof(str.c_str());
        }
        else
        {
            i = 0.0;
            if(firstMinus)
                str.insert(str.begin(), '-');
            r = atof(str.c_str());
        }
}
ofstream outputfile;
outputfile.open("126expotr.txt", std::ofstream::out);

ifstream inputfile;
inputfile.open("126import.txt", std::ifstream::in);

string str;
while(!inputfile.eof())
{
    double r, i;
    getline(inputfile, str);
    parseComplexTypeString(str, r, i);
            complexType value(r, i);
            outputfile << value << "\r\n";
}

inputfile.close();
outputfile.close();