如果某个对象的某个部分出现在某个特定情况下,我怎么能输出它?

时间:2014-02-10 04:48:14

标签: c++

对不起,如果标题不清楚,我遇到的问题是我必须导入一个复杂数字的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开头因为它不知道如何分开它们

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

1 个答案:

答案 0 :(得分:0)

首先,您应该从班级中删除sign char字段。这很容易出错,因为你的类的用户可以调用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 << "\r\n";
}

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