<pre id="best-content-675808822" class="best-text mb-10" style="margin-top: 0px; margin-bottom: 10px; background-color: rgb(243, 255, 236); padding: 0px; font-family: arial, 'courier new', courier, 宋體, monospace, 'Microsoft YaHei'; white-space: pre-wrap; word-wrap: break-word; font-size: 14px; color: rgb(51, 51, 51); line-height: 24px;">百家姓的一部分:
{
趙 錢 孫 李 周 吳 鄭 王 馮 陳 褚 衛 蔣 沈 韓 楊 朱 秦 尤 許
何 呂 施 張 孔 曹 嚴 華 金 魏 陶 姜 戚 謝 鄒 喻 柏 水 竇 章
雲 蘇 潘 葛 奚 範 彭 郎 魯 韋 昌 馬 苗 鳳 花 方 俞 任 袁 柳
酆 鮑 史 唐 費 廉 岑 薛 雷 賀 倪 湯 滕 殷 羅 畢 郝 鄔 安 常
樂 於 時 傅 皮 卞 齊 康 伍 餘 元 卜 顧 孟 平
}
#include <fstream>
typedef basic_fstream<char, char_traits<char> > fstream;
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
#define MAXNAMES 1000
void 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;
}