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

How can i output a certain part of an object only if it's there/a certain circumstance?

本文关键字:对象 输出 个部 情况下 何仅 存在      更新时间:2023-10-16

抱歉,如果标题不清楚,我遇到的问题是我必须导入一个带有复数的 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

.cpp类文件:

#include "Complexobj.h"
#include <iostream>
using namespace std;

complexType::complexType()
{
real=0;
imagine=0;
sign= '+';
}
complexType::complexType(double r, double i, char signin)
{
real=r;
imagine=i;
sign=signin;
}
ostream& operator<<(ostream& os, const complexType& obj)
{
os << obj.real<< obj.sign << obj.imagine << "i";
return os;
}
double complexType::getreal() const
{
return real;
}

CPP 主文件:

#include "Complexobj.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
void sorter(complexType[], int countin);
int main()
{
ofstream outputfile;
ifstream inputfile;
string str;
double realpart;
double imaginarypart;
int symbol;
char ch;
string strone;
string strtwo;
complexType storage[100];
int counter = 0;
inputfile.open("126import.txt");
if(inputfile.fail())
{
    cout << "File opening failed." << endl;
    exit(1);
}
outputfile.open("126export.txt");
inputfile >> str;
while(inputfile)
{
    char firstch = '+';
    if(str.at(0) == '-')
    {
        str = str.substr(1,str.length() - 1);
        firstch = '-';
    }
    symbol=str.find("+");
    ch = '+';
    if(symbol < 0)
    {
        symbol = str.find("-");
        ch = '-';
    }
    stringstream streamin(str);
    getline(streamin, strone, ch);
    getline(streamin, strtwo, 'i');

    realpart= atof(strone.c_str());
    imaginarypart= atof(strtwo.c_str());
    if(ch == '-')
        realpart *= -1;
    complexType outpobj(realpart, imaginarypart, ch);
    storage[counter]=outpobj;

    counter++;
    inputfile >> str;
}
sorter(storage, counter);

for(int u=0; u<counter;u++)
{
    outputfile << "Object " << u+1 << ": " << storage[u] << endl;
}


    inputfile.close();
    outputfile.close();
    return 0;
}
void sorter(complexType storarray[], int countin)
{
complexType temp;
for(int k=1; k<countin;k++)
{
  for(int j=0;j<countin-k;j++)
  {
    if(storarray[j].getreal() > storarray[j+1].getreal())
    {
        temp=storarray[j];
        storarray[j]=storarray[j+1];
        storarray[j+1] = temp;
    }
}
}
}

在大多数情况下,代码有效,但我的输入文件是:

1+1i
2+2i
3.3+3.4i
4.4-4.5i
-5.5-5.6i
-6
7i
-8i

它不是正确导出它,而是导出:

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 i 在开始时,因为它不知道如何将它们分开

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

首先,您应该从类中删除符号字符字段。它很容易出错,因为类的用户可以调用 complexType(1.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;
}

第二步是创建可以将字符串解析为 complexType 的函数:

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 << "rn";
}
inputfile.close();
outputfile.close();