如何从输入中删除空格?它是一个整数数组。输入是这样的:
5 6 2 9
3 1 11 4
我将不得不把它当作一个整数。然后使用冒泡排序对其进行排序。问题是我得到这样的输出:9 8 7 4 2 1 -858993460 -858993460 -858993460 -858993460
class inout
{
public :
int i[10];
inout()
{
}
void read()
{
ifstream inClientFile( "input.txt", ios::in );
if ( !inClientFile )
{
cerr << "File could not be opened" << endl;
exit( 1 );
}
int n=0;
while (inClientFile >> i[n])
{
n++;
}
cout << " Data read complete" << endl;
}
void write()
{
ofstream outClientFile( "output.txt", ios::out );
if ( !outClientFile ) {
cerr << "File could not be opened" << endl;
exit( 1 );
}
int n=0;
for (int l=0 ; l < 10 ; l++)
{
outClientFile << i[l] << " " ;
}
cout << " Data Write complete" << endl;
}
void sortData(int asc )
{
int pos=0;
int temp;
temp = i[0];
// asending
if (asc == 1)
{
for (int p = 0; p < 10 ; p ++ )
{
int pos=p;
int temp2,temp = i[p];
for (int j = p+1 ; j < 10 ; j ++ )
{
if (pos == p)
{
if (i[p] > i[j])
{
pos = j;
temp = i[j];
}
}
else {
if (i[pos] > i[j])
{
pos = j;
temp = i[j];
}
}
}
temp2 = i[pos];
i[pos] = i[p];
i[p] = temp2;
}
}
else
{
for (int p = 0; p < 10 ; p ++ )
{
int pos=p;
int temp2,temp = i[p];
for (int j = p+1 ; j < 10 ; j ++ )
{
if (pos == p)
{
if (i[p] < i[j])
{
pos = j;
temp = i[j];
}
}
else {
if (i[pos] < i[j])
{
pos = j;
temp = i[j];
}
}
}
temp2 = i[pos];
i[pos] = i[p];
i[p] = temp2;
}
}
}
};
int main()
{
int d;
inout x;
x.read();
cout<<"Press 1 to sort into ascending order"<<endl<<"Press any other key to sort into descending order"<<endl;
cin>>d;
x.sortData(d);
x.write();
}