百家姓排序(C++)

<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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章