vc++ 6.0 用MFC做一个简单的通讯录,要求能做到录入信息,修改信息,查找信息等等功能

vc++ 6.0 用MFC做一个简单的通讯录,要求能做到录入信息,修改信息,查找信息等等功能,第1张

第一步:建一个结构体

struct{姓名,电话,注释}

或建一个类

class{public:姓名,电话,注释}

第二步:增加控件

一个文本框,几个按钮

第三步:录入按钮

将新增电话写入TXT文件

#include

#include

#include

#include

#define maxlen 15

struct persons

{

int num

char name[20]

char e_addr[20]

char tel_no[15]

char sim_no

char arch

}persons[maxlen]

typedef struct lnode{

int num

char name[20]

char e_addr[20]

char tel_no[15]

char sim_no

char arch

struct lnode *next

}listnode,*linklist

linklist head=NULL,r=NULL

listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9

int i

char name1[10],chchar tel_no1[15]char arch1char sim_no1char e_addr1[20]

char s1[20]

FILE *fp

void creat()

{

int j

long k

fp=fopen("数据文件.txt","r t")

if(fp!=NULL)

{for(i=0i<=maxleni++ )

{ j=fgetc(fp)

if(j==EOF)

return

k=i

fseek(fp,k*sizeof(struct persons),0)

fread(&persons[i],sizeof(struct persons),1,fp)

s=(linklist)malloc(sizeof(listnode))

s->num=persons[i].num

strcpy(s->name,persons[i].name)

strcpy(s->e_addr,persons[i].e_addr)

strcpy(s->tel_no,persons[i].tel_no)

s->sim_no=persons[i].sim_no

s->arch=persons[i].arch

if(head==NULL)

{head=sr=headhead->next=NULL}

else

{r->next=s

r=sr->next=NULL

}}fclose(fp)}

else

{ fp=fopen("数据文件.txt","w")

i=1

}

}

void ShowA()

{p1=head

while(p1!=NULL)

{if(p1->arch=='A')

{

cout<<endl

cout<name<<endl

cout<tel_no<<endl

cout<sim_no<<endl}

p1=p1->nextcontinue}

}

void ShowB()

{p1=head

while(p1!=NULL)

{if(p1->arch=='B')

{

cout<<endl

cout<name<<endl

cout<tel_no<<endl

cout<sim_no<<endl}

p1=p1->nextcontinue}

}

void ShowC()

{p1=head

while(p1!=NULL)

{if(p1->arch=='C')

{

cout<<endl

cout<name<<endl

cout<tel_no<<endl

cout<sim_no<<endl}

p1=p1->nextcontinue}

}

void Show()

{char ch1

printf(" Which arch do you want to show?\n")

cout<<" A:同学同事"<<"B:家人"<<"C:其他"<<endl

cout<<"请选择:"<<endl

cin>>ch1

switch(ch1)

{case'A':ShowA()break

case'B':ShowB()break

case'C':ShowC()break

default:cout<<"error"<<endl

}

}

void Delete()

{ printf("\n\n\t 请输入要删除用户的姓名:")

cin>>name1

p4=head

if(strcmp(p4->name,name1)==0)

{ p4=p4->next

head=p4

}

else

{ while(strcmp(p4->next->name,name1)!=0)

p4=p4->next

p5=p4->next

p4->next=p5->next

free(p5)

}

}

void Input()

{ s=(linklist)malloc(sizeof(listnode))

printf("\n\n\t 请输入该用户的信息:")

cout<<"name:"

cin>>s->name

cout<<"tel_no:"

cin>>s->tel_no

cout<<"sim_no:"

cin>>s->sim_no

cout<<"e_addr:"

cin>>s->e_addr

cout<<"arch:"

cin>>s->arch

if(head==NULL)printf("\n\n")

else

{p8=head

while(p8!=NULL&&strcmp(s->name,p8->name)!=0&&strcmp(s->tel_no,p8->tel_no)!=0)

p8=p8->next

if(p8!=NULL)

{printf(" 您添加的用户已存在!")

free(s)}}

if(head==NULL)

{head=shead->next=NULL}

else

{r->next=s

r=sr->next=NULL}

}

void Alter()

{

printf("\n\n\t 请输入姓名:")

cin>>name1

p3=head

while(p3!=NULL&&strcmp(name1,p3->name)!=0)

p3=p3->next

if(p3==NULL)

printf("\n\n\t 您选择的用户不存在!")

else

{ printf("\n\n\t 请输入该用户的新信息!")

cout<<"name:"

cin>>name1

cout<<"tel_no:"

cin>>tel_no1

cout<<"e_addr:"

cin>>e_addr1

cout<<"sim_no:"

cin>>sim_no1

cout<<"arch:"

cin>>arch1

while(p9!=NULL&&strcmp(name1,p9->name)!=0&&strcmp(tel_no1,p9->tel_no)!=0)

p9=p9->next

if(p9==NULL)

{strcpy(p3->name,name1)

strcpy(p3->tel_no,tel_no1)

strcpy(p3->e_addr,e_addr1)

p3->sim_no=sim_no1

p3->arch=arch1

}

else

printf(" 您添加的用户已存在!")

}

}

void Dial()

{int achar b

cout<<"1.拨叫用户:"<<endl

cout<<"2.使用单键拨号:"<<endl

cin>>a

p0=head

if(a==1)

{cout<<" 请输入拨叫用户的姓名:"

cin>>name1

while(strcmp(name1,p0->name)!=0&&p0!=NULL)

p0=p0->next

if(p0==NULL)

cout<<" 您选择的用户不存在!"

else

/*for(c=0c<15c++)

{cout<<endl

cout<<(*p0).tel_no[c]}*/

{cout<<endl

cout<tel_no

cout<<endl}

}

else

{cout<<"请输入单键号码:"

cin>>b

while(p0!=NULL&&b!=p0->sim_no)

p0=p0->next

if(p0==NULL)

cout<<" 您输入的单键号码不存在!"

else

{cout<<endl

cout<name<<endl

cout<tel_no

cout<<endl}

}

}

void Save()

{ int j

fp=fopen("数据文件.txt","w")

for(p2=head,j=0p2!=NULLj++ ,p2=p2->next)

{

strcpy(persons[j].name,p2->name)

strcpy(persons[j].tel_no,p2->tel_no)

persons[j].sim_no=p2->sim_no

strcpy(persons[j].e_addr,p2->e_addr)

persons[j].arch=p2->arch

fwrite(&persons[j],sizeof(struct persons),1,fp)

}

}

void main()

{ creat()

do

{

printf(" **********************************************************")

printf("\n\n\n\n\t 欢迎来到通讯录!")

printf("\n\n\t\t 请选择 *** 作:")

printf("\n\t\t1. 显示通讯录")

printf("\n\t\t2. 删除通讯录")

printf("\n\t\t3. 添加通讯录")

printf("\n\t\t4. 编辑通讯录")

printf("\n\t\t5. 拨号功能")

printf("\n\t\t6. 保存刚才的 *** 作并退出\n")

printf(" **********************************************************")

printf("\n\n\n")

printf("\t 选择您想要的 *** 作:")

cin>>ch

switch(ch)

{ case '1': Show()

break

case '2': Delete()

break

case '3': Input()

break

case '4': Alter()

break

case '5': Dial()

break

case '6': Save()

fclose(fp)

exit(0)

break

default:

printf("\n\t*********************************\n")

printf("\n\t The num should 1-6!!! \n")

printf("\n\t**********************************")

break

}

}while(1)

}

转的.......woyuyuchao

这个是我编的,用数组和链表两种功能实现的通讯录

基本能满足你的要求!!

代码如下:

#include "stdlib.h"

#define NEW (struct node *)malloc(sizeof(struct node))

struct student

{ char name[10],tel[11]

}a[20]

struct node

{ char name[20],tel[11]

struct node *next

}

main()

{ struct student *jianli(),*delete(struct student *)

struct student *charu(struct student *)

void xianshi(struct student *)

struct node *create(),*delnode(struct node*,char *)

struct node *insert(struct node *,struct node *,char *)

void prlist(struct node *)

struct student *p

struct node *head=NULL,*stu

char s[80],name[20],q[80]

int c,w

a:

system("cls")

printf("\nEnter your choice\n")

printf("1.SHUZU\n2.LIANBIAO\n0.Quit\n")

gets(q)

w=atoi(q)

switch(w)

{ case 1:

do

{ do

{ printf("----------------------------------------------\n")

printf("******************Phone book******************\n")

printf("----------------------------------------------\n")

printf(" | | <1>Add a note | |\n")

printf(" | | <2>Show the list | |\n")

printf(" | | <3>Delete a note | |\n")

printf(" | | <4>Insert a note | |\n")

printf(" | | <0>Quit | |\n")

printf("----------------------------------------------\n")

printf(" Enter your choice(0-4):")

gets(s)

c=atoi(s)

}while(c<0||c>4)

system("cls")

switch(c)

{ case 1: p=jianli()break

case 2: xianshi(p)break

case 3: printf("\nPlease input the name to deleted\n")

p=delete(p)break

case 4: printf("\nPlease input the new name\n")

p=charu(p)break

}

}while(c)goto a

case 2:

do

{ do

{printf("----------------------------------------------\n")

printf("******************Phone book******************\n")

printf("----------------------------------------------\n")

printf(" | | <1>Add a note | |\n")

printf(" | | <2>Show the list | |\n")

printf(" | | <3>Delete a note | |\n")

printf(" | | <4>Insert a note | |\n")

printf(" | | <0>Quit | |\n")

printf("----------------------------------------------\n")

printf(" Enter your choice(0-4):")

gets(s)

c=atoi(s)

}while(c<0||c>4)

system("cls")

switch(c)

{ case 1: head=create()break

case 2: prlist(head)break

case 3: printf("\nInput the name to deleted\n")

gets(name)

head=delnode(head,name)break

case 4: stu=NEW

printf("\nInput the new node\n")

printf("name:")

gets(stu->name)

printf("tel:")

gets(stu->tel)

stu->next=NULL

printf("\nInsert position\n")

printf("name:")

gets(name)

head=insert(head,stu,name)

}

}while(c)goto a

}

}

#include "string.h"

struct student *jianli()

{ char c1[10],c2[11]

int i=0

printf("name:")

gets(c1)

while(strlen(c1)!=0)

{ strcpy(a[i].name,c1)

printf("tel:")

gets(c2)

strcpy(a[i].tel,c2)

i++

printf("name:")

gets(c1)

}

return a

}

#include "string.h"

struct student *delete(struct student *p)

{ char c1[10]

int i=0,j,l=0

while(strlen(p[i].name)!=0)

i++

printf("name:")

gets(c1)

for(j=0j<=i+1j++)

if(strcmp(c1,p[j].name)==0)

{p[j]=p[j+1]

l=j}

while(l<i+1)

{p[l]=p[l+1]

l++}

return p

}

#include "string.h"

struct student *charu(struct student *p)

{ char c1[10],c2[11]

int i=0

while(strlen(p[i].name)!=0)

i++

printf("name:")

gets(c1)

strcpy(p[i].name,c1)

printf("tel:")

gets(c2)

strcpy(p[i].tel,c2)

return p

}

#include "string.h"

void xianshi(struct student *p)

{ int i=0

printf("name\ttel\n\n")

while(strlen(p[i].name)!=0)

{ printf("%s\t%s\n",p[i].name,p[i].tel)

i++}

}

#include "stdlib.h"

#include "string.h"

#define NEW (struct node *)malloc(sizeof(struct node))

struct node *create()

{ struct node *h

struct node *p,*q

char name[20]

h=q=NULL

printf("name:")

gets(name)

while(strlen(name)!=0)

{ p=NEW

if(p==NULL)

{ printf("Allocation failure\n")

exit(0)

}

strcpy(p->name,name)

printf("tel:")

gets(p->tel)

p->next=NULL

if(h==NULL)

h=p

else

q->next=p

q=p

printf("name:")

gets(name)

}

return h

}

struct node *insert(struct node *head,struct node *p0,char *x)

{ struct node *p,*q

if(head==NULL)

{ head=p0

p0->next=NULL

}

else

{ p=head

while(strcmp(x,p->name)!=0&&p->next!=NULL)

{ q=pp=q->next}

if(strcmp(x,p->name)==0)

{ if(p==head)

head=p0

else

q->next=p0

p0->next=p

}

else

{ p->next=p0

p0->next=NULL

}

}

return head

}

void prlist(struct node *head)

{ struct node *p

p=head

printf("name\ttel\n\n")

while(p!=NULL)

{ printf("%s\t%s\n",p->name,p->tel)

p=p->next

}

}

struct node *delnode(struct node *head,char *x)

{ struct node *p,*q

if(head==NULL)

{ printf("this is a empty list.")

return head

}

p=head

while(strcmp(x,p->name)!=0&&p->next!=NULL)

{ q=pp=p->next}

if(strcmp(x,p->name)==0)

{ if(p==head)

head=p->next

else

q->next=p->next

free(p)

}

else

printf("Not found.")

return head

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11891801.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存