2014-02-10 48 views
0

對不起,如果標題不清楚,我遇到的問題是我必須導入一個複雜的數字文本文件,其中一些然而沒有虛構或真實的部分,我不知道如何只輸出想像或真正的部分,如果其他人失蹤。只有在某個特定情況下,我如何才能輸出某個對象的某個部分?

這裏是我的代碼:

的.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我的起步,因爲它不知道如何它們分開

我知道問題是我的輸出過載,或者當我的主讀入複雜的對象,但我不知道如何解決它。

回答

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(); 
相關問題