1 [cpp] view plain copy
2 第一步:构建一个头文件(**.h)
3 [cpp] view plain copy
4 #include<iostream>
5 #include<iomanip>
6 #include<fstream>
7 #include<string>
8 using namespace std;
9 class CStudentRec
10 {
11 public:
12 char chFlag;//标志,A表示正常,N表示空
13 char strName[20];//姓名
14 char strID[10];//学号
15 float fScore[3];//3门成绩
16 float fAve;//总平均分
17 CStudentRec(char *name,char *id,float score[]);
18 CStudentRec(){chFlag='N';};//默认构造函数
19 ~CStudentRec(){};//默认析构函数
20 void Input(void);
21 float Validate(void);//成绩数据的输入验证,返回正确值
22 void Print(bool isTitle=false);
23 friend ostream& operator<<( ostream& os,CStudentRec& stu);
24 friend istream& operator>>( istream& is,CStudentRec& stu);
25 };
26
27 CStudentRec::CStudentRec(char *name,char *id,float score[])
28 {
29 strncpy(strName,name,20);
30 strncpy(strID,id,10);
31 fAve=0;
32 for(int i=0;i<3;i++)
33 {
34 fScore[i]=score[i];
35 fAve+=fScore[i];
36 }
37 fAve=float(fAve/3.0);
38 chFlag='A';
39 }
40 void CStudentRec::Input(void)
41 {
42 cout<<"姓名:";
43 cin>>strName;
44 cout<<"学号:";
45 cin>>strID;
46 float fSum=0;
47 for(int i=0;i<3;i++)
48 {
49 cout<<"成绩"<<i+1<<":";
50 fScore[i]=Validate();
51 fSum+=fScore[i];
52 }
53 fAve=(float)(fSum/3.0);
54 chFlag='A';
55 getchar();
56 }
57 float CStudentRec::Validate(void)
58 {
59 int s;
60 char buf[80];
61 float res;
62 for(;;)
63 {
64 cin>>res;
65 s=cin.rdstate();
66 while(s)
67 {
68 cin.clear();
69 cin.getline(buf,80);
70 cout<<"非法输入,重新输入:";
71 cin>>res;
72 s=cin.rdstate();
73 }
74 if((res<=100.0)&&(res>=0.0))break;
75 else
76 cout<<"输入的成绩超过范围!请重新输入:";
77 }
78 return res;
79 }
80 void CStudentRec::Print(bool isTitle)
81 {
82 cout.setf(ios::left);
83 if(isTitle)
84 cout<<setw(20)<<"姓名:"<<setw(10)<<"学号:"<<"\t成绩1:"<<"\t成绩2:"<<"\t成绩3:"<<"\t平均分:"<<endl;
85 cout<<setw(20)<<strName<<setw(10)<<strID;
86 for(int i=0;i<3;i++)
87 cout<<"\t"<<fScore[i];
88 cout<<"\t"<<fAve<<endl;
89 getchar();
90 }
91 ostream& operator<<(ostream& os,CStudentRec& stu)
92 {
93 os.write(&stu.chFlag,sizeof(char));
94 os.write(stu.strName,sizeof(stu.strName));
95 os.write(stu.strID,sizeof(stu.strID));
96 os.write((char*)&stu.fScore,sizeof(float)*3);
97 os.write((char*)&stu.fAve,sizeof(float));
98 return os;
99 }
100 istream& operator>>(istream& is,CStudentRec& stu)
101 {
102 char name[20],id[10];
103 is.read(&stu.chFlag,sizeof(char));
104 is.read(name,sizeof(name));
105 is.read(id,sizeof(id));
106 is.read((char*)stu.fScore,sizeof(float)*3);
107 is.read((char*)&stu.fAve,sizeof(float));
108 strncpy(stu.strName,name,sizeof(name));
109 strncpy(stu.strID,id,sizeof(id));
110 return is;
111 }
112
113 class CStuFile
114 {
115 public:
116 CStuFile(char* filename);
117 ~CStuFile();
118 void Add(CStudentRec stu);
119 int Seek(char* id,CStudentRec &stu);
120 int List(int nNum=-1);
121 private:
122 char* strFileName;
123 };
124 CStuFile::CStuFile(char* filename)
125 {
126 strFileName=new char[strlen(filename)+1];
127 strcpy(strFileName,filename);
128 }
129 CStuFile::~CStuFile()
130 {
131 if(strFileName)delete []strFileName;
132 }
133 void CStuFile::Add(CStudentRec stu)
134 {
135 fstream file(strFileName,ios::out|ios::app|ios::binary);
136 file<<stu;
137 file.close();
138 }
139 int CStuFile::Seek(char* id,CStudentRec& stu)
140 {
141 int nRec=-1;
142 fstream file(strFileName,ios::in|ios::_Nocreate);
143 if(!file)
144 {
145 cout<<"文件"<<strFileName<<"不能打开!\n";
146 return nRec;
147 }
148 int i=0;
149 while(!file.eof())
150 {
151 file>>stu;
152 if((strcmp(id,stu.strID)==0)&&(stu.chFlag!='N'))
153 {
154 nRec=i;break;
155 }
156 i++;
157 }
158 file.close();
159 return nRec;
160 getchar();
161 }
162 int CStuFile::List(int nNum)
163 {
164 fstream file(strFileName,ios::in|ios::_Nocreate);
165 if(!file)
166 {
167 cout<<"文件"<<strFileName<<"不能打开!\n";
168 return 0;
169 }
170 int nRec=0;
171 if((nNum==-1)||(nNum>0))
172 {
173 cout.setf(ios::left);
174 cout<<setw(6)<<"记录"<<setw(20)<<"姓名"<<setw(10)<<"学号"<<"\t成绩1\t成绩2\t成绩3\t平均分"<<endl;
175 }
176 while(!file.eof())
177 {
178 CStudentRec data;
179 file>>data;
180 if(data.chFlag=='A')
181 {
182 nRec++;
183 if((nNum==-1)||(nRec<=nNum))
184 {
185 cout.setf(ios::left);
186 cout<<setw(6)<<nRec;
187 data.Print();
188 }
189 }
190 }
191 file.close();
192 return nRec;
193 }
194 [cpp] view plain copy
195 第二部:创建一个.cpp文件
196 [cpp] view plain copy
197 #include<iostream>
198 #include"**.h"
199 CStuFile theStu("student.txt");
200 void AddTo(int nNum)
201 {
202 CStudentRec stu;
203 for(int i=0;i<nNum;i++)
204 {
205 cout<<"请输入第"<<i+1<<"记录:"<<endl;
206 stu.Input();
207 theStu.Add(stu);
208 }
209 }
210 void main()
211 {
212 AddTo(3);
213 theStu.List();
214 CStudentRec one;
215 if(theStu.Seek("21050102",one)>=0)
216 one.Print(true);
217 else
218 cout<<"没有找到!\n";
219 theStu.List();
220 getchar();
221 }
222 [cpp] view plain copy
223 ok
原文链接: https://www.cnblogs.com/heruonan/p/8329734.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/268127
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!