0

私はこのプロジェクトに取り組んでおり、C++ にはかなり慣れていません。私がやろうとしていることを説明するのはちょっと難しいですが、やってみます。だから私は flix.txt と呼ばれるファイルで作業しており、その中は次のようになっています:

1 A 5
1 B 4
1 D 3
1 F 5
2 A 1
3 E 3
3 F 1
4 A 2

最初の列は人 (私のオブジェクト)、2 番目の列は映画、3 番目の列はオブジェクトによって与えられた評価です。

最初にすべての行から最初の int を抽出し、「演算子 new」を使用してオブジェクトを作成しようとしています。次に、映画を取得して int に変換し、評価を配列にプラグインできるようにします。紛らわしく聞こえる場合は申し訳ありません。私が今持っているコードは次のとおりです。

//flix program

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

#define NUMBER_OF_MOVIES 6

using namespace std;

int tokenize(string line);
int getMovieNum(char movie);
void resetPos(istream& flix);

class Matrix{
    public:
        int movieRate[NUMBER_OF_MOVIES];
};

int main(){

    int distinctCount = 0;
    int checker = -1;
    int check = 0;
    string line;

    int personNum;
    char movie;
    int rating;
    int movieNum;

    ifstream flix("flix.txt"); 
    ofstream flick("flix1.txt");

    //identify distinct account numbers in file    
    while(getline(flix, line)){
        check = tokenize(line);

        if(check != checker)
            distinctCount++;

        checker = check;
        check = 0;
    }

    //reset position in file
    resetPos(flix);

    //create objects in accordance with distinct numbers
    Matrix* person = new Matrix[distinctCount];

    for(int i = 0; i < distinctCount; i++){
        for(int j = 0; j < NUMBER_OF_MOVIES; j++){
            person[i].movieRate[j] = 0;
            cout << i + 1 << ' ' << person[i].movieRate[j] << endl;
        }
        cout << "\n";
    }
    //reset position in file
    resetPos(flix);

    //get data from file and put into respective variables
    while(getline(flix, line)){
        flix >> personNum >> movie >> rating;
        cout << personNum << ' ' << movie << ' ' << rating << endl;

        //changes the char into an int
        movieNum = getMovieNum(movie);

        person[personNum].movieRate[movieNum] = rating;
    }

    //reset position in file
    resetPos(flix);

    //input ratings into movie array
    for(int i = 0; i < distinctCount; i++){
        for(int j = 0; j < NUMBER_OF_MOVIES; j++){
            cout << i + 1 << ' ' << person[i].movieRate[j] << endl;
            flick << i + 1 << ' ' << person[i].movieRate[j] << endl;
        }
    }


    //write data to text file
    //??

    flick.close();
    //free memory
    delete[] person;

    system("pause");
    return 0;
}

int tokenize(string line){
    string myText(line);
    istringstream iss(myText);
    string token;

    getline(iss, token, ' ');

    int strInt = atoi(token.c_str());

    return strInt;
}

int getMovieNum(char movie){
    int movieNum = 0;

    switch(movie){
        case 'A':
            movieNum = 1;
            break;
        case 'B':
            movieNum = 2;
            break;
        case 'C':
            movieNum = 3;
            break;
        case 'D':
            movieNum = 4;
            break;
        case 'E':
            movieNum = 5;
            break;
        case 'F':
            movieNum = 6;
            break;
        default:
            movieNum = 0;
            break;
    }

    return movieNum;    
}
void resetPos(istream& flix){
        flix.clear();
        flix.seekg(0);
}

また、ここに些細な間違いがある場合は、事前にお詫び申し上げます。

問題は while ループのどこかにあると思います。そこでロックし続けます。これに何時間も費やしましたが、なぜ機能しないのかわかりません。while ループでは、ファイルのすべての行にアクセスし、その行からデータを取得し、動画の char を取得して int に変換し、そのデータをオブジェクト内の配列に挿入しようとしています。私がそれを機能させたとき、すべてのデータも間違っていました。どんな入力でも大歓迎です。前もって感謝します。

4

3 に答える 3

0

では、簡単な出発点について、少なくともいくつかのアイデアを提供してみましょう。

#include <iostream>
#include <iterator>
#include <vector>
#include <fstream>

struct rater {
    std::vector<int> ratings;

    rater() : ratings(6) {}

    friend std::istream &operator>>(std::istream &is, rater &r) {
        char movie;
        is >> movie;
        return is >> r.ratings[movie-'A'];
    }

    friend std::ostream &operator<<(std::ostream &os, rater const &r) {
        for (int i=0; i<r.ratings.size(); i++) {
            os << char(i + 'A') << ":" << r.ratings[i] << "\t";
        }
        return os;
    }
};

int main() { 
    std::ifstream in("flix.txt");
    std::vector<rater> ratings(5);

    int i;

    while (in >> i) 
        in >> ratings[i-1];

    i=1;
    for (auto r : ratings) 
        std::cout << i++ << "-> " << r << "\n";    
}
于 2013-03-05T05:19:07.587 に答える
0

ここで少しクリーンアップします。Person および Movie キーを追跡するために使用std::mapします。これは、あらゆる種類のテキスト文字列 (空白を除く) を使用できるため、より柔軟です。人々が評価しなかった映画を出力で具体的にリストしたいというコメントを追加しましたstd::set。人の評価: 演習として残します。

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <map>

using namespace std;

typedef std::map<std::string, int> Movie_Ratings;
typedef std::map<std::string, Movie_Ratings> Persons_Movie_ratings;

int main()
{
    if (!ifstream flix("flix.txt"))
    {
        std::cerr << "error opening input\n";
        exit(1);
    }

    if (!ofstream flick("flix1.txt"))
    {
        std::cerr << "error opening output\n";
        exit(1);
    }

    Persons_Movie_Ratings ratings;

    std::string line;
    while (getline(flix, line))
    {
        istringstream iss(line);
        string person, movie;
        int rating;
        if (line >> person >> movie >> rating)
            ratings[person][movie] = rating;                
    }

    // input ratings into movie array
    for (Persons_Movie_Ratings::const_iterator i = ratings.begin();
         i != ratings.end(); ++i)
    {
        for (Movie_Ratings::const_iterator j = i->second.begin();
             j != i->second.end(); ++j)
        {
            cout << i->first << ' ' << j->second << endl;
            flick << i->first << ' ' << j->second << endl;
        }
    }

    system("pause");
}
于 2013-03-05T05:21:06.510 に答える
0

プログラムを少し変更する必要があり、変更された部分のみを貼り付けます。person[].movi​​eRate[] をゼロにリセットした後のどこかに、このwhileループを記述しました

resetPos(flix);
int k = NUMBER_OF_MOVIES + 2;  //this is the variable that i have declared
//get data from file and put into respective variables
while(k){      //do you see that instead of getline() i have used the variable k. i'l tell you why later

    flix >> personNum >> movie >> rating;
    //personNum = tokenize(line,1);
    cout << personNum << ' ' << movie << ' ' << rating << endl;

    //changes the char into an int
    movieNum = getMovieNum(movie);
    person[personNum - 1].movieRate[movieNum] = rating;  //this is personNum-1 and NOT personNum the most common mistake while indexing array.
    k--;
}

このコードはあなたの基準として機能しているようです。getline() を削除した理由は、getline を呼び出すと、get ポインターの位置がインクリメントされるためです。この後、 flix >> something... を呼び出すと、2 行目からデータが読み込まれます。最初の行 1 A 5 が失われました。これがトラブルの原因でした。変更してお知らせください。

于 2013-03-05T06:47:40.193 に答える