📄 address-list.c
字号:
/*----------------------------------------------------------------------------
*-----2003-级计算机科学与技术系(department of computer)--杨越(yang-yue)------
*
*-----------------通讯录程序(the program is address list)-------------------
*
*----------------编于:2006-3-18 11:00(made in 2006-3-18)------------------
----------------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#define NUM 10
#define NAME 20
#define SEX 5
#define FILE_NAME yyang
#define TEMP_LEN 20
/*
*定义通讯录的“节点 ”的结构体:
*num : 编号 (char array)
*name : 姓名 (char array)
*sex : 性别 (char array)
*score: 成绩 (float)
*
*/
typedef struct address_list
{
char num[NUM];
char name[NAME];
char sex[SEX];
float score;
struct address_list *next;
}My_friend,*PTR_My_friend;
#define LEN sizeof(My_friend)
/*-----------------------------------------------------------------------------
*数出一条记录 :void print_one(PTR_My_friend head)
*
----------------------------------------------------------------------------*/
void print_one(PTR_My_friend head)
{
printf("the fun is print_one()\n");
printf(" num is:%s\n", head->num);
printf(" sex is:%s\n", head->sex);
printf(" name is:%s\n", head->name);
printf(" score is:%f\n", head->score);
printf("----------------------------------------------------------\n");
}
/*-----------------------------------------------------------------------------
*自己改造的输入字符串的函数 void input_str( char * string , int size )
*功能类似于 库函数 gets(*address)
----------------------------------------------------------------------------*/
void input_str( char * string , int len )
{
/*想用getchar() 和 for()改写*/
int i;
for ( i=0 ; i < len ; i++ )
{
string[i]=getchar();
if(string[i]=='\n')
{
string[i] = '\0' ;
break;
}
}
}
/*-----------------------------------------------------------------------------
*录入数据 void input_data(PTR_My_friend temp)
*
----------------------------------------------------------------------------*/
void input_data(PTR_My_friend temp)
{
PTR_My_friend front=temp;
printf("(the type is char array %d)",NUM);
printf("input the num:-->");
input_str(front->num,NUM);
printf("%s", front->num);
printf("\n");
printf("(the type is char array %d)",NAME);
printf("input the name:-->");
input_str(front->name,NAME);
printf("%s", front->name);
printf("\n");
printf("the type is char array %d)",SEX);
printf("input the sex:-->");
input_str(front->sex,SEX);
printf("%s", front->sex);
printf("\n");
printf("(the type is float)");
printf("input the score:-->");
scanf("%f",&front->score);
getchar();
printf("%f", front->score);
printf("\n");
}
/*-----------------------------------------------------------------------------
*插入记录 int inset() 提示这次你要插入的记录的条数 并写入文件
*可以把插入的链返回给调用函数
----------------------------------------------------------------------------*/
int inset()
{
/*PTR_My_friend temp ;
temp=head; PTR_My_friend head*/
printf("the fun is inset()\n");
PTR_My_friend head,front,back;
int i,n;
FILE *fp ;
if((fp=fopen("FILE_NAME","a+"))==NULL)
{
printf("open the file FILE_NAME failed !!");
exit(0);
}
/* it's have the head node*/
head=(PTR_My_friend)malloc(LEN);
head->next=NULL;
back=head;
printf("input the count of your need create the list:\n-->");
scanf("%d",&n);
getchar();
for( i=0; i<n; i++ )
{
front=(PTR_My_friend)malloc(LEN);
input_data(front);
if(fwrite(front,LEN,1,fp)!=1)
{
printf("the %d th info write to the file \" FILE_NAME \" failed",i+1);
exit(0);
}
back->next=front;
back=front;
}
back->next=NULL;
head=head->next;
fclose(fp);
return 1;
}
/*-----------------------------------------------------------------------------
*删除记录int del ( )
*删除记录 可批量删除 做成相数据库一样
----------------------------------------------------------------------------*/
int del ( )
{
printf("the fun is del()\n");
return 1;
}
/*-----------------------------------------------------------------------------
*数据拷贝函数 :void copy_data(PTR_My_friend data1, PTR_My_friend data1)
*
----------------------------------------------------------------------------*/
void data_copy(PTR_My_friend data1, PTR_My_friend data2)
{
printf("the function is data_copy()\n");
strcpy(data1->num,data2->num);
strcpy(data1->name,data2->name);
strcpy(data1->sex,data2->sex);
data1->score=data1->score;
}
/*-----------------------------------------------------------------------------
*查找函数 PTR_My_friend search( PTR_My_friend head ,char *key)
*查找记录 按姓名 编号 成绩 等 做成相数据库一样
----------------------------------------------------------------------------*/
PTR_My_friend search( PTR_My_friend head ,char *key)
{
PTR_My_friend temp=head ;
PTR_My_friend node=NULL,front=NULL,back=NULL;
node=(PTR_My_friend)malloc(LEN);
front=node;
back=front;
printf("the fun is search()\n");
while(temp->next!=NULL)
{
if( strcmp(temp->num,key)==0 )
{
print_one(temp);
back=(PTR_My_friend)malloc(LEN);
data_copy(back,temp);
front->next=back;
front=back;
}
temp=temp->next;
}
front->next=NULL;
return node->next;
}
/*-----------------------------------------------------------------------------
*更新记录 int updata( )
*更新记录 按姓名 编号 成绩 等 做成相数据库一样
----------------------------------------------------------------------------*/
int updata( )
{
printf("the fun is updata()\n");
return 1;
}
/*-----------------------------------------------------------------------------
*load_file()将文件读入内存
*
----------------------------------------------------------------------------*/
PTR_My_friend load_file()
{
PTR_My_friend head,front,back;
FILE *fp_load;
if((fp_load=fopen("FILE_NAME","rb"))==NULL)
{
printf("open the file FILE_NAME failed !!");
exit(0);
}
/* it's have the head node*/
head=(PTR_My_friend)malloc(LEN);
head->next=NULL;
back=head;
while( !feof( fp_load ) )
{
front=(PTR_My_friend)malloc(LEN);
if(fread(front , LEN ,1,fp_load)!=0)
{
if(feof(fp_load))
{
fclose(fp_load);
printf("\n at the file end!\n");
return 0;
}
}
back->next=front;
back=front;
}
back->next=NULL;
head=head->next;
fclose(fp_load);
return head;
}
/*-----------------------------------------------------------------------------
*显示函数 :void show(PTR_My_friend head)
*
----------------------------------------------------------------------------*/
void show(PTR_My_friend head)
{
PTR_My_friend temp=head;
int count=1;
printf("the function is show()\n");
while (temp!=NULL)
{
printf("the %d th info:-->\n",count);
printf(" num is:%s\n", temp->num);
printf(" sex is:%s\n", temp->sex);
printf(" name is:%s\n", temp->name);
printf(" score is:%f\n", temp->score);
printf("----------------------------------------------------------\n");
temp=temp->next;
count++;
}
}
/*-----------------------------------------------------------------------------
*主函数开始
*
----------------------------------------------------------------------------*/
int main()
{
PTR_My_friend head ,temp;
char menu;
/*
*下面是建立文件并授权为 可读 ,可写 的
*/
if(fopen("FILE_NAME","a+")==NULL)
{
printf("\nopen file error!!\n");
}
else
{
chmod("./FILE_NAME",S_IRUSR|S_IWUSR);
}
/*
* 画出选项菜点 (print the "menu")
* i : 为插入 inset(); f: 为查找 search();
* d : 为删除 del(); u: 更新记录 updata();
*
*/
printf("please enter an char :\n");
printf("\nMY-Address-list-->");
do
{
menu=getchar();
getchar();
switch( menu )
{
case 'l':
case 'L': temp=load_file();
show(temp);
printf("\nMY-Address-list-->");
break;
case 'i':
case 'I': inset( );
printf("\nMY-Address-list-->");
break;
case 'd':
case 'D': del( );
printf("\nMY-Address-list-->");
break;
case 'f':
case 'F': {
char temp[TEMP_LEN];
printf("please enter the NUM witch you want to find!\n");
input_str(temp,TEMP_LEN);
head=load_file();
show (search(head,temp));
printf("\nMY-Address-list-->");
break;
}
case 'u':
case 'U': updata( );
printf("\nMY-Address-list-->");
break;
case 'q':
case 'Q':
printf("you sure quit ?!!\n ");
printf("press the key 'N/n' is not to quit! \n");
printf("press the key 'Y/n' is sure to quit! \n");
{/*复合语句完成判断*/
char flag;
flag=getchar();
getchar();
if (flag=='n'||flag=='N')
{
printf("\nMY-Address-list-->");
break ;
}
else if(flag=='y'||flag=='Y')
{
exit(11);
}
}
default:
printf("you enter an error key!! !\n");
printf("\nMY-Address-list-->");
break;
}
}
while( menu!='i'||menu!='d'||menu!='f'||menu!='u'||menu!='l'||menu!='q'||
menu!='I'||menu!='D'||menu!='F'||menu!='U'||menu!='L'||menu!='Q');
system("pause");
return 1;
}
/*--------------------------------主函数结束----------------------------------*/
/*----------------------------create_list建立链表-----------------------------
PTR_My_friend create_list()
{
PTR_My_friend head,front,back;
int i,n;
FILE *fp ;
if((fp=fopen("FILE_NAME","wb"))==NULL)
{
printf("open the file FILE_NAME failed !!");
exit(0);
}
it's have the head node
head=(PTR_My_friend)malloc(LEN);
head->next=NULL;
back=head;
printf("input the count of your need create the list:\n-->");
scanf("%d",&n);
getchar();
for( i=0; i<=n; i++ )
{
front=(PTR_My_friend)malloc(LEN);
input_data(front);
if(fwrite(front,LEN,1,fp)!=1)
{
printf("the %d th info write to the file \" FILE_NAME \" failed",i+1);
exit(0);
}
back->next=front;
back=front;
}
back->next=NULL;
head=head->next;
fclose(fp);
return head;
} */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -