数据结构实验课程----实验一(利用顺序表实现学生健康系统)

题目:

实现学生健康情况管理的几个操作功能(新建、插入、删除、从文件读取、写入文件和查询、屏幕输出等功能)。健康表中学生的信息有学号、姓名、出生日期、性别、身体状况等。

实验内容:

必做内容:

1.利用顺序存储结构来实现

2.系统的菜单功能项如下: 1------新建学生健康表 2------向学生健康表插入学生信息 3------在健康表删除学生信息 4------从文件中读取健康表信息 5------向文件写入学生健康表信息 6------在健康表中查询学生信息(按学生学号来进行查找) 7------在屏幕中输出全部学生信息 8-----退出

PS:如果有时间会继续完善!主要参考vector的代码实现!

  1 #include<iostream>
  2 #include<string>
  3 #include<fstream>
  4 using namespace std;
  5 
  6 struct Student
  7 {
  8     string number;
  9     string name;
 10     string dob;
 11     string sex;
 12     string condition;
 13 };
 14 
 15 ostream & operator << ( ostream & os, const Student & x )
 16 {
 17     os << "学号:" << x.number << endl;
 18     os << "姓名: " << x.name  << endl;
 19     os << "出生日期: " << x.dob << endl;
 20     os << "性别:" << x.sex << endl;
 21     os << "身体状况:" << x.condition << endl;
 22     return os;
 23 }
 24 /*重载输出“<<”符号,方便Student结构体的输出*/
 25 
 26 class SeqList
 27 {
 28 public:
 29     enum { SPARE_CAPACITY = 16 };
 30     explicit SeqList( int initSize = 0 ) : theSize( initSize ), theCapacity( initSize + SPARE_CAPACITY )
 31     {
 32         data = new Student [ theCapacity ];
 33     }
 34     /*explicit构造函数,防止隐式类型转换!*/
 35 
 36     /*SeqList ( const SeqList & rhs )
 37     {
 38         operator= ( rhs );
 39     }
 40     const SeqList & operator= ( const SeqList & rhs )
 41     {
 42         if( this != &rhs )
 43         {
 44             delete [ ] data;
 45             theSize = rhs.size( );
 46             theCapacity = rhs.capacity( );
 47 
 48             data = new Student[ theCapacity ];
 49             for( int i = 0; i < size( ); ++i)
 50                 data[ i ] = rhs.data[ i ];
 51         }
 52         return *this;
 53     }*/
 54     /*以上函数可以省去!因为参考vector代码实现,所以也敲上以上代码!*/
 55 
 56     ~SeqList( )
 57     {
 58         delete [ ] data;
 59         theSize = 0;
 60     }
 61     /*析构函数*/
 62 
 63     void insert( const Student & x )
 64     {
 65         data[ theSize++ ] = x;
 66         if( theSize == theCapacity )
 67         {
 68             reserve( 2 * theCapacity + 1 );
 69         }
 70     }
 71     /*插入操作!可以防止插入元素后,元素的数目大于SeqList的最大容量!*/
 72 
 73     /*void resize( int newSize )
 74     {
 75         if( newSize > theCapacity )
 76             reserve( 2 * newSize + 1 );
 77         theSize = newSize;
 78     }*/
 79     /*vector的代码实现的一部分!可省!*/
 80 
 81     void reserve( int newCapacity )
 82     {
 83         if( newCapacity < theCapacity )
 84             return;
 85 
 86         Student * oldData = data;
 87 
 88         data = new Student[ newCapacity ];
 89         for( int i = 0; i < size( ); ++i)
 90         {
 91             data[ i ] = oldData[ i ];
 92         }
 93 
 94         theCapacity = newCapacity;
 95 
 96         delete [ ] oldData;
 97     }
 98     /*SeqList的容量动态增长的实现!*/
 99 
100     void remove( const Student & x )
101     {
102         int i;
103         for( i = 0; i < size( ); ++i)
104         {
105             if( data[ i ].number == x.number )
106                 break;
107         }
108         if( i == size( ) )
109             cerr << "对不起,这个健康表不存在你要删除的学生及其信息,请先查看全部学生的信息!谢谢!" << endl;
110         else
111         {
112             for( int j = i; j < size( ) - 1 ; ++j)
113                 data[ j ] = data[ j + 1 ];
114             --theSize;
115             cout << "删除学生及其信息成功!" << endl;
116         }
117     }
118     /*删除学生信息的操作!*/
119 
120     void serach( string & number)
121     {
122         int i;
123         for( i = 0; i < size( ); ++i )
124         {
125             if( data[ i ].number == number )
126                 break;
127         }
128         if( i != size( ) )
129         {
130             cout << "该学号的学生的信息存在健康表!" << endl;
131             cout << data[ i ] << endl;
132         }
133         else
134             cerr << "对不起,这个健康表不存在你要查找的学生及其信息,请先查看全部学生的信息!谢谢!!" << endl;
135     }
136     /*给出学号查找学生的信息!*/
137 
138     /*bool serach( Student & x )
139     {
140         int i;
141         for( i = 0; i < size( ); ++i )
142         {
143             if( data[ i ].number == x.number )
144                 break;
145         }
146         if( i != size( ) )
147         return true;
148         else
149         return false;
150     }*/
151 
152     int size( ) const
153     {
154         return theSize;
155     }
156     /*返回SeqList的元素数目*/
157 
158     int capacity( ) const
159     {
160         return theCapacity;
161     }
162     /*返回SeqList的最大容量*/
163 
164     Student & operator[] ( int index )
165     {
166         return data[ index ];
167     }
168     /*[]操作符的重载*/
169 
170     const Student & operator[] ( int index ) const
171     {
172         return data[ index ];
173     }
174     /*同上*/
175 
176     void output( )
177     {
178         for( int i = 0; i < size( ); ++i)
179             cout << data[ i ] <<" ";
180         cout << endl;
181     }
182     /*将SeqList里面的元素全部输出*/
183 
184 private:
185     Student * data;                 //Student结构体的数组
186     int theCapacity;                //最大容量
187     int theSize;                    //元素的数目
188 };
189 
190 
191 
192 int main( )
193 {
194     //bool flag1 = false;
195     bool flag2 = false;
196     //bool flag3 = false;
197     cout << "---------------------------欢迎使用学生健康管理系统---------------------------" << endl;
198     SeqList List;
199     cout << "健康表已经自动建立!" << endl;
200     int m;
201     while( 1 )
202     {
203         cout << "请选择需要的操作!" << endl;
204         cout << "1:插入学生信息。 2:删除学生信息。 3:从文件读入信息。" << endl;
205         cout << "4:向文件写入信息。 5:查询。 6:输出全部信息。7:退出。" << endl;
206 
207         cin >> m;
208 
209         if( m == 1 )
210         {
211                 Student x;
212                 cout << "请依次输入学生的学号、姓名、出生年月日、性别、身体情况!" << endl;
213                 cin >> x.number >> x.name >> x.dob >> x.sex >> x.condition;
214                 List.insert( x );
215         }
216 
217         else if( m == 2 )
218         {
219                 Student y;
220                 cout << "请依次输入学生的学号、姓名、出生年月日、性别、身体情况!" << endl;
221                 cin >> y.number >> y.name >> y.dob >> y.sex >> y.condition;
222                 List.remove( y );
223         }
224 
225         else if( m == 3 )
226         {
227                 if( !flag2 )
228                     cout << "你还没有向文件写入学生健康信息!请先写入学生健康信息后在" << endl;
229                 else
230                 {
231                     ifstream read( "list.txt" );
232                     char s;
233                     while( read.get( s ) )
234                     cout << s;
235                     cout << endl;
236                     cout << "--------------------读取结束!--------------------" << endl;
237                     read.close( );
238                 }
239         }
240 
241         else if( m == 4 )
242         {
243                 string select;
244                 cout << "是否已经将需要记录的学生全部记录?(该操作是依次将健康表里面的全部信息写入文件里)" << endl;
245                 cout << "如果选择是就输入“YES”;否则输入“NO”,继续将学生健康信息插入健康表里。" << endl;
246                 cin >> select;
247 
248                 if( select == "YES" )
249                 {
250                     ofstream write( "list.txt" );
251                     for( int i = 0; i != List.size( ); ++i )
252                     {
253                         write << List[ i ];
254                     }
255                     flag2 = true;
256                     write.close( );
257                 }
258                 else
259                 {
260                     cout << "请继续插入或删除操作!" << endl;
261                 }
262         }
263 
264         else if( m == 5)
265         {
266             string num;
267             cout << "请输入你想查询的学生的学号!" << endl;
268             cin >> num;
269             List.serach( num );
270         }
271 
272         else if( m == 6 )
273         {
274             cout << "输出健康表中全部学生的信息!" << endl;
275             List.output( );
276         }
277 
278         else
279         {
280             cout << "---------------------------谢谢使用学生健康管理系统---------------------------" << endl;
281             break;
282         }
283     }
284     return 0;
285 }

 

posted @ 2012-09-20 21:10  alan_forever  阅读(1871)  评论(0编辑  收藏  举报