代码如下:
#include
#include
#include
struct Worker
{
int id;
char name[16];
char sex[8];
int age;
char edu_exp[32];
int wage;
char adress[32];
char phong[16];
Worker* next;
};
Worker m_head = { 0 };
int user_input(Worker* w)
{
printf("ID:");
scanf("%d", &w->id);
getchar();
printf("name:");
gets_s(w->name);
printf("sex:");
gets_s(w->sex);
printf("age:");
scanf("%d", &w->age);
getchar();
printf("edu_exp:");
gets_s(w->edu_exp);
printf("wage:");
scanf("%d", &w->wage);
getchar();
printf("adress:");
gets_s(w->adress);
printf("phone:");
gets_s(w->phong);
return 0;
}
void add(Worker* w)
{
w->next = m_head.next;
m_head.next = w;
}
int insert(Worker* w)
{
Worker* cur = m_head.next;
Worker* pre = &m_head;
while (cur)
{
if (w->id id)
break;
pre = cur;
cur = cur->next;
}
w->next = pre->next;
pre->next = w;
return 0;
}
int remove(int id)
{
Worker* cur = m_head.next;
Worker* pre = &m_head;
while (cur)
{
if (id == cur->id)
{
pre->next = cur->next;
free(cur);
return 1;
}
pre = cur;
cur = cur->next;
}
return 0;
}
int find(int id)
{
Worker* p = m_head.next;
while (p)
{
if (id == p->id)
{
printf("ID:%d\nName:%s\nSex:%s\nAge:%d\nEdu-exp:%s\nWage:%d\nAdress:%s\nPhone:%s\n",
p->id, p->name, p->sex, p->age, p->edu_exp, p->wage, p->adress, p->phong);
return 1;
}
p = p->next;
}
return 0;
}
void show_all()
{
Worker* p = m_head.next;
if (!p)
{
printf("暂无员工信息!\n");
return;
}
printf("工 号 姓 名 性 别 年 龄 学 历 工 资 住 址 电 话\n");
printf("----------------------------------------------------------------\n");
while (p)
{
printf("%d %s %s %d %s %d %s %s\n", p->id, p->name, p->sex, p->age, p->edu_exp,
p->wage, p->adress, p->phong);
p = p->next;
}
}
int save()
{
const char* filename = "F:\\data.txt";
FILE* fp = fopen(filename, "a");
if (fp == NULL)
{
printf("can not open the file....");
exit(0);
}
Worker* p = m_head.next;
while (p)
{
fprintf(fp, "\r");
fprintf(fp, "%d ", p->id);
fprintf(fp, "%s ", p->name);
fprintf(fp, "%s ", p->sex);
fprintf(fp, "%d ", p->age);
fprintf(fp, "%s ", p->edu_exp);
fprintf(fp, "%d ", p->wage);
fprintf(fp, "%s ", p->adress);
fprintf(fp, "%s ", p->phong);
fprintf(fp, "\t\r\n");
p = p->next;
}
fclose(fp);
return 0;
}
void rewrite(int id)
{
Worker* p = m_head.next;
while (p)
{
if (id == p->id)
{
user_input(p);
}
p = p->next;
}
}
void List()
{
Worker* w = NULL;
char key;
while (1)
{
do
{
system("cls");
printf("*******************************************************\n");
printf("* 主菜单操作界面 *\n");
printf("* *\n");
printf("* 1.录入职工信息 *\n");
printf("* 2.浏览职工信息 *\n");
printf("* 3.查询职工信息 *\n");
printf("* 4.删除职工信息 *\n");
printf("* 5.修改职工信息 *\n");
printf("* 6.使用说明 *\n");
printf("* 7.退出 *\n");
printf("* *\n");
printf("*******************************************************\n");
key = getchar();
} while (key != '1'&&key != '2'&&key != '3'&&key != '4'
&&key != '5'&&key != '6'&&key != '7');
switch (key)
{
case '1':system("cls");
while (1)
{
Worker* w = (Worker*)malloc(sizeof(Worker));
user_input(w);
insert(w);
if (getchar() == 'N')
{
break;
}
}
save();
free(w);
break;
case '2':system("cls");
show_all();
system("pause");
break;
case '3':system("cls");
if (!m_head.next)
{
printf("暂无员工信息!\n");
system("pause");
break;
}
while (1)
{
int n;
printf("---------------------------------\n");
printf("请输入要查找的ID号:");
scanf("%d", &n);
if (!find(n))
{
printf("找不到ID号为%d 的职工信息...\n", n);
}
char ch = getchar();
if (ch == 'N')
{
break;
}
}
break;
case '4':system("cls");
if (!m_head.next)
{
printf("暂无员工信息!\n");
system("pause");
break;
}
while (1)
{
int n;
printf("---------------------------------\n");
printf("请输入要删除的ID号:");
scanf("%d", &n);
if (remove(n))
{
printf("成功删除ID号为%d的职工信息.\n", n);
}
else
printf("无法删除ID号为%d的职工信息.\n", n);
char ch = getchar();
if (ch == 'N')
{
break;
}
}
break;
case '5':system("cls");
if (!m_head.next)
{
printf("暂无员工信息!\n");
system("pause");
break;
}
while (1)
{
int n;
printf("请输入要修改的ID号:\n");
scanf("%d", &n);
printf("请修改:\n");
rewrite(n);
if (getchar() == 'N')
{
break;
}
}
break;
case '6':system("cls");
printf("职工系统使用说明:摁相应数字进入相应的子菜单,若想退出则摁N\n开始无数据,需要先进行录入.\n");
system("pause");
break;
case '7':system("cls");
return;
break;
}
}
}
int main()
{
List();
return 0;
}
这是一个简单的小例子,主要是给大家一些思路,我这里只是简单的写了保存输入的数据到文件中,你也可用写一个从文件中读取数据到控制台,并且可以通过链表进行修改.这就麻烦一点,但是思路很简单.有兴趣的同学可以do better.