【生生被气死的一周】头秃
1 //************************************************************************************************************** 2 //作者: 3 //日期:2018/3/19 4 //学号: 5 //题号:5-5 6 //题目:编写一个程序,实现以下功能: 7 8 // 1)输入一系列的学生数据(包括学生的学号、姓名和成绩等基本信息),将学生信息写入二进制文件student.dat中。 9 10 // 2)从student.dat文件中读出这些数据并显示出来。 11 12 // 3)在student.dat文件中按姓名进行查询,如输入“李”,则将所有姓“李”的学生的 数据显示出来。 13 14 // 4)可对指定学生的数据进行修改。 15 16 // 5)可以删除指定的学生数据。 17 18 //************************************************************************************************************** 19 20 21 22 #include<iostream> 23 #include<fstream> 24 #include<stdlib.h> 25 #include<cstring> 26 using namespace std; 27 struct Std 28 { 29 char name[20]; 30 char id[20]; 31 char score[5]; 32 }; 33 void getinfo(char filename[]); 34 void showinfo(char filename[]); 35 void searchinfo(char filename[]); 36 void editinfo(char filename[]); 37 void deleteinfo(char filename[]); 38 39 int main() 40 { 41 fstream student; 42 char choice; 43 char filename[20]; 44 cout << "Please type in the filename:"; 45 cin.getline(filename,20); 46 cout << endl; 47 while(true) 48 { 49 cout << "Please type down the manipulation code:\nA.Input information\tB.Show information\tC.Search\tD.Edit\tE.Delete\tF.Exit\n"; 50 cout << endl; 51 cin >> choice; 52 switch(choice) 53 { 54 case'A': 55 case'a':getinfo(filename);break; 56 case'B': 57 case'b':showinfo(filename);break; 58 case'C': 59 case'c':searchinfo(filename);break; 60 case'D': 61 case'd':editinfo(filename);break; 62 case'E': 63 case'e':deleteinfo(filename);break; 64 case'F': 65 case'f':exit(0); 66 } 67 cout << endl; 68 } 69 return 0; 70 } 71 72 void getinfo(char filename[]) 73 { 74 fstream student; 75 student.open(filename,ios::out|ios::binary); 76 if(!student) 77 { 78 cout << "Error!"; 79 exit(0); 80 } 81 int count; 82 cout << "How many student's information would you like to input?"; 83 cin >> count; 84 Std *Student; 85 Student = new struct Std[count]; 86 for(int i=0;i<count;i++) 87 { 88 cout << "Please input student "<<i+1<<" 's name:"; 89 cin.get(); 90 cin.getline(Student[i].name,20); 91 cout << "Please input student "<<i+1<<" 's id:"; 92 cin.getline(Student[i].id,20); 93 cout << "Please input student "<<i+1<<" 's score:"; 94 cin >> Student[i].score; 95 student.write((char*)&Student[i],sizeof(Student[i])); 96 } 97 cout << "Done."; 98 delete[]Student; 99 student.close(); 100 } 101 102 void showinfo(char filename[]) 103 { 104 fstream student; 105 student.open(filename,ios::in|ios::binary); 106 if(!student) 107 { 108 cout << "Error!"; 109 exit(0); 110 } 111 Std Student; 112 student.read((char*)&Student,sizeof(Student)); 113 while(!student.eof()) 114 { 115 cout << "name:"; 116 cout << Student.name <<endl; 117 cout << "id:"; 118 cout << Student.id <<endl; 119 cout << "score:"; 120 cout << Student.score <<endl; 121 student.read((char*)&Student,sizeof(Student)); 122 } 123 cout << "Above are all the information."<<endl; 124 student.close(); 125 } 126 127 void searchinfo(char filename[]) 128 { 129 fstream student; 130 Std Student; 131 student.open(filename,ios::in|ios::binary); 132 if(!student) 133 { 134 cout << "Error!"; 135 exit(0); 136 } 137 char ch,Name; 138 cout << "Please input the first name:"; 139 cin >> Name; 140 long position = student.tellg(); 141 cout << "position:"<<position<<endl; 142 student.seekg(0L,ios::beg); 143 student.get(ch); 144 cout << "ch:"<<ch<<endl; 145 //cout << "\nch:"<<ch<<endl; 146 position = student.tellg(); 147 cout << "position:"<<position<<endl; 148 student.seekg(0L,ios::beg); 149 long current_pos = 0L; 150 while(!student.eof()) 151 { 152 if(ch == Name) 153 { 154 cout << "ch:"<<ch<<endl; 155 student.read((char*)&Student,sizeof(Student)); 156 long position; 157 position = student.tellg(); 158 cout << "position:"<<position<<endl; 159 cout << "name:"; 160 cout << Student.name <<endl; 161 cout << "id:"; 162 cout << Student.id <<endl; 163 cout << "score:"; 164 cout << Student.score <<endl; 165 student.seekg(-(sizeof(Student)),ios::cur); 166 position = student.tellg(); 167 cout << "position:"<<position<<endl; 168 } 169 current_pos = current_pos + sizeof(Student); 170 student.seekg(current_pos,ios::cur); 171 // long position = student.tellg(); 172 // cout << "position:"<<position<<endl; 173 //student.seekg(1L,ios::cur); 174 //cin.ignore(); 175 //position = student.tellg(); 176 //cout << "position:"<<position<<endl; 177 // student.get(ch); 178 // position = student.tellg(); 179 // cout << "position:"<<position<<endl; 180 // student.seekg(-sizeof(ch),ios::cur); 181 // position = student.tellg(); 182 //s cout << "position:"<<position<<endl; 183 } 184 cout << "Above are all the information that you are looking for."<<endl; 185 student.close(); 186 } 187 188 void editinfo(char filename[]) 189 { 190 fstream student; 191 Std Student; 192 student.open(filename,ios::in|ios::out|ios::binary); 193 if(!student) 194 { 195 cout << "Error!"; 196 exit(0); 197 } 198 char editid[20]; 199 cout << "Please input the id of the student whose information needs to be edited:"; 200 cin >> editid; 201 student.seekg(0L,ios::beg); 202 student.seekg(sizeof(Student.name),ios::beg); 203 student.seekp(0L,ios::beg); 204 student.read((char*)&Student.id,sizeof(Student.id)); 205 while(!student.eof()) 206 { 207 if(strcmp(editid,Student.id)==0) 208 { 209 student.seekg(-sizeof(Student.id)-sizeof(Student.name),ios::cur); 210 student.read((char*)&Student,sizeof(Student)); 211 cout << "The information of the student:"<<endl; 212 cout << "name:"<<Student.name<<endl; 213 cout << "id:"<<Student.id<<endl; 214 cout << "score:"<<Student.score<<endl; 215 cout << "Now please input the new information:"; 216 cout << "name:"; 217 cin >> Student.name; 218 cout << "id:"; 219 cin >> Student.id; 220 cout << "score:"; 221 cin >> Student.score; 222 student.write((char*)&Student,sizeof(Student)); 223 break; 224 } 225 student.seekg(sizeof(Student),ios::cur); 226 student.seekp(sizeof(Student),ios::cur); 227 } 228 cout << "Done."; 229 student.close(); 230 } 231 232 void deleteinfo(char filename[]) 233 { 234 fstream student; 235 Std Student; 236 student.open(filename,ios::in|ios::out|ios::binary); 237 if(!student) 238 { 239 cout << "Error!"; 240 exit(0); 241 } 242 char deleid[20]; 243 cout << "Please input the id of the student whose information needs to be erased:"; 244 cin >> deleid; 245 student.seekg(0L,ios::beg); 246 student.seekg(sizeof(Student.name),ios::beg); 247 student.seekp(0L,ios::beg); 248 student.read((char*)&Student.id,sizeof(Student.id)); 249 while(!student.eof()) 250 { 251 if(strcmp(deleid,Student.id)==0) 252 { 253 student.seekg(-sizeof(Student.id)-sizeof(Student.name),ios::cur); 254 student.read((char*)&Student,sizeof(Student)); 255 cout << "The information of the student:"<<endl; 256 cout << "name:"<<Student.name<<endl; 257 cout << "id:"<<Student.id<<endl; 258 cout << "score:"<<Student.score<<endl; 259 cout << "type 'd' to delete this student's information:"; 260 char code; 261 cin >> code; 262 if(code == 'd') 263 { 264 strcpy(Student.name,0); 265 strcpy(Student.id,0); 266 strcpy(Student.score,0); 267 } 268 student.write((char*)&Student,sizeof(Student)); 269 break; 270 } 271 student.seekg(sizeof(Student),ios::cur); 272 student.seekp(sizeof(Student),ios::cur); 273 } 274 cout << "Done."; 275 student.close(); 276 }
------------------
有为,有守,有趣。
------------------

浙公网安备 33010602011771号