미안 제목이 불분명하다면, 문제는 내가 복소수를 가진 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;
}
}
}
}
코드의 대부분은 작동하지만 my in 풋 파일은 다음과 같습니다
1+1i
2+2i
3.3+3.4i
4.4-4.5i
-5.5-5.6i
-6
7i
-8i
대신 수출의 제대로은 수출 : 시작시 5.6 내가 년대와
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
그것이 내가 알고
그들에게 분리해서하는 방법을 알고하지 않기 때문에 문제는 내 출력 과부하 또는 내 메인 복잡한 개체를 읽습니다 있지만 그것을 수정하는 방법을 잘 모르겠습니다.