麻雀雖小五髒俱全 現代C++思想 值得推敲

來源:互聯網
上載者:User

真正的C++

        (1)不提倡指標

        (2)提倡資料抽象,提倡使用類

        (3)提倡使用庫

                                ------<<C++沉思錄>>

本文中問題的解決很好的體現了以上幾點,如果不想學習C++誤入歧途,陷入方言.請仔細研讀本程式的實現思路,雖然它不是本人原創.

//       問題條件:文字檔中有如下學生資料

//        要求:對文本中的資料按成績排序,之後輸出到result.txt檔案中,同時顯示結果

//        解決思路:將檔案中的資料讀入到vector對象vectStu中進行排序,vector必須盛放一行資料.

//建立student類,用其對象作為vector的元素.每次從檔案中讀一行到string的臨時對象line中.

//用string流對象從line中讀取此行的三個資料(學號,姓名,分數)到三個臨時變數中.

//用這三個臨時對象初始化一個student對象,將這個初始化的對象放入vectStu中.

//為利用sort對vectStu排序,需要student類支援operator<操作符重載.

//最後將vectStu的內容輸出到終端,輸出到檔案.此時對vectStu中的每個student,為擷取其私人

//成員,要求student類提供唯讀介面.

 

//完整實現

#include <iostream>
#include <fstream>
//#include <cstdlib>
//#include <cstdio>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;

class Student
{
public:
    Student();
    Student ( string Number, string Name, int Score ): number( Number ), name( Name ), score( Score ){}
    ~Student (){};

    int getScore() const{
        return score;
    }
   
    string getNumber() const{
        return number;
    }
   
    string getName() const{
        return name;
    }
   
    bool operator<( const Student &Stu ) const{
        return score < Stu.getScore();
    }
private:
    /* data */
    string number;
    string name;
    int score;
};

void PrintVec( vector< Student > &vec ){
    int i = 0;
 for(vector<Student>::size_type i = 0; i < vec.size(); i ++ )
    {
        Student stu = vec[i];
        cout<<"Number:"<<stu.getNumber()<<" "<<"Name:"<<stu.getName()<<" "<<"Score:"<<stu.getScore()<<endl;
    }
}

void WriteResult( vector< Student > &vec ){
    ofstream fout("result.txt");
   
    int i = 0;
 for(vector<Student>::size_type  i = 0; i < vec.size(); i ++ )
    {
        Student stu = vec[i];
        fout<<stu.getNumber()<<" "<<stu.getName()<<" "<<stu.getScore()<<"/n";
    }   
   
    fout.close();
}

int main (int argc, char const* argv[])
{
    ifstream fin("test.txt");
   
    vector< Student > students;
    string lineIterm("");
    string num;
    string name;
    int score;
   
    while( getline( fin, lineIterm ) ){               
        //cout<<lineIterm<<endl;
       
        //以空格來分隔string       
        istringstream iss( lineIterm );
       
        iss>>num;
        iss>>name;
        iss>>score;
        Student stu( num, name, score );
       
        students.push_back( stu );
    }
   
   
    PrintVec( students );
    cout<<"begin sort......."<<endl;
    sort( students.begin(), students.end() );
    cout<<"end sort........."<<endl;
    PrintVec( students );
    fin.close();
   
    WriteResult( students );
   
    return 0;
}

 

 

 /*

2004101 張大志 89
2004102 楊小敏 65
2004103 李兵 76  
2004104 周星華 60
2004105 王小青 82
2004106 陳江 70
2004107 劉慧姍 85
2004108 張真 80
2004109 林華 70
2004110 郭雲風 62

 */

 

注:代碼參考CSDN部落格,非本人原版.思想也是來自CSDN部落格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.