1

所以我对 C++ 相当陌生,我有这个班级作业。通常我会向我的教授寻求帮助,但他不回复学生的电子邮件,我们的课是每周一次。几天来我一直试图以奇怪的结果完成这项工作,所以这就是问题所在。

这是提示:

对单列数组进行排序和/或对给定任何列的二维字符数组进行排序。这是我用作模板规范的内容。

这是他使用的类(我们无法更改):

//This class sorts arrays either ascending or descending
template<class T>
class Prob2Sort
{
private:
    int *index;  //Index that is utilized in     the sort
public:
    Prob2Sort(){index=NULL;}; //Constructor
    ~Prob2Sort(){delete []index;};  //Destructor
    T * sortArray(const T*,int,bool); //Sorts a single column array
    T * sortArray(const T*,int,int,int,bool); //Sorts a 2 dimensional array
};

这是驱动程序(也不能触摸它):

cout<<"The start of Problem 2, the sorting problem"<<endl;
Prob2Sort<char> rc;
bool ascending=true;
ifstream infile;
infile.open("Problem2.txt",ios::in);
char *ch2=new char[10*16];
char *ch2p=ch2;
while(infile.get(*ch2)){cout<<*ch2;ch2++;}
infile.close();
cout<<endl;
cout<<"Sorting on which column"<<endl;
int column;
cin>>column;
char *zc=rc.sortArray(ch2p,10,16,column,ascending);
for(int i=0;i<10;i++)
{
    for(int j=0;j<16;j++)
    {
        cout<<zc[i*16+j];
    }
}
delete []zc;
cout<<endl;

并输出:

这个问题的输出。

问题2的开始,排序问题
    Lcekoeddhoffbmg
    Lkcmggjcdhhglif
    Cgldjhcekjigcdd
    Cgldjhcekjigcdn
    Bffmdbkcenlafjk
    Fggdijijegfblln
    Jjlncnimjldfedj
    氨茶碱
    Balgfcaelhfkgeb
    Kmlhmhcddfoielc

在第 15 列排序
    Cgldjhcekjigcdn
    Fggdijijegfblln
    氨茶碱
    Bffmdbkcenlafjk
    Jjlncnimjldfedj
    Lcekoeddhoffbmg
    Lkcmggjcdhhglif
    Cgldjhcekjigcdd
    Kmlhmhcddfoielc
    Balgfcaelhfkgeb

现在我确实改变了一些东西只是为了让它工作;这是我在 main.cpp 中的疯狂代码:

cout<<"The start of Problem 2, the sorting problem"<<endl;
Prob2Sort<char> rc;
bool ascending=true;
ifstream infile;
infile.open("Problem2.txt",ios::in);
char *ch2=new char[(4*17)+ 1];
char *ch2p=ch2;
while(infile.get(*ch2)){ch2++;}

infile.close();
cout<<endl;
cout<<"Sorting on which column"<<endl;
int column;
cin>>column;
char *zc=rc.sortArray(ch2p,4,17,column,ascending);
for(int i=0;i<4;i++)
{
        for(int j=0;j<17;j++)
        {
                cout<<zc[i*17+j];
        }
}
delete []zc;
cout<<endl;

现在这是我的课:

template<class T>
T* Prob2Sort<T>::sortArray(const T* arry, int rows, int cols, int column, bool order)
{
    // put into new array to sort and return
    T* a_ray = new T[(rows*cols) + 1];

    for(int i=0;i<rows;i++)
    {
        for(int j=0;j<cols;j++)
        {
               a_ray[i*cols+j] = arry[i*cols+j];
        }
        cout << endl;
    }

    for(int i=0;i<rows;i++)
    {
        for(int j=0;j<cols;j++)
        {
               cout << a_ray[i*cols+j] ; // displays fine
        }
        cout << endl;
    }

    T temp;
    bool test = true;

    do
    {
        test = false;
        for(int i = 0; i < rows - 1; i++)
        {
            cout << "Row " << i << endl;
            if(a_ray[i*cols+column] > a_ray[(i+1)*cols+column])
            {
                cout << "ELEMENT 1 IS " << a_ray[i*cols+column] << endl;
                temp = arry[i*cols+column];
                cout << "TEMP IS " << temp << endl;
                a_ray[i*cols+column] = a_ray[(i+1)*cols+column];
                cout << "ELEMENT 1 NEW is  " << a_ray[(i)*cols+column] << endl;
                cout << "ELEMENT 2 is " << a_ray[(i+1)*cols+column] << endl;
                a_ray[(i+1)*cols+column] = temp;
                cout << "ELEMENT 2 NEW is  " << a_ray[(i+1)*cols+column] << endl;
                test = true;
             } 

        }      
    }while(test);

现在的问题是(我在触及所有内容之前就遇到了这个问题)是......不知道如何解释它,但我添加了这些 couts 来查看问题,所以我将发布输出我选择第 0 行进行排序所以我的方式让它只对每个元素中的一个字符进行排序,所以我还没有尝试做我的教授想要做的事情

 // this is the file read in (i also has 2 elements at end of each line for the newline 
 //characters which I can see in a hex editor) 

abcdefghijklmno
ABCDEFGHIJKLMNO
1234567890pqrst
uvwxyzPQRSTUVWX
Row 0
ELEMENT 1 IS a
TEMP IS a
ELEMENT 1 NEW is  A
ELEMENT 2 is A
ELEMENT 2 NEW is  a
Row 1

// **PORBLEM HERE***


ELEMENT 1 IS a
TEMP IS A

//**the two couts above should be the same... as you can see why here,

   cout << "ELEMENT 1 IS " << a_ray[i*cols+column] << endl;
   temp = arry[i*cols+column];
   cout << "TEMP IS " << temp << endl;

//////////////////////////////// should be same value!!!! but it is not
// t only does this every other loop if I remember right. it will not change the vlauebut retain the last one in it

ELEMENT 1 NEW is  1
ELEMENT 2 is 1

ELEMENT 2 NEW is  A

感谢任何提供帮助的人,为我的混乱感到抱歉,但我真的需要这个,我不知道发生了什么,甚至我的教授在某个时候我可以问他,但我们仍然不得不转进去了,我就是无法修复这个错误

4

0 回答 0