#include #include #include using namespace std; const int Max_No_Record=100; //maximum number of records in the file record.txt const int Max_Len_ID=7; //each ID contains 6 characters const int Max_Len_grades=11; //ten grades at most and they are separated by a space const char file_name[20]="record.txt"; void read_record(ifstream &in,char ID[Max_No_Record][Max_Len_ID], char grade[Max_No_Record][Max_Len_grades],int no_records); void sort_by_ID(char ID[Max_No_Record][Max_Len_ID],char grade[Max_No_Record][Max_Len_grades],int no); void sort_by_grades(char ID[Max_No_Record][Max_Len_ID],char grade[Max_No_Record][Max_Len_grades],int no); void print(char ID[Max_No_Record][Max_Len_ID],char grade[Max_No_Record][Max_Len_grades],int no); int main() { char ID[Max_No_Record][Max_Len_ID],grades[Max_No_Record][Max_Len_grades]; int no_records=0; //total number of records in the file int option; ifstream fin; //create input file stream ofstream fout; //create output file stream fin.open(file_name); //associate a file to the input file stream read_record(fin, ID, grades, no_records); //read records into arrays do { do { cout << "Please choose an option:" << endl << "1.sort by ID 2.sort by grades 3.exit: "; cin >> option; }while (option!=1 && option!=2 && option!=3); if (option==1) { sort_by_ID(ID, grades, no_records); }else if (option==2) { sort_by_grades(ID, grades, no_records); }else if (option==3) { return 0; } print(ID, grades, no_records); //print all the records }while(option!=3); return 1; } /* Purpose: Read student records from a file "record.txt" into arrays. Each record consists of a student ID and a list of CE grades separated by a space. Parameter: Input file stream, 2-D Array for storing ID, 2-D Array for storing grades, total number of records (pass by ref) Output: a list of record, number of records */ void read_record(ifstream &in,char ID[Max_No_Record][Max_Len_ID], char grade[Max_No_Record][Max_Len_grades],int no_records){ int check=0; for (int Max_No_Record=0;Max_No_Record<=100;Max_No_Record++){ for (int Max_Len_ID=0;Max_Len_ID<=5;Max_Len_ID++){ in.get(ID[Max_No_Record][Max_Len_ID]); if (ID[Max_No_Record][Max_Len_ID]=='\t')break; } for (int Max_Len_grades=0;Max_Len_grades<=10;Max_Len_grades++){ in.get(grade[Max_No_Record][Max_Len_grades]); if (grade[Max_No_Record][Max_Len_grades] == '\n')break; if (in.eof()){ break; } } if (in.eof()){ break; } } } /* Purpose: Sorting the records by ID. Parameter: ID, grades, total number of records Output: Sorted list of records */ void sort_by_ID(char ID[Max_No_Record][Max_Len_ID],char grade[Max_No_Record][Max_Len_grades],int no_records) { char line[Max_No_Record][7]; int row; for (row=0;row<=Max_No_Record;row++) strcpy(line[row],ID[row]); for (row=0;row<=100;row++) for (int col=0;col<=7;)cout << line[row]; } /* Purpose: Sorting the records by grades. Parameter: ID, grades, total number of records Output: Sorted list of records */ void sort_by_grades(char ID[Max_No_Record][Max_Len_ID],char grade[Max_No_Record][Max_Len_grades],int no_records) { } //print all record void print(char ID[Max_No_Record][Max_Len_ID],char grade[Max_No_Record][Max_Len_grades],int no_records) { for (int i=0; i 1
Hosted by www.Geocities.ws