데이터 구조 실험 과정 - 실험 2 (링크 를 이용 하여 학생 건강 시스템 실현)

92504 단어 데이터 구조
제목:
학생 건강 상태 관 리 를 위 한 몇 가지 조작 기능 (새로 만 들 기, 삽입, 삭제, 파일 읽 기, 파일 쓰기, 조회, 화면 출력 등 기능) 을 실현 합 니 다.건강 표 중 학생 의 정 보 는 학 번, 성명, 생년월일, 성별, 신체 상태 등 이 있다.
실험 내용:
필수 내용:
1. 싱글 체인 시 계 를 이용 하여 실현 (PS: 저 는 더 블 체인 시 계 를 사 용 했 습 니 다)
2. 시스템 의 메뉴 기능 항목 은 다음 과 같다.7 - --- 모든 학생 정 보 를 화면 에 출력 8 - - - 종료
PS: 나중에 수정 할 게 요.
주로 에서 STL 의 코드 실현 을 참고 했다. http://www.cnblogs.com/alan-forever/archive/2012/09/16/2687480.htm l
  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     bool operator == ( const Student & x )

 15     {

 16         return number == x.number;

 17     }

 18     //==     

 19 };

 20 

 21 ostream & operator << ( ostream & os, const Student & x )

 22 {

 23     os << "  :" << x.number << endl;

 24     os << "  : " << x.name  << endl;

 25     os << "    : " << x.dob << endl;

 26     os << "" << x.sex << endl;

 27     os << "" << x.condition << endl;

 28     os << endl;

 29     return os;

 30 }

 31 /*    “<<”  ,  Student      */

 32 

 33 template <typename Object>

 34 class List

 35 {

 36 private:

 37     struct Node

 38     {

 39         Object data;

 40         Node * next;

 41         Node * prev;

 42 

 43         Node ( const Object & d = Object( ), Node *p = NULL, Node *n = NULL ) : data( d ), prev( p ), next( n ) { }

 44     };

 45     //       ,    ,   ,   。

 46 

 47 public:

 48     List( )

 49     {

 50         init( );

 51     }

 52                                                 //List    ,      init         ,    ,   。

 53     ~List( )

 54     {

 55         clear( );

 56         delete head;

 57         delete tail;

 58     }

 59                                                 //    

 60     void clear( )

 61     {

 62         Node * p = begin( );

 63         Node * q;

 64         while( p != end( ) )

 65         {

 66             q = p;

 67             p = p->next;

 68             delete q;

 69         }

 70         theSize = 0;

 71     }

 72                                                 //

 73     int size( ) const

 74     {

 75         return theSize;

 76     }

 77                                                 //

 78     bool empty( )

 79     {

 80         return size( ) == 0;

 81     }

 82                                                 //

 83     Node * locate( int i )

 84     {

 85         if( i < 0 || i > size( ))

 86             return NULL;

 87         else

 88         {

 89             Node * p = head;

 90             for( int j = 1; j <= i; ++j )

 91             {

 92                 p = p->next;

 93             }

 94             return p;

 95         }

 96     }

 97                                                 //   i   。

 98     Node * begin( )

 99     {

100         return head->next;

101     }

102                                                 //

103     Node * end( )

104     {

105         return tail;

106     }

107                                                 //

108     Object getLocate( int i )

109     {

110         Node *p = locate( i );

111         return p->data;

112     }

113                                                 //   i        。

114     void push_front( const Object & x )

115     {

116         insert( begin( ), x );

117     }

118                                                 //

119     void push_back( const Object & x )

120     {

121         insert( end( ), x );

122     }

123                                                 //

124     void insert( Node * p, const Object & x )

125     {

126         p->prev = p->prev->next = new Node( x, p->prev, p );

127         theSize++;

128     }

129                                                 // p         ,    x。

130     int search( const Object & x )

131     {

132         int i;

133         Node * p = begin( );

134         for( i = 1; i <= size( ); ++i )

135         {

136             if( p->data == x )

137             {

138                 break;

139             }

140             p = p->next;

141         }

142         /*if( i <= size( ))

143         {

144             cout << "              !" << endl;

145         }

146         else

147             cout << "   ,                   ,           !  !!" << endl;*/

148         if( i <= size( ) )

149             return i;

150         else

151             return -1;

152     }

153                                                 //    x      ,     ,  -1

154     void remove( int i )

155     {

156         Node * p = locate( i );

157         p->prev->next = p->next;

158         p->next->prev = p->prev;

159         delete p;

160         theSize--;

161     }

162                                                 //   i   。

163     void output( )

164     {

165         Node * p = begin( );

166         for( int i = 1; i <= size( ); ++i)

167         {

168             cout << p->data;

169             p = p->next;

170         }

171         cout << endl;

172     }

173                                                 //

174 private:

175     int theSize;                                //

176     Node * head;                                //

177     Node * tail;                                //

178 

179     void init( )

180     {

181         theSize = 0;

182         head = new Node;

183         tail = new Node;

184         head->next = tail;

185         tail->prev = head;

186     }                                           //

187 };

188 

189 int main( )

190 {

191     bool flag = false;

192     bool flag1 = false;

193     List<Student>  list;

194     cout << "---------------------------            ---------------------------" << endl;

195     cout << "" << endl;

196     int m, m1;

197     while( 1 )

198     {

199         cout << "" << endl;

200         cout << "1:      。 2:      。 3:       。" << endl;

201         cout << "4:       。 5:  。 6:      。  :  。" << endl;

202         cout << endl;

203 

204         cin >> m;

205 

206         if( m == 1 )

207         {

208             int num;

209             Student x;

210             cout << "" << endl;

211             cout << "1:   。 2:   。" << endl;

212             cin >> m1;

213             cout << "" << endl;

214             cin >> num;

215             if( m1 == 1 )

216             {

217                 for( int i = 1; i <= num; ++i)

218                 {

219                     cout << "          、  、     、  、    !" << endl;

220                     cin >> x.number >> x.name >> x.dob >> x.sex >> x.condition;

221                     list.push_front( x );

222                 }

223                 cout << "" << endl;

224             }

225             else

226             {

227                 for( int i = 1; i <= num; ++i)

228                 {

229                     cout << "          、  、     、  、    !" << endl;

230                     cin >> x.number >> x.name >> x.dob >> x.sex >> x.condition;

231                     list.push_back( x );

232                 }

233                 cout << "" << endl;

234             }

235             if( flag1 ==  false )

236             flag1 = true;

237             cout << endl;

238         }

239 

240         else if( m == 2 )

241         {

242             int i;

243             Student y;

244             cout << "          、  、     、  、    !" << endl;

245             cin >> y.number >> y.name >> y.dob >> y.sex >> y.condition;

246             i = list.search( y );

247             if( i > -1 && i <= list.size( ) )

248             {

249                 list.remove( i );

250                 cout << "" << endl;

251             }

252             else

253                 cout << "   ,                   ,           !  !!" << endl;

254             cout << endl;

255         }

256 

257         else if( m == 3 )

258         {

259             if( !flag )

260                 cout << "" << endl;

261             else

262             {

263                 cout << "--------------------    !--------------------" << endl;

264                 ifstream read( "list.txt" );

265                 char s;

266                 while( read.get( s ) )

267                     cout << s;

268                 cout << "--------------------    !--------------------" << endl;

269                 read.close( );

270             }

271             cout << endl;

272         }

273 

274         else if( m == 4 )

275         {

276             string select;

277             cout << "                ?(                      )" << endl;

278             cout << "        “Y”;    “N”,               。" << endl;

279             cin >> select;

280 

281             if( select == "Y" )

282             {

283                 ofstream write( "list.txt" );

284                 for( int i = 1; i <= list.size( ); ++i )

285                 {

286                     write << list.getLocate( i );

287                 }

288                 flag = true;

289                 write.close( );

290             }

291             else

292             {

293                 cout << "" << endl;

294             }

295             cout << endl;

296         }

297 

298         else if( m == 5 )

299         {

300             int i;

301             Student z;

302             string num;

303             bool flag2 = false;

304             cout << "" << endl;

305             cin >> num;

306             for( i = 1; i <= list.size( ); ++i)

307             {

308                 z = list.getLocate( i ) ;

309                 if( z.number == num )

310                 {

311                     flag2 = true;

312                     break;

313                 }

314             }

315             if( flag2 )

316                 cout << "" << endl;

317             else

318                 cout << "   ,                   ,           !  !!" << endl;

319 

320             cout << endl;

321         }

322 

323         else if( m == 6 )

324         {

325             if( flag1 )

326             {

327                 cout << "" << endl;

328                 list.output( );

329             }

330             else

331             {

332                 cout << "     ,        !" << endl;

333             }

334             cout << endl;

335         }

336 

337         else

338         {

339             cout << "---------------------------            ---------------------------" << endl;

340             break;

341         }

342     }

343 }
  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     bool operator == ( const Student & x )

 15     {

 16         return number == x.number;

 17     }

 18     //==     

 19 };

 20 

 21 ostream & operator << ( ostream & os, const Student & x )

 22 {

 23     os << "  :" << x.number << endl;

 24     os << "  : " << x.name  << endl;

 25     os << "    : " << x.dob << endl;

 26     os << "" << x.sex << endl;

 27     os << "" << x.condition << endl;

 28     os << endl;

 29     return os;

 30 }

 31 /*    “<<”  ,  Student      */

 32 

 33 template <typename Object>

 34 class List

 35 {

 36 private:

 37     struct Node

 38     {

 39         Object data;

 40         Node * next;

 41         Node * prev;

 42 

 43         Node ( const Object & d = Object( ), Node *p = NULL, Node *n = NULL ) : data( d ), prev( p ), next( n ) { }

 44     };

 45     //       ,    ,   ,   。

 46 

 47 public:

 48     List( )

 49     {

 50         init( );

 51     }

 52                                                 //List    ,      init         ,    ,   。

 53     ~List( )

 54     {

 55         clear( );

 56         delete head;

 57         delete tail;

 58     }

 59                                                 //    

 60     void clear( )

 61     {

 62         Node * p = begin( );

 63         Node * q;

 64         while( p != end( ) )

 65         {

 66             q = p;

 67             p = p->next;

 68             delete q;

 69         }

 70         theSize = 0;

 71     }

 72                                                 //

 73     int size( ) const

 74     {

 75         return theSize;

 76     }

 77                                                 //

 78     bool empty( )

 79     {

 80         return size( ) == 0;

 81     }

 82                                                 //

 83     Node * locate( int i )

 84     {

 85         if( i < 0 || i > size( ))

 86             return NULL;

 87         else

 88         {

 89             Node * p = head;

 90             for( int j = 1; j <= i; ++j )

 91             {

 92                 p = p->next;

 93             }

 94             return p;

 95         }

 96     }

 97                                                 //   i   。

 98     Node * begin( )

 99     {

100         return head->next;

101     }

102                                                 //

103     Node * end( )

104     {

105         return tail;

106     }

107                                                 //

108     Object getLocate( int i )

109     {

110         Node *p = locate( i );

111         return p->data;

112     }

113                                                 //   i        。

114     void push_front( const Object & x )

115     {

116         insert( begin( ), x );

117     }

118                                                 //

119     void push_back( const Object & x )

120     {

121         insert( end( ), x );

122     }

123                                                 //

124     void insert( Node * p, const Object & x )

125     {

126         p->prev = p->prev->next = new Node( x, p->prev, p );

127         theSize++;

128     }

129                                                 // p         ,    x。

130     int search( const Object & x )

131     {

132         int i;

133         Node * p = begin( );

134         for( i = 1; i <= size( ); ++i )

135         {

136             if( p->data == x )

137             {

138                 break;

139             }

140             p = p->next;

141         }

142         /*if( i <= size( ))

143         {

144             cout << "              !" << endl;

145         }

146         else

147             cout << "   ,                   ,           !  !!" << endl;*/

148         if( i <= size( ) )

149             return i;

150         else

151             return -1;

152     }

153                                                 //    x      ,     ,  -1

154     void remove( int i )

155     {

156         Node * p = locate( i );

157         p->prev->next = p->next;

158         p->next->prev = p->prev;

159         delete p;

160         theSize--;

161     }

162                                                 //   i   。

163     void output( )

164     {

165         Node * p = begin( );

166         for( int i = 1; i <= size( ); ++i)

167         {

168             cout << p->data;

169             p = p->next;

170         }

171         cout << endl;

172     }

173                                                 //

174 private:

175     int theSize;                                //

176     Node * head;                                //

177     Node * tail;                                //

178 

179     void init( )

180     {

181         theSize = 0;

182         head = new Node;

183         tail = new Node;

184         head->next = tail;

185         tail->prev = head;

186     }                                           //

187 };

188 

189 int main( )

190 {

191     bool flag = false;

192     bool flag1 = false;

193     List<Student>  list;

194     cout << "---------------------------            ---------------------------" << endl;

195     cout << "" << endl;

196     int m, m1;

197     while( 1 )

198     {

199         cout << "" << endl;

200         cout << "1:      。 2:      。 3:       。" << endl;

201         cout << "4:       。 5:  。 6:      。  :  。" << endl;

202         cout << endl;

203 

204         cin >> m;

205 

206         if( m == 1 )

207         {

208             int num;

209             Student x;

210             cout << "" << endl;

211             cout << "1:   。 2:   。" << endl;

212             cin >> m1;

213             cout << "" << endl;

214             cin >> num;

215             if( m1 == 1 )

216             {

217                 for( int i = 1; i <= num; ++i)

218                 {

219                     cout << "          、  、     、  、    !" << endl;

220                     cin >> x.number >> x.name >> x.dob >> x.sex >> x.condition;

221                     list.push_front( x );

222                 }

223                 cout << "" << endl;

224             }

225             else

226             {

227                 for( int i = 1; i <= num; ++i)

228                 {

229                     cout << "          、  、     、  、    !" << endl;

230                     cin >> x.number >> x.name >> x.dob >> x.sex >> x.condition;

231                     list.push_back( x );

232                 }

233                 cout << "" << endl;

234             }

235             if( flag1 ==  false )

236             flag1 = true;

237             cout << endl;

238         }

239 

240         else if( m == 2 )

241         {

242             int i;

243             Student y;

244             cout << "          、  、     、  、    !" << endl;

245             cin >> y.number >> y.name >> y.dob >> y.sex >> y.condition;

246             i = list.search( y );

247             if( i > -1 && i <= list.size( ) )

248             {

249                 list.remove( i );

250                 cout << "" << endl;

251             }

252             else

253                 cout << "   ,                   ,           !  !!" << endl;

254             cout << endl;

255         }

256 

257         else if( m == 3 )

258         {

259             if( !flag )

260                 cout << "" << endl;

261             else

262             {

263                 cout << "--------------------    !--------------------" << endl;

264                 ifstream read( "list.txt" );

265                 char s;

266                 while( read.get( s ) )

267                     cout << s;

268                 cout << "--------------------    !--------------------" << endl;

269                 read.close( );

270             }

271             cout << endl;

272         }

273 

274         else if( m == 4 )

275         {

276             string select;

277             cout << "                ?(                      )" << endl;

278             cout << "        “Y”;    “N”,               。" << endl;

279             cin >> select;

280 

281             if( select == "Y" )

282             {

283                 ofstream write( "list.txt" );

284                 for( int i = 1; i <= list.size( ); ++i )

285                 {

286                     write << list.getLocate( i );

287                 }

288                 flag = true;

289                 write.close( );

290             }

291             else

292             {

293                 cout << "" << endl;

294             }

295             cout << endl;

296         }

297 

298         else if( m == 5 )

299         {

300             int i;

301             Student z;

302             string num;

303             bool flag2 = false;

304             cout << "" << endl;

305             cin >> num;

306             for( i = 1; i <= list.size( ); ++i)

307             {

308                 z = list.getLocate( i ) ;

309                 if( z.number == num )

310                 {

311                     flag2 = true;

312                     break;

313                 }

314             }

315             if( flag2 )

316                 cout << "" << endl;

317             else

318                 cout << "   ,                   ,           !  !!" << endl;

319 

320             cout << endl;

321         }

322 

323         else if( m == 6 )

324         {

325             if( flag1 )

326             {

327                 cout << "" << endl;

328                 list.output( );

329             }

330             else

331             {

332                 cout << "     ,        !" << endl;

333             }

334             cout << endl;

335         }

336 

337         else

338         {

339             cout << "---------------------------            ---------------------------" << endl;

340             break;

341         }

342     }

343 }

좋은 웹페이지 즐겨찾기