一个学生管理系统求帮助
#include <stdio.h>#include <cstdlib>
#include <string.h>
#define LEN sizeof(Phone)
#define FILENAME_LEN 10
#define NULL 0
#define FORMAT "\n%20d%20s%20d\n"
struct phone
{
char name[10]; /*姓名*/
int number; /*电话号码*/
struct phone *pNext;
};
typedef struct phone Phone;//?
/*函数的初始化*/
Phone *Creat(void);
Phone *Delect(Phone *pHeadGet,int numSelect);
Phone *ReadList(void );
void Insert(Phone *pHeadGet, int numInsert);
char WelcomeAndTip(void);
void Show(Phone *pHeadGet);
void ListSave(Phone *pHeadGet);
/*
===========================
功能:main,对各个函数进行选择
返回:void
===========================
*/
void main()
{
Phone *pHead =NULL;
char select;
int client;
SELECT: select = WelcomeAndTip(); /*欢迎界面 */
switch(select)
{
case 'I' :case 'i' : /*指定处(数据的那一条的后面)*/
if (pHead == NULL)
{
pHead = Creat();
system("cls");//清屏
Show(pHead);
}
else
{
system("cls");
printf("\n链表已经从文件读入存在\n");
}
break;
case 'D' :case 'd' : /*删除指定联系人的节点 */
printf("\n请输入您要删除的联系人:");
scanf("%d", &client);
pHead = Delect(pHead, client);
system("cls");
Show(pHead);
break;
case 'S' :case 's' : /*指定处(数据的那一条的后面)插入节点*/
printf("请输入您要插入记录的id(不要超过当前链表的长度):");
scanf("%d", &client);
Insert(pHead, client);
system("cls");
Show(pHead);
break;
case 'Q' :case 'q' : /*输出整个节点 */
system("cls");
Show(pHead);
break;
case 'B' :case 'b': /*保存当前链表为文件 */
ListSave(pHead);
break;
case 'R' :case 'r': /*读入文件并创建为当前链表 */
pHead = ReadList();
system("cls");
Show(pHead);
break;
default : /*等待选择 */
system("pause");
system("cls");
printf("\n请重新选择\n");
break;
}
goto SELECT;
}
/*
===========================
功能:打印开始的欢迎消息和操作提示
返回:void
传入:void
===========================
*/
char WelcomeAndTip(void) /*欢迎和提示界面 */
{
int client;
char select;
printf("\t\t\t 通讯录管理(学习)\n");
for( client =0; client < 80; client++)
{
printf("=");
}
printf("\n");
printf("I:创建\tD:删除\tQ:显示\tS:插入\tB:保存\tR:读入\n");
for( client =0; client < 80; client++)
{
printf("=");
}
printf("\n请输入您要进行的操作:");
scanf("%c", &select);
return select;
}
/*
===========================
功能:输出整个节点
返回:void
传入:链表的头指针
===========================
*/
void Show(Phone *pHeadGet)
{
Phone *pClient;
pClient = pHeadGet;
if(pClient == NULL)
{
printf("\n数据为空没有数据可以打印\n");
}
else
{
printf("\t\t姓名\t\t电话号码");
do
{
printf(FORMAT, pClient->name, pClient->number);
pClient = pClient->pNext;
}while(pClient != NULL);
}
}
/*
===========================
功能:创造整个节点
返回:链表头指针
传入:void
===========================
*/
Phone *Creat(void)
{
struct phone *pHead =NULL,*pOne =NULL,*pTwo = NULL;
int numberTemp =1;
pOne = (struct phone *)malloc(LEN);
if (pOne == NULL)
{
printf("\n创建不成功请重试\n");
}
else
{
pTwo= pOne;
pHead=pOne;
pTwo->pNext =NULL;
printf("\n请输入第%d个的电话号码,以0 0结束输入",numberTemp);
scanf("%d",&pOne->number);
getchar();
printf("\n请输入第%d个通信人的名字: ", numberTemp);
gets(pOne->name);
}
while(pOne->number !=0)
{
pTwo = pOne;//在开辟之前,前应该给后面的指针赋予前面的指针
pOne = (struct phone *) malloc(LEN);
numberTemp++;
printf("\n请输入第%d个的电话号码",numberTemp);
scanf("%d",&pOne->number);
getchar();
printf("\n请输入第%d个通信人的名字: ", numberTemp);
gets(pOne->name);
pTwo->pNext = pOne;
}
pTwo->pNext = NULL;
free(pOne);
pOne = NULL;
// free(pTwo); 空间没有开辟,何来释放
// pTwo = NULL;
return(pHead);
}
/*
===========================
功能:删除指定学号的节点
返回:void
传入:链表的头指针和要删除的结点次序
===========================
*/
Phone *Delect(Phone *pHeadGet, int numSelect)
{
Phone *pOne = NULL, *pTwo = NULL;
pOne = pHeadGet;
if (pHeadGet == NULL)
{
printf("\n数据为空没有数据可以打印\n");
}
else
{
while( (pOne->number != numSelect) && (pOne->pNext != NULL) )
{
pTwo = pOne;
pOne = pOne->pNext;
}
if( numSelect == pOne->number )
{
if(pOne == pHeadGet)
{
pHeadGet = pOne->pNext;
}
else
{
pTwo->pNext = pOne->pNext;
}
}
else
{
printf("\n没有找到要删除的电话号码:\n");
}
}
return pHeadGet; /*若数据位于头节需要返回头一节*/
}
/*
===========================
功能:指定处(数据的那一条的后面)插入节点
返回:void
传入:链表的头指针和插入的位置
===========================
*/
void Insert(Phone *pHeadGet, int numInsert)
{
Phone *pOne = NULL, *pTwo = NULL, *pInsert = NULL;
int temp;
pTwo = pOne = pHeadGet;
/*这里的算法很糟糕,怎样快速定位指定的节点?*/
for( temp = 1; temp < numInsert; temp++ )
{
pTwo = pTwo->pNext;
}
pOne = pTwo->pNext;
pInsert = (Phone *) malloc(LEN);
printf("\n请输入该电话号码:");
scanf("%d", &pInsert->number );
getchar();
printf("\n请输入要插入的名字: ");
gets(pInsert->name);
pTwo->pNext = pInsert;
pInsert->pNext = pOne;
}
/*
===========================
功能:保存当前链表为文件
返回:void
传入:链表的头指针
===========================
*/
void ListSave(Phone *pHeadGet)
{
FILE *fposition = NULL;
char fileName[FILENAME_LEN];
Phone *pOne = pHeadGet;
int listLen = 0;
if (pHeadGet == NULL)
{
printf("\n数据为空没有数据可以保存\n");
}
else
{
while (pOne)
{
pOne = pOne ->pNext;
listLen++;
} /*得到链表的长度*/
printf("\n请输入文件名,以txt方式(*.txt):");
getchar();
gets(fileName);
if ((fposition = fopen (fileName, "w")) == NULL)
{
printf("\n打开文件失败\n");
exit(0);
}
pOne = pHeadGet;
// fprintf(fposition, "\t\t电话号码\t\t姓名");
while (pOne != NULL)
{
if (! fprintf(fposition, FORMAT, pOne->number,pOne->name))
{
printf("\n读入文件出错\n");
}
pOne = pOne->pNext;
}
fclose(fposition);
}
}
/*
===========================
功能:读入文件并创建为当前链表
返回:pHead
传入:void
===========================
*/
Phone *ReadList(void)
{
FILE *fposition = NULL;
char fileName[FILENAME_LEN];
Phone *pOne = NULL, *pTwo = NULL, *pHead = NULL;
printf("\n请输入您要打开的文件名,以txt方式(*.txt):");
getchar();
gets(fileName);
if ((fposition = fopen (fileName, "r")) == NULL)
{
printf("\n打开文件失败\n");
exit(0);
}
pOne = (struct phone *) malloc(LEN);
pTwo = pOne;
pHead = pOne;
pHead->pNext = NULL;
// fseek(fposition, (long)60L, 0);
while (!feof(fposition))
{
if (!fscanf(fposition, FORMAT, &pOne->number, &pOne->name))
printf("\n读入文件出错,可能文件为空,或文件不存在!\n");
pTwo = pOne;
pOne = (struct phone *) malloc(LEN);
pTwo->pNext = pOne;
}
pTwo->pNext = NULL;
free(pOne); /*释放多余的创建*/
pOne = NULL;
return pHead;
}
运行后 会出现 求改错