文件中有一組整數,要求排序後輸出到另一個文件中
答案:
#i nclude
#i nclude
using namespace std;
void Order(vector& data) //bubble sort
{
int count = () ;
int tag = false ; // 設置是否需要繼續冒泡的標誌位
for ( int i = 0 ; i < count ; i++)
{
for ( int j = 0 ; j < count – i – 1 ; j++)
{
if ( data[j] > data[j+1])
{
tag = true ;
int temp = data[j] ;
data[j] = data[j+1] ;
data[j+1] = temp ;
}
}
if ( !tag )
break ;
}
}
void main( void )
{
vectordata;
ifstream in(“c:”);
if ( !in)
{
cout<<”file error!”;
exit(1);
}
int temp;
while (!())
{
in>>temp;
_back(temp);
}
e(); //關閉輸入文件流
Order(data);
ofstream out(“c:”);
if ( !out)
{
cout<<”file error!”;
exit(1);
}
for ( i = 0 ; i < () ; i++)
out< e(); //關閉輸出文件流
}