借鉴了循环判定条件
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream file1("text.txt");
char a[20],ch;
int i=0;
while(file1.get(ch)){//文件中读取的数据存入数组
cout<<ch;
a[i]=ch;
i++;
}
file1.close();
cout<<endl;
bool flag1=false,flag2=false;
while(flag1==false||flag2==false){
flag1=true;
flag2=true;
for(int j=1;j+1<i;j+=2){
if(a[j]>a[j+1]){
char temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag1=false;
}
}
for(int j=0;j+1<i;j+=2){
if(a[j]>a[j+1]){
char temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag2=false;
}
}
}
for(int j=0;j<i;j++){
cout<<a[j];
}
return 0;
}