/* * ContractManager.cc * * Created on: 2010-7-29 * Author: LiuFeng */
#include <stdio.h> #include <stdlib.h> #include <string.h>
#define INPUT_LINK_LENGTH 40 #define BUFSIZE 16 #define DBSIZE 100
typedef struct { int ID; char name[BUFSIZE]; char workPlace[BUFSIZE]; char teleOff[BUFSIZE]; char teleHome[BUFSIZE]; }*ADDRP, ADDRESS;
ADDRESS addrTmp[DBSIZE], adTmp;
int cntAddr = 0; const char * strFmt = "\n%d\t%s\t%s\t%s\t%s\n"; const char * fstrFmt = "%d %s %s %s %s \n";
// ------------------------------------------------------------------------------------------
// Utilities
void inputItem(ADDRP itp) { printf("请输入ID:\n"); scanf("%d", &(itp->ID)); printf("请输入姓名:\n"); scanf("%s", itp->name); printf("请输入工作单位:\n"); scanf("%s", itp->workPlace); printf("请输入办公电话:\n"); scanf("%s", itp->teleOff); printf("请输入家庭电话:\n"); scanf("%s", itp->teleHome); };
void outputItem(ADDRP itemp) { printf(strFmt, itemp->ID, itemp->name, itemp->workPlace, itemp->teleOff,itemp->teleHome); };
void list(ADDRESS addr[], int length) { for (int i = 0; i < length; ++i) outputItem(&addr[i]); }
// ------------------------------------------------------------------------------------------
// Sorted Display
void idsort(ADDRESS addr[]) { for (int i = 0; i < cntAddr; ++i) addrTmp[i] = addr[i];
for (int i = 0; i < cntAddr - 1; ++i) for (int j = i + 1; j < cntAddr; ++j) if (addrTmp[i].ID > addrTmp[j].ID) { adTmp = addrTmp[i]; addrTmp[i] = addrTmp[j]; addrTmp[j] = adTmp; } list(addrTmp, cntAddr); };
void namesort(ADDRESS addr[], int length) { for (int i = 0; i < length; ++i) addrTmp[i] = addr[i];
for (int i = 0; i < length - 1; ++i) for (int j = i + 1; j < length; ++j) if (strcmp(addrTmp[i].name, addrTmp[j].name) > 0) { adTmp = addrTmp[i]; addrTmp[i] = addrTmp[j]; addrTmp[j] = adTmp; } list(addrTmp, length); };
void unitssort(ADDRESS addr[], int length) { for (int i = 0; i < length; ++i) addrTmp[i] = addr[i];
for (int i = 0; i < length - 1; ++i) for (int j = i + 1; j < length; ++j) if (strcmp(addrTmp[i].workPlace, addrTmp[j].workPlace) > 0) { adTmp = addrTmp[i]; addrTmp[i] = addrTmp[j]; addrTmp[j] = adTmp; } list(addrTmp, length); };
// ------------------------------------------------------------------------------------------
// Search
void namesearch(ADDRESS adr[], int length) { char in[BUFSIZE]; printf("请输入姓名:\n"); scanf("%s", in);
for (int i = 0; i < length; ++i) if (strcmp(adr[i].name, in) == 0) outputItem(&adr[i]); };
void unitssearch(ADDRESS adr[], int length) { char in[BUFSIZE]; printf("请输入工作单位:\n"); scanf("%s", in);
for (int i = 0; i < length; ++i) if (strcmp(adr[i].workPlace, in) == 0) outputItem(&adr[i]); };
void officenumbersearch(ADDRESS adr[], int length) { char in[BUFSIZE]; printf("请输入办公电话:\n"); scanf("%s", in);
for (int i = 0; i < length; ++i) if (strcmp(adr[i].teleOff, in) == 0) outputItem(&adr[i]); };
void homenumbersearch(ADDRESS adr[], int length) { char in[BUFSIZE]; printf("请输入家庭电话:\n"); scanf("%s", in);
for (int i = 0; i < length; ++i) if (strcmp(adr[i].teleHome, in) == 0) outputItem(&adr[i]); };
// ------------------------------------------------------------------------------------------
// Delete
int delet(ADDRESS adr[], int length) { int j; printf("要修改第几条记录:\n"); scanf("%d", &j); --j;
for (int i = j; i < length - 1; ++i) adr[i] = adr[i + 1]; return --cntAddr; };
// ------------------------------------------------------------------------------------------
// Modify
void rewrite(ADDRESS adr[]) { int j; printf("要修改第几条记录:\n"); scanf("%d", &j);
inputItem(&adr[j]); };
// ------------------------------------------------------------------------------------------
// Add
int add(ADDRESS adr[], int last) { inputItem(&adr[++cntAddr]); return cntAddr; };
int enter(ADDRESS adr[]) { while (1) { inputItem(&adr[cntAddr++]);
printf("\n输入下一条?Y/N:"); fflush( stdin); char quitChar = getchar(); if ('N' == quitChar || 'n' == quitChar) break; } return cntAddr; };
// ------------------------------------------------------------------------------------------
// file I/O
int load(ADDRESS adr[]) { FILE *fp; if ((fp = fopen("D:\\ConData.dat", "r+")) != NULL) { cntAddr = 0; while (!feof(fp)) { fscanf(fp, fstrFmt, &(adr[cntAddr].ID), adr[cntAddr].name,adr[cntAddr].workPlace, adr[cntAddr].teleOff,adr[cntAddr].teleHome); ++cntAddr; } } fclose(fp); return cntAddr; };
void display(ADDRESS* addr) { list(addr, cntAddr); };
void save(ADDRESS adr[], int length) { FILE *fp = fopen("D:\\ConData.dat", "w+"); for (int i = 0; i < cntAddr; ++i) fprintf(fp, fstrFmt, adr[i].ID, adr[i].name, adr[i].workPlace,adr[i].teleOff, adr[i].teleHome); fclose(fp); };
void copy(void) { char in[BUFSIZE]; FILE *fp, *fc; printf("请输入新文件名(路径中的反斜杠用\\表示)\n"); scanf("%s", in);
if ((fp = fopen("D:\\ConData.dat", "r+")) != NULL) { fc = fopen(in, "w"); while (!feof(fp)) { fscanf(fp, fstrFmt, &(adTmp.ID), adTmp.name, adTmp.workPlace,adTmp.teleOff, adTmp.teleHome); fprintf(fc, fstrFmt, adTmp.ID, adTmp.name, adTmp.workPlace,adTmp.teleOff, adTmp.teleHome); } fclose(fc); } fclose(fp); };
// ------------------------------------------------------------------------------------------
// Menu
void showpaixu(ADDRESS adr[], int length) { int choice; menu1: system("cls"); printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n"); printf(" 1. 全部显示通讯录信息\n\n"); printf(" 2. 按序号显示通讯录信息\n\n"); printf(" 3. 按姓名排序\n\n"); printf(" 4. 按工作单位排序\n\n"); printf(" 0. 返回上一级菜单\n\n"); printf(" ********************************************************************\n"); printf(" 请选择(0~4)\n ");
scanf("%d", &choice);
switch (choice) { case 1: display(adr); system("pause"); break; case 2: idsort(adr); system("pause"); break; case 3: namesort(adr, length); system("pause"); break; case 4: unitssort(adr, length); system("pause"); break; case 0: return; default: printf("输入选项有误,请重新输入(0-4):"); system("pause"); break; } goto menu1; };
void chazhao(ADDRESS adr[], int length) { int choice; menu2: system("cls"); printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n"); printf(" 1. 通过姓名查找\n\n"); printf(" 2. 通过工作单位查找\n\n"); printf(" 3. 通过办公电话查找\n\n"); printf(" 4. 通过家庭电话查找\n\n"); printf(" 0. 返回上一级菜单\n\n"); printf(" ********************************************************************\n"); printf(" 请选择(0~4)\n "); scanf("%d", &choice); switch (choice) { case 1: namesearch(adr, length); system("pause"); break; case 2: unitssearch(adr, length); system("pause"); break; case 3: officenumbersearch(adr, length); system("pause"); break; case 4: homenumbersearch(adr, length); system("pause"); break; case 0: return; default: printf("输入选项有误,请重新输入(0-4):"); system("pause"); break; } goto menu2; };
void biangeng(ADDRESS adr[], int length) { int choice; menu3: system("cls"); printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n"); printf(" 1. 删除记录\n\n"); printf(" 2. 修改记录\n\n"); printf(" 3. 添加记录\n\n"); printf(" 0. 返回上一级菜单\n\n"); printf(" ********************************************************************\n"); printf(" 请选择(0~3)\n "); scanf("%d", &choice); switch (choice) { case 1: length = delet(adr, length); save(adr, length); getchar(); break; case 2: rewrite(adr); save(adr, length); break; case 3: length = add(adr, length); save(adr, length); getchar(); break; case 0: return; default: printf("输入选项有误,请重新输入(0-3):"); system("pause"); break; } goto menu3; };
int wenjian(ADDRESS adr[], int length) { int choice; menu4: system("cls"); printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n"); printf(" 1. 导入通讯录到内存\n\n"); printf(" 2. 把当前文件复制到新文件上\n\n"); printf(" 0. 返回上一级菜单\n\n"); printf(" ********************************************************************\n"); printf(" 请选择(0~2)\n "); scanf("%d", &choice); switch (choice) { case 1: length = load(adr); printf("您已经成功的导入通讯录文件到内存!\n"); system("pause"); break; case 2: copy(); printf("复制成功!\n"); system("pause"); break; case 0: return length; default: printf("输入选项有误,请重新输入(0-2):"); system("pause"); break; } goto menu4; };
void about() { system("color 87"); system("cls"); printf("\n\n\n\n 《通讯录信息管理系统》\n\n\n"); printf(" *****************************************\n\n\n"); printf(" 版权所有(C) 2010-2011 \n\n\n"); printf(" 制作人:\n\n\n"); printf(" *****************************************\n\n\n"); system("pause"); };
int main(void) { int choice; int length = 0; ADDRESS adr[DBSIZE];
length = load(adr);
menu: system("color f9"); system("cls"); printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n"); printf(" 请选择以下通讯录管理功能:\n\n"); printf(" 1. 通讯录信息输入\n\n"); printf(" 2. 通讯录信息浏览及排序\n\n"); printf(" 3. 通讯录信息查询\n\n"); printf(" 4. 通讯录信息更改\n\n"); printf(" 5. 通讯录及文件操作\n\n"); printf(" 6. 关于通讯录信息管理系统\n\n"); printf(" 0. 退出通讯录信息管理系统\n\n"); printf(" ********************************************************************\n"); printf(" 请选择(0~6)\n "); scanf("%d", &choice); switch (choice) { case 1: length += enter(adr); save(adr, length); system("pause"); break; case 2: showpaixu(adr, length); break; case 3: chazhao(adr, length); break; case 4: biangeng(adr, length); break; case 5: length = wenjian(adr, length); break; case 6: about(); break; case 0: return 0; default: printf("输入选项有误,请重新输入(0-6):"); system("pause"); break; } goto menu;
return 0; };
|