有两个磁盘文件a和b,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件c中去
#include<iostream>
#include<fstream>
#include<string>
int main()
{
<span style="white-space:pre"> </span>ifstream in("C:\\Users\\Administrator\\Desktop\\a.txt");
char ch;
string str;
if(!in)
{
cout<<"Cannot open a.txt!"<<endl;
return -1;
}
while(in>>ch)
{
str+=ch;
}
in.close();
in.open("C:\\Users\\Administrator\\Desktop\\b.txt");
if(!in)
{
cout<<"Cannot open a.txt!"<<endl;
return -1;
}
while(in>>ch)
{
str+=ch;
}
in.close();
char temp;
for(int i=0;i<str.length();i++)
{
int t=i;
for(int j=i+1;j<str.length();j++)
{
if(((str[t]>='A' && str[t]<='Z') && (str[j]>='A' && str[j]<='Z')) || ((str[t]>='a' && str[t]<='z') && (str[j]>='a' && str[j]<='z')))
{
if(str[j]<str[t])
{
t=j;
}
}
else if((str[t]>='A' && str[t]<='Z') && (str[j]>='a' && str[j]<='z'))
{
if(str[j]-32<str[t])
{
t=j;
}
}
else if((str[t]>='a' && str[t]<='z') && (str[j]>='A' && str[j]<='Z'))
{
if(str[j]+32<str[t])
{
t=j;
}
}
}
if(t!=i)
{
temp=str[t];
str[t]=str[i];
str[i]=temp;
}
}
ofstream out("C:\\Users\\Administrator\\Desktop\\c.txt");
if(!out)
{
cout<<"Cannot open c.txt!"<<endl;
return -1;
}
out<<str;
out.close();
return 0;
}
结果如下: