gpt4 book ai didi

c++ - 从文件中读取的 ARRAY 类 BubbleSort

转载 作者:行者123 更新时间:2023-11-28 07:24:51 28 4
gpt4 key购买 nike

<分区>

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

  ////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 文件的调用进行排序,我不知道要传递什么参数,也不知道如何进行排序,因为自从我沉迷于离散数学以来已经有一段时间了

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com