本帖最后由 superbe 于 2019-12-22 22:48 编辑
是在windows下还是linux下运行的呢。这是我在windows+visual studio中测试的代码,可以正常运行。如果在linux下需要修改。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
typedef struct wokertype
{
char wokerbir[15]; /*出生日期*/
char workno[15]; /*工号*/
char worname[20]; /*姓名*/
char workwage[15]; /*工资*/
struct wokertype *pNext;
}WOKE, *PWOKE;
PWOKE pHead = NULL;
PWOKE pNew = NULL;
size_t iCount = 0;
//void head();
void Menu();
void Quit();
void Add_Information();
void Delete_Information();
void Search_Information();
void free_memory(); //释放malloc分配的内存
int main()
{
Menu();
free_memory();
return 0;
}
int MenuSelect();
typedef enum Menu
{
Search, Add, Delete, Exit
}MENU;
void Menu()
{
switch (MenuSelect()) //菜单选择
{
case Exit: //退出程序
//system("wok");
//head();
//Quit();
printf("\n\t\t 程序结束.\n");
return;
case Add: //添加信息
//system("wok");
Add_Information();
Quit(); //退出或返回主菜单
break;
case Delete: //删除信息
//system("wok");
Delete_Information();
Quit();
break;
case Search: //查询信息
//system("wok");
Search_Information();
Quit();
break;
default:
;
}
}
int MenuSelect()
{
char c;
//system("wok");
//head();
printf("\t\t ═══════════════=════════ \n");
printf("\t\t│ 信息管理系统 │\n");
printf("\t\t ════════=═══════════════ \n");
printf("\t\t┌────────────────-─────-┐\n");
printf("\t\t│ │\n");
printf("\t\t│ 1. 查询员工 │\n");
printf("\t\t│ │\n");
printf("\t\t│ 2. 添加员工 │\n");
printf("\t\t│ │\n");
printf("\t\t│ 3. 删除员工 │\n");
printf("\t\t│ │\n");
printf("\t\t│ 4. 退出程序 │\n");
printf("\t\t│ │\n");
printf("\t\t│ │\n");
printf("\t\t└──────────────────────┘\n");
printf("\t\t 请您选择(1-4):");
while ((c = getchar()) < '1' || c > '4'){
printf("\n\t\t 选择错误,请选择1-4:");
if (c == '\n') continue;
while(getchar() != '\n');
}
while (getchar() != '\n');
return c - '1';
}
void Quit()
{
char tc;
int i;
printf("\n\t");
for (i = 0; i < 66; i++)
{
printf("*");
}
printf("\n\t\t 要返回登录界面按任意键\t\t退出程序请按Esc键\n\n");
tc = getch();
if (tc == 27) //esc键按下
{
printf("\n\t\t 程序结束.\n");
exit(0);
}
//重新进入主菜单
Menu();
}
void Add_Information()
{
PWOKE p;
//system("WOK");
//head();
if (pHead == NULL) {
pHead = (PWOKE)malloc(sizeof(WOKE));
pHead->pNext = NULL;
}
pNew = (PWOKE)malloc(sizeof(WOKE));
printf("\n\t\t 添加员工信息:\n");
printf("\n\t\t 工号: ");
scanf("%s", pNew->workno);
printf("\n\t\t 姓名: ");
scanf("%s", pNew->worname);
printf("\n\t\t 工资: ");
scanf("%s", pNew->workwage);
printf("\n\t\t 出生日期: ");
scanf("%s", pNew->wokerbir);
pNew->pNext = NULL;
//pNew = NULL;
for (p = pHead; p->pNext != NULL; p = p->pNext);
p->pNext = pNew;
printf("\n\t\t 添加成功...\n");
iCount++;
while (getchar() != '\n');
}
void Delete_Information()
{
char ID[10];
char sc;
int found = 0;
if (iCount == 0) {
printf("\n\t\t 员工信息为空!\n");
return;
}
PWOKE pCurrnode = pHead; //指向头结点
//head();
printf("\n\t\t 删除信息:\n");
printf("\n\t\t 请输入员工工号:");
scanf("%s", ID);
while (getchar() != '\n');
//遍历信息
while (pCurrnode->pNext != NULL)
{
if (strcmp(pCurrnode->pNext->workno, ID) == 0)
{
found = 1;
//信息库里面有要删除的信息
printf("\n\t\t 要删除的员工信息...\n");
printf("\n\t\t 工号\t姓名\t工资\t出生日期");
printf("\n\t\t %s\t %s\t %s\t %s\t\n", pCurrnode->pNext->workno,
pCurrnode->pNext->worname, pCurrnode->pNext->workwage, pCurrnode->pNext->wokerbir);
printf("\n\t\t 是否删除该信息(y/Enter):");
sc = getchar();
if(sc != '\n') while (getchar() != '\n');
if (sc == 'y' || sc == 'Y' || sc == '\n')
{
PWOKE pTemp = pCurrnode->pNext;
pCurrnode->pNext = pTemp->pNext;
free(pTemp);
iCount--;
printf("\n\t\t 删除成功...\n");
return;
}
else {
printf("\n\t\t 删除失败...\n");
return;
}
}
pCurrnode = pCurrnode->pNext;
}
if(!found)
printf("\n\t\t 没有找到要删除的信息......\n");
}
void Search_Information()
{
char ID[10];
PWOKE pCurrnode;
//head();
if (iCount == 0) {
printf("\n\t\t 员工信息为空!\n");
return;
}
printf("\n\t\t 查询信息:\n");
printf("\n\t\t 请输入员工工号:");
scanf("%s", ID);
pCurrnode = pHead->pNext;
while (pCurrnode != NULL)
{
if (strcmp(pCurrnode->workno, ID) == 0)
{
printf("\n\t\t 要查询的学生信息...\n");
printf("\n\t\t 工号\t姓名\t工资\t出生日期");
printf("\n\t\t %s\t %s\t %s\t %s\t\n", pCurrnode->workno,
pCurrnode->worname, pCurrnode->workwage, pCurrnode->wokerbir);
return;
}
pCurrnode = pCurrnode->pNext;
}
pCurrnode = NULL;
printf("\n\t\t 没有找到要查询的学生信息......\n\n");
while (getchar() != '\n');
}
void free_memory()
{
PWOKE p, q;
for (p = pHead; p != NULL; p = q) {
q = p->pNext;
free(p);
}
}
贴上代码后菜单就乱了,可以自己调整下。 |