2016-04-18 29 views
0

データファイルからアルバム名、曲、年のリストを読み込み、そのアルバム名をアルファベット順に並べ替え、アルファベット順にソートする必要があります。私はファイル全体を読むことができますが、私は通常どのように配列でこれを行うのかデータファイルを変更することはできません。データファイルから特定の変数へのC++の読み込み

これはこれまで私が行ってきたことです。

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <fstream> 

using namespace std; 



int main() { 
int n = 30; 
    int albums[n]; 
    string content; 

ifstream infile; 
infile.open("BeatlesData.txt"); 
while (getline(infile,content)) { 
cout <<content<< endl; 

} 
infile.close(); 

これは私が読もうとしているデータファイルです。各アルバムは、「=」もそう、私はそれを読んだときにも考慮それを取らなければならないで区切られ

The Beatles White Album 
Year: 1968 
1. Back in the U.S.S.R. 
2. Dear Prudence 
3. Glass Onion 
4. Ob-La-Di, Ob-La-Da 
5. Wild Honey Pie 
6. The Continuing Story of Bungalow Bill 
7. While My Guitar Gently Weeps 
8. Happiness Is a Warm Gun 
9. Martha My Dear 
10. I'm So Tired 
11. Blackbird 
12. Piggies 
13. Rocky Raccoon 
14. Don't Pass Me By 
15. Why Don't We Do It in the Road? 
16. I Will 
17. Julia 
18. Birthday 
19. Yer Blues 
20. Mother Nature's Son 
21. Everybody's Got Something to Hide Except Me and My Monkey 
22. Sexy Sadie 
23. Helter Skelter 
24. Long, Long, Long 
25. Revolution 1 
26. Honey Pie 
27. Savoy Truffle 
28. Cry Baby Cry 
29. Revolution 9 
30. Good Night 
=============================== 
Abbey Road 
Year: 1969 
1. Come Together 
2. Something 
3. Maxwell's Silver Hammer 
4. Oh! Darling 
5. Octopus's Garden 
6. I Want You (She's So Heavy) 
7. Here Comes the Sun 
8. Because 
9. You Never Give Me Your Money 
10. Sun King 
11. Mean Mr. Mustard 
12. Polythene Pam 
13. She Came in Through the Bathroom Window 
14. Golden Slumbers 
15. Carry That Weight 
16. The End 
17. Her Majesty 
=============================== 
Magical Mystery Tour 
Year: 1967 
1. Magical Mystery Tour 
2. The Fool on the Hill 
3. Flying 
4. Blue Jay Way 
5. Your Mother Should Know 
6. I Am the Walrus 
7. Hello Goodbye 
8. Strawberry Fields Forever 
9. Penny Lane 
10. Baby You're a Rich Man 
11. All You Need Is Love 
=============================== 
Sgt. Pepper's Lonely Hearts Club Band 
Year: 1967 
1. Sgt. Pepper's Lonely Hearts Club Band 
2. With a Little Help from My Friends 
3. Lucy in the Sky With Diamonds 
4. Getting Better 
5. Fixing a Hole 
6. She's Leaving Home 
7. Being for the Benefit of Mr. Kite! 
8. Within You Without You 
9. When I'm Sixty-Four 
10. Lovely Rita 
11. Good Morning Good Morning 
12. Sgt. Pepper's Lonely Hearts Club Band (Reprise) 
13. A Day in the Life 
=============================== 
Hey Jude 
Year: 1970 
1. Can't Buy Me Love 
2. I Should Have Known Better 
3. Paperback Writer 
4. Rain 
5. Lady Madonna 
6. Revolution 
7. Hey Jude 
8. Old Brown Shoe 
9. Don't Let Me Down 
10. The Ballad of John and Yoko 

答えて

3
  1. ご質問がある。「Q:?どのように私は、このファイルを解析します」

  2. A:いいスタートがあります。ファイルの最後まで一度にstd::getline()と呼んでください。

  3. int albums[n];は悪い考えです。 49枚のアルバムがあればどうなりますか?なし?または、最悪の場合、51枚のアルバム?代わりにstd::vectorを使用してください。

  4. "難しい部分"は実際には各行を解析しています。私たちにいくつかの仕事を教えてください。遭遇する特定の問題に関する具体的な質問にお答えします。

STRONG提案:

  • Albumクラスを作成することを検討してください。
  • 最終結果はvector<Album>になります。
  • この仮想クラスは、アルバムが完成するまで( "===")、各行を読み取る公開parseLine(string line)メソッドを持つことがあります。
  • 入力テキストを適切なクラス変数に読み込むために、プライベートparseTitle()parseYear()、およびparseTrack()メソッドを持つこともできます。
  • title,yearvector<Track>などのパブリックプロパティもあります。
1

申し訳ありませんが、私はあなたの宿題を書くつもりはありません。

あなたはクラスアルバムを試すことができます。それらのベクトルを作る。アルバムクラスの中には、クラストラックのベクトルがあります。 アルバムクラスには、フィールド名と年があります。トラックにはフィールド番号と名前があります。

最初の2行はアルバム名とアルバム年に読み込まれます。次に、次の行を文字列に読み込み、 '='で始まらない場合は、トラック番号と名前に解析します。等号を見つけた場合は、ファイルの終わりを除き、アルバム名と年の読み取りを開始します。

1

これは、標準C++ではありません。

int n = 30; 
    int albums[n]; 

あなたはconst int nに変更、または好ましくはstd::vector<int>を使用する必要があります。nconstでない場合は、VLA(可変長配列)拡張を使用します。次のように

解析については、アルゴリズムは次のようになります。

  1. ファーストライン(右?)いつもCDの名前です
  2. その後常に今年は歌の
  3. N倍名います"======"を含む行が来るまで、1にジャンプし、ファイルの最後までジャンプします。

ので、擬似コードで:

struct record_type { 
    std::string name; 
    std::string year; 
    std::vector<std::string name> songs; 
} 
int main() { 
// ... 
std::vector<record_type> discs; 
std::string content; 
while(getline(infile,content)) { 
    record_type rec; 
    rec.name = content; 
    if (!getline(infile,content)) 
     break; 
    rec.year = content; 
    while (getline(infile,content)) { 
     if (content.find("=====")!=std::string::npos) break; 
     rec.songs.push_back(content); 
    } 
    discs.push_back(rec); 
}; 
} 

私は、各ラインの解析を掘り下げるないのですが、これは、あなたがstd::stringstreamで行うことができるものです。たとえば、年を解析する場合:

std::stringstream str("Year: 1968 "); 
int year; 
std::string tmp; 
str >> tmp >> year; 
関連する問題