addresslist

时间:2021-05-06 13:14:56
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<algorithm>
using namespace std;
//好友信息
typedef struct frie{
int num;
char name[];
char sex[];
int year,month,day;
char phone[][];
char address[];
int groupid;
}fre; fre *fri=new fre[]; //分组信息
typedef struct group{
int id;
char name[];
}gr; int now,size;
int se[];
gr *group=new gr[];
//读friend.txt中的内容
void readfriend()
{ FILE *fp=fopen("friend.txt","r");
if(fp==NULL){
printf("\t\t\t***********fail to open friend.txt***********\n");
}
int i;
int n;
now=;
for(i=;!feof(fp);i++){
if(fscanf(fp,"%d",&n)==EOF){
break;
}
fri[n].num=n;
if(now<n)
now=n;
// fscanf(fp,"%s%s %d %d %d %s %s %s %d\n",fri[n].name,,&fri[n].year,,,,fri[n].phone[1],fri[n].phone[2],fri[n].address,&fri[n].groupid);
fscanf(fp,"%s",fri[n].name);
fscanf(fp,"%s",fri[n].sex);
fscanf(fp,"%d",&fri[n].year);
fscanf(fp,"%d",&fri[n].month);
fscanf(fp,"%d",&fri[n].day);
fscanf(fp,"%s",fri[n].phone[]);
fscanf(fp,"%s",fri[n].phone[]);
fscanf(fp,"%s",fri[n].phone[]);
fscanf(fp,"%s",fri[n].address);
fscanf(fp,"%d",&fri[n].groupid);
}
size=i;
now=now+;
fclose(fp);
printf("\t\t\t************read friend.txt success!**************\n");
} void readgroup()
{
FILE *fp=fopen("group.txt","r");
if(fp==NULL){
printf("\t\t\t***********fail to open group.txt***********\n");
}
int i;
while(!feof(fp)){ if(fscanf(fp,"%d",&i)==EOF){
break;
}
group[i].id=i;
fscanf(fp,"%s",group[i].name);
}
fclose(fp);
printf("\t\t\t************read group.txt success!**************\n");
} void print(fre f)//输出格式
{
/* printf("%4d%20s %4c%5d%6d%5d%12s%12s%12s%20s%4d\n",fri[i].num,fri[i].name,fri[i].sex,fri[i].year,
fri[i].month,fri[i].day,fri[i].phone[0],fri[i].phone[1],fri[i].phone[2],fri[i].address,
fri[i].groupid);
*/
printf("%4d",f.num);
printf("%24s",f.name);
if(f.sex[]!='o'){
printf("%4s",f.sex);
}
else{
printf("%4s"," ");
}
if(f.year==&&f.month==&&f.day==){
printf("%5s%6s%5s"," "," "," ");
}
else{
printf("%5d%6d%5d",f.year,f.month,f.day);
}
for(int i=;i<;i++){
if(f.phone[i][]!='')
printf("%12s",f.phone[i]);
else{
printf("%12s"," ");
}
}
printf("%20s%4d%20s\n",f.address,f.groupid,group[f.groupid].name); } void print2(gr g){
printf("%4d%20s\n",g.id,g.name);
}
void begin()
{
//cout<<"input i for insert,s for select,d for delete,u for update,p for display:"<<endl;
printf("\t\t\t************i for insert function!************\n");
printf("\t\t\t************s for select function!************\n");
printf("\t\t\t************d for delete function!************\n");
printf("\t\t\t************u for update function!************\n");
printf("\t\t\t************p for display function!************\n");
printf("\t\t\t************e for exit the system!************\n");
} void insert(fre &f)
{
printf("\t\t**********insert begin!***********\n");
f.num=now;
printf("please input name:\n");
cin>>f.name;
printf("please input sex 男,女 and o for no:\n"); //g gor girl,b for boy,o for no
cin>>f.sex;
printf("please input birthday as yyyy-mm-dd and 0 0 0 for no:\n");
scanf("%d-%d-%d",&f.year,&f.month,&f.day);
int n;
for(int i=;i<;i++){
f.phone[i][]='';
}
cout<<"how many phone you want to save:"<<endl;
cin>>n;
for(int i=;i<n;i++){
printf("please input phone:\n");
scanf("%s",f.phone[i]);
}
printf("please input address :\n");
scanf("%s",f.address);
printf("please input group id if no this please create:\n");
cin>>f.groupid;
if(group[f.groupid].name[]=='\0'){
cout<<"please create group name:"<<endl;
group[f.groupid].id=f.groupid;
scanf("%s",group[f.groupid].name);
cout<<group[f.groupid].name<<" created!"<<endl;
}
now++;
size++;
} //展示所有内容
void display()
{
printf("\t\t\t************display all**************\n");
if(size>){
printf("%4s%20s%4s%5s%6s%5s%12s%12s%12s%20s%4s%20s\n","num","\tname","sex","year","month","day","phone1",
"phone2","phone3","address","id","groupname");
for(int i=;i<now;i++){
if(fri[i].num!=-)
print(fri[i]);
}
}
printf("%10s%20s\n","groupid","groupname");
for(int i=;i<;i++){
if(group[i].name[]!='\0'){
print2(group[i]);
}
}
} void select()
{
printf("\t\t\t***********update begin!*************\n");
int n;
cout<<"please input how you want to select:"<<endl;
cout<<"1 for num,2 for name,3 for sex,4 for birthday"<<endl;
cout<<"5 for phone,6 for address,7 for group id,8 for group name"<<endl;
cout<<"0 for end selece!"<<endl;
for(int i=;i<now;i++){
se[i]=i;
}
int i,s,j;
s=now;
while(cin>>n){
switch(n){
case :
if(s!=){
printf("%4s%20s%4s%5s%6s%5s%12s%12s%12s%20s%4s%20s\n","num","\tname","sex","year","month","day","phone1",
"phone2","phone3","address","id","groupname");
for(int i=;i<s;i++){
print(fri[se[i]]);
}
}
return;
case :
int nu;
cout<<"please input num:"<<endl;
cin>>nu;
if(nu<now&&fri[nu].num!=-){
printf("%4s%20s%4s%5s%6s%5s%12s%12s%12s%20s%4s%20s\n","num","\tname","sex","year","month","day","phone1",
"phone2","phone3","address","id","groupname");
print(fri[nu]);
}
else{
cout<<"there is no that match you!"<<endl;
}
return;
case :
j=;
cout<<"please input name:"<<endl;
char na[];
cin>>na;
for(int i=;i<s;i++){
if(strcmp(fri[se[i]].name,na)==){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input sex:"<<endl;
char c[];
cin>>c;
for(int i=;i<s;i++){
if(strcmp(fri[se[i]].sex,c)==){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input birthday:"<<endl;
int y,m,d;
scanf("%d-%d-%d",&y,&m,&d);
for(int i=;i<s;i++){
if(fri[se[i]].year==y&&fri[se[i]].month==m&&fri[se[i]].day==d){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input phone:"<<endl;
char p[];
cin>>p;
for(int i=;i<s;i++){
for(int k=;k<;k++){
if(strcmp(p,fri[se[i]].phone[k])==)
{
se[j++]=se[i];
break;
}
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input address:"<<endl;
char a[];
cin>>a;
for(int i=;i<s;i++){
if(strcmp(fri[se[i]].address,a)==){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input group id:"<<endl;
int b;
cin>>b;
for(int i=;i<s;i++){
if(fri[se[i]].groupid==b){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input group name:"<<endl;
char nm[];
cin>>nm;
int mo;
mo=-;
for(int i=;i<;i++){
if(strcmp(group[i].name,nm)==)
{
mo=i;
break;
}
}
if(mo==-){
cout<<"no this group name!"<<endl;
return;
}
else{
for(int i=;i<s;i++){
if(mo==fri[se[i]].groupid)
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
default:
cout<<"input error please retry"<<endl;
}
}
} //怎么改变friend
void modify(fre &f)
{
int n;
cout<<"please select which you want to update:"<<endl;
cout<<"2 for name,3 for sex,4 for birthday"<<endl;
cout<<"5 for phone,6 for address,7 for group id,8 for group name"<<endl;
cin>>n;
switch(n){
case :
cout<<"please input update name:"<<endl;
char na[];
cin>>na;
strcpy(f.name,na);
break;
case :
cout<<"please input update sex:"<<endl;
char c[];
cin>>c;
strcpy(f.sex,c);
break;
case :
cout<<"please input update birthday:"<<endl;
int y,m,d;
scanf("%d-%d-%d",&y,&m,&d);
f.year=y;
f.day=d;
f.month=m;
break;
case :
cout<<"please input update phone:"<<endl;
char p[];
cin>>p;
strcpy(f.phone[],p);
break;
case :
cout<<"please input update address:"<<endl;
char a[];
cin>>a;
strcpy(f.address,a);
break;
case :
cout<<"please input update group id:"<<endl;
int b;
cin>>b;
f.groupid=b;
break;
case :
cout<<"please input update group name:"<<endl;
char nm[];
cin>>nm;
strcpy(group[f.groupid].name,nm);
break;
default:
cout<<"input error please retry"<<endl;
break;
} } void update()
{
printf("\t\t\t***************update begin!*************\n");
int n;
cout<<"please first select which you want to update :"<<endl;
cout<<"1 for num,2 for name,3 for sex,4 for birthday"<<endl;
cout<<"5 for phone,6 for address,7 for group id,8 for group name"<<endl;
cout<<"0 for end selece and delete begin!"<<endl;
for(int i=;i<now;i++){
se[i]=i;
}
int i,s,j;
s=now;
while(cin>>n){
switch(n){
case :
if(s!=){
for(int i=;i<s;i++){
modify(fri[se[i]]);
}
}
return;
case :
int nu;
cout<<"please input num:"<<endl;
cin>>nu;
if(nu<now&&fri[nu].num!=-){
modify(fri[nu]);
}
else{
cout<<"there is no that match you!"<<endl;
}
return;
case :
j=;
cout<<"please input name:"<<endl;
char na[];
cin>>na;
for(int i=;i<s;i++){
if(strcmp(fri[se[i]].name,na)==){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input sex:"<<endl;
char c[];
cin>>c;
for(int i=;i<s;i++){
if(strcmp(fri[se[i]].sex,c)==){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input birthday:"<<endl;
int y,m,d;
scanf("%d-%d-%d",&y,&m,&d);
for(int i=;i<s;i++){
if(fri[se[i]].year==y&&fri[se[i]].month==m&&fri[se[i]].day==d){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input phone:"<<endl;
char p[];
cin>>p;
for(int i=;i<s;i++){
for(int k=;k<;k++){
if(strcmp(fri[se[i]].phone[k],p)==)
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input address:"<<endl;
char a[];
cin>>a;
for(int i=;i<s;i++){
if(strcmp(fri[se[i]].address,a)==){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input group id:"<<endl;
int b;
cin>>b;
for(int i=;i<s;i++){
if(fri[se[i]].groupid==b){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input group name:"<<endl;
char nm[];
cin>>nm;
int mo;
mo=-;
for(int i=;i<;i++){
if(strcmp(group[i].name,nm)==)
{
mo=i;
break;
}
}
if(mo==-){
cout<<"no this group name!"<<endl;
return;
}
else{
for(int i=;i<s;i++){
if(mo==fri[se[i]].groupid)
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
default:
cout<<"input error please retry"<<endl;
break;
}
}
} void dele()
{
printf("\t\t\t***********delete begin!************\n");
int n;
cout<<"please input how you want to delete by:"<<endl;
cout<<"1 for num,2 for name,3 for sex,4 for birthday"<<endl;
cout<<"5 for phone,6 for address,7 for group id,8 for group name"<<endl;
cout<<"0 for end selece and delete begin!"<<endl;
for(int i=;i<now;i++){
se[i]=i;
}
int i,s,j;
s=now;
while(cin>>n){
switch(n){
case :
if(s!=){
size-=s;
printf("%4s%20s%4s%5s%6s%5s%12s%12s%12s%20s%4s%20s\n","num","\tname","sex","year","month","day","phone1",
"phone2","phone3","address","id","groupname");
for(int i=;i<s;i++){
fri[se[i]].num=-;
strcpy(fri[se[i]].name,"\0");
fri[se[i]].sex[]='o';
fri[se[i]].year=;
fri[se[i]].month=;
fri[se[i]].day=;
for(int j=;j<;j++){
fri[se[i]].phone[j][]='';
}
strcmp(fri[se[i]].address,"\0");
fri[se[i]].groupid=-;
}
}
return;
case :
int nu;
cout<<"please input num:"<<endl;
cin>>nu;
if(nu<now&&fri[nu].num!=-){
fri[nu].num=-;
strcpy(fri[nu].name,"\0");
fri[nu].sex[]='o';
fri[nu].year=;
fri[nu].month=;
fri[nu].day=;
for(int i=;i<;i++){
fri[se[i]].phone[i][]='';
}
strcmp(fri[nu].address,"\0");
fri[nu].groupid=-;
size--;
}
else{
cout<<"there is no that match you!"<<endl;
}
return;
case :
j=;
cout<<"please input name:"<<endl;
char na[];
cin>>na;
for(int i=;i<s;i++){
if(strcmp(fri[se[i]].name,na)==){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input sex:"<<endl;
char c[];
cin>>c;
for(int i=;i<s;i++){
if(strcmp(fri[se[i]].sex,c)==){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input birthday:"<<endl;
int y,m,d;
scanf("%d-%d-%d",&y,&m,&d);
for(int i=;i<s;i++){
if(fri[se[i]].year==y&&fri[se[i]].month==m&&fri[se[i]].day==d){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input phone:"<<endl;
char p[];
cin>>p;
for(int i=;i<s;i++){
for(int k=;k<;k++){
if(strcmp(fri[se[i]].phone[k],p)==)
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input address:"<<endl;
char a[];
cin>>a;
for(int i=;i<s;i++){
if(strcmp(fri[se[i]].address,a)==){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input group id:"<<endl;
int b;
cin>>b;
for(int i=;i<s;i++){
if(fri[se[i]].groupid==b){
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
case :
j=;
cout<<"please input group name:"<<endl;
char nm[];
cin>>nm;
int mo;
mo=-;
for(int i=;i<;i++){
if(strcmp(group[i].name,nm)==)
{
mo=i;
break;
}
}
if(mo==-){
cout<<"no this group name!"<<endl;
return;
}
else{
for(int i=;i<s;i++){
if(mo==fri[se[i]].groupid)
se[j++]=se[i];
}
}
s=j;
if(j==){
cout<<"there is no that match you!"<<endl;
}
break;
default:
cout<<"input error please retry"<<endl;
break;
}
}
}
void save()
{
FILE *fp=fopen("friend.txt","w");
if(fp==NULL){
printf("打开friend.txt不成功!\n");
}
for(int i=;i<now;i++)
{
if(fri[i].num!=-)
{
fprintf(fp,"%d %s %s %d %d %d %s %s %s %s %d\n",fri[i].num,fri[i].name,fri[i].sex,fri[i].year,fri[i].month,fri[i].day,fri[i].phone[],
fri[i].phone[],fri[i].phone[],fri[i].address,fri[i].groupid);
}
}
fclose(fp);
FILE *f=fopen("group.txt","w");
if(fp==NULL){
printf("打开group.txt不成功!\n");
}
for(int i=;i<;i++){
if(group[i].name[]!='\0'){
fprintf(f,"%d %s\n",group[i].id,group[i].name);
}
}
fclose(f);
printf("\t\t\t*********谢谢使用*********\n");
} int main()
{
printf("\t\t\t**************system begin!************\n");
char o;
int sta=;
for(int i=;i<;i++)
{
group[i].name[]='\0';
}
readfriend();
readgroup();
// freopen("in.txt","r",stdin);
begin();
while(cin>>o){
switch(o){
case 'p':
display();
break;
case 'i': insert(fri[now]);//如果没有该groupid则创建此id的name
break;
case 's':
select();
break;
case 'd':
dele();
break;
case 'u':
update();
break;
case 'e':
save();
return ;
default:
cout<<"input error please try again:"<<endl;
break;
}
begin();
}
return ;
}

相关文章