//当指针为负值时指针中没有数值导致赋值给其他指针出错
//例如char * pCh gets(pch) ;当gets读取pCh中的数值时候未赋值导致错误引发中断,在给函数传递参数时候也会发生此类错误需要注意
#include "StdAfx.h"
#include <stdio.h>
#include <math.h>
#include <windows.h>
#define MENUCHOICE "abcdef"
#define MAXREAD 100
typedef struct item{
int stdNum ;
int age ;
char name[20] ;
float height ;
float weight ;
}Item ;
typedef struct node {
Item item ;
struct node * next ;
}Node ;
typedef struct list{
Node * head ;
int memberNum ;
}List ;
int showMenu ;
char str[MAXREAD] ;
void Initialize(List * pList) ;
void reg (char * pPassword) ;
BOOL login(void) ;
void loadData(List * pList) ;
int Menu(List * pList) ;
int ShowMember(List * pList) ;
int ShowAllMember(List * pList) ;
int AddMember(List * pList) ;
int DeleteMember(List * pList) ;
int AlterMember(List * pList) ;
BOOL NoMember(List * pList) ;
int ExitSave(List * pList) ;
void ExitPrintf(void) ;
BOOL StrToDigit(char * str,int * piData,float * pfData) ;
void gotoxy (int x,int y) ;
void setColor (int fg) ;
void main(void)
{
List list ;
char ch ;
Initialize(&list) ;
loadData(&list) ;
login () ;
while(TRUE) //选择功能
{
Menu(&list) ;
scanf ("%c",&ch) ;
fflush (stdin) ;
showMenu = TRUE ;
switch(ch)
{
case 'a':
ShowAllMember (&list) ;
break ;
case 'b':
ShowMember (&list) ;
break ;
case 'c':
AddMember (&list) ;
break ;
case 'd':
DeleteMember (&list) ;
break ;
case 'e':
AlterMember (&list) ;
break ;
case 'f':
ExitSave (&list) ;
break ;
default:
Menu(&list) ;
printf ("\n\t\t\t错误:请输入字母a到f\n") ;
gotoxy (41,15) ;
showMenu = FALSE ;
} ;
}
}
/*数据初始化*/
void Initialize(List * pList)
{
pList->head = NULL ;
pList->memberNum = 0 ;
showMenu = TRUE ;
setColor(10) ;
atexit (ExitPrintf) ;
}
/*登陆*/
BOOL login(void)
{
char password[MAXREAD] ,input[MAXREAD];
FILE * fp ;
reg (password) ; //第一次登陆注册
if(NULL == (fp = fopen ("password","rb"))) //读取系统密码
{
printf ("\t\t\t错误:不能打开这个文件") ;
exit (EXIT_FAILURE) ;
}
else
fread (password,sizeof(password),1,fp) ;
system ("cls") ;
printf ("\n\n\n\n\n\t\t╭═════════════════════╮\n") ;
printf ("\t\t║ ║\n") ;
printf ("\t\t║ 欢迎使用成员信息管理系统 ║\n") ;
printf ("\t\t║ ║\n") ;
printf ("\t\t╰═════════════════════╯\n") ;
printf ("\n\n\t\t请输入系统密码:") ;
while (gets (input))
{
if (0 == strcmp (input,password))
{
fflush (stdin) ;
return TRUE ;
}
else
system ("cls") ;
printf ("\n\n\n\n\n\t\t╭═════════════════════╮\n") ;
printf ("\t\t║ ║\n") ;
printf ("\t\t║ 欢迎使用成员信息管理系统 ║\n") ;
printf ("\t\t║ ║\n") ;
printf ("\t\t╰═════════════════════╯\n") ;
gotoxy (0,13) ;
printf ("\t\t密码错误,请重新输入") ;
gotoxy (0,12) ;
printf ("\t\t请输入系统密码:") ;
fflush (stdin) ;
}
return FALSE ;
}
/*注册*/
void reg(char * pPassword)
{
FILE * fp ;
if(NULL == (fp = fopen ("password","rb")) || 0 == fread(pPassword,MAXREAD * sizeof (char),1,fp)) //创建一个存储信息文件
{
if(NULL == (fp = fopen ("password","wb")))
{
printf ("\t\t\t错误:不能创建文件\n") ;
exit (EXIT_FAILURE) ;
}
printf ("\n\n\n\n\n\t\t╭═════════════════════╮\n") ;
printf ("\t\t║ ║\n") ;
printf ("\t\t║ 欢迎使用成员信息管理系统 ║\n") ;
printf ("\t\t║ ║\n") ;
printf ("\t\t╰═════════════════════╯\n") ;
printf ("\n\n\t\t第一次登陆") ;
printf ("\n\t\t请输入初始系统密码(密码长度为1到100位):") ;
while(gets (pPassword))
{
system ("cls") ;
if ('\0' == *pPassword)
{
printf ("\n\n\n\n\n\t\t╭═════════════════════╮\n") ;
printf ("\t\t║ ║\n") ;
printf ("\t\t║ 欢迎使用成员信息管理系统 ║\n") ;
printf ("\t\t║ ║\n") ;
printf ("\t\t╰═════════════════════╯\n") ;
printf ("\n\n\t\t第一次登陆") ;
printf ("\n\t\t请输入初始系统密码(密码长度为1到100位):") ;
printf ("\n\t\t密码不能为空") ;
gotoxy (55,13) ;
}
else
break ;
}
if(NULL == (fp = fopen ("password","wb")))
{
printf ("\t\t\t错误:不能打开这个文件") ;
exit (EXIT_FAILURE) ;
}
else
{
if(0 == fwrite (pPassword,MAXREAD * sizeof (char),1,fp))
{
printf ("\t\t\t错误:不能写入密码") ;
exit (EXIT_FAILURE) ;
}
}
printf ("\n\n\n\n\n\t\t╭═════════════════════╮\n") ;
printf ("\t\t║ ║\n") ;
printf ("\t\t║ 欢迎使用成员信息管理系统 ║\n") ;
printf ("\t\t║ ║\n") ;
printf ("\t\t╰═════════════════════╯\n") ;
printf ("\n\n\t\t密码输入成功,请详记您的密码:%s",pPassword) ;
printf ("\n\t\t按任意键进入系统") ;
getchar () ;
}
}
/*加载数据*/
void loadData(List * pList)
{
FILE * fp ;
if(NULL == (fp = fopen ("data","rb"))) //创建一个存储信息文件
{
if(NULL == (fp = fopen ("data","wb")))
{
printf ("\t\t\t错误:不能创建文件\n") ;
exit (EXIT_FAILURE) ;
}
}
while(TRUE) //读取内容
{
static int i = 1 ;
Node * tempNode,* nextNode ;
long size ;
size = sizeof(Node) ;
tempNode = (Node *)malloc (size) ;
if(1 == fread (&(tempNode->item),size,1,fp))
{
if(NULL == pList->head)
pList->head = tempNode ;
else
{
nextNode = pList->head ;
while(nextNode->next != NULL)
nextNode = nextNode->next ;
nextNode->next = tempNode ;
}
fseek (fp,size * i,SEEK_SET) ;
(pList->memberNum)++ ;
i++ ;
}
else
break ;
}
fclose(fp) ;
}
/*展示菜单*/
int Menu (List * pList)
{
if (FALSE == showMenu)
return 0 ;
system ("cls") ;
printf ("\n\n\t ---------------------------------------------------------\n") ;
printf ("\t | 成员信息管理系统 |\n") ;
printf ("\t ---------------------------------------------------------\n") ;
printf ("\n\t\t\t当前成员个数:%d \n",pList->memberNum) ;
printf ("\t\t\ta)查看所有成员信息 \n") ;
printf ("\t\t\tb)查看一个成员信息 \n") ;
printf ("\t\t\tc)增加一个成员信息 \n") ;
printf ("\t\t\td)删除一个成员信息 \n") ;
printf ("\t\t\te)修改一个成员信息 \n") ;
printf ("\t\t\tf)退出 \n") ;
printf ("\t\t\t输入a至f选择功能:") ;
}
/*展示成员*/
int ShowMember(List * pList)
{
int stdNum ;
Node * tempNode ;
tempNode = pList->head ;
if (TRUE == NoMember(pList))
return 0 ;
system ("cls") ;
printf ("\n\n\t\t\t请输入成员号码:") ;
while(gets (str))
{
if (TRUE == StrToDigit(str,&stdNum,NULL))
break ;
system ("cls") ;
printf ("\n\n\t\t\t请输入成员号码:") ;
printf ("\n\t\t\t错误:请输入整数\n") ;
gotoxy (39,2) ;
fflush (stdin) ;
}
while(tempNode != NULL)
{
if(stdNum == tempNode->item.stdNum)
break ;
tempNode = tempNode->next ;
}
if(NULL == tempNode)
{
printf ("\t\t\t错误:没找到该成员\n") ;
printf ("\t\t\t显示失败按任意键返回\n") ;
}
else
{
printf ("\n\n\t\t\t成员号码:%d\n",tempNode->item.stdNum) ;
printf ("\t\t\t成员姓名:%s\n",tempNode->item.name) ;
printf ("\t\t\t成员年龄:%d\n",tempNode->item.age) ;
printf ("\t\t\t成员体重:%f\n",tempNode->item.weight) ;
printf ("\t\t\t成员身高:%f\n",tempNode->item.height) ;
printf ("\n") ;
printf ("\t\t\t显示成功按任意键返回\n") ;
}
fflush(stdin) ;
getchar () ;
}
/*展示所有成员*/
int ShowAllMember(List * pList)
{
Node * tempNode ;
tempNode = pList->head ;
if (TRUE == NoMember(pList))
return 0 ;
system ("cls") ;
while(tempNode != NULL)
{
printf ("\n\n\t\t\t成员号码:%d\n",tempNode->item.stdNum) ;
printf ("\t\t\t成员姓名:%s\n",tempNode->item.name) ;
printf ("\t\t\t成员年龄:%d\n",tempNode->item.age) ;
printf ("\t\t\t成员体重:%f\n",tempNode->item.weight) ;
printf ("\t\t\t成员身高:%f\n",tempNode->item.height) ;
printf ("\n");
tempNode = tempNode->next ;
}
printf ("\t\t\t显示成功按任意键返回\n") ;
getchar () ;
}
/*增加成员*/
int AddMember(List * pList)
{
Node * tempNode, * nextitem ;
tempNode = (Node *)malloc (sizeof(Node)) ;
system ("cls") ;
printf ("\n\n\t\t\t请输入成员号码:") ; //学号
while(gets (str))
{
if (TRUE == StrToDigit(str,&(tempNode->item.stdNum),NULL))
break ;
system ("cls") ;
printf ("\n\n\t\t\t请输入成员号码:") ;
printf ("\n\t\t\t错误:请输入数字\n") ;
gotoxy (39,2) ;
fflush (stdin) ;
}
fflush (stdin) ;
printf ("\t\t\t请输入成员名字:");
gotoxy (39,3) ;
while(gets (str))
{
if ('\0' == *str)
{
gotoxy (39,2) ;
printf ("\n\t\t\t请输入成员名字:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b");
printf ("\n\t\t\t你又不是无名,快报上大名!") ;
gotoxy (39,3) ;
}
else
{
strcpy (tempNode->item.name,str) ;
break ;
}
fflush (stdin) ;
}
fflush (stdin) ;
printf ("\t\t\t请输入成员年龄:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ; //年龄
gotoxy (39,4) ;
while(gets (str))
{
if (TRUE == StrToDigit(str,&(tempNode->item.age),NULL))
break ;
gotoxy (39,3) ;
printf ("\n\t\t\t请输入成员年龄:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ;
printf ("\n\t\t\t错误:请输入整数") ;
gotoxy (39,4) ;
fflush (stdin) ;
}
fflush (stdin) ;
printf ("\t\t\t请输入成员体重:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ; //体重
gotoxy (39,5) ;
while(gets (str))
{
if (TRUE == StrToDigit(str,NULL,&(tempNode->item.weight)))
break ;
gotoxy (39,4) ;
printf ("\n\t\t\t请输入成员体重:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ;
printf ("\n\t\t\t错误:请输入数字\n") ;
gotoxy (39,5) ;
fflush (stdin) ;
}
fflush (stdin) ;
printf ("\t\t\t请输入成员身高:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ; //身高
gotoxy (39,6) ;
while(gets (str))
{
if (TRUE == StrToDigit(str,NULL,&(tempNode->item.height)))
break ;
gotoxy (39,5) ;
printf ("\n\t\t\t请输入成员身高:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ;
printf ("\n\t\t\t错误:错误请输入数字\n") ;
gotoxy (39,6) ;
fflush (stdin) ;
}
fflush (stdin) ;
tempNode->next = NULL;
if(NULL == pList->head)
pList->head = tempNode ;
else
{
nextitem = pList->head ;
while(nextitem->next != NULL)
nextitem = nextitem->next ;
nextitem->next = tempNode ;
}
(pList->memberNum)++ ;
printf ("\n\t\t\t添加成功按任意键返回\n") ;
getchar () ;
return 0 ;
}
/*删除成员*/
int DeleteMember(List * pList)
{
int stdNum ;
Node * tempNode ,* prevNode ;
tempNode = pList->head ;
if (TRUE == NoMember(pList))
return 0 ;
system("cls") ;
printf ("\n\n\t\t\t请输入您想要删除的成员号码:") ;
while(gets (str))
{
if (TRUE == StrToDigit(str,&stdNum,NULL))
break ;
system ("cls") ;
printf ("\n\n\t\t\t请输入您想要删除的成员号码:") ;
printf ("\n\t\t\t错误:请输入整数\n") ;
gotoxy (51,2) ;
fflush (stdin) ;
}
fflush (stdin) ;
while(tempNode != NULL)
{
if(stdNum == tempNode->item.stdNum)
{
break ;
}
else
{
prevNode = tempNode ;
tempNode = tempNode->next ;
}
}
if(NULL == tempNode)
{
printf ("\t\t\t错误:找不到该成员\n") ;
printf ("\t\t\t按任意键返回\n") ;
getchar () ;
return 0 ;
}
else if(pList->head == tempNode) //判断删除的成员是否是表头,并将删除的成员下一个成员接上上一个的next
{
pList->head = tempNode->next ;
pList->memberNum-- ;
}
else
{
prevNode->next = tempNode->next ;
pList->memberNum-- ;
}
printf ("\n\n\t\t\t删除成功按任意键返回\n") ;
getchar () ;
}
/*修改成员*/
int AlterMember(List * pList)
{
int stdNum ;
char input[8];
char judgeStr[4][8] = {
"姓名",
"年龄",
"身高",
"体重"
} ;
Node * tempNode ;
tempNode = pList->head ;
if (TRUE == NoMember(pList))
return 0 ;
system ("cls") ;
printf ("\n\n\t\t\t请输入要修改的成员号码:") ;
while(gets (str))
{
if (TRUE == StrToDigit(str,&stdNum,NULL))
break ;
gotoxy (47,1) ;
printf ("\n\t\t\t请输入要修改的成员号码:") ;
printf ("\n\t\t\t错误:请输入整数\n") ;
gotoxy (47,2) ;
fflush (stdin) ;
}
while(tempNode != NULL) //寻找成员
{
if(stdNum == tempNode->item.stdNum)
break ;
else
tempNode = tempNode->next ;
}
if(NULL == tempNode) //判断是否发现成员
{
printf ("\t\t\t错误:找不到该成员\n") ;
printf ("\n\t\t\t修改失败按任意键返回") ;
getchar () ;
}
else //发现成员执行功能
{
printf ("\t\t\t成员号码:%d\t\t\t\b\b\b\n",tempNode->item.stdNum) ; //显示成员信息
printf ("\t\t\t成员姓名:%s\n",tempNode->item.name) ;
printf ("\t\t\t成员年龄:%d\n",tempNode->item.age) ;
printf ("\t\t\t成员体重:%f\n",tempNode->item.weight) ;
printf ("\t\t\t成员身高:%f\n",tempNode->item.height) ;
printf ("\t\t\t您想要修改的信息是:") ; //显示用户可以修改信息
printf ("\n\t\t\t(请输入:姓名,年龄,身高,体重)") ;
gotoxy(43,8) ;
while(gets (input)) //判断用户输入是否正确
{
if(0 == strcmp (input,judgeStr[0]))
{
printf ("\t\t\t请输入成员姓名:\t\t\t\t\t\t\t\b\b\b\b\b\b\b") ;
gotoxy(39,9) ;
while (gets (tempNode->item.name))
{
if ('\0' == *(tempNode->item.name))
{
gotoxy(39,8) ;
printf ("\n\t\t\t请输入成员姓名:\t\t\t\b\b\b") ;
printf ("\n\t\t\t你又不是无名,快报上大名!\n") ;
gotoxy(39,9) ;
}
else
{
printf ("\n\t\t\t修改成功按任意键返回\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ;
fflush (stdin) ;
break ;
}
}
break ;
}
else if(0 == strcmp (input,judgeStr[1]))
{
printf("\t\t\t请输入成员年龄:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ;
gotoxy(39,9) ;
while(gets (str))
{
if (TRUE == StrToDigit(str,&(tempNode->item.age),NULL))
break ;
gotoxy(39,8) ;
printf ("\n\t\t\t请输入成员年龄:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ;
printf ("\n\t\t\t错误:请输入整数") ;
gotoxy(39,9) ;
fflush (stdin);
}
printf ("\n\t\t\t修改成功按任意键返回\n") ;
fflush (stdin) ;
break ;
}
else if(0 == strcmp (input,judgeStr[2])) //正确使用修改身高功能
{
printf("\t\t\t请输入成员的身高:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ;
while(gets (str))
{
if (TRUE == StrToDigit(str,NULL,&(tempNode->item.height)))
break ;
gotoxy (41,8) ;
printf("\n\t\t\t请输入成员的身高:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ;
printf("\n\t\t\t错误:请输入字数") ;
gotoxy (41,9) ;
fflush(stdin) ;
}
printf ("\n\t\t\t修改成功按任意键返回\n") ;
fflush(stdin) ;
break ;
}
else if(0 == strcmp (input,judgeStr[3])) //正确使用修改体重功能
{
printf("\t\t\t请输入成员的体重:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ;
while(gets (str))
{
if (TRUE == StrToDigit(str,NULL,&(tempNode->item.weight)))
break ;
gotoxy (41,8) ;
printf ("\n\t\t\t请输入成员的体重:\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ;
printf ("\n\t\t\t错误:请输入成员的号码") ;
gotoxy (41,9) ;
fflush (stdin) ;
}
printf ("\n\t\t\t修改成功按任意键返回\n") ;
fflush (stdin) ;
break ;
}
else
{
gotoxy (43,8) ;
printf ("\n\t\t\t错误:请输入姓名,年龄,身高,体重\n") ;
gotoxy (43,8) ;
printf ("\t\t\t\t\t\t\t\t\b\b\b\b\b\b\b\b") ;
gotoxy (43,8) ;
}
}
}
fflush (stdin) ;
getchar () ;
}
/*无成员判断功能*/
BOOL NoMember(List * pList)
{
if (0 == pList->memberNum)
{
Menu (pList) ;
printf ("\n\t\t\t错误:当前系统录入成员为零,请增加成员后再使用该功能\n") ;
gotoxy (41,15) ;
showMenu = FALSE ;
return TRUE ;
}
return FALSE ;
}
/*退出保存*/
int ExitSave(List * pList)
{
Node * tempNode ;
FILE * fp ;
tempNode = pList->head ;
if(NULL == (fp = fopen ("data","wb")))
printf ("\t\t\t错误:不能打开这个文件") ;
else
{
while(tempNode != NULL)
{
Node * nextNode ;
nextNode = tempNode->next ;
tempNode->next = NULL ;
if(0 == fwrite (tempNode,sizeof(Node),1,fp))
{
printf ("\t\t\t错误:不能保存信息") ;
exit (EXIT_FAILURE) ;
}
else
tempNode = nextNode ;
}
}
exit (EXIT_SUCCESS) ;
}
/*退出打印*/
void ExitPrintf(void)
{
int i ;
system ("cls") ;
gotoxy (0,5) ;
printf ("\t\t\t正在保存中.") ;
for (i = 0;i < 10;i++)
{
Sleep (40) ;
printf ("..") ;
}
system ("cls") ;
gotoxy (0,5) ;
printf ("\t\t\t保存完毕,谢谢使用该系统\n") ;
Sleep (500) ;
}
/*字符转数字*/
BOOL StrToDigit(char * str,int * piData,float * pfData)
{
int intNum,i,j,pPos,bpNum,apNum,allNum; //bpNum为小数点前的浮点个数 apNum为小数点后的个数
intNum = 0 ;
bpNum = 0 ;
allNum = 0 ;
pPos = 0 ;
if (piData != NULL) //启动整数转换
{
*piData = 0 ;
for (i = 0 ;*(str + i) != '\0';i++)
{
if(!isdigit (*(str + i)))
return FALSE ;
intNum++;
}
if (0 == intNum)
return FALSE ;
for (i = 0 ;*(str + i) != '\0';i++)
{
*piData += (*(str+i) - '0') * pow ((double)10,(double)(intNum -1));
intNum--;
}
}
else //小数转换
{
*pfData = 0 ;
for (i = 0 ;*(str + i) != '\0';i++)
{
if(isdigit (*(str + i)) || '.' == *(str + i))
{
if ('.' == *(str + i))
{
if (0 == i)
return FALSE ;
else
pPos = i ;
}
}
else
return FALSE ;
}
if (0 == i)
return FALSE ;
if (pPos != 0)
{
bpNum = pPos;
allNum = i - 1;
}
else
{
allNum = i;
bpNum = allNum;
}
for (i = 0,j = bpNum;i < bpNum;i++)
{
*pfData += (*(str + i) - '0') * pow ((double)10,(double)(j - 1));
j--;
}
if (0 == pPos)
return TRUE ;
for (j = 1;++i < allNum + 1;j++)
{
*pfData += (*(str + i) - '0') * pow ((float)10,(int)(-j));
}
}
return TRUE ;
}
/*API坐标转换*/
void gotoxy (int x,int y)
{
COORD coord = {x,y} ;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord) ;
}
/*API设置颜色*/
void setColor (int fg)
{
WORD wc ;
HANDLE h = GetStdHandle (STD_OUTPUT_HANDLE) ;
wc = (fg & 0x000f ) ;
SetConsoleTextAttribute (h,wc) ;
}
转载于:https://www.cnblogs.com/Bug-Man/p/3826498.html