我有一个上学项目。需要阅读文件并按目的地,出发(24小时格式),flightNo和gateNo排序。 我尝试排序,但它不起作用..我是编程新手,如果有人可以帮助我,我会很感激。
// Name : Flight.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
using std::string;
class Flight
{
private:
string flightNo;
string destination;
string departure;
string gateNo;
public:
Flight(void);
~Flight(void);
void readingFile();
void writingFile();
};
// Flight.cpp
#include "Flight.h"
#include <fstream>
#include <cstring>
using namespace std;
Flight::Flight(void)
{
}
Flight :: ~Flight(void)
{
}
void Flight :: readingFile()
{
string FileName;
string line;
cout << "Enter a file name: " << endl;
cin >> FileName;
ifstream inFile(FileName.c_str());
if (inFile.is_open())
{
while (getline( inFile, line))
{
cout << line <<endl;
}
inFile.close();
}else
cout << "File is not open!" <<endl;
}
//Sort.h
#pragma once
#include "Flight.h"
#include <vector>
// Sort class
class Sort
{
protected:
// number of comparisons performed in sort function
unsigned long num_cmps;
public:
// main entry point
//virtual void sort(vector<Flight>& data) = 0;
void sortflights();
// 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);
};
class MergeSort : public Sort
{
// main entry point
void sort(std::vector<Flight>& data);
};
//Sort.cpp
#include "Sort.h"
#include <iostream>
using namespace std;
unsigned long Sort::getNumCmps()
{
return num_cmps;
}
void Sort::resetNumCmps()
{
num_cmps = 0;
}
void Sort :: sortflights()
{
int choose;
cout<<"Choose an option!"<<endl;
cout<<"PRESS 1 FOR SORT BY DESTINATION"<<endl;
cout<<"PRESS 2 FOR SORT BY DEPARTURE"<<endl;
cout<<"PRESS 3 FOR SORT BY FLIGHT NUMBER"<<endl;
cout<<"PRESS 3 FOR SORT BY GATE NUMBER"<<endl;
cin>>choose;
if(choose==1)
{
vector<string> destination;
for(int i=0; i<destination.size(); i++)
{
string elem1=destination[i];
string elem2=destination[i+1];
if (strcmp(elem1.c_str(),elem2.c_str())>0)
{
cout << elem1<<endl ;
}
else
cout << elem2<<endl ;
cout << "Number of iteration = " << i << endl;
}
}
else if(choose==2)
{
vector<string> departure;
for(int i=0; i<departure.size(); i++)
{}
答案 0 :(得分:0)
首先,没有理由将航班号和门号作为字符串。他们只是数字。将字符串从文件转换为int。如果你不能使用stl,你只需要将日期时间保持为字符串我猜:(
将其分解成碎片。您需要进行三种不同的比较:数字,日期时间和词典,这取决于您要排序的内容。
数字排序只是数字比较:if( a < b )
。
字典排序相对简单。字符只是8位数字,映射到存储在某处的字形。您可以使用与比较整数相同的方式逐个字符地比较字符串。一旦找到彼此不同的字符,就可以比较字符并确定哪个字符更大。
没有stl,日期时间会很棘手。你必须找到一种方法来将字符串解析成你可以比较的东西。谷歌解析该字符串的方法。
唯一剩下的就是排序算法。访问有关插入排序,合并排序,快速排序和选择排序的维基百科页面。它们详细描述了每种算法的工作原理,并且在大多数情况下会对其实现进行伪代码。
在这个问题上真的没什么好说的。