C++ 讀取檔案練習2


題目

跟練習一差不多
只是要將cmd.txt要查詢的成績顯示出來

完整程式碼

#include<iostream>
#include<fstream>
#include<string>
#include <sstream>
#include<vector>
using namespace std;

//二維vector : https://ramihaha.tw/c-program-container-vector-array-linklist/
//           : https://shengyu7697.github.io/std-vector/
//文件讀取 : https://blog.csdn.net/guowenyan001/article/details/11231927
//string切割 : https://shengyu7697.github.io/cpp-string-split/
//文件寫入 : https://shengyu7697.github.io/cpp-write-text-file/
//string 轉 int : https://www.programiz.com/cpp-programming/string-int-conversion

vector<string> split(const string &s, char delimiter) {
    vector<string> tokens;     
    string token;     
    istringstream tokenStream(s);     
    while (getline(tokenStream, token, delimiter)) {      
        tokens.push_back(token);     
    }     
    return tokens;  
}


int main(){

    //標準分數array
    int stander[4]={12,12,8,12};  

    //存cmd的查詢結果
    vector<string> cmdResult;

    //1.建立可以存score.txt資料之vector
    vector<vector<string>> scoreData;

    //2.讀取的score.txt文件內容
    ifstream readScore;
    readScore.open("score.txt",ios::binary);

    //3.將score.txt資料存入scoreDate中
    string line;  //文件內容一行行讀取
    while(getline(readScore,line)){
        vector<string> to_scoreData = split(line, ' ');
        scoreData.push_back(to_scoreData);
    }
    readScore.close();

    //4.讀取的cmd.txt文件內容
    ifstream readCmd;
    readCmd.open("cmd.txt",ios::binary);

    //5.將score.txt資料存入scoreDate中
    string line2;  //文件內容一行行讀取
    while(getline(readCmd,line2)){

        //分析好cmd資料
        vector<string> cmd = split(line2, ' ');
        string name = cmd[0];
        string subject = cmd[1];
        //尋找scoreData中符合cmd的資料 存入cmdResult
        for(int i=0;i<scoreData.size();i++){
            string theName = scoreData[i][0];
            if(name == theName){
                switch (subject[0])
                {
                case 'c':
                    cmdResult.push_back(scoreData[i][1]);
                    break;
                case 'e':
                    cmdResult.push_back(scoreData[i][2]);
                    break;
                case 'm':
                    cmdResult.push_back(scoreData[i][3]);
                    break;
                case 's':
                    cmdResult.push_back(scoreData[i][4]);
                    break;

                default:
                    break;
                }
            }
        }
    }
    readCmd.close();

    //6.將Scoredata run一次,把入學檢測結果寫入output.txt
    ofstream toOutput;
    toOutput.open("output.txt");

        //無法寫入的處理程序

    if (!toOutput.is_open()) {
        cout << "Failed to open file.\n";
        return 1; // EXIT_FAILURE
    }

        //可以寫入的程序處理

    for(int i=0;i<scoreData.size();i++){
        //1為全都過 ,0為沒過
        int flag=1;
        for(int j=0;j<4;j++){

            //string --> int
            string score = scoreData[i][j+1];
            int scoreInt;
            scoreInt= std::stoi(score);
            if(scoreInt < stander[i]){
                flag=0;
                break;
            }
        }

        string name = scoreData[i][0];
        //錄取成功的輸出值
        if(flag) toOutput << "Hello " << name << ", welcome to NCU CSIE!\n";
        //錄取失敗的輸出值
        else toOutput << "Sorry, " << name << " can't enter NCU CSIE.\n";
    }


    //7.印出cmd要求的分數
    for(int i=0;i<cmdResult.size();i++){
        toOutput << cmdResult[i] << endl;
    }


        //關掉輸出stream
    toOutput.close();

    return 0;
}

解題觀念及思路








你可能感興趣的文章

JavaScript 學習筆記 - Object

JavaScript 學習筆記 - Object

Day 130

Day 130

[TensorFlow Certification Day2] 課程規劃與安排時間

[TensorFlow Certification Day2] 課程規劃與安排時間






留言討論