1

如何从输入中删除空格?它是一个整数数组。输入是这样的:

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();

}
4

3 回答 3

0

您应该记录实际读取了多少输入数字,inout::read()而不是总是对整个数组进行排序和打印。您的示例输入中有 8 个数字,但您的代码始终会排序并打印 10 个数字。

于 2012-08-14T00:41:31.760 回答
0

可以使用 ifstream 的 rdbuf() 方法将整个文件的内容读入字符串:

std::ifstream in("myfile");

std::stringstream buffer;
buffer << in.rdbuf();

std::string contents(buffer.str());

然后使用通常的string操作技巧来获取整数。像这样的东西:

istringstream iss(contents);
int x=0;
do{
x=0;
iss>>x;
}while(iss);
于 2012-08-14T16:06:23.423 回答
0

问题不在于空格。流提取器将空格(包括多个空格、换行符、制表符)视为分隔符。

输入循环仔细计算它已读取的值的数量,但其余代码忽略该计数。您的输入样本有 8 个元素,但代码硬编码为 10 个元素。数组中的最后两个元素i具有无意义的值,这是一个麻烦的秘诀。

于 2012-08-14T01:13:10.257 回答