The C language implements the address book system

  • 2020-06-03 07:39:19
  • OfStack

C language address book system, for your reference, the specific content is as follows

Demand analysis:

Use the method of file reading and writing

Add address book contact information

Delete contact information in the address book

Realize to find contact information in the address book

Implement modification of address book contact information

Implementation to view existing address book contact information

Code implementation:


// main.c 
// C Language address book implementation  
// 
// Created by Brisinga on 15/10/14. 
// Copyright © 2015 years  yan. All rights reserved. 
// 
 
#include <stdio.h> 
#include<string.h> 
#define LEN 10 
#define NAMELEN 22 
#define TELLEN 12 
 
//************* Structure variable definition **************** 
// Define the number of contacts  
int contactCount = 0; 
// Define structure  
typedef struct{ 
 
 // Define contact name  
 char name[NAMELEN]; 
 // Define contact number  
 char tel[TELLEN]; 
 
}Person; 
 
// Initializes the contact group  
Person contact[LEN]; 
// Define filename  
charchar *path = "a.data"; 
 
// Receive the user's input number  
int no; 
//************** Function declaration ******************** 
 
// Initialization declaration  
void init(); 
// Check the statement  
int isValid(int n,int min,int max); 
// Add contacts  
void addContact(); 
// Delete contacts  
void deleteContact(); 
// Modify contact  
void updateContact(); 
// View all contacts  
void doList(); 
// Search contacts  
void searchContact(); 
// Log out  
void quit(); 
// Contact write file  
void writeFile(); 
//*************************************** 
 
 
int main(int argc, const charchar * argv[]) { 
  
 
 // Address book initialization  
 init(); 
 printf(" Address book initialization succeeded! \n"); 
  
  
  
 while (1) { 
   
  // Define the interface  
 printf("**********************************\n"); 
 printf("*********** Welcome to the address book ***********\n"); 
 printf("***********1. Add contacts *************\n"); 
 printf("***********2. Delete contacts *************\n"); 
 printf("***********3. Modify contact *************\n"); 
 printf("***********4. View all contacts **********\n"); 
 printf("***********5. Search contacts *************\n"); 
 printf("***********6. Log out ***************\n"); 
 printf("**********************************\n"); 
   
   
  // Prompt the user for a number  
  printf(" Please enter the operation number: \n"); 
  scanf("%d",&no); 
  // Determine if the input is valid  
  isValid(no, 1, 6); 
  // Determine the user's actions  
  switch (no) { 
   case 1: 
    // Add contacts  
    addContact(); 
    break; 
   case 2: 
    // Delete contacts  
    deleteContact(); 
    break; 
   case 3: 
    // Update contacts  
    updateContact(); 
    break; 
   case 4: 
    // View contacts  
    doList(); 
    break; 
   case 5: 
    // Search contacts  
    searchContact(); 
    break; 
   case 6: 
    // exit  
    quit(); 
    return 0; 
     
   default: 
    break; 
  } 
   
 } 
 return 0; 
} 
 
// Address book initialization  
void init(){ 
 
 // Definition file pointer  
 FILEFILE *fp = fopen(path, "r"); 
 // Determine if the address book exists  
 if (fp!=NULL) { 
 // If there is  
 //1. Number of read contacts  
  fread(&contactCount, sizeof(contactCount), 1, fp); 
 //2. Read each 1 A contact nts 
  for (int i=0; i<contactCount; i++) { 
   fread(&contact[i], sizeof(Person), 1, fp); 
  } 
   
 }else{ 
  
 // If it doesn't exist  
 // Create address book  
  fp = fopen(path, "wb"); 
 // Writes the number of current contacts  
  fwrite(&contactCount, sizeof(contactCount), 1, fp); 
 
 } 
 fclose(fp); 
 
} 
 
// Verify that the input is valid  
int isValid(int n,int min,int max){ 
  
 // If the input number is greater than min Less than max, It returns 0 
 if (n>=min&&n<=max) { 
  return 0; 
 } 
  
 // Otherwise returns 1 
 printf(" Illegal import! \n"); 
 return 1; 
 
} 
 
// Add contacts  
void addContact(){ 
  
 // The user is prompted for a name to add a contact  
 printf(" Please enter the name of the contact to be added: * Note that there should be no Spaces between names \n"); 
 // Receiving contact name  
 scanf("%s",contact[contactCount].name); 
 // Prompt the user for the number to add  
 printf(" Please enter the number to add a contact:  * Note that there should be no Spaces between the Numbers \n"); 
 // Receive contact number  
 scanf("%s",contact[contactCount].tel); 
 // Ask if you are sure to add  
 printf(" Are you sure you want to add it? 1. determine  0. cancel \n"); 
 scanf("%d",&no); 
 if (no) { 
  contactCount++; 
  // Written to the file  
  writeFile(); 
  printf(" Added successfully! \n"); 
 } 
 
 
} 
 
// Delete contacts  
void deleteContact(){ 
  
 // Show contacts  
 doList(); 
 int flag; 
 // The user is prompted for the number of contacts to delete  
 printf(" Please enter the number of the contact to delete: \n"); 
 // Receive the user's input number  
 scanf("%d",&no); 
 // Determine if the number is valid  
 if(!isValid(no, 1, contactCount)){ 
 // Number the legitimate  
  printf(" Are you sure you want to delete it? It cannot be restored after deletion! 1. determine  0. cancel \n"); 
  scanf("%d",&flag); 
  if (flag) { 
   // If the contact to be deleted is at the end  
  if (no==contactCount) { 
   // the contactCount-1 
   contactCount--; 
  }else{ 
  
 // If the contact to be deleted is not at the end  
 // Move array elements  
   for (int i=no-1; i<contactCount-1; i++) { 
    contact[i]=contact[i+1]; 
   } 
    
  } 
 //contactCount-- 
   contactCount--; 
 // Write files  
  writeFile(); 
  } 
  
 }else 
 // The number is illegal, exit  
  printf(" The number is illegal! \n"); 
  return ; 
 
} 
 
// Modify contact  
void updateContact(){ 
  
 // Show contacts  
 doList(); 
 // Prompt the user for the number of contacts to change  
 printf(" Please enter the number of the contact to be modified: \n"); 
 // Receive the user's input number  
 scanf("%d",&no); 
 // Determine if the number is valid  
 int flag; 
 if (!isValid(no, 1, contactCount)) { 
  // Prompt the user for the name of the new contact  
   printf(" Please enter your new name: * Note: No Spaces between names \n"); 
   // Receive the user's input name  
   scanf("%s",contact[no-1].name); 
   // Prompt the user for a new phone number  
   printf(" Please enter your new phone number: * Note: There should be no space between the phone Numbers \n"); 
   // Receive the phone number entered by the user  
   scanf("%s",contact[no-1].tel); 
   
   // Is the user asked to confirm the change?  
  printf(" Are you sure you want to change it? 1. determine  0. cancel \n"); 
  scanf("%d",&flag); 
  if (flag) { 
   
   // Write files  
   writeFile(); 
   printf(" Contact modified successfully! \n"); 
  }else{ 
  
   printf(" The number is illegal!! \n"); 
   return ; 
   
  } 
  
 } 
  
 
} 
 
// View all contacts  
void doList(){ 
  
 printf(" Display all contacts: \n"); 
 printf(" Serial number \t The name \t The phone \n"); 
 for (int i=0; i<contactCount; i++) { 
  printf("%d\t%s\t%s\n",i+1,contact[i].name,contact[i].tel); 
 } 
 
} 
 
// Search contacts  
void searchContact(){ 
  
 int flag = 1; 
 // Receive the user's input name  
 char searchName[NAMELEN]; 
 // Receive calls entered by the user  
 char searchTel[TELLEN]; 
 // Ask the user how to search  
 printf(" Please enter how to find:  1. Search by name  2. Look it up by phone number \n"); 
 // Receive the user's search method  
 scanf("%d",&no); 
 if (no==1) { 
  // Ask the user to enter the name to look for  
  printf(" Please enter the name to find: \n"); 
  scanf("%s",searchName); 
  for (int i=0; i<contactCount; i++) { 
   if (!strcmp(searchName,contact[i].name)) { 
    printf(" The contact you are looking for is: \n"); 
    printf("%d\t%s\t%s\n",i+1,contact[i].name,contact[i].tel); 
    return ; 
   }else{ 
    
    flag = 0; 
   } 
  } 
   
 }else if(no==2){ 
  
   // Ask the user to enter the phone number they are looking for  
  printf(" Please enter the phone number you are looking for: \n"); 
  scanf("%s",searchTel); 
  for (int i=0; i<contactCount; i++) { 
   if (!strcmp(searchTel,contact[i].tel)) { 
    printf(" The contact you are looking for is: \n"); 
    printf("%d\t%s\t%s\n",i+1,contact[i].name,contact[i].tel); 
    return ; 
   }else{ 
     
    flag = 0; 
   } 
  } 
   
 
 }else{ 
  printf(" Illegal input! \n"); 
  return ; 
 } 
 if (!flag) { 
  printf(" I'm sorry! The friend is not in the address book! \n"); 
 } 
  
  
  
} 
 
// Log out  
void quit(){ 
  
 printf(" System is exiting ...\n"); 
 printf(" System is out! \n"); 
  
 
} 
 
// Contact write file  
void writeFile(){ 
 
 // Definition file pointer  
 FILEFILE *fp = fopen(path, "wb"); 
 if (fp!=NULL) { 
  // Number of contacts written  
  fwrite(&contactCount, sizeof(contactCount), 1, fp); 
  // Write to each contact  
  for (int i=0; i<contactCount; i++) { 
    
   fwrite(&contact[i], sizeof(Person),1, fp); 
  } 
   
 } 
 fclose(fp); 
 
}


Related articles: