从intANDchar.txt只读取字符并从小到大排序输出

 1#include <iostream>
 2using namespace std;
 3#include <fstream>
 4#include <vector>
 5typedef char T;
 6
 7void sort(vector<T>::iterator a,int n){//快速排序
 8    if(n<=1return;
 9    swap(*a,a[n>>1]);
10    vector<T>::iterator L=a+1;
11    vector<T>::iterator R=a+n-1;
12    int V=*a;
13    while(L<R){
14        while(*L<&& L<R) L++;
15        while(*R>=&& R>a) R--;
16        if(L<R) swap(*L,*R);
17    }

18    if(V>*R) swap(*a,*R);
19    sort(a,R-a);
20    sort(R+1,n-(R-a)-1);
21}

22
23int main()
24{
25    ifstream fin("C:\\intANDchar.txt");//.txt中l后面我自己敲了一个空格
26    int n;
27    char ch;
28    char temp;
29    vector<T> array;
30    if(!fin){
31        cout<<"file error!";
32        exit(1);
33    }

34    while(!fin.eof()){
35        ch=fin.peek();
36        if(ch<='9'&&ch>='0'){
37            fin>>n;                    
38        }

39        else if(ch<='z'&&ch>='a' || ch<='Z'&&ch>='A'){
40            fin>>ch; 
41            cout<<ch<<endl;
42            array.push_back(ch);    
43        }

44        else{
45            fin.get(temp);
46            cout<<"temp["<<temp<<"]"<<endl;
47            array.push_back(temp);
48        }

49    }

50    fin.close();
51    sort(array.begin(),array.size());
52    //////////////////////////////////////////////////////////////////////////
53    //排序后输出
54    //////////////////////////////////////////////////////////////////////////
55    cout<<"从小到大排完序如下:"<<endl;
56    cout<<"array[0]:["<<array[0]<<"]"<<endl;
57    cout<<"array[1]:["<<array[1]<<"]"<<endl;
58    cout<<"array[2]:["<<array[2]<<"]"<<endl;
59    cout<<"array[3]:["<<array[3]<<"]"<<endl;
60    cout<<"array[4]:["<<array[4]<<"]"<<endl;
61    cout<<"========================="<<endl;
62    cout<<"array[5]:["<<array[5]<<"]"<<endl;
63    cout<<"array.size()="<<array.size()<<endl;
64    cout<<"array[]的最后一个元素是:["<<array[array.size()-1]<<"]"<<endl;
65    for(vector<T>::size_type i=0;i<array.size();i++){
66        cout<<array[i];
67    }

68
69    system("pause");
70    return 0;
71}

 l后面我自己敲了1个空格
注意q后面的
temp[
]

posted on 2011-05-20 14:20 Hsssssss 阅读(91) 评论(0)  编辑 收藏 引用 所属分类: C++代码


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿

文章分类

文章档案

收藏夹

搜索

最新评论