C语言链表通讯录

编译器:vc++6.0、 devc++
程序功能:

  1. 添加信息姓名,性别,年龄,电话,地址。 在这里插入图片描述
  2. 删除信息,查找电话号删除。在这里插入图片描述
  3. 查找联系人并打印。在这里插入图片描述
  4. 浏览全部的联系人。在这里插入图片描述
  5. 保存到电脑的文件上,存储位置和文件名称由自己确定。在这里插入图片描述
  6. 修改信息,查找电话号确定联系人。

头文件:

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>// getch();
#include<windows.h>

定义一个结构体
必须有联系人的基本信息、

typedef struct L{//typedef 可以不写
	char name[20];
	int ega;
	char m[2];
	long long hua;
	//char hua[13];
	char dz[50];
	struct L *next;
}laoshi, *plaoshi;

枚举:对应 ‘上’ ‘下’ 的ascll码。
int choose();函数需要这个枚举(函数功能:通过上下控制菜但单的选择)

enum{
	PU=72, //上
	DOWN=80,//下
	ESC=27,//退出
	ENTER=13,//进入
};

直接定义一个char型数组实现菜单
可以方便的设计菜单页的格式

char *pmenu[]={ 
        "*       菜单     *",//      1      
		"******************",//
		"*    添加联系人  *",//1     3     23
		"*    删除联系人  *",
		"*      查找      *",
		"*    修改信息    *",
		"*    浏览信息    *",
		"*      保存      *",//6     8     28
		"******************",//8    9      30      // 数据是Y坐标
};

全局变量全部附上:

CONSOLE_CURSOR_INFO cci;
COORD pos={0,0};
laoshi head;
HANDLE hout;
int by=0;
int a1=23;
int prby =0;
int n=0;
int adby=0;
int by1=0;
plaoshi p1=NULL;

函数功能:

void addshi();//添加联系人,如果输入的联系人已经保存那么会有提示。
void delshi();//删除联系人
void funtion();//查找联系人
void gaishi();//修改联系人的信息
void print();//浏览全部联系人
void creat();//定义初始化链表
void menu();//副--菜单 打印菜单页
int choose();//控制方向  需要枚举
void colorcege(int a);//辅助--改变颜色 被 int choose();调用
void excel();// 表格打印
void pcols(int x);//   void excel()
void pcols1(int x);// void excel_h()
void prmenu(int x,char ch[]);//辅助--打印菜单   void excel_h()
void excel_h();//表格头打印
void stroage(); //保存
void posxy(int x,int y);//定义光标位置

全部代码附上

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>// getch();
#include<windows.h>
#include<string.h>
#define TIMU "        通讯录"
#define TIME   "     2019-11-26"
#define PS "按上下和Enter选择  退出按ESC"
void addshi();
void delshi();
void funtion();
void gaishi();
void print();
void creat();//定义链表
void menu();//副--菜单
int choose();//控制方向
void pcols1(int x);
void colorcege(int a);//辅助--改变颜色
void excel();//信息表框 1)
void pcols(int x);//辅助--信息裱框 
void prmenu(int x,char ch[]);//辅助--打印菜单
void excel_h();//信息表头
void stroage(); //保存
void posxy(int x,int y);
int x1,y1;
typedef struct L{
	char name[20];
	int ega;
	char m[2];
	long long hua;
	//char hua[13];
	char dz[50];
	struct L *next;
}laoshi, *plaoshi;
enum{
	PU=72,
	DOWN=80,
	ESC=27,
	ENTER=13,
};
char *pmenu[]={ 
        "*       菜单     *",//      1      
		"******************",//
		"*    添加联系人  *",//1     3     23
		"*    删除联系人  *",
		"*      查找      *",
		"*    修改信息    *",
		"*    浏览信息    *",
		"*      保存      *",//6     8     28
		"******************",//8    9      30
};
CONSOLE_CURSOR_INFO cci;
COORD pos={0,0};
laoshi head;
HANDLE hout;
int by=0;
int a1=23;
int prby =0;
int n=0;
int adby=0;
int by1=0;
plaoshi p1=NULL;
int main(){
	int xuan,z;  z=123;  //密码
	SetConsoleTitle("                                                                                                    课程设计");
	system("color 5");
	creat();
	system("mode con cols=110 lines=40");
	hout=GetStdHandle(STD_OUTPUT_HANDLE);
	GetConsoleCursorInfo(hout,&cci);
	cci.bVisible=1;
	cci.dwSize=2;
	SetConsoleCursorInfo(hout,&cci);
	if(z!=123){exit(0);}
    system("cls"); 
	while(1)	{
		menu();
	    xuan=choose();
		posxy(56,30);
		system("cls");
		switch(xuan){
		case 1:system("cls");fflush(stdin);	addshi();       system("cls");break;	           
		case 2:system("cls");fflush(stdin);	delshi();       system("cls");fflush(stdin);break;	           
		case 3:	system("cls");fflush(stdin);	funtion();     system("cls");fflush(stdin);break;            
		case 4:	system("cls");fflush(stdin);	 gaishi();      system("cls");fflush(stdin);break;           
		case 5:	system("cls");fflush(stdin);print();getchar(); system("cls");fflush(stdin);break;	            
		case 6:system("cls");fflush(stdin);stroage();     system("cls");fflush(stdin);break;	            
		case 0:exit(0);
		}	
	}
	getchar();
}
void menu(){
	SetConsoleTextAttribute(hout,FOREGROUND_GREEN);
	posxy(56,1);
	printf("%s",TIMU);
	posxy(56,2);
	printf("%s",TIME);
	int i=1;
	while(i<=9)	{
		posxy(56,20+i);
		printf("%s\t",pmenu[i-1]);
		i++;
	}
	SetConsoleTextAttribute(hout,FOREGROUND_RED);
	posxy(56,23);
	printf("%s",pmenu[2]);
	SetConsoleTextAttribute(hout,FOREGROUND_GREEN);
	posxy(56,35);
	printf("%s",PS);
}
int choose(){
	int getinput=0,color=23;
	while(1){
		switch(getinput)	{
		case PU:if(color==23){color=28;}else{color--;}colorcege(color);break;
		case DOWN:if(color==28){color=23;} else{color++;}colorcege(color);break;
		case ENTER:return color-22;break;
		case ESC:return 0;break;
		}
		getinput=getch();
	}
}
void colorcege(int a){
	int i=a-21,j=a1-21;
	if(a!=23){
		SetConsoleTextAttribute(hout,FOREGROUND_GREEN);
		posxy(x1,23);
		printf("%s",pmenu[2]);
	}
    SetConsoleTextAttribute(hout,FOREGROUND_GREEN);
	posxy(56,a1);
	printf("%s",pmenu[j]);
	SetConsoleTextAttribute(hout,FOREGROUND_RED);
	posxy(x1,a);
	printf("%s",pmenu[i]);
	a1=a;
	SetConsoleTextAttribute(hout,FOREGROUND_GREEN);
}
void creat()  {	head.next = NULL;}
void addshi(){
	plaoshi p2=&head;
	if(n==0)  {n++; by=by-4; excel_h();}
	else print();
	plaoshi p= (plaoshi)malloc(sizeof(laoshi));
	by=adby;
	excel();
	posxy(26,adby+11);
	scanf("%s", &p->name,20);
	posxy(33,y1);
	scanf("%d", &p->ega);
	posxy(36,y1);
	scanf("%s", &p->m,2);
	while (p2) {
		posxy(39,y1);
		scanf("%lld", &p->hua);
		p2=head.next;
		if(p2==NULL) break;
		while(p2->hua!=p->hua){
			p2=p2->next; 
			if(p2==NULL) break;
		} 
		if(p2==NULL) break;
		if(p2->hua==p->hua) {	
			posxy(39,adby+10);
			printf("该号码已有");
			posxy(39,adby+11);
			printf("重新输入:");
			posxy(39,adby+12);
			printf("           ");
		}
	}
	posxy(51,adby+11);
	scanf("%s", &p->dz,50);
	printf("\n\n");
	if(!head.next){
		head.next = p;p1=p;p->next = NULL;adby=adby+4;return;
	}
	p1->next=p;	
	p1=p;
	p->next = NULL;
	adby=adby+4;
}
void delshi(){
	long long hua;
	plaoshi p, p2;
	p2 = &head;
	p = head.next;
	posxy(51,11);
	printf("要删除的号码:");
	scanf("%lld", &hua);
	system("cls");
	if (p==NULL)	{ posxy(51,15);printf("无信息");getchar();getchar();return;}
	while (p->hua != hua && p->next != NULL)	{
		p2 = p;
		p = p->next;
	}
	if (p->hua == hua )	{
		p2->next = p->next;
		posxy(51,21);
		printf("删除完成");
		adby=adby-4;prby=prby-4;
	}
	else	{
		posxy(51,15);
		printf("没有这个号码 ");
	}
	getchar();getchar();
}
void funtion(){
	long long hua;
	plaoshi p;
	p = head.next;
	posxy(51,11);
	printf("要搜索的号码:");
	scanf("%lld", &hua);
	system("cls");
	if (p==NULL)	{ 
		posxy(51,15);
		printf("无信息");
		getchar();getchar();
		return;
	}
	while (p->hua != hua && p->next != NULL)	{
		p = p->next;
	}
	if (p->hua == hua )	{
		by=by-4;
	    excel_h();
		by=0;
		excel();
		posxy(26,11); //6
		printf("%s\n", p->name);
		posxy(33,11);//3
		printf("%d\n", p->ega);
		posxy(36,11); //2
		printf("%s\n", p->m);
		posxy(39,11);//11
		printf("%lld\n", p->hua);
		posxy(51,11);
		printf("%s\n", p->dz);
		posxy(51,21);
		printf("搜索完成");
		getchar();getchar();
	}
	else	{
		posxy(51,15);
		printf("没有这个号码");
		getchar();getchar();
	}
}
void gaishi(){
	int  long hua;
	posxy(51,11);
	printf("要修改的号码:");
	scanf("%lld", &hua);
	system("cls");
	plaoshi p;
	p = head.next;
	if (p==NULL)	{ posxy(51,15);printf("无信息");getchar();getchar();return;}
	while (p->hua != hua && p->next != NULL )	{
		p = p->next;
	}
	system("cls");
	if (p->hua == hua )	{
		by=by-4;
		excel_h();
		by=0;
		excel();
		posxy(26,11);
		scanf("%s", &p->name);
		posxy(33,y1);
		scanf("%d", &p->ega);
		posxy(36,y1);
		scanf("%s", &p->m);
		posxy(39,y1);
		scanf("%lld", &p->hua);
		posxy(51,y1);
		scanf("%s", &p->dz);
		printf("修改成功");
		getchar();getchar();
	}
	else	{
		posxy(51,11);
		printf("没有这个号码");
		getchar();getchar();
	}
}
void print(){
	prby=0;
	plaoshi p;
	p = head.next;
	by=by-4;
	excel_h();
	if (p==NULL)	{ posxy(51,15);printf("无信息"); return;}
	while (p!=NULL) {
		by=prby;
		excel();
		posxy(26,11+prby);
		printf("%s\n", p->name);
		posxy(33,y1);
		printf("%d\n", p->ega);
		posxy(36,y1);
		printf("%s\n", p->m);
		posxy(39,y1);
		printf("%lld\n", p->hua);
		posxy(51,y1);
		printf("%s\n", p->dz);
		p = p->next;
		prby=prby+4;
	}
}
void pcols1(int x)
{
	int i;
	pos.X=x;
	for(i=6;i<9;i++)	{
		posxy(x,i+by1);
		printf("*");
	}
}
void pcols(int x) {
	int i;
	pos.X=x;
	for(i=10;i<13;i++)	{
		posxy(x,i+by);
		printf("*");
	}
}
void prmenu(int x,char ch[]){	posxy(x,7+by1); printf(ch); }
void excel(){
	int i; 
	pos.Y=13+by;
	for( i=25;i<=101;i++)	{
		posxy(i,13+by);
		printf("*");
	}
	pos.X=25;
	for(i=10;i<13;i++)	{
		posxy(25,i+by);
		printf("*");
	}    
	pcols(25);     pcols(32);      pcols(35);     pcols(38);     pcols(50);     pcols(101);     
}
void excel_h(){
	for(int p=0;p<2;p++){
		pos.Y=5+by1+p*4;
		for(int i=25;i<=101;i++){
			posxy(i,5+by1+p*4);
			printf("*");
		}
	}
	pos.X=25;
	for(int i=6;i<9;i++){
		posxy(25,i+by1);
		printf("*");
	}
	prmenu(26,"名字");prmenu(33,"年");prmenu(36,"性");prmenu(39,"电话");prmenu(51,"地址");
	posxy(33,8+by1);
	printf("龄");
	posxy(36,y1);
	printf("别");    
	             pcols1(32);      pcols1(35);     pcols1(38);     pcols1(50);    pcols1(101); 
}
void stroage(){
	char a[20];
	char b[20]={'\n'};
	char aa[9]={"    "};
	char c[9][7]={{"姓名:"},{"年龄:"},{"性别:"},{"电话:"},{"地址:"}};
	plaoshi p=head.next;
	if (p==NULL)	{ posxy(51,15);printf("无信息保存"); getchar();return;}
	FILE *pf; 
	int i=0;
	posxy(56,19);
	printf("输入文件名(带后缀名 如:H:\\XXX.TXT):") ;
	scanf("%s",&a);
	getchar();
	pf=fopen(a,"a");
	while(p){
		fputs(c[0],pf);fputs(p->name,pf);	fputs(aa,pf);
		fputs(c[1],pf);fprintf(pf,"%d",p->ega);fputs(aa,pf);
		fputs(c[2],pf);fputs(p->m,pf);fputs(aa,pf);
		fputs(c[3],pf);fprintf(pf,"%lld",p->hua);fputs(aa,pf);
		fputs(c[4],pf);fputs(p->dz,pf);fputs(aa,pf);
		p=p->next;
		fputs(b,pf);
	}
	posxy(58,20);
	printf("保存成功");
	getchar();
}
void posxy(int x,int y){
	x1=x;y1=y;
	pos.X=x1; 
	pos.Y=y1;
	SetConsoleCursorPosition(hout,pos);
}

菜单
用上下控制。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值