Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
5.2 kB
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct person{
    char name[100];
    char lastname[100];
    char phonenumber[100];
};
struct book{
    struct person arry[101];
    struct person arry2[101];
    int personNo;
};
struct book member;

int search(char name[],char lastname[]){
    int i;
    for(i=0;i<member.personNo;i++){
        if(strcmp(member.arry[i].name,name)==0 && strcmp(member.arry[i].lastname,lastname)==0 ){
            return i;
        }
    }
    return -1;

}

int insert(char name[],char lastname[],char phonenum[]){

    int i,position;
    position=search(name,lastname);
    if(position==-1){
        strcpy(member.arry[member.personNo].name,name);
        strcpy(member.arry[member.personNo].lastname,lastname);
        strcpy(member.arry[member.personNo].phonenumber,phonenum);
         member.personNo++;
    }else{
         strcpy(member.arry[position].phonenumber,phonenum);
    }
    return 1;
}

int edit(char name[],char lastname[],char newname[],char newlastname[],char newphonenum[]){
    int i,position;
    position=search(newname,newlastname);
    if(position==-1){
        position=search(name,lastname);
        strcpy(member.arry[position].name,newname);
        strcpy(member.arry[position].lastname,newlastname);
        strcpy(member.arry[position].phonenumber,newphonenum);
    }else{
         strcpy(member.arry[position].phonenumber,newphonenum);
         delet(name,lastname);
    }
     return 1;
}
void delet(char name[],char lastname[]){
    int i,position;
    position=search(name,lastname);
    if(position!=-1){
    for(i=position;i< member.personNo-1;i++){
        strcpy(member.arry[i].name,member.arry[i+1].name);
        strcpy(member.arry[i].lastname,member.arry[i+1].lastname);
        strcpy(member.arry[i].phonenumber,member.arry[i+1].phonenumber);
    }
     member.personNo--;
    }
     return 1;
}
int searchw(char query[]){
    int i,lennam,t=1,sw=1,lenlanam,j,sw2=1,k,lenqu;
    lenqu=strlen(query);
    for(i=0;i<member.personNo;i++){
        lennam=strlen(member.arry[i].name);
        for(j=0;j< lennam;j++){
            sw2=1;
            if(member.arry[i].name[j]==query[0]){
                sw2=0,sw=1,t=1;
                for(k=j+1;k<=lenqu;k++){
                    if(member.arry[i].name[k]!=query[t]){
                        sw=0;}
                    t++;}
                if(sw==1){
                    break;}
            }
        }

        if(sw!=1 || sw2!=0){

             lenlanam=strlen(member.arry[i].lastname);
             for(j=0;j<lenlanam;j++){
                     sw2=1;
                    if(member.arry[i].lastname[j]==query[0]){

                          sw2=0,sw=1, t=1;
                        for(k=j+1;k<lenqu;k++){
                            if(member.arry[i].lastname[k]!=query[t]){
                                sw=0;}
                            t++;}
                       if(sw==1){
                            break;}
                }
            }
        }
         if(sw==1 && sw2==0){
            printf("%s %s %s\n",member.arry[i].name,member.arry[i].lastname,member.arry[i].phonenumber);}
        t=1;
    }

}
int list(){
    int t=0,i,j,cap;
    for(cap=65;cap<=90;cap++){
        for(i=0;i<member.personNo;i++){
            if((int)member.arry[i].name[0]==cap){
                strcpy(member.arry2[t].lastname,member.arry[i].lastname);
                strcpy(member.arry2[t].name,member.arry[i].name);
                strcpy(member.arry2[t].phonenumber,member.arry[i].phonenumber);
                t++;
            }
        }
        for(j=0;j<member.personNo;j++){
                if((int)member.arry[j].name[0]==cap+32){
                    strcpy(member.arry2[t].lastname,member.arry[j].lastname);
                    strcpy(member.arry2[t].name,member.arry[j].name);
                    strcpy(member.arry2[t].phonenumber,member.arry[j].phonenumber);
                    t++;
                            }
                }
    }
    for(i=0;i<t;i++){
        printf("%s %s %s\n",member.arry2[i].name,member.arry2[i].lastname,member.arry2[i].phonenumber);

    }
    return 1;

}
int main(){
    int n,i;
    char opp[100];
    char name[100];
    char lastname[100];
    char phonenum[100];
    char newname[100];
    char newlastname[100];
    char newphonenum[100];
    char query[100];
     member.personNo=0;
    scanf("%d",&n);
    for(i=0;i<n;i++){
         scanf("%s",opp);
        if(strcmp(opp,"add")==0){
            scanf("%s",name);
            scanf("%s",lastname);
            scanf("%s",phonenum);
            insert(name,lastname,phonenum);
        }
         if(strcmp(opp,"delete")==0){
            scanf("%s",name);
            scanf("%s",lastname);
            delet(name,lastname);
         }
         if(strcmp(opp,"edit")==0){
            scanf("%s",name);
            scanf("%s",lastname);
            scanf("%s",newname);
            scanf("%s",newlastname);
            scanf("%s",newphonenum);
            edit(name,lastname,newname,newlastname,newphonenum);
         }
          if(strcmp(opp,"list")==0){
             list();
          }
         if(strcmp(opp,"search")==0){
             scanf("%s",query);
             searchw(query);
          }
    }
}

Leave a Comment