C++ 使用列表C实现选择排序++;
我想实现选择排序来对航班的起飞时间进行排序,但它不会打印结果(我不确定它是否正确)。抱歉问了这么长又愚蠢的问题,我是编程新手。以下是我现在编写的代码:C++ 使用列表C实现选择排序++;,c++,C++,我想实现选择排序来对航班的起飞时间进行排序,但它不会打印结果(我不确定它是否正确)。抱歉问了这么长又愚蠢的问题,我是编程新手。以下是我现在编写的代码: // Sort class class Sort { protected: // number of comparisons performed in sort function unsigned long num_cmps; public: // main entry point virtual void so
// Sort class
class Sort
{
protected:
// number of comparisons performed in sort function
unsigned long num_cmps;
public:
// main entry point
virtual void sort(std::vector<Flight>& data) = 0;
// returns false if not sorted true otherwise
bool testIfSorted(const std::vector<Flight>& data);
// returns number of comparisons
unsigned long getNumCmps();
// resets the number of comparisons
void resetNumCmps();
};
// SelectionSort class
class SelectionSort : public Sort
{
public:
// main entry point
void sort(std::vector<Flight>& data);
};
// BubbleSort class
class BubbleSort : public Sort
{
public:
// main entry point
void sort(std::vector<Flight>& data);
};
#include "Sort.h"
using namespace std;
unsigned long Sort::getNumCmps()
{
return num_cmps;
}
void Sort::resetNumCmps()
{
num_cmps = 0;
}
void Menu::selection_sort(){
system("cls");
ifstream in("inputFileExample.txt");
if (!in)
{
cerr << "ERROR: wrong input file name!";
exit(-1);
}
SelectionSort();
}
void SelectionSort::sort(std::vector<Flight>& data){
for (int i = 0; i < (num_cmps - 1); i++)
{
int smallest = i;
for(int j = (i+1); j<num_cmps; j++)
{
if(data[j] < data[smallest])
{
smallest = j;
}
}
num_cmps++;
cout << num_cmps;
}
}
//排序类
类排序
{
受保护的:
//在排序函数中执行的比较数
无符号长数字CMP;
公众:
//主要入口点
虚空排序(标准::向量和数据)=0;
//如果未排序为true,则返回false,否则返回true
bool testIfSorted(const std::vector&data);
//返回比较次数
无符号长getnumcps();
//重置比较的数量
void resetnumcps();
};
//选择分类
类别选择排序:公共排序
{
公众:
//主要入口点
无效排序(标准::向量和数据);
};
//泡泡糖类
类BubbleSort:公共排序
{
公众:
//主要入口点
无效排序(标准::向量和数据);
};
#包括“Sort.h”
使用名称空间std;
无符号长排序::getNumCmps()
{
返回num_cmps;
}
void Sort::resetNumCmps()
{
num_cmps=0;
}
无效菜单::选择\排序(){
系统(“cls”);
ifstream-in(“inputFileExample.txt”);
如果(!in)
{
cerr这句话
SelectionSort();
创建一个类型为SelectionSort
的临时对象
实际上,您没有从文件中读取任何内容,您没有要排序的向量,您没有调用排序函数。您的main
在哪里?您是否在任何地方打印结果?尝试在SelectionSort()中打印结果,我在Menu::Selection\u sort()中调用了选择排序但不管我怎么做,都没有显示,甚至连“Hello world”都没有。主菜单是菜单,我没有发布它,因为它与工作代码无关。