回复 5楼 陆小果
简单改编了一下之前一位同学的代码,作为示例参考。
程序代码:
/*
数据表
*/
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
typedef struct _TABLE_
{
unsigned int dwID; //ID
char szNumber[10]; //编号
char szName[30]; //姓名
char szGender[10]; //性别
char Age; //年龄
char szLevel[10]; //级别
double qwScore; //评分
} TABLE, *PTABLE;
typedef struct _TABLE_LIST_
{
TABLE stTable;
struct _TABLE_LIST_ *lpPrior, *lpNext;
} TABLELIST, *PTABLELIST;
PTABLELIST lpTop = NULL; //表首指针
PTABLELIST lpBottom = NULL; //表尾指针
PTABLELIST lpRecord = NULL; //表记录指针
const char szFileName[260] = {"c:\\TEMP\\Test.db\0"};
PTABLELIST _Append(void);
PTABLELIST _Insert(void);
PTABLELIST _Delete(void);
int _NewID(void);
void _Free(void);
void _ListHeader(void);
void _ListRecord(PTABLELIST p);
void _List(void);
void _Edit(PTABLELIST p);
void _Input(void);
bool _Open(const char *lpFileName);
bool _Save(const char *lpFileName);
void _Locate(void);
/* 添加记录 */
PTABLELIST _Append(void)
{
PTABLELIST p = (PTABLELIST)malloc(sizeof(TABLELIST));
if (p != NULL)
{
memset(p, 0, sizeof(TABLELIST));
p->lpNext = NULL;
if (lpTop != NULL)
{
lpBottom->lpNext = p;
p->lpPrior = lpBottom;
p->stTable.dwID = _NewID();
}
else
{
p->lpPrior = NULL;
p->stTable.dwID = 1;
lpTop = p;
}
lpBottom = p;
lpRecord = p;
return p;
}
return NULL;
}
/* 插入记录 */
PTABLELIST _Insert(void)
{
PTABLELIST p = (PTABLELIST)malloc(sizeof(TABLELIST));
if (p != NULL)
{
memset(p, 0, sizeof(TABLELIST));
if (lpRecord != NULL)
{
p->lpPrior = lpRecord;
p->lpNext = lpRecord->lpNext;
p->stTable.dwID = _NewID();
if (lpRecord != lpBottom)
{
lpRecord->lpNext->lpPrior = p;
}
else
{
lpBottom = p;
}
lpRecord->lpNext = p;
}
else
{
p->lpPrior = NULL;
p->lpNext = NULL;
p->stTable.dwID = 1;
lpTop = p;
lpBottom = p;
}
lpRecord = p;
return p;
}
return NULL;
}
int _NewID(void)
{
int dwID = 0;
PTABLELIST p;
p = lpTop;
while (p != NULL)
{
if (p->stTable.dwID > dwID)
{
dwID = p->stTable.dwID;
}
p = p->lpNext;
}
return ++dwID;
}
/* 删除记录 */
PTABLELIST _Delete(void)
{
if (lpRecord != NULL)
{
PTABLELIST p;
if ((lpRecord != lpTop) && (lpRecord != lpBottom))
{
p = lpRecord->lpNext;
p->lpPrior = lpRecord->lpPrior;
p->lpPrior->lpNext = p;
}
else if ((lpRecord != lpTop) && (lpRecord == lpBottom))
{
p = lpRecord->lpPrior;
p->lpNext = NULL;
lpBottom = p;
}
else if ((lpRecord == lpTop) && (lpRecord != lpBottom))
{
p = lpRecord->lpNext;
p->lpPrior = NULL;
lpTop = p;
}
else
{
p = NULL;
lpTop = NULL;
lpBottom = NULL;
}
free(lpRecord);
lpRecord = p;
}
return lpRecord;
}
/* 释放所有分配的内存空间 */
void _Free(void)
{
PTABLELIST p;
lpRecord = lpTop;
while (lpRecord != NULL)
{
p = lpRecord->lpNext;
free(lpRecord);
lpRecord = p;
}
lpTop = NULL;
lpBottom = NULL;
lpRecord = NULL;
}
/* 列表头 */
void _ListHeader(void)
{
printf("\nID 编号 姓名 性别 年龄 级别 评分\n");
}
/* 列表记录 */
void _ListRecord(PTABLELIST p)
{
if (p != NULL)
{
printf("%-10u %-10s %-10s %-10s %-10d %-10s %-10.2lf\n",
p->stTable.dwID,
p->stTable.szNumber,
p->stTable.szName,
p->stTable.szGender,
p->stTable.Age,
p->stTable.szLevel,
p->stTable.qwScore);
}
}
/* 列表 */
void _List(void)
{
_ListHeader();
char ch;
lpRecord = lpTop;
while (lpRecord != NULL)
{
_ListRecord(lpRecord);
lpRecord = lpRecord->lpNext;
}
lpRecord = lpBottom;
_ListHeader();
_ListRecord(lpRecord);
do
{
printf("选择:1上记录,2下记录,3修改,4插入,5删除,6查询,0退出:");
ch = ' ';
while (ch<'0' || ch>'6')
{
fflush(stdin);
ch = getchar();
}
switch(ch)
{
case '1':
if (lpRecord != lpTop) lpRecord = lpRecord->lpPrior;
break;
case '2':
if (lpRecord != lpBottom) lpRecord = lpRecord->lpNext;
break;
case '3':
_Edit(lpRecord);
break;
case '4':
_Insert();
_Edit(lpRecord);
break;
case '5':
_Delete();
break;
case '6':
_Locate();
break;
case '0':
break;
default :
break;
}
if (ch != '0')
{
_ListHeader();
_ListRecord(lpRecord);
}
}
while (ch != '0');
}
/* 编辑数据 */
void _Edit(PTABLELIST p)
{
if (p != NULL)
{
fflush(stdin);
printf("请输入员工的信息(ID:%u)\n", p->stTable.dwID);
printf("编号:");
scanf("%s", p->stTable.szNumber);
printf("姓名:");
scanf("%s", p->stTable.szName);
printf("性别:");
scanf("%s", p->stTable.szGender);
printf("年龄:");
scanf("%d", &p->stTable.Age);
printf("级别:");
scanf("%s", p->stTable.szLevel);
printf("评分:");
scanf("%lf",&p->stTable.qwScore);
}
}
/* 录入数据 */
void _Input(void)
{
char ch;
PTABLELIST p;
do
{
p = _Append();
_Edit(p);
printf("是否继续录入?(Y/N):");
fflush(stdin);
ch = getchar();
}
while (ch!='N' && ch!='n');
}
/* 打开表 */
bool _Open(const char *lpFileName)
{
if (access(lpFileName, 0) != 0)
{
return false;
}
FILE *fp;
if ((fp=fopen(lpFileName, "r")) == NULL)
{
return false;
}
TABLE stTable;
PTABLELIST p;
fread(&stTable, sizeof(TABLE), 1, fp);
while (feof(fp) == 0)
{
p = _Append();
if (p != NULL)
{
memcpy(p, &stTable, sizeof(TABLE));
}
fread(&stTable, sizeof(TABLE), 1, fp);
}
fclose(fp);
return true;
}
/* 保存表 */
bool _Save(const char *lpFileName)
{
FILE *fp;
if ((fp=fopen(lpFileName, "w")) == NULL)
{
return false;
}
lpRecord = lpTop;
while (lpRecord != NULL)
{
if (fwrite(lpRecord, sizeof(TABLE), 1, fp) != 1)
{
fclose(fp);
return false;
}
lpRecord = lpRecord->lpNext;
}
fclose(fp);
_Free();
return true;
}
/* 查询数据 */
void _Locate(void)
{
char szName[30];
fflush(stdin);
printf("输入查找的姓名:");
gets(szName);
lpRecord = lpTop;
while (lpRecord != NULL)
{
if (strcmp(lpRecord->stTable.szName, szName) == 0)
{
break;
}
lpRecord = lpRecord->lpNext;
}
if (lpRecord == NULL)
{
printf("\n无符合条件的记录\n");
lpRecord = lpBottom;
}
}
/* 菜单 */
int menu(void)
{
char ch;
do
{
system("cls");
printf("\t\t********员工管理系统 **************\n");
printf("\t\t 1.录入数据 \n");
printf("\t\t 2.显示数据 \n");
printf("\t\t 0.退出系统 \n");
printf("\t\t***********************************\n");
printf("\t\t选择(0-2):");
fflush(stdin);
ch = getchar();
}
while (ch<'0' || ch>'2');
return(ch - '0');
}
int main(void)
{
_Open(szFileName);
while (1)
{
switch(menu())
{
case 1:
_Input();
break;
case 2:
_List();
break;
case 0:
_Save(szFileName);
goto _EXIT;
default :
break;
}
}
_EXIT:
return 0;
}