如何从输入中删除空格?它是一个整数数组。 输入是这样的:如何忽略输入文件中的空格?
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();
}
应该避免 “幻数” 等在'对(INT p = 0时,P <10;的p ++)十'。 – ChiefTwoPencils 2012-08-14 00:42:57
如何获得一个字符串等价并做String.Replace(“”,“”);然后转换为整数使用string = int.Parse(string); – user959631 2012-08-14 00:45:56