⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 课程设计-飞扬工作组.cpp

📁 添加同学信息
💻 CPP
字号:
#include<conio.h>/*屏幕操作函数*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h> //是 system函数的头文件
#include<time.h>
#define Node struct node
Node             //结构体类型定义,包括:姓名、电话号码和联系地址
{
 char szName[20];
 char szXueNum[12];
 char szmobileNumber[20]; 
 char szemail[30];
 char szAddress[30];
 Node *next;
};
Node *strpHead;  //全局变量,链表头指针
Node *strpCurrent;  //用于指向当前正在操作的结点
 //函数原型声明
void HandleChoice_f(int);//    选择功能
void AddRecord_f();         //增加记录
void InsertNode_f(Node * );
Node *InsertPoint_f(char * );//插入
void MakeNewHead_f(Node * );
void AddToEnd_f(Node * );
void MoveToEnd_f();
void DisplayList_f();   //显示
void modify_f();       //修改
void DeleteRecord_f();
void DelHead_f();
void DelEnd_f(Node * );
void DelMid_f(Node * );
int VerifyDel_f();  /*删除信息时要求予以确认*/
void DelNode_f(Node * );
void DelList_f();
void SearchByName_f();//查找
void WriteFile_f();
void LoadFile_f();
void Help_f();
void SearchBynum_f();//按学号查找
int VerifyDel_f1(); //修改信息时要求予以确认
//主程序
int main()
{
 int nChoice;
 system("color 1b");
 printf("             欢迎来到电话簿管理系统                \n");
 system("pause");//调用shell来执行命令
 system("cls");
 strpHead=NULL;
 LoadFile_f();
 do
 {printf("   ☆☆☆☆☆☆☆☆☆菜单☆☆☆☆☆☆☆☆☆☆\n");
 printf("   ┌─────────────────────┐  \n");
 printf("   │     1.增加记录                           │  \n");
 printf("   │     2.显示所有记录                       │ \n");
 printf("   │     3.按名字查找学生的信息               │ \n");
 printf("   │     4.按学号查找学生的信息               │ \n");
 printf("   │     5.删除记录                           │  \n");
 printf("   │     6.修改记录                           │  \n");
 printf("   │   ★7.请求帮助                           │  \n");
 printf("   │     8.退出程序                           │  \n");                   
 printf("   └─────────────────────┘  \n");
 printf("      ☆飞扬工作组☆  星空飞扬          \n");
 printf("    请选择代码(1,2,3,4,5,6,7,8):");
 scanf("%d",&nChoice);
 HandleChoice_f(nChoice);        /*接受用户的选择*/
 }while(nChoice!=8);
 return 0;
 
}
void HandleChoice_f(int nChoice)   /*根据用户选择nChoice调用相应的函数*/
{
 switch(nChoice)
 {
 case 1:
  AddRecord_f();
  break;
 case 2:
  DisplayList_f();
  break;
 case 3:
  SearchByName_f();
  break;
 case 4:
  SearchBynum_f();
 break;
 case 5:
  DeleteRecord_f();
  break;
 case 6:
 modify_f();
  break;
 case 7:
 Help_f();
  break;
 case 8:
  WriteFile_f();       /*将链表中的数据写回文件*/
  if(strpHead!=NULL)
  {
   DelList_f();
  }
  break;
 default:
  printf("没有您要的选项!\n");
  break;
 
 }
}

void AddRecord_f()
{
 Node *strpNew;/*为新记录定义临时指针变量*/
 strpNew=(Node *)malloc(sizeof(Node));/*开辟空间存放新记录数据*/
 getchar();
 printf("姓名:");
 gets(strpNew->szName);
 printf("学号:");
 gets(strpNew->szXueNum);
 printf("电话号码:");
 gets(strpNew->szmobileNumber);
 printf("电子信箱:");
 gets(strpNew->szemail);
 printf("联系地址:");
 gets(strpNew->szAddress);
 InsertNode_f(strpNew);
 system("cls");
}
void InsertNode_f(Node *strpNew)
{
 Node *strpFront;
 Node *strpBack;
 system("cls");
 if(strpHead==NULL)
 {
  strpNew->next=NULL;
  strpHead=strpNew;
 }
 else
 {
  if(strcmp(strpNew->szName,strpHead->szName)>0)
  {
   MakeNewHead_f(strpNew);
  }
  else //查找新结点的位置
  {
   strpCurrent=InsertPoint_f(strpNew->szName);
   strpFront=strpCurrent;
   strpBack=strpCurrent->next;
   if(strpBack==NULL)
   {
    AddToEnd_f(strpNew);
   }
   else
   {
    strpFront->next=strpNew;
    strpNew->next=strpBack;
   }
  }
 }
}
Node *InsertPoint_f(char *szName) /*根据新增记录的姓氏,返回其将插入的正确位置*/
{
 char szTempName[20];
 Node *strpTemp;
 int nTemp;
 if(strpHead->next!=NULL)
 {
  strpCurrent=strpHead;
  strpTemp=strpCurrent->next;
  strcpy(szTempName,strpTemp->szName);
  nTemp=strcmp(szName,szTempName);
  while((nTemp>0)&&(strpCurrent->next!=NULL))
  {
   strpCurrent=strpTemp;
   if(strpCurrent->next!=NULL)
   {
    strpTemp=strpCurrent->next;
    strcpy(szTempName,strpTemp->szName);
    nTemp=strcmp(szName,szTempName);
   }
  }
 }
 else
 {
  strpCurrent=strpHead;
 }
 return(strpCurrent);
}
void MakeNewHead_f(Node *strpNew)/*新结点成为链表的头结点*/
{
 Node *strpTemp;
 strpTemp=strpHead;
 strpNew->next=strpTemp;
 strpHead=strpNew;
}
void AddToEnd_f(Node *strpNew)/*新结点成为链表的尾结点*/
{
 strpNew->next=NULL;
 MoveToEnd_f();
 strpCurrent->next=strpNew;
}
void MoveToEnd_f()/*当前指针移到链表尾*/
{
 strpCurrent=strpHead;
 while(strpCurrent->next!=NULL)
 {
  strpCurrent=strpCurrent->next;
 }
}
void DisplayList_f()
{
 
 strpCurrent=strpHead;
 if(strpCurrent!=NULL)
 {
  printf("\n");
  printf("      姓名        学号         电话号码       电子邮箱        联系地址         \n");
  printf("-------------------------------------------------------------------------------\n");
  do
  {
   printf("%10s",strpCurrent->szName);
   printf("%12s",strpCurrent->szXueNum);
   printf("%15s",strpCurrent->szmobileNumber);
   printf("%15s",strpCurrent->szemail);
   printf("%15s\n",strpCurrent->szAddress);
   strpCurrent=strpCurrent->next;
   printf("\n");
  }while(strpCurrent!=NULL);
  system("pause");
  system("cls");
 }
 else
 {
  printf("没有记录可以显示!\n");
 }
}
void SearchByName_f()
{
 char szSearch[20];
 strpCurrent=strpHead;
 system("cls");
 getchar();//从键盘输入
 printf("\n输入您要查找的姓名:");
 gets(szSearch);
 while((strpCurrent!=NULL)&&(strcmp(strpCurrent->szName,szSearch)!=0))
 {
  strpCurrent=strpCurrent->next;
 }
 if(strpCurrent!=NULL)
 {
  printf("\n记录找到了!\n");
  printf("      姓名         学号        电话号码          电子邮箱         联系地址         \n");
  printf("--------------------------------------------------------------------------------\n");
  printf("%10s",strpCurrent->szName);
   printf("%12s",strpCurrent->szXueNum);
   printf("%15s",strpCurrent->szmobileNumber);
   printf("%15s",strpCurrent->szemail);
   printf("%15s\n",strpCurrent->szAddress);
 }
 else
 {
  printf("没有相应的记录!\n");
  printf("按ENTER键继续\n");
  system("pause");
  system("cls");
 }
}
void DeleteRecord_f()
{
 char szSearch[20];
 Node *strpFront;
 system("cls");
 strpFront=NULL;
 strpCurrent=strpHead;
 getchar();
 printf("\n输入朋友的姓名以删除该记录:");
 gets(szSearch);
 while((strpCurrent!=NULL)&&(strcmp(strpCurrent->szName,szSearch)!=0))
 {
  strpFront=strpCurrent;
  strpCurrent=strpCurrent->next;
 }
 if(strpCurrent!=NULL)
 {
  printf("\n记录找到了\n");
  printf("      姓名           学号        电话号码          电子邮箱           联系地址         \n");
  printf("--------------------------------------------------------------------------------------\n");
  printf("%10s",strpCurrent->szName);
   printf("%12s",strpCurrent->szXueNum);
   printf("%15s",strpCurrent->szmobileNumber);
   printf("%15s",strpCurrent->szemail);
   printf("%15s\n",strpCurrent->szAddress);
  if(VerifyDel_f())
  {
   DelNode_f(strpFront);
   printf("\n该记录已经删除!\n");
  }
  else
  {
   printf("\n该记录没有删除!\n");
  }
 }
 else
 {
  printf("\n没有匹配的记录被删除!\n");
 }
system("pause");
 system("cls");
}
void modify_f()
{
	char NY;
 char szSearch[20];
 Node *strpFront;
 system("cls");
 strpFront=NULL;
 strpCurrent=strpHead;
 getchar();
 printf("\n输入朋友的姓名以修改该记录:");
 gets(szSearch);
 while((strpCurrent!=NULL)&&(strcmp(strpCurrent->szName,szSearch)!=0))
 {
  strpFront=strpCurrent;
  strpCurrent=strpCurrent->next;
 }
 if(strpCurrent!=NULL)
 {
  printf("\n记录找到了\n");
  printf("   姓名        学号        电话号码        电子邮箱         联系地址         \n");
  printf("-------------------------------------------------------------------------------\n");
  printf("%10s",strpCurrent->szName);
   printf("%12s",strpCurrent->szXueNum);
   printf("%15s",strpCurrent->szmobileNumber);
   printf("%15s",strpCurrent->szemail);
   printf("%15s\n",strpCurrent->szAddress);
  if(VerifyDel_f1())
  {
   DelNode_f(strpFront);
  }
  else
  {
   printf("\n该记录没有修改!\n");
   main();
  }
 }
 else
 {
  printf("\n没有匹配的记录要被修改!\n");
  printf("\n是否继续查找(Y/N):\n");
  scanf("%s",&NY);
  if(NY=='y'||NY=='Y')
	 modify_f();
  else main();
 }
Node *strpNew;/*为新记录定义临时指针变量*/
 strpNew=(Node *)malloc(sizeof(Node));/*开辟空间存放新记录数据*/
 printf("\n 请输入您要修改的新信息:\n");
 getchar();
 printf("姓名:");
 gets(strpNew->szName);
 printf("学号:");
 gets(strpNew->szXueNum);
 printf("电话号码:");
 gets(strpNew->szmobileNumber);
 printf("电子信箱:");
 gets(strpNew->szemail);
 printf("联系地址:");
 gets(strpNew->szAddress);
 InsertNode_f(strpNew);
 system("cls");
}


 


void Help_f()
{
 int nChoice;
 do
 {
  system("cls");
  printf("欢迎来到帮助栏,请选择代号\n");
  printf("1:电话簿的功能\n");
  printf("2:怎么清除所有记录\n");
  printf("3:在加入新的信息时,原来的信息还在吗\n");
  printf("4:你操作时需要注意的事项!\n");
  printf("5:退出\n");
  scanf("%d",&nChoice);
  switch(nChoice)
  {
  case 1:
   printf("这是一个简单的通讯录,刚开始是一个空的电话本。你可以根据提示加入或删除纪录,还可以根据需要显示,查询和修改学生信息\n");
   printf("按ENTER键继续\n");
   getch();
   system("cls");
   break;
  case 2:
   printf("你只要删除文件Friend.txt或者退出程序释放存储空间,就可以删除所有纪录了\n");
   printf("按ENTER键继续\n");
   getch();
   system("cls");
   break;
  case 3:
   printf("加入信息时不会改变原来的信息\n");
   printf("按ENTER键继续\n");
   getch();
   system("cls");
   break;
  case 4:
   printf("在查找,删除和修改纪录时,应该把要找的学生的全名或学号写好,否则将无法显示和操作\n修改与删除只能以姓名查找\n");
   printf("按ENTER键继续\n");
   getch();
   system("cls");
   break;
  default:
  case 5:
   printf("按ENTER键继续\n");
   break;
  }
 }while(nChoice!=5);
}
int VerifyDel_f() /*删除信息时要求予以确认*/
{
 char chYesNo;
 printf("你确定要删除吗?(Y/N)");
 scanf("%c",&chYesNo);
 if((chYesNo=='Y')||(chYesNo=='y'))
 {
  return(1);
 }
 else
 {
  return(0);
 }
}
void DelNode_f(Node *strpFront)/*删除结点*/
{
 if(strpCurrent==strpHead)
  DelHead_f();
 else
 {
  if(strpCurrent->next==NULL)
   DelEnd_f(strpFront);
  else
   DelMid_f(strpFront);
 } 
}
void DelHead_f()  /*删除头结点*/
{
 strpCurrent=strpHead;
 if(strpHead->next!=NULL)
  strpHead=strpCurrent->next;
 else
  strpHead=NULL;
 free(strpCurrent);
}
void DelEnd_f(Node *strpFront) /*删除尾结点*/
{
 free(strpCurrent);
 strpFront->next=NULL;
 strpCurrent=strpHead;
}
void DelMid_f(Node *strpFront) /*删除链表中的结点*/
{
 strpFront->next=strpCurrent->next;
 free(strpCurrent);
 strpCurrent=strpHead;
}
void DelList_f()/*删除链表,释放空间*/
{
 Node *strpTemp;
 strpCurrent=strpHead;
 do
 {
  strpTemp=strpCurrent->next;
  free(strpCurrent);
  strpCurrent=strpTemp;
 }while(strpTemp!=NULL);
}
void Deln(char *szString)/*删除字符串后的回车符*/
{
 int nLength;
 nLength=strlen(szString);
 if(nLength>=2)
 {
  if(szString[nLength-1]=='\n')
   szString[nLength-1]='\0';
 }
}
void WriteFile_f()/*程序退出,将链表数据写回文件*/
{
 FILE *fpoutfile;
 if((fpoutfile=fopen("FRIENDS.txt","w"))==NULL)
 {
  printf("File Error!\n");
  exit(0);
 }
 strpCurrent=strpHead;
 if(strpHead!=NULL)
 {
  do
  {
   fprintf(fpoutfile,"%s\n",strpCurrent->szName);
   fprintf(fpoutfile,"%s\n",strpCurrent->szXueNum);
   fprintf(fpoutfile,"%s\n",strpCurrent->szmobileNumber);
   fprintf(fpoutfile,"%s\n",strpCurrent->szemail);
   fprintf(fpoutfile,"%s\n",strpCurrent->szAddress);
   strpCurrent=strpCurrent->next;
  }while(strpCurrent!=NULL);
 }
 fprintf(fpoutfile,"END OF FILE");
 fclose(fpoutfile);//关闭文件
}
void LoadFile_f()/*从数据文件FRIEND.txt中读取数据重建链表*/
{
 Node *strpNew;
 FILE *fpinfile;/*输入文件指针*/
 int nEndLoop=0;
 if((fpinfile=fopen("FRIENDS.TXT","r"))==NULL)
 {
  printf("现在没有可用数据载入,链表为空\n");
 }
 else{
  do{
  strpNew=(Node *)malloc(sizeof(Node));
  if(strpNew!=NULL)
  {
   fgets(strpNew->szName,20,fpinfile);
   if((strcmp(strpNew->szName,"")!=0)&&(strcmp(strpNew->szName,"END OF FILE")!=0))
   {
    fgets(strpNew->szXueNum,15,fpinfile);
    fgets(strpNew->szmobileNumber,15,fpinfile);
    fgets(strpNew->szemail,15,fpinfile);
    fgets(strpNew->szAddress,15,fpinfile);
    Deln(strpNew->szName);
    Deln(strpNew->szXueNum);
    Deln(strpNew->szmobileNumber);
    Deln(strpNew->szemail);
    Deln(strpNew->szAddress);
    InsertNode_f(strpNew);
   }
   else
   {
    free(strpNew);
    nEndLoop=1;
   }
  }
  else
  {
   printf("WARNING:Memory error!\n");
   nEndLoop=1;
  }   
  }while(nEndLoop==0);
  fclose(fpinfile);
 }
}
void SearchBynum_f()
{
 char xuehao[20];
 strpCurrent=strpHead;
 system("cls");
 getchar();
 printf("\n输入您要查找的学号:");
 gets(xuehao);
 while((strpCurrent!=NULL)&&(strcmp(strpCurrent->szXueNum,xuehao)!=0))
 {
  strpCurrent=strpCurrent->next;
 }
 if(strpCurrent!=NULL)
 {
  printf("\n记录找到了!\n");
  printf("      姓名         学号        电话号码          电子邮箱           联系地址         \n");
  printf("-------------------------------------------------------------------------------------\n");
  printf("%10s",strpCurrent->szName);
   printf("%12s",strpCurrent->szXueNum);
   printf("%15s",strpCurrent->szmobileNumber);
   printf("%15s",strpCurrent->szemail);
   printf("%15s\n",strpCurrent->szAddress);
 }
 else
 {
  printf("没有相应的记录!\n");
  printf("按ENTER键继续\n");
  system("pause");
  system("cls");
 }
}
int VerifyDel_f1() /*修改信息时要求予以确认*/
{
 char chYesNo;
 printf("你确定要修改吗?(Y/N)");
 scanf("%c",&chYesNo);
 if((chYesNo=='Y')||(chYesNo=='y'))
 {
  return(1);
 }
 else
 {
  return(0);
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -