65,187
社区成员




#include <iostream>
#include <string>
#include <vector>
using namespace std;
static int num=0;
class address
{
public:
void set();
void correct();
string getname();
string gettel();
void get();
private:
string name;
string tel;
string sex;
string Class;
};
void address::set()
{
cout<<"姓名:";
cin>>name;
cout<<""<<endl;
cout<<"性别:";
cin>>sex;
cout<<""<<endl;
cout<<"班级:";
cin>>Class;
cout<<""<<endl;
cout<<"电话:";
cin>>tel;
cout<<"输入完毕!"<<endl;
}
string address::getname()
{
return name;
}
string address::gettel()
{
return tel;
}
void address::get()
{
cout<<"姓名:"<<name<<"----";
cout<<"性别:"<<sex<<"----";
cout<<"班级:"<<Class<<"----";
cout<<"电话:"<<tel<<endl;
}
void main()
{
vector<address> P;
cout<<"Welcome to the address system!"<<endl;
for(int y=0;;y++)
{
cout<<"*******************************"<<endl;
cout<<"功能介绍:"<<endl;
cout<<"1.浏览通讯录" <<endl;
cout<<"2.增加新成员"<<endl;
cout<<"3.修改原数据"<<endl;
cout<<"4.按姓名查询"<<endl;
cout<<"5.按电话查询"<<endl;
cout<<"6.退出"<<endl;
cout<<"*******************************"<<endl;
int choise;
string NA,m,z;
string TE;
lop:cout<<"请选择你要进行的操作编号:";
cin>>choise;
int j,i,h;
address pus;
switch (choise)
{
case 1:
for(j=1;j<=num;j++)
{
P[j-1].get();
}
break;
case 2:
cout<<"请输入:"<<endl;
pus.set();
P.push_back(pus);
num++;
break;
case 3:
cout<<"请输入要修改的人的姓名:";
cin>>m;
for(i=0;i<=num-1;i++)
{
z=P[i].getname();
if(z==m)
break;
}
cout<<"请输入新信息:";
P[i].set();
break;
case 4:
cout<<"请输入要查找的人的姓名:";
cin>>NA;
cout<<""<<endl;
for( j=1;j<=num;j++)
{
if(P[j-1].getname()==NA)
{
P[j-1].get();
break;
}
}
break;
case 5:
cout<<"请输入要查询的号码:";
cin>>TE;
for(h=1;h<=num;h++)
{
if(P[h-1].gettel()==TE)
{
P[h-1].get();
break;
}
}
break;
case 6:goto loop;
default:
cout<<"输入有误,请重新输入!"<<endl;
goto lop;
}
}
loop:cout<<"欢迎再次使用本系统,再见!" <<endl;
}
struct FileHeader
{
DWORD dwHeader; //四个字节 游戏名称
DWORD dwVersion; //四个字节 游戏版本
DWORD dwSize; //文件大小
DWORD dwReserve; //保留
};
struct PacketHeader
{
DWORD dwPacketCount; //4个字节 包的个数
DWORD dwReserve;
};
struct PacketInfo //包的信息
{
WORD PacketID; //包的协议ID
WORD PacketLen; //包的长度
CHAR PacketBuf[1024]; //包的内容
};
int _tmain(int argc, _TCHAR* argv[])
{
FileHeader fh;
fh.dwHeader = 0x4A475A44;
fh.dwVersion = 0x10409;
fh.dwSize = 0;
fh.dwReserve = 0;
ofstream fout("Packet.dat",ios::binary);
fout.write((char*)(&fh),sizeof(fh));
PacketHeader ph;
ph.dwPacketCount = 1;
ph.dwReserve = 0;
fout.write((char*)(&ph),sizeof(ph));
//写包数据
PacketInfo phInfo;
phInfo.PacketID = 1;
phInfo.PacketLen = 16;
strcpy(phInfo.PacketBuf,"杭州边锋网络");
fout.write((char*)&phInfo.PacketID,sizeof(phInfo.PacketID));
fout.write((char*)&phInfo.PacketLen,sizeof(phInfo.PacketLen));
fout.write((char*)&phInfo.PacketBuf,phInfo.PacketLen);
fout.close();
//读取数据
ifstream fin("Game.rpg",ios::binary);
FileHeader ifh;
fin.read((char*)&ifh,sizeof(ifh));
cout<<"FileHeader: dwHeader = "<<ifh.dwHeader<<" dwVersion= "<<ifh.dwVersion<<" dwSize = "<<ifh.dwSize<<" dwReserve "<<ifh.dwReserve<<endl;
PacketHeader iph;
fin.read((char*)&iph,sizeof(iph));
cout<<"PacketHeader: dwPacketCount = "<<iph.dwPacketCount<<" dwReserve= "<<iph.dwReserve<<endl;
for (int i= 0; i< iph.dwPacketCount;i++)
{
PacketInfo iphInfo;
fin.read((char*)&iphInfo.PacketID,sizeof(iphInfo.PacketID));
fin.read((char*)&iphInfo.PacketLen,sizeof(iphInfo.PacketLen));
fin.read((char*)&iphInfo.PacketBuf,iphInfo.PacketLen);
iphInfo.PacketBuf[iphInfo.PacketLen] = '\0';
cout<<"PacketInfo: PacketID ="<<iphInfo.PacketID<<" PacketLen= "<<iphInfo.PacketLen<<" PacketBuf = "<<iphInfo.PacketBuf<<endl;
cout<<"==========================================================="<<endl;
}
system("pause");
return 0;
}
PacketHeader iph; //文件头结构
fin.read((char*)&iph,sizeof(iph));//读取文件头
//读取文件中的消息包
for (int i= 0; i< iph.dwPacketCount;i++)
{
PacketInfo *iphInfo = new PacketInfo ;
fin.read((char*)&iphInfo->PacketID,sizeof(iphInfo->PacketID));
fin.read((char*)&iphInfo->PacketLen,sizeof(iphInfo->PacketLen));
fin.read((char*)&iphInfo->PacketBuf,iphInfo->PacketLen);
g_PacketList.push_back(iphInfo);
}
PacketHeader iph; //文件头结构
fin.read((char*)&iph,sizeof(iph));//读取文件头
//读取文件中的消息包
for (int i= 0; i< iph.dwPacketCount;i++)
{
PacketInfo *iphInfo = new PacketInfo ;
fin.read((char*)&iphInfo->PacketID,sizeof(iphInfo->PacketID));
fin.read((char*)&iphInfo->PacketLen,sizeof(iphInfo->PacketLen));
fin.read((char*)&iphInfo->PacketBuf,iphInfo->PacketLen);
g_PacketList.push_back(iphInfo);
}
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
static int num=0;
struct AddressHeader
{
DWORD dwPacketCount; //4个字节 包的个数
DWORD dwReserve;
};
struct AddressInfo //包的信息
{
CHAR Name[32];
CHAR Tel[32];
CHAR Sex[32];
CHAR Class[32];
};
class address
{
public:
address(){};
address(string na,string te,string se,string cl)
{
name = na;
tel = te;
sex = se;
Class = cl;
}
void set();
void correct();
string getname();
string gettel();
string getsex(){return sex;};
string getclass(){return Class;};
void get();
private:
string name;
string tel;
string sex;
string Class;
};
void address::set()
{
cout<<"姓名:";
cin>>name;
cout<<""<<endl;
cout<<"性别:";
cin>>sex;
cout<<""<<endl;
cout<<"班级:";
cin>>Class;
cout<<""<<endl;
cout<<"电话:";
cin>>tel;
cout<<"输入完毕!"<<endl;
}
string address::getname()
{
return name;
}
string address::gettel()
{
return tel;
}
void address::get()
{
cout<<"姓名:"<<name<<"----";
cout<<"性别:"<<sex<<"----";
cout<<"班级:"<<Class<<"----";
cout<<"电话:"<<tel<<endl;
}
void ReadRecord(vector<address*>& p)
{
ifstream fin("Address.dat",ios::binary);
AddressHeader ifh;
memset(&ifh,0,sizeof(AddressHeader));
fin.read((char*)&ifh,sizeof(ifh));
for (int i= 0; i< ifh.dwPacketCount;i++)
{
AddressInfo iphInfo;
fin.read((char*)&iphInfo.Name,32);
fin.read((char*)&iphInfo.Tel,32);
fin.read((char*)&iphInfo.Sex,32);
fin.read((char*)&iphInfo.Class,32);
address *ad = new address(iphInfo.Name,iphInfo.Tel,iphInfo.Sex,iphInfo.Class);
p.push_back(ad);
}
fin.close();
}
void SaveRecord(vector<address*>& p)
{
ofstream fout("Address.dat",ios::binary);
AddressHeader ph;
ph.dwPacketCount = p.size();
ph.dwReserve = 0;
fout.write((char*)(&ph),sizeof(ph));
for (int i = 0; i < p.size(); i++)
{
//写包数据
AddressInfo phInfo;
strcpy(phInfo.Name, p[i]->getname().c_str());
strcpy(phInfo.Class, p[i]->getclass().c_str());
strcpy(phInfo.Sex, p[i]->getsex().c_str());
strcpy(phInfo.Tel, p[i]->gettel().c_str());
fout.write((char*)&phInfo.Name,32);
fout.write((char*)&phInfo.Tel,32);
fout.write((char*)&phInfo.Sex,32);
fout.write((char*)&phInfo.Class,32);
}
fout.close();
}
void main()
{
vector<address*> P;
ReadRecord(P);
num = P.size();
cout<<"Welcome to the address system!"<<endl;
for(int y=0;;y++)
{
cout<<"*******************************"<<endl;
cout<<"功能介绍:"<<endl;
cout<<"1.浏览通讯录" <<endl;
cout<<"2.增加新成员"<<endl;
cout<<"3.修改原数据"<<endl;
cout<<"4.按姓名查询"<<endl;
cout<<"5.按电话查询"<<endl;
cout<<"6.退出"<<endl;
cout<<"*******************************"<<endl;
int choise;
string NA,m,z;
string TE;
lop:cout<<"请选择你要进行的操作编号:";
cin>>choise;
int j,i,h;
address* pus = new address;
switch (choise)
{
case 1:
for(j=1;j<=num;j++)
{
P[j-1]->get();
}
break;
case 2:
cout<<"请输入:"<<endl;
pus->set();
P.push_back(pus);
num++;
break;
case 3:
cout<<"请输入要修改的人的姓名:";
cin>>m;
for(i=0;i<=num-1;i++)
{
z=P[i]->getname();
if(z==m)
break;
}
cout<<"请输入新信息:";
P[i]->set();
break;
case 4:
cout<<"请输入要查找的人的姓名:";
cin>>NA;
cout<<""<<endl;
for( j=1;j<=num;j++)
{
if(P[j-1]->getname()==NA)
{
P[j-1]->get();
break;
}
}
break;
case 5:
cout<<"请输入要查询的号码:";
cin>>TE;
for(h=1;h<=num;h++)
{
if(P[h-1]->gettel()==TE)
{
P[h-1]->get();
break;
}
}
break;
case 6:goto loop;
default:
cout<<"输入有误,请重新输入!"<<endl;
goto lop;
}
}
loop:cout<<"欢迎再次使用本系统,再见!" <<endl;
SaveRecord(P);
for (int i = 0; i < P.size(); i++)
{
if (P[i])
{
delete P[i];
P[i] = NULL;
}
}
}