こんにちはタイトルは本当にすべてを言います。材料の組成(ポリエチレンや鉛の自然な組成など)に関するデータを保持しているファイルを読み込む方法があります。これらの材料はそれぞれ異なるオブジェクトに保持されており、それぞれが異なる組成を持っています。物理システムを構成する材料が別のファイルから取得されると、それは自然な次のステップであるため、makeメソッドは順次呼び出されます。リストに書き込まれるのはファイルに対するo/pであり、実際にリストにあるのはそのファイルの最後に対するopです。こぼれないようにするにはどうすればよいですか?
void material::make(char* filename)
{
ifstream fin(filename) ;
ofstream filenameOUT ;
string outfile = (string)filename + "OUT.txt" ;
filenameOUT.open(outfile.c_str()) ;
string ZStr, propStr, isotope ;
vector<float> component(2,0) ;
getline(fin, propStr, '\n') ; //store first entry in file as a str (prop is used to save mem allocation)
lamda = atof(propStr.c_str()) ; //then convert to float so calcs can be done
filenameOUT<<"lamda: "<<lamda<<endl;
while(!fin.eof())
{
getline(fin, isotope, ' ') ; //get the element name
getline(fin, ZStr, '\t') ; //get the Z's and abunancies from the file.
getline(fin, propStr) ;
component[0] = atof(ZStr.c_str()) ;
component[1] = atof(propStr.c_str()) ;
filenameOUT<<"isotope: "<<isotope<<" isotope Z: "<<component[0]<<" proportional amount: "<<component[1]<<endl;
composition.push_back(component) ;
elements.push_back(isotope) ;
}
filenameOUT<<filename<<" is loaded"<<endl;
for(c=composition.begin();c!=composition.end();c++)
{
filenameOUT<<(*c)[0]<<" : "<<(*c)[1]<<" is loaded"<<endl;
}
}
たとえば、ポリエチレンの入力ファイル:
.335657
carbon 12 .33333
hydrogen 1 .66667
これを生成します(鉛、銅、ホウ素、水素を3回、炭素を1回含む同位体を含みます)。
lamda: 0.335657
isotope: carbon isotope Z: 12 proportional amount: 0.33333
isotope: hydrogen isotope Z: 1 proportional amount: 0.66667
poly.txt is loaded
11 : 0.04 is loaded
10 : 0.01 is loaded
12 : 0.31778 is loaded
1 : 0.63332 is loaded
1 : 0.63332 is loaded
204 : 0.014 is loaded
206 : 0.241 is loaded
207 : 0.221 is loaded
208 : 0.524 is loaded
208 : 0.524 is loaded
106 : 0.0125 is loaded
108 : 0.0089 is loaded
110 : 0.1249 is loaded
111 : 0.128 is loaded
112 : 0.2413 is loaded
113 : 0.1222 is loaded
114 : 0.2873 is loaded
116 : 0.0749 is loaded
12 : 0.33333 is loaded
1 : 0.66667 is loaded
どんなアドバイスも大歓迎です!(そして、はい、それはリターンとして「void」と言います。次を実行する前に、各メソッド呼び出しからのリターンを待つことができますが、それを行う方法がわかりません。)
これが修正された問題へのケースリンクが素晴らしい場合、私は用語が何であるかを本当に知らないので、私はTINTERWEBでこれを行う方法を見つけられなかったかもしれません!