# include & ltiostream & gt
# include & ltcstring & gt
# include & ltfstream & gt
# include & ltiomanip & gt
#定義最大計數2
使用命名空間std
課程信息{
公共:
信息();
~信息();
void de info();
void ModifyInfo(char *newName,char *newClass,char * new phone);
公共:
char Name[20];
char類[20];
充電電話[12];
公共:
bool空;
};
信息::信息()
{
strcpy(名稱," ");
strcpy(類," ");
strcpy(電話," ");
空=假;
}
信息::~信息()
{
}
無效信息::DelInfo()
{
strcpy(名稱," ");
strcpy(類," ");
strcpy(電話," ");
空=真;
}
void Information::modify info(char * new name,char *newClass,char *newPhone)
{
strcpy(Name,newName);
strcpy(Class,new Class);
strcpy(Phone,new Phone);
}
void ShowInfo(信息Info)
{
cout & lt& ltsetios flags(IOs _ base::left)& lt;& ltsetw(20)& lt;& lt信息。名稱& lt& ltsetw(20)& lt;& lt信息。Class & lt& lt信息。電話& lt& ltendl
}
int main()
{
Information *pInfo =新信息[2];
int i,nChoice
///////////首先初始化///////////////。
cout & lt& lt"請輸入"
char newName[20],newClass[20],newPhone[20],delName[20];
for(I = 0;我& ltMAXCOUNTi++)
CIN & gt;& gtpInfo[i]。名稱& gt& gtpInfo[i]。Class & gt& gtpInfo[i]。電話;
ofstream os1("test.txt ",IOs _ base::out);
for(I = 0;我& ltMAXCOUNTi++) {
if (pInfo[i].Empty == false)
OS 1 & lt;& ltsetios flags(IOs _ base::left)& lt;& ltsetw(20)& lt;& ltpInfo[i]。名稱& lt& ltsetw(20)& lt;& ltpInfo[i]。Class & lt& ltpInfo[i]。電話& lt& ltendl
}
刪除[]pInfo;
OS 1 . close();
//////////////////////////////////////////////////
做{
cout & lt& lt1,刪除2,添加3,修改4,顯示
CIN & gt;& gtnChoice
開關(nChoice) {
/////////////////////////////////////////////
案例1: {
ifstream是1("test.txt ",IOs _ base::in);
pInfo =新信息[MAXCOUNT];
for(I = 0;我& ltMAXCOUNTi++)
is 1 & gt;& gtpInfo[i]。名稱& gt& gtpInfo[i]。Class & gt& gtpInfo[i]。電話;
cout & lt& lt"輸入要刪除的名稱:";
CIN & gt;& gtdelName
for(I = 0;我& ltMAXCOUNTi++) {
if (strcmp(delName,pInfo[i])。Name) == 0)
pInfo[i]。de linfo();
}
ofstream os2("test.txt ",IOs _ base::trunc);
for(I = 0;我& ltMAXCOUNTi++) {
if (pInfo[i].Empty == false) {
os2 & lt& ltsetios flags(IOs _ base::left)& lt;& ltsetw(20)& lt;& ltpInfo[i]。名稱& lt& ltsetw(20)& lt;& ltpInfo[i]。Class & lt& ltpInfo[i]。電話& lt& ltendl
}
}
刪除[]pInfo;
os2 . close();
打破;
}
/////////////////////////////////////////////////////////
案例2: {
Information *pInfoAdd =新信息;
cout & lt& lt輸入要插入的姓名、班級和電話號碼
CIN & gt;& gtpInfoAdd-& gt;名稱& gt& gtpInfoAdd-& gt;Class & gt& gtpInfoAdd-& gt;電話;
ofstream os3("test.txt ",IOs _ base::app);
os3 & lt& ltsetios flags(IOs _ base::left)& lt;& ltsetw(20)& lt;& ltpInfoAdd-& gt;名稱& lt& ltsetw(20)& lt;& ltpInfoAdd-& gt;Class & lt& ltpInfoAdd-& gt;電話& lt& ltendl
刪除pInfoAdd
os3 . close();
打破;
}
////////////////修改/////////////////////////////////
案例3: {
Information *pInfoModify =新信息[MAXCOUNT];
cout & lt& lt"輸入要修改的名稱:"
CIN & gt;& gtnewName
cout & lt& lt"輸入新的班級和電話號碼:"
CIN & gt;& gtnewClass & gt& gtnewPhone
ifstream is2("test.txt ",IOs _ base::in);
for(I = 0;我& ltMAXCOUNTi++) {
is2 & gt& gtpInfoModify[i]。名稱& gt& gtpInfoModify[i]。Class & gt& gtpInfoModify[i]。電話;
if (strcmp(newName,pInfoModify[i])。Name) == 0)
pInfoModify[i]。ModifyInfo(newName,newClass,new phone);
}
ofstream os4("test.txt ",IOs _ base::trunc);
for(I = 0;我& ltMAXCOUNTi++) {
if (pInfoModify[i].Empty == false) {
os4 & lt& ltsetios flags(IOs _ base::left)& lt;& ltsetw(20)& lt;& ltpInfoModify[i]。名稱& lt& ltsetw(20)& lt;& ltpInfoModify[i]。Class & lt& ltpInfoModify[i]。電話& lt& ltendl
}
}
刪除[]pInfoModify;
os4 . close();
打破;
}
///////////////////////////////////////////////////
案例4: {
Information *pShowInfo =新信息[MAXCOUNT];
ifstream is3("test.txt ",IOs _ base::in);
for(I = 0;我& ltMAXCOUNTi++) {
is3 & gt& gtpShowInfo[i]。名稱& gt& gtpShowInfo[i]。Class & gt& gtpShowInfo[i]。電話;
ShowInfo(pShowInfo[I]);
}
打破;
}
默認:退出(1);
}
} while(n choice = = 1 | | n choice = = 2 | | n choice = = 3 | | n choice = = 4);
////////////////////////////////////////////////////////
返回0;
}