百家姓排序(C++)

來源:互聯網
上載者:User

標籤:百家姓排序   百家姓   組建檔案   

百家姓的一部分:
{
趙 錢 孫 李 周 吳 鄭 王 馮 陳 褚 衛 蔣 沈 韓 楊 朱 秦 尤 許何 呂 施 張 孔 曹 嚴 華 金 魏 陶 薑 戚 謝 鄒 喻 柏 水 竇 章雲 蘇 潘 葛 奚 範 彭 郎 魯 韋 昌 馬 苗 鳳 花 方 俞 任 袁 柳酆 鮑 史 唐 費 廉 岑 薛 雷 賀 倪 湯 滕 殷 羅 畢 郝 鄔 安 常樂 於 時 傅 皮 卞 齊 康 伍 餘 元 蔔 顧 孟 平
}
#include <fstream>
typedef basic_fstream<char, char_traits<char> > fstream;

#include <fstream>#include <string>#include <iostream>using namespace std;#define MAXNAMES 1000void input(string nm[],int n){    for(int i=0; i!=n; i++)    {        cin>>nm[i];    }}void output(string nm[],int n){    ofstream out("names.txt");  //建立一個檔案,存放姓名    for(int i=0; i<n; i++)    {        cout<<nm[i]<<endl;        out<<nm[i]<<endl;//調用庫函數 <fstream>    }    out.close();//關閉Names.txt檔案}int analyze(char x){    int i;    string firstNm= " 趙錢孫李周吳鄭王馮陳褚            衛蔣沈韓楊朱秦尤許何呂施張孔曹嚴華金魏陶薑戚謝鄒喻柏水竇章雲蘇潘葛奚範彭郎魯韋昌馬苗 鳳花方俞任袁柳酆鮑史唐費廉岑 薛雷賀倪湯滕殷羅畢郝鄔安常樂 於時傅皮卞齊康伍餘元蔔顧孟平 ";      for(i=0; firstNm[i]!=x; i++);    return i;}void sort(string nm[],int n){string temp;for(int i=0; i<n-1; i++){for(int j=i+1; j<n; j++){if(analyze(nm[i][0])>analyze(nm[j][0])) //判斷姓氏先後,進行排序{temp=nm[i];nm[i]=nm[j];nm[j]=temp;}}}}int main(){    string names[MAXNAMES];int n;cout<<"請輸入姓名個數:";cin>>n;    input(names,n);    sort(names,n);    cout<<"After sort"<<endl;    output(names,n);    system("pause");    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

百家姓排序(C++)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.