2014-02-10 2 views
0

미안 제목이 불분명하다면, 문제는 내가 복소수를 가진 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 

그것이 내가 알고

그들에게 분리해서하는 방법을 알고하지 않기 때문에 문제는 내 출력 과부하 또는 내 메인 복잡한 개체를 읽습니다 있지만 그것을 수정하는 방법을 잘 모르겠습니다.

답변

0

먼저 수업에서 기호 문자 필드를 제거해야합니다. 클래스 사용자가 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; 
} 

두 번째 단계는 복합 타입에 문자열을 구문 분석 할 수있는 기능을 작성하는 것입니다 :

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(); 
관련 문제