Application of linear list(线性表的应用)

版权声明:本文为博主原创文章,未经博主允许不得转载2018-10-06

这里贴出英文的题目要求,因为作业是以英文的形式提交的,所以以下的注释也是用英文,不懂的可以直接去翻译。

Comprehensive experiment 1Application of linear list

One. Experimental target

1Use the basic operations to implement the specific operations for the linear table;

2Master the application of file operations;

3Improve the understanding of the data structure of linked storage structure, and gradually cultivate the programming ability to solve practical problems.

Two. Experimental environment

A computer with Visual C ++ 6.0 / CFree.

This experiment has 4 class hours in all.

Three. Experiment content

1. address list design

Design a classmate's address list, requested as follows:

ü Each student in the address list contains the following information: student idnametelephone number. If you need more fields, please add them yourself.

ü The program has a main menu containing the following functions:

(1) Add a record: Add a student record from the input.

(2) Delete a record: Delete a student record according to the student id from the input.

(3) Output all records: Display all the records in the address list.

(4) Search by name: Input the student name and then output the whole information of the student.

(5) Save records: Save all the records in the address list to a certain file.

(6) Clear records: Delete all the records in the address list and then delete the file.

(7) Quit

hint

ü When the program starts, it should be determined whether there is a record file. If the file exists, read each record from it to the sequence list.

ü After the user selects and completes a function of the main menu, the program should return to the main menu.

ü When a record is added, it should be inserted into the tail of the list.

ü If a record does not exist when performing delete or and search operation, the program should output some information to the user.

ü You do not need to write files when adding records or deleting records.

ü When you want to save a record you’d better overwrite the file. (Or delete the original file first, and then save all the records)

ü Each module is written in the form of a function, called by the main function.

optional

ü Add a sorting function in the main menu, the sorting result should be in an ascending order according to the student number. Sorting methods can be done by bubble sort or insert sort.

 

Four. Requirement

1Submit the experimental reports in groups (no more than 3 persons in each group).

2Submit the source code individually for submission. The file name is named as: 

         Long student ID_Name_CE1.doc   OR   Long student ID_Name_CE1.pdf

The report template is shown as follows:

 1 #ifndef CLASSMATE_ADDRESS_LIST_H
 2 #define CLASSMATE_ADDRESS_LIST_H
 3 
 4 #include<string>
 5 
 6 using namespace std;
 7 
 8 const int MAXN = 100;
 9 class classmate_address_list{
10 private:
11     string student_id[MAXN];
12     string student_name[MAXN];
13     string telephone_number[MAXN];
14     int length = 0;
15 public:
16     int get_length();
17     void add_length();
18     string get_student_id(int i);
19     string get_student_name(int i);
20     string get_student_numbers(int i);
21     void output_data(classmate_address_list list);
22     void read_data_from_file(classmate_address_list &list);
23     void add_data(classmate_address_list &list);
24     void clear_data(classmate_address_list &list);
25     void del_data(classmate_address_list &list);
26     void save_data(classmate_address_list list);
27     void menu();
28     void search_data(classmate_address_list list);
29     void fun(string *s, int x, int y);
30 };
31 
32 #endif
classmate_address_list.h
  1 #include"classmate_address_list.h"
  2 #include<iomanip>
  3 #include<fstream>
  4 #include<iostream>
  5 #include<conio.h>
  6 #include<Windows.h>
  7 #include<algorithm>
  8 
  9 using namespace std;
 10 
 11 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);                            //Get the standard output handle
 12 
 13 void setOutputPosition(int x, int y, HANDLE hOut){                        //Mouse cursor moved the a specific location(x, y)
 14     COORD position;
 15     position.X = x;
 16     position.Y = y;
 17     SetConsoleCursorPosition(hOut, position);
 18 }
 19 
 20 
 21 
 22 int classmate_address_list::get_length() {                                //get the length of data in the list
 23     return this->length;
 24 }
 25 
 26 void classmate_address_list::add_length() {                                //the length of data in the list is increased by 1
 27     length++;
 28 }
 29 
 30 string classmate_address_list::get_student_id(int i) {                    //get the student number of i student
 31     return this->student_id[i];
 32 }
 33 
 34 string classmate_address_list::get_student_name(int i) {                //get the student name of i student
 35     return this->student_name[i];
 36 }
 37 
 38 string classmate_address_list::get_student_numbers(int i) {                //get the student telephone number of i student
 39     return this->telephone_number[i];
 40 }
 41 
 42 void classmate_address_list::fun(string *s, int x, int y) {
 43     system("cls");
 44     for (int i = x; i < y; i++) {
 45         cout << s[i] << endl;
 46     }
 47     Sleep(100);
 48 }
 49 
 50 void classmate_address_list::menu() {
 51     system("cls");
 52     int j = 11;
 53     string s[12];
 54     s[0] = "                                                                      @         @";
 55     s[1] = "                                                                      @        @@               @";
 56     s[2] = "                                                                      @                         @";
 57     s[3] = "                                                                      @        @@@    @@@@@  @@@@@@";
 58     s[4] = "                                                                      @         @     @@  @     @";
 59     s[5] = "                                                                      @         @     @@        @";
 60     s[6] = "                                                                      @         @      @@@      @";
 61     s[7] = "                                                                      @         @        @@@    @";
 62     s[8] = "                                                                      @    @    @     @    @    @  @";
 63     s[9] = "                                                                      @    @    @     @    @    @  @";
 64     s[10] = "                                                                      @@@@@@   @@@@@   @@@@     @@@@";
 65     for (int i = 0; i < 11; i++) {
 66         cout << s[i] << endl;
 67     }
 68     /*cout << "********************************************************************************" << endl;
 69     cout << setw(36) << "welcome to student address list" << endl;
 70     cout << endl;
 71     cout << setw(35) << "Please press 1 to add a record" << endl;
 72     cout << endl;
 73     cout << setw(38) << "Please press 2 to delete a record" << endl;
 74     cout << endl;
 75     cout << setw(40) << "Please press 3 to output all record" << endl;
 76     cout << endl;
 77     cout << setw(37) << "Please press 4 to search by name" << endl;
 78     cout << endl;
 79     cout << setw(35) << "Please press 5 to save records" << endl;
 80     cout << endl;
 81     cout << setw(36) << "Please press 6 to clear records" << endl;
 82     cout << endl;
 83     cout << setw(39) << "Please press 7 to exit the program" << endl;
 84     cout << endl;
 85     cout << "********************************************************************************" << endl;
 86     cout << endl;
 87     cout << setw(49) << "Please enter the option of your choice (1-7):";*/
 88     for (int i = 1; i < 10; i++) {
 89         setOutputPosition(57, 5 + i, hOut);                                //Mouse cursor moved the a specific location(57, 5+i)
 90         cout << "*";
 91         setOutputPosition(16, 5 + i, hOut);                                //Mouse cursor moved the a specific location(16, 5+i)
 92         cout << "*";
 93     }
 94     setOutputPosition(20, 4, hOut);
 95 //    SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);
 96     cout << "welcome to student address list" << endl;
 97 
 98     setOutputPosition(17, 6, hOut);
 99     cout << "*****************************************" << endl;
100     setOutputPosition(20, 7, hOut);
101     SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);//set the foreground color of the text
102     cout << "Please press 1 to add a record" << endl;
103     setOutputPosition(20, 8, hOut);
104     SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
105     cout << "Please press 2 to delete a record" << endl;
106     setOutputPosition(20, 9, hOut);
107     SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
108     cout << "Please press 3 to output all record" << endl;
109     setOutputPosition(20, 10, hOut);
110     SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
111     cout << "Please press 4 to search by name" << endl;
112     setOutputPosition(20, 11, hOut);
113     SetConsoleTextAttribute(hOut, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
114     cout << "Please press 5 to save records" << endl;
115     setOutputPosition(20, 12, hOut);
116     SetConsoleTextAttribute(hOut, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
117     cout << "Please press 6 to clear records" << endl;
118     setOutputPosition(20, 13, hOut);
119     SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
120     cout << "Please press 7 to exit the program" << endl;
121     setOutputPosition(17, 14, hOut);
122     SetConsoleTextAttribute(hOut, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
123     cout << "*****************************************" << endl;
124     setOutputPosition(17, 15, hOut);
125     cout << "Please enter the option of your choice (1-7):";
126 }
127 
128 void classmate_address_list::read_data_from_file(classmate_address_list &list) {    //read data from file
129     ifstream in("data.txt");                                                        //open the file of data
130 
131     string id;
132     string name;
133     string number;
134 
135     if (in.fail()) {
136         cerr << "Error, file is not exist, Press any key to exit...." << endl;        //if the file not exist, program will quit
137         _getch();
138         exit(0);
139     }
140     int i = 0;
141     while (in >> id >> name >> number && !in.fail()){                                //read data from file
142         list.student_id[i] = id;                                                    
143         list.student_name[i] = name;
144         list.telephone_number[i] = number;
145         list.length++;                                                                //Record the number of fields read
146         i++;
147     }
148     return;
149 }
150 
151 
152 void classmate_address_list::add_data(classmate_address_list &list) {
153 
154     while (1) {
155         list.menu();
156         int i = list.length;
157         if (i >= MAXN) {                                                            //Exception handling
158             cerr << "full" << endl;
159             return;
160         }
161 
162         string id, name, number;
163         setOutputPosition(17, 16, hOut);
164         cout << "Please enter the student id you want to add: ";                    //Enter the message of student you want yo add
165         cin >> id;
166         setOutputPosition(17, 17, hOut);
167         cout << "Please enter the student name you want to add: ";
168         cin >> name;
169         setOutputPosition(17, 18, hOut);
170         cout << "Please enter the telephone number you want to add: ";
171         cin >> number;
172         list.student_id[i] = id;
173         list.student_name[i] = name;
174         list.telephone_number[i] = number;
175         list.add_length();                                                            //the length of list is increased by 1
176         setOutputPosition(17, 19, hOut);
177         cout << "Whether to continue(y/n): ";
178         char op;
179         cin >> op;
180         if (op == 'y')
181             continue;
182         else return;
183     }
184 }
185 
186 void classmate_address_list::del_data(classmate_address_list &list) {
187     while (1) {
188         string id;
189         list.menu();
190         cout << endl;
191         cout << "                " << "Please enter the student id you want to delete: ";//Prompt input
192         cin >> id;
193         int i = 0;
194         while (i < list.get_length()) {
195             if (list.student_id[i] == id) {
196                 cout << "                the student id of you delete: " << list.student_id[i] << endl;
197                 cout << "                the student name of you delete: " << list.student_name[i] << endl;
198                 cout << "                the student telephone number of you delete: " << list.telephone_number[i] << endl;
199 
200                 for (int j = i + 1; j < list.length; j++) {                                //Move the data after the i-th bit forward
201                     list.student_id[j - 1] = list.student_id[j];
202                     list.student_name[j - 1] = list.student_name[j];
203                     list.telephone_number[j - 1] = list.telephone_number[j];
204                 }
205                 i--;                                                                    //The index of research is decreased by 1
206                 list.length--;                                                            //The length of list is decreased by 1
207             }
208             i++;
209         }
210         if (i>=list.get_length())
211             cerr << "                " << "not research this id" << endl;                //Exception handing
212 
213         cout << "                " << "Whether to continue(y/n): ";
214         char op;
215         op = _getch();
216         if (op == 'y')continue;
217         else return;
218     }
219 }
220 
221 void classmate_address_list::clear_data(classmate_address_list &list) {
222     list.length = 0;                                                                    //Delete all the records in the address list
223     remove("data.txt");                                                                    //delete the file.
224     return;
225 }
226 
227 void classmate_address_list::save_data(classmate_address_list list) {
228     ofstream out("data.txt");
229     for (int i = 0; i < list.length; i++) {                                                // Save all the records in the address list to a certain file.
230         out << list.student_id[i] << " " << list.student_name[i] << " " << list.telephone_number[i] << endl;
231     }
232 }
233 
234 void classmate_address_list::search_data(classmate_address_list list) {
235     while (1) {
236         list.menu();
237         string name;
238         cout << endl;
239         cout << "                " << "Please enter the name of the student you want to query: ";
240         cin >> name;                                                //Enter the name to research
241         int i = 0;
242         while (i++ < list.get_length()) {
243             if (name == list.student_name[i]) {                        //Output the sepcific records
244                 cout << "                " << "The id of the student you want to query: " << list.student_id[i] << endl;
245                 cout << "                " << "The name of the student you want to query: " << list.student_name[i] << endl;
246                 cout << "                " << "The telephone number of the student you want to query: " << list.telephone_number[i] << endl;
247                 break;
248             }
249         }
250 
251         if (i >= list.get_length()){                            //Exception handing
252             cerr << "                " << "not find this message" << endl;
253         }
254 
255         cout << "                " << "Whether to continue(y/n):";
256         char op;
257         cin >> op;
258         if (op == 'y') continue;
259         else return;
260     }
261 }
262 
263 void classmate_address_list::output_data(classmate_address_list list) {
264     if (list.get_length() == 0) {                            //Exception handing
265         cerr << "                " << "it's empty! Please press any key to return the main menu.....";
266         getchar();
267         getchar();
268         return;
269     }
270     while (1) {
271         list.menu();                                        //Display the main menu
272         int i = 0;
273         cout << endl;
274         classmate_address_list tmp = list; 
275         for (int i = 0; i < tmp.get_length(); i++) {        //Sorting result in an ascending order according to the student number. 
276             for (int j = i + 1; j < tmp.get_length(); j++) {
277                 if (tmp.student_id[i]>tmp.student_id[j]) swap(tmp.student_id[i], tmp.student_id[j]);
278             }
279         }
280         while (i < tmp.length) {                            //Output all records
281             cout << "                number " << i + 1 << ":" << endl;
282             cout << "                student_id: " << tmp.student_id[i] << endl;
283             cout << "                student_name: " << tmp.student_name[i] << endl;
284             cout << "                telephone_number: " << tmp.telephone_number[i] << endl;
285             i++;
286             cout << endl;
287         }
288         cout << "                " << "Whether to continue(y/n):";
289         char op;
290         cin >> op;
291         if (op == 'y')
292             continue;
293         else return;
294     }
295 }
classmate_address_list.cpp
 1 #include<iostream>
 2 #include<fstream>
 3 #include<iomanip>
 4 #include<sstream>
 5 #include<stdlib.h>                 //Clear screen file
 6 #include<Windows.h>
 7 #include<time.h>
 8 #include<conio.h>
 9 #include"classmate_address_list.h"//import the header file of the class of classmate_address_list
10 using namespace std;
11 
12 
13 
14 int main() {
15     classmate_address_list list;
16     string s[12];
17     s[0] = "                                                                      @         @";
18     s[1] = "                                                                      @        @@               @";
19     s[2] = "                                                                      @                         @";
20     s[3] = "                                                                      @        @@@    @@@@@  @@@@@@";
21     s[4] = "                                                                      @         @     @@  @     @";
22     s[5] = "                                                                      @         @     @@        @";
23     s[6] = "                                                                      @         @      @@@      @";
24     s[7] = "                                                                      @         @        @@@    @";
25     s[8] = "                                                                      @    @    @     @    @    @  @";
26     s[9] = "                                                                      @    @    @     @    @    @  @";
27    s[10] = "                                                                      @@@@@@   @@@@@   @@@@     @@@@";
28 
29     int j = 11;
30     while (j) {
31         list.fun(s, j, 11);
32         j--;
33     }
34     
35     list.read_data_from_file(list);
36 
37     while (1) {
38         list.menu();
39         int op;                                        //display main menu
40         std::cin >> op;                                //input the number of function
41 
42         switch (op) {
43         case 1:
44             list.add_data(list);                    //Add a record
45             break;
46         case 2:
47             list.del_data(list);                    //Delete a record
48             break;
49         case 3:
50             list.output_data(list);                    //Output all records
51             break;
52         case 4:
53             list.search_data(list);                    //Search by name
54             break;
55         case 5:
56             list.save_data(list);                    //Save records
57             break;
58         case 6:
59             list.clear_data(list);                    //Clear records
60             break;
61         case 7:
62             exit(0);                                //exit the program
63         default:
64             cout << "                " << "You make a mistake, press any key to return to the menu.....";
65             _getch();
66         }
67     }
68 }
main.cpp

 

posted @ 2018-10-06 00:14  明楼  阅读(182)  评论(0)    收藏  举报