📄 sorting.cpp
字号:
#include <iostream>#include <fstream>#include <string>#include <cassert>using namespace std;#define MAX_NAME 32#define SIZE 255#define TRUE 1#define FALSE 0struct student { char lastName[MAX_NAME + 1]; char firstName[MAX_NAME + 1]; int studentNumber; int examResult; // (0 - 100)};template<class T>int compare(T first, T second){ if(first > second) return 1; else if(first < second) return -1; else return 0;}template <class T>void MergeSort(T a[], int capacity, int (*comapre)(T, T)) { int first = 0; int last = size - 1; if((*compare)() == -1){ int mid = (first + last) / 2; MergeSort(a, mid, (*compare)(a[first], a[mid])); MergeSort(a, mid + 1, (*compare)(a[mid], a[last])); merge(a, first, mid, last); }}template <class T>void merge(T a[], int first, int mid, int last) { int ndx1 = first; int last1 = mid; int ndx2 = mid + 1; int last2 = last; int i = 0; int j; T* temp = new T[last - first + 1]; if(!temp) { cerr << "Error allocating memory.\n"; exit(1); } while(ndx1 <= last1 && ndx2 <= last2) { if(a[ndx1] <= a[ndx2]) temp[i++] = a[ndx1++]; else temp[i++] = a[ndx2++]; } while(ndx1 <= last1) temp[i++] = a[mdx1++]; while(ndx2 <= last2) temp[i++] = a[ndx2++]; i = 0; for(j = first; j <= last; j++) a[j] = temp[i++]; delete [] temp;}template <class T>void BubbleSort(T a[], int capacity, int(*compare)(T,T)){ int sorted = FALSE; while (sorted == FALSE) { sorted = TRUE; for (int i = 0; i < last; i++) { if (a[i] > a[i+1]) { swap (a[i], a[i+1]); sorted = FALSE; } } }}template <class T> void swap (char &a, char &b) { T temp = a; a = b; b = temp;}int main() { ifstream inputFile; ofstream outputFile; string inputFileName; string outputFileName; char orderChoice; char algorithmChoice; cout << "Enter input file: " << inputFileName << endl; cout << "Enter output file: " << outputFileName << endl; inputFile.open(inputFileName.data()); assert(inputFile.is_open()); outputFile.open(outputFileName.data()); assert(outputFile.is_open()); student a[SIZE]; int capacity; inputFile >> capacity; for(int i = 0; i < capacity; i++){ if(inputFile.eof()) break; inputFile >> a[i].lastName >> a[i].firstName >> a[i].studentNumber >> a[i].examResult; } cout << "n: by last name " << endl; cout << "s: by student number " << endl; cout << "r: by exam result " << endl; while((orderChoice != 'n') || (orderChoice != 's') || (orderChoice != 'r')){ cout << "Enter option [nsr]: " << orderChoice << endl; cin >> orderChoice; } cout << "Enter the sorting algorithm to use: " << endl; cout << "b: bubble sort " << endl; cout << "m: merge sort " << endl; while((algorithmChoice != 'b') || (algorithmChoice != 'm')){ cout << "Enter option [bm]: " << algorithmChoice << endl; cin >> algorithmChoice; } if(algorithmChoice == 'b'){ if(orderChoice == 'n') BubbleSort(a, capacity, compare(a[0].lastName, a[capacity - 1].lastName)); else if(orderChoice == 's') BubbleSort(a, capacity, compare(a[0].studentNumber, a[capacity - 1].studentNumber)); else if(orderChoice == 'r') BubbleSort(a, capacity, compare(a[0].examResult, a[capacity - 1].examResult)); } else if(algorithmChoice == 'm'){ if(orderChoice == 'n') MergeSort(a, capacity, compare(a[0].lastName, a[capacity - 1].lastName)); else if(orderChoice == 's') MergeSort(a, capacity, compare(a[0].studentNumber, a[capacity - 1].studentNumber)); else if(orderChoice == 'r') MergeSort(a, capacity, compare(a[0].examResult, a[capacity - 1].examResult)); } for(int i = capacity-1; i >= 0; i--){ outputFile << a[i].lastName << a[i].firstName << a[i].studentNumber << a[i].examResult << endl; } inputFile.close(); outputFile.close(); cout << "Finished!!!" << endl;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -