本来我自己参加ACM集训,可以将短学期的两个课程替换掉,不需要写程序设计实训的作业。但是因为妹子要学语言,我就花了两个半小时写了一份。用标准C写的,约500行代码,贴出来供大家互相学习交流。

第八组:通讯录管理系统 (3人) 

通信录信息:名字,出生年月,班级,学校,工作单位,住宅电话,手机,家庭住址,QQ,email等

需要完成的主要功能:

1. 添加通信录信息

2. 更新通信录信息(修改,删除,添加)

3. 查询通信录信息(按姓名,按班级,按工作单位等)

4. 通信录信息的相关统计:年龄的分布情况等

特别声明

1、如果发现文中任何错误请在下方评论,我会及时修改。

2、转载本文请带上版权,建议直接将本站或者本文链接推荐给朋友,以免错过更新。

3、代码仅供参考,禁止照搬应付作业。

代码

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <time.h>
#include <windows.h>
#define lname 20
#define lschool 50
#define lwork 50
#define lnumber 20
#define lphone 20
#define lhome 50
#define lqq 20
#define lemail 30

struct student {
	char iname[lname];
	int iyear, imonth, iday;
	int iclass;
	char ischool[lschool];
	char iwork[lwork];
	char inumber[lnumber];
	char iphone[lphone];
	char ihome[lhome];
	char iqq[lqq];
	char iemail[lemail];
	student *pre, *next;
	student() {
		pre = next = NULL;
	}
	~student() {
		free(pre);
		free(next);
	}
};

int num, querynum;
student *f, *q;

void addInfomation();
void deleteInfomation(int);
void queryInfomation(int);
void censusInfomation();
void mainMenu();
void addMenu();
void deleteMenu();
void queryMenu();
void censusMenu();
void getData();
void setData();
void iPrint(int, student*);
int calculateAge(student*);
void Init();

int main() {
	Init();
	getData();
	mainMenu();
	setData();
	return 0;
}

void Init() {
	system("title 通讯录管理系统 @haipz.com");
	system("mode con lines=32 cols=82");
	system("color 1B");
	return;
}

void iPrint(int i, student *t) {
	printf(" NO. %d\n", i);
	printf(" Name: %-20s Year: %-5d Month: %-2d Day: %-2d Class: %-2d\n Number: %-18s Phone: %-20s\n QQ: %-22s Email: %-20s\n School: %-50s\n Home: %-50s\n Work: %-50s\n ********************************************************************************\n",
	t->iname, t->iyear, t->imonth, t->iday, t->iclass, t->inumber, t->iphone, t->iqq, t->iemail, t->ischool, t->ihome, t->iwork);
	return;
}

void addInfomation() {
	printf(" ********************************************************************************\n");
	student *t = (student*)malloc(sizeof(student));
	printf(" Name: ");
	scanf("%s", t->iname);
	printf(" Year: ");
	scanf("%d", &t->iyear);
	printf(" Month: ");
	scanf("%d", &t->imonth);
	printf(" Day: ");
	scanf("%d", &t->iday);
	printf(" Class: ");
	scanf("%d", &t->iclass);
	printf(" School: ");
	scanf("%s", t->ischool);
	printf(" Number: ");
	scanf("%s", t->inumber);
	printf(" Work: ");
	scanf("%s", t->iwork);
	printf(" Phone: ");
	scanf("%s", t->iphone);
	printf(" Home: ");
	scanf("%s", t->ihome);
	printf(" QQ: ");
	scanf("%s", t->iqq);
	printf(" Email: ");
	scanf("%s", t->iemail);
	t->next = f->next;
	f->next = t;
	++num;
	setData();
	getData();
	printf(" Success!\n");
	system("pause");
	return;
}

void deleteInfomation(int comid) {
	student *p = q;
	for (int i = 0; i < comid; ++i) p = p->next;
	p->pre->next = p->next;
	p->next->pre = p->pre;
	--num;
	setData();
	getData();
	return;
}

void queryInfomation(int comid) {
	printf(" ********************************************************************************\n");
	student* p = q;
	querynum = 0;
	switch(comid) {
		case 1: {
			char tname[lname];
			printf(" Enter Name: ");
			scanf("%s", tname);
			student *t = f;
			for (int i = 0; i < num; ++i) {
				t = t->next;
				if (strcmp(t->iname, tname) == 0) {
					p->next = t;
					p = p->next;
					querynum++;
					iPrint(querynum, t);
				}
			}
			break;
		}
		case 2: {
			int tyear, tmonth, tday;
			printf(" Enter Year: ");
			scanf("%d", &tyear);
			printf(" Enter Month: ");
			scanf("%d", &tmonth);
			printf(" Enter Day: ");
			scanf("%d", &tday);
			student *t = f;
			for (int i = 0; i < num; ++i) {
				t = t->next;
				if (t->iyear == tyear && t->imonth == tmonth && t->iday == tday) {
					p->next = t;
					p = p->next;
					querynum++;
					iPrint(querynum, t);
				}
			}
			break;
		}
		case 3: {
			int tclass;
			printf(" Enter Class: ");
			scanf("%d", &tclass);
			student *t = f;
			for (int i = 0; i < num; ++i) {
				t = t->next;
				if (t->iclass == tclass) {
					p->next = t;
					p = p->next;
					querynum++;
					iPrint(querynum, t);
				}
			}
			break;
		}
		case 4: {
			char tschool[lschool];
			printf(" Enter School: ");
			scanf("%s", tschool);
			student *t = f;
			for (int i = 0; i < num; ++i) {
				t = t->next;
				if (strcmp(t->ischool, tschool) == 0) {
					p->next = t;
					p = p->next;
					querynum++;
					iPrint(querynum, t);
				}
			}
			break;
		}
		case 5: {
			char twork[lwork];
			printf(" Enter Work: ");
			scanf("%s", twork);
			student *t = f;
			for (int i = 0; i < num; ++i) {
				t = t->next;
				if (strcmp(t->iwork, twork) == 0) {
					p->next = t;
					p = p->next;
					querynum++;
					iPrint(querynum, t);
				}
			}
			break;
		}
		case 6: {
			char tnumber[lnumber];
			printf(" Enter Number: ");
			scanf("%s", tnumber);
			student *t = f;
			for (int i = 0; i < num; ++i) {
				t = t->next;
				if (strcmp(t->inumber, tnumber) == 0) {
					p->next = t;
					p = p->next;
					querynum++;
					iPrint(querynum, t);
				}
			}
			break;
		}
		case 7: {
			char tphone[lphone];
			printf(" Enter Phone: ");
			scanf("%s", tphone);
			student *t = f;
			for (int i = 0; i < num; ++i) {
				t = t->next;
				if (strcmp(t->iphone, tphone) == 0) {
					p->next = t;
					p = p->next;
					querynum++;
					iPrint(querynum, t);
				}
			}
			break;
		}
		case 8: {
			char thome[lhome];
			printf(" Enter Home: ");
			scanf("%s", thome);
			student *t = f;
			for (int i = 0; i < num; ++i) {
				t = t->next;
				if (strcmp(t->ihome, thome) == 0) {
					p->next = t;
					p = p->next;
					querynum++;
					iPrint(querynum, t);
				}
			}
			break;
		}
		case 9: {
			char tqq[lqq];
			printf(" Enter QQ: ");
			scanf("%s", tqq);
			student *t = f;
			for (int i = 0; i < num; ++i) {
				t = t->next;
				if (strcmp(t->iqq, tqq) == 0) {
					p->next = t;
					p = p->next;
					querynum++;
					iPrint(querynum, t);
				}
			}
			break;
		}
		case 10: {
			char temail[lemail];
			printf(" Enter Email: ");
			scanf("%s", temail);
			student *t = f;
			for (int i = 0; i < num; ++i) {
				t = t->next;
				if (strcmp(t->iemail, temail) == 0) {
					p->next = t;
					p = p->next;
					querynum++;
					iPrint(querynum, t);
				}
			}
			break;
		}
		default: break;
	}
	if (querynum) system("pause");
	else {
		printf(" Nothing Found!\n");
		system("pause");
	}
	return;
}

int calculateAge(student *t) {
	time_t nowtime;
	struct tm *timeinfo;
	time(&nowtime);
	timeinfo = localtime(&nowtime);
	int tyear = timeinfo->tm_year + 1900;
	int tmonth = timeinfo->tm_mon + 1;
	int tday = timeinfo->tm_mday;
	int res = tyear - t->iyear;
	if (tmonth > t->imonth) --res;
	else if (tmonth == t->imonth && tday > t->iday) --res;
	return res;
}

void censusInfomation() {
	printf(" ********************************************************************************\n");
	printf(" Total: %d\n", num);
	int sum[10] = {0};
	student *t = f;
	for (int i = 0; i < num; ++i) {
		t = t->next;
		int x = calculateAge(t)/10;
		if (x >= 0 && x < 10) ++sum[x];
	}
	for (int i = 0; i < 10; ++i)
		printf(" Age %d - %d: %d\n", i*10, (i + 1)*10 - 1, sum[i]);
	printf(" ********************************************************************************\n");
	system("pause");
	return;
}

void mainMenu() {
	while(true) {
		system("cls");
		printf(" ********************************************************************************\n");
		printf(" *                          Contacts Management System                          *\n");
		printf(" * 0 Quit                                                                       *\n");
		printf(" * 1 Add                                                                        *\n");
		printf(" * 2 Delete                                                                     *\n");
		printf(" * 3 Query                                                                      *\n");
		printf(" * 4 Census                                                                     *\n");
		printf(" ********************************************************************************\n");
		printf(" Enter Command: ");
		int comid;
		scanf("%d", &comid);
		switch(comid) {
			case 0: return;
			case 1: addMenu(); break;
			case 2: deleteMenu(); break;
			case 3: queryMenu(); break;
			case 4: censusMenu(); break;
			default: break;
		}
	}
	return;
}

void addMenu() {
	while(true) {
		system("cls");
		printf(" ********************************************************************************\n");
		printf(" *                          Contacts Management System                          *\n");
		printf(" * 0 Return                                                                     *\n");
		printf(" * 1 Add                                                                        *\n");
		printf(" ********************************************************************************\n");
		printf(" Enter Command: ");
		int comid;
		scanf("%d", &comid);
		switch(comid) {
			case 0: return;
			case 1: addInfomation(); break;
			default: break;
		}
	}
	return;
}

void deleteMenu() {
	while(true) {
		system("cls");
		printf(" ********************************************************************************\n");
		printf(" *                          Contacts Management System                          *\n");
		printf(" * 0 Return                                                                     *\n");
		printf(" * 1 Delete By Name                                                             *\n");
		printf(" * 2 Delete By Birthday                                                         *\n");
		printf(" * 3 Delete By Class                                                            *\n");
		printf(" * 4 Delete By School                                                           *\n");
		printf(" * 5 Delete By Work                                                             *\n");
		printf(" * 6 Delete By Number                                                           *\n");
		printf(" * 7 Delete By Phone                                                            *\n");
		printf(" * 8 Delete By Home                                                             *\n");
		printf(" * 9 Delete By QQ                                                               *\n");
		printf(" * 10 Delete By Email                                                           *\n");
		printf(" ********************************************************************************\n");
		printf(" Enter Command: ");
		int comid;
		scanf("%d", &comid);
		if (comid == 0) return;
		else if (comid <= 10) {
			queryInfomation(comid);
			if (querynum != 0) {
				printf(" Enter Id: ");
				scanf("%d", &comid);
				deleteInfomation(comid);
				printf(" Success!\n");
				system("pause");
			}
		}
	}
	return;
}

void queryMenu() {
	while(true) {
		system("cls");
		printf(" ********************************************************************************\n");
		printf(" *                          Contacts Management System                          *\n");
		printf(" * 0 Return                                                                     *\n");
		printf(" * 1 Query Name                                                                 *\n");
		printf(" * 2 Query Birthday                                                             *\n");
		printf(" * 3 Query Class                                                                *\n");
		printf(" * 4 Query School                                                               *\n");
		printf(" * 5 Query Work                                                                 *\n");
		printf(" * 6 Query Number                                                               *\n");
		printf(" * 7 Query Phone                                                                *\n");
		printf(" * 8 Query Home                                                                 *\n");
		printf(" * 9 Query QQ                                                                   *\n");
		printf(" * 10 Query Email                                                               *\n");
		printf(" ********************************************************************************\n");
		printf(" Enter Command: ");
		int comid;
		scanf("%d", &comid);
		if (comid == 0) return;
		else if (comid <= 10) queryInfomation(comid);
	}
	return;
}

void censusMenu() {
	while(true) {
		system("cls");
		printf(" ********************************************************************************\n");
		printf(" *                          Contacts Management System                          *\n");
		printf(" * 0 Return                                                                     *\n");
		printf(" * 1 Census                                                                     *\n");
		printf(" ********************************************************************************\n");
		printf(" Enter Command: ");
		int comid;
		scanf("%d", &comid);
		switch(comid) {
			case 0: return;
			case 1: censusInfomation(); break;
			default: break;
		}
	}
	return;
}

void getData() {
	FILE *fp = fopen("contact.txt", "r");
	fscanf(fp, "%d", &num);
	f = (student*)malloc(sizeof(student));
	q = (student*)malloc(sizeof(student));
	student *t = f;
	for (int i = 0; i < num; ++i) {
		t->next = (student*)malloc(sizeof(student));
		t->next->pre = t;
		t = t->next;
		fscanf(fp, "%s%d%d%d%d%s%s%s%s%s%s%s", t->iname, &t->iyear, &t->imonth, &t->iday, &t->iclass, t->ischool, t->iwork, t->inumber, t->iphone, t->ihome, t->iqq, t->iemail);
	}
	fclose(fp);
	return;
}

void setData() {
	FILE *fp = fopen("contact.txt", "w");
	fprintf(fp, "%d\n", num);
	student *t = f;
	for (int i = 0; i < num; ++i) {
		t = t->next;
		fprintf(fp, "%s %d %d %d %d %s %s %s %s %s %s %s\n", t->iname, t->iyear, t->imonth, t->iday, t->iclass, t->ischool, t->iwork, t->inumber, t->iphone, t->ihome, t->iqq, t->iemail);
	}
	fclose(fp);
	return;
}
转载保留版权:http://haipz.com/blog/i/6068 - 海胖博客