0

无法实现排序功能......

  ////ARRAY.h FILE
////////////////////////
#include <iostream>
#include <string>
using namespace std;

class ARRAY
{
public:
    ARRAY(); //Default constructor
    ARRAY(int); //Create Array
    ARRAY(const ARRAY &);
    ~ARRAY(){cout << "Destructor Called in\n"; delete [] DB;}
    ARRAY & operator+(const string &);
    ARRAY & operator=(ARRAY &);
    friend ostream & operator<<(ostream &, ARRAY &);
    void Size_Times_Two();
    void Remove(const string &);
    int Search(const string &);
    void ReadFile(const string &);
    void Sort(const string&);
    bool Is_Full(){return count == capacity;}
    bool Is_Empty(){return count == 0;}
private:
    int count;  
    int capacity;
    string *DB;
};



///////////////////////
//array.cpp FILE/////////////////////

#include <string>
#include "ARRAY.h"
#include <iostream>
#include <fstream>

using namespace std;





  ARRAY::ARRAY()
 {
count = 0;  
capacity = 15;
DB = new string[capacity];


//Read in from file array_strings.txt
ifstream in("array_strings.txt");

while (!in.eof())
{

    if (Is_Full())
    {
        Size_Times_Two();
    }

    in>>DB[count];
    count++;
}
 }

//ARRAY
ARRAY::ARRAY(int size)
 {
count = 0;
capacity = size;
DB = new string[capacity];

  } 

//Fill Array
ostream & operator<<(ostream & out, ARRAY & myArray)
 {
for(int i=0; i<myArray.count; i++)
{
    out<<myArray.DB[i]<<endl;
}   
return out;
 }
///


//Array Initialization Function
ARRAY::ARRAY(const ARRAY & myArray)
{
count = myArray.count;
capacity = myArray.capacity;
DB = new string[capacity];

for (int i=0; i<count; i++)
{
    DB[i] = myArray.DB[i];
}   
  }

//Overload OPERATOR+
ARRAY & ARRAY::operator+(const string & word)
  {
if (Is_Full())
{
    Size_Times_Two();
}

DB[count]=word;
count++;
return *this;

  }
//Overload OPERATOR=
ARRAY & ARRAY::operator=(ARRAY & myArray)
{
if (this != &myArray)
{
delete []DB;
count = myArray.count;
capacity =myArray.capacity;
DB = new string[capacity];

for (int i=0; i<count; i++) 
{
DB[i] =myArray.DB[i];
}   
  }
return *this;
  } 
//SIZE TIMES TWO 
// Increase size of ARRAY Times two 
void ARRAY::Size_Times_Two()
  {
capacity *=2;

string *temp = new string [capacity];

for(int i=0; i<count; i++)
{
    temp[i] = DB[i];
}

delete [] DB;

DB = temp;

}


//SEARCH Function
int ARRAY::Search(const string & word)
{
        for (int i=0; i<<count; i++)
        {
            if (DB[i] == word)
                return i;
        }
    return -1;
}



//Remove Function
void ARRAY::Remove(const string & word)
{
    int loc = Search(word);

    if (loc == -1)
    {
            cout << "ERROR!!!: WORD NOT FOUND\n";
    }
    else {
        for (int i=loc;i<count-1; i++) {
            DB[i] = DB[i+1];
        }
    }
}


    //ReadFile Function
    void ARRAY::ReadFile(const string & filename)
    { 
        ifstream in2(filename.c_str());
        string word;

        if (!in2.fail())
        {
            while (!in2.eof())
            {
                getline(in2, word); 
                *this + word;
            }
            in2.close();
 }
else {
    cout << "File did not open\n";

    }

}




    //Bubble Sort Funtcion
void ARRAY::Sort(const string)
 {
bool swapped = true;
int j = 0;

while (swapped)
{
    swapped =false;
    j++;        
}

for (int i=0; i<count -1; i++) 
{
    for (int j=0; j<DB[i].length(); j++) {

    }
}
return 0;
 }

请帮助我的代码我被困在从 ARRAY.h 文件中实现排序函数排序调用等我不知道要传递什么参数,也不知道如何进行排序,因为我已经有一段时间沉迷于离散数学了

4

1 回答 1

1

我不确定它是否足以使代码真正工作,但至少有几个问题应该很容易解决:

  1. 您在 ARRAY::sort 的声明(表示它应该引用一个字符串)和定义(表示它应该按值接受一个字符串)之间存在不匹配。您似乎没有使用参数,因此您可能只想摆脱两者的参数声明。
  2. 您已经定义ARRAY::sort了一个void返回,但随后您尝试return 0;从中返回。您可能希望return完全删除该语句。
于 2013-09-21T02:52:45.083 回答