Chinaunix首页 | 论坛 | 博客
  • 博客访问: 922149
  • 博文数量: 210
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2070
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-19 21:54
文章分类

全部博文(210)

文章存档

2020年(2)

2019年(18)

2018年(27)

2017年(5)

2016年(53)

2015年(88)

2014年(17)

分类: C/C++

2015-05-25 16:45:20

1代码:#include


#include


#include


#include


#include


#include




#define BOOKNAME "address_book.txt"




int fd;


void menu();


int file_open(const char *pathname);


void *file_write(int fds, char *buf);


void add();


void query();


void delete();//申明一个address_book结构体


struct address_book
{

char name[20]; //名字

char addr[50]; //家庭住址


char tel[12]; //手机号


char qq[12]; //qq号


char mail[30]; //邮箱


};




//定义一个struct address_book指针


struct address_book *abook;




int main()


{


char operation;


menu();


file_open(BOOKNAME);




while(1)

{

scanf("%c", &operation);


getchar();

if(operation == '1')


query();


else if(operation == '2')


add();

else if(operation == '3')


delete();


else

break;


}




}


//菜单,用户可以根据菜单的提示操作


void menu()


{

system("clear"); //清屏


printf("|-----------------------------------------------------|\n");


printf("|           please input your operation               |\n");


printf("|-----------------------------------------------------|\n");


printf("|           1、 query 查询                            |\n");


printf("|           2、 insert增加                            |\n");


printf("|           3、 delete删除                            |\n");


printf("|           4、 exit  退出                            |\n");


printf("|-----------------------------------------------------|\n");


}




int file_open(const char *pathname)


{


//以读写的方式打开pathname, 打开之后文件指针在最后


fd = open(pathname, O_RDWR|O_APPEND);


//如果打开成功返回文件描述符,如果失败返回-1


if(fd<0)


{


printf("open data file error\n");


return -1;


}


return fd;


}




void *file_write(int fds, char *buf)


{


int num;



write(fds, ((struct address_book *)buf)->name, sizeof(((struct address_book *)buf)->name));


write(fds, "\t\t", 2);


write(fds, ((struct address_book *)buf)->addr, sizeof(((struct address_book *)buf)->addr));


write(fds, "\t\t", 2);


write(fds, ((struct address_book *)buf)->tel, sizeof(((struct address_book *)buf)->tel));


write(fds, "\t\t", 2);


write(fds, ((struct address_book *)buf)->qq, sizeof(((struct address_book *)buf)->qq));


write(fds, "\t\t", 2);


write(fds, ((struct address_book *)buf)->mail, sizeof(((struct address_book *)buf)->mail));
write(fds, "\n", 1);
}




void add()


{


int err;
//申请一块动态内存,用来存放结构体,所以动态内存的大小和返回值都必须和结构体一样


//malloc失败的时候会返回NULL


abook = (struct address_book *)malloc(sizeof(struct address_book));


if(abook == NULL)


{


printf("malloc fialed\n");


return;
}


name:
//从个屏幕上获取信息


         printf("please input your name:\t\t");


       scanf("%s", abook->name);


       err = is_name(abook->name);
if(err == -1)


{


printf("your name is wrong\n");


goto name;


}




address:


printf("please input your address:\t");


scanf("%s", abook->addr);

//标号,相当于书签。格式  名字:


//goto 跳转,可以跳到制定的标号,但是不能超出函数的范围


tel:

printf("please input your tel num:\t");


scanf("%s", abook->tel);


err = is_tel(abook->tel);


if(err != 1)


{

if(err == -1)

printf("your tel number is not enough\n");


if(err == -2)


printf("your tel number must be '0'~'9'\n");


goto tel;


}




qq:


printf("please input your qq:\t\t");


scanf("%s", abook->qq);


err = is_qq(abook->qq);


if(err == -1)


{


printf("your qq is wrong\n");


goto qq;


}




mail:


printf("please input your email:\t");


scanf("%s", abook->mail);


err = is_mail(abook->mail);


if(err != 1)


{

printf("your email is wrong\n");


goto mail;


}


// @163.com    @qq.com  @yahoo.cn @yahoo.com  @126.com




//将abook的内容写入到文件
fd
file_write(fd, (char *)abook);


}




//判断电话号码,如果是一个正确的号码,返回1.错误的号码返回负数
//长度不正确返回-1,非法字符返回-2


int is_tel(char *str)


{


int error=1;


int i;


if(strlen(str)!=11)


error = -1;


else


{


for(i=0; i<11; i++)


{


if(str[i]<'0' || str[i]>'9')


{


error = -2;


break;


}


}


}




return error;


}




//名字只能包含'a'~'z'  'A'~'Z'.
//如果是一个合法的名字,返回1,否则返回-1


int is_name(char *str)


{


int error=1;


int i;


for(i=0; i

{


if(str[i]<'A' || str[i]>'z' || str[i]>'Z'&&str[i]<'a')


{


error = -1;


break;


}


}




return error;


}




//qq号码只能是'0'~'9'


int is_qq(char *str)


{


int error=1;


int i;



for(i=0; i

{



if(str[i]<'0' || str[i]>'9')


{


error = -1;


break;


}


}



return error;


}




//邮箱只包含一个'@', 除了'@'和'.',不能包含其他特殊字符
//用户名长度大等于4
//邮箱的格式  用户名@xxx.com


int is_mail(char *str)


{


int i;


int error = 0;


int pos;


int count=0;




for(i=0; i

{


if(str[i] == '@')


{

count++;   //'@'的个数


pos = i+1; //'@'的位置


}


}


if(count==1  &&  pos>=4)


error = 1;


else


{

if(count != 1)


error = -1; //count!=1 说明邮箱的格式不对


if(pos < 4)     //pos<4 说明用户名长度不对


error = -2;


}


//邮箱的用户名只能是:字母、数字、下划线,而且只能以字母开头


for(i=0; i

{


if(str[i]<'0' || (str[i]>'9'&&str[i]<'A') || (str[i]>'Z'&&str[i]<'a') || str[i]>'z')


error = -3;


}


return error;


}




void query()


{






}




void delete()


{






}
2 通讯录的简洁:
 (1) query 查询      (2)  insert增加       (3)  delete删除      (4) exit  退出  
3执行的状态
*|------------------------------------------------------------|
*|            please select your operation                    |
*|----------------------------------------------------------- |
*|              1 query  查询                                 |
*|              2 insert 增加                                 |
*|              3 delete 删除                                 |
*|              4 exit   退出                                 |
*|----------------------------------------------------------- |
2
请输入您的名字:
ladfadf
输入您的地址:
adfdsf
输入您的电话号码:
18340566467
输入您的qq:
12345679
输入您的电子邮件:
hjkl@163.com


[root@localhost Desktop]# cat address_book.txt

ladfadf        adfdsf         18340566467          123456712345679         hjkl@163.com


    
阅读(1293) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~