1. 程式人生 > 其它 >大佬幫忙看看錯誤,沒有報錯但是結果不對不能滿足刪除查詢和修改功能,而且輸入的時候有誤

大佬幫忙看看錯誤,沒有報錯但是結果不對不能滿足刪除查詢和修改功能,而且輸入的時候有誤

技術標籤:vc++6.0

在這裡插入圖片描述

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>

#define MAX_ID 12
#define MAX_NAME 11
#define MAX_QQ 10
#define MAX_TEL 12

#define EXIT 0
#define INPUT 1
#define DELETE 2
#define SEARCH 3
#define UPDATE 4
#define OUTPUT 5

#define SEARCH_ID 1

#define SEARCH_NAME 2

typedef struct _StuInfo
{
char id[MAX_ID];
char name[MAX_NAME];
char tel[MAX_TEL];
char qq[MAX_QQ];

}StuInfo;

typedef struct _StuNode
{
StuInfo stu;
struct _StuNode *next;

}StuNode;

typedef StuNode* StuList;
StuList book=NULL;

void ShowMenu();
void AddStu();
void DeleteStu();
void SearchStu();

void SearchStuID();
void SearchStuName();
void UpdateStu();
void OutputStu();
void Exit();
void About();

void ReadFile();
void WriteFile();

int FindStu(char *id);

void ShowMenu()
{
int typeID=0;
ReadFile();
while(1)
{
system(“cls”);
printf("***********************************\n");
printf("

通訊錄管理系統 \n");
printf("
**********************************\n");
printf("
0-退出系統 \n");
printf("
1-新增聯絡人 \n");
printf("
2-刪除聯絡人 \n");
printf("
3-查詢聯絡人 \n");
printf("
4-修改聯絡人 \n");
printf("
5-輸入聯絡人 \n");
printf("
***********************************\n");
printf("->請選擇操作:\n");
scanf("%d",&typeID);

	if(typeID==EXIT)
	{
		WriteFile();
		Exit();
		break;
	}

	switch(typeID)
	{
	case INPUT:
		system("cls");
		AddStu();
		system("pause");
		break;

	case DELETE:
		system("cls");
		DeleteStu();
		system("pause");
		break;

	case SEARCH:
		SearchStu();
		break;

	case UPDATE:
		system("cls");
		UpdateStu();
		system("pasue");
		break;

	case OUTPUT:
		system("cls");
		OutputStu();
		system("pause");
		break;

	default:
		printf("輸入錯誤!\n");
		system("pause");
		break;

	}

}

}

void AddStu()
{
StuNode p=(StuNode )malloc(sizeof(StuNode));
printf("
********************************\n");
printf("
請輸入聯絡人資訊 **\n");
printf("@ 請輸入學號(最大長度為%d個字元)\n->",MAX_ID-1);
scanf("%s",p->stu.id);
while(FindStu(p->stu.id)==1)
{
printf("@ 該聯絡人已存在,請重新輸入呢\n->");
scanf("%s",p->stu.id);

}
printf("@  請輸入姓名(最大長度為%d個字元)\n->",MAX_NAME-1);
scanf("%s",p->stu.name);
printf("@  請輸入手機號碼\n->");
scanf("%s",p->stu.tel);
printf("@  請輸入QQ號碼\n->");
scanf("%s",p->stu.qq);
p->next=book;
book=p;
printf("**         聯絡人新增成功!       **\n");
printf("************************************\n");

}

void DeleteStu()
{
StuNode pre=book;
StuNode p=book;
char id[MAX_ID];
printf("
********************************\n");
printf("
請輸入要刪除聯絡人的學號;\n->");
scanf("%s",id);

while(p)
{
	if(strcmp(p->stu.id,id)==0)
		break;
	pre=p;
	p=p->next;
}
if(!p)
	printf("**         查無此人!       **\n");
else
{
	if(p==book)book=p->next;
	else pre->next=p->next;
	free(p);
	printf("**         刪除成功!       **\n");
}
printf("***********************************\n");

}

void SearchStu()
{
int type,flag=1;
while(flag)
{
system(“cls”);
printf(“\n");
printf("
1-按學號查詢 \n");
printf("
2-按姓名查詢 \n");
printf("
\n”);
printf("->選擇查詢方式:\n");
scanf("%d",&type);

	switch(type)
	{
	case SEARCH_ID:
		 system("cls");
		 SearchStuID();
		 flag=0;
		 break;

	case SEARCH_NAME:
		system("cls");
		SearchStuName();
		flag=0;
		break;
	default:
		printf("輸入有誤!\n");
		break;
	}
	system("pause");
}

}

void SearchStuID()
{
StuNode p=book;
char id[MAX_ID];
printf("
******************************\n");
printf("
請輸入要查詢的學號:\n->");
scanf("%s",id);

while(p)
{
	if(strcmp(p->stu.id,id)==0)
		break;
	p=p->next;
}
if(!p)
{
	printf("**         聯絡人不存在!       **\n");
	printf("**********************************\n");
}
else
{
	printf("**********************************\n");
	printf("**            聯絡人資訊        **\n");
	printf("**********************************\n");
	printf("$學    號:%s\n",p->stu.id);
	printf("$姓    名:%s\n",p->stu.name);
	printf("$手機號碼:%s\n",p->stu.tel);
	printf("$Q Q 號碼:%s\n",p->stu.qq);
	printf("**********************************\n");
}

}

void SearchStuName()
{
StuNode p=book;
char name[MAX_NAME];
printf("
*********************************\n");
printf("** 請輸入要查詢聯絡人的姓名: \n->");
scanf("%s",name);

while(p)
{
	if(strcmp(p->stu.name,name)==0)
		break;
	p=p->next;
}
if(!p)
{
	printf("**         聯絡人不存在!       **\n");
	printf("**********************************\n");
}
else
{
	printf("**********************************\n");
	printf("**            聯絡人資訊        **\n");
	printf("**********************************\n");
	printf("$學    號:%s\n",p->stu.id);
	printf("$姓    名:%s\n",p->stu.name);
	printf("$手機號碼:%s\n",p->stu.tel);
	printf("$Q Q 號碼:%s\n",p->stu.qq);
	printf("**********************************\n");
}

}

void UpdateStu()
{
StuNode p=book;
char id[MAX_ID];
printf("
*********************************\n");
printf("** 請輸入要更新聯絡人的學號:\n->");
scanf("%s",id);

while(p)
{
	if(strcmp(p->stu.id,id)==0)
		break;
	p=p->next;
}
if(!p)
{
	printf("**            查無此人!        **\n");
	printf("**********************************\n");
}
else
{
	printf("$ 原姓名 :%s\n",p->stu.name);
	printf("->新姓名:");
	scanf("%s",p->stu.name);

	printf("$ 原手機號碼 :%s\n",p->stu.tel);
	printf("->新手機號碼:");
	scanf("%s",p->stu.tel);

	printf("$ 原QQ號碼 :%s\n",p->stu.qq);
	printf("->新QQ號碼:");
	scanf("%s",p->stu.qq);

	printf("**            修改成功!        **\n");
	printf("**********************************\n");
}

}

void OutputStu()
{
int i=0;
StuNode p=book;
if(!p)
{
printf("
\n");
printf("** 通訊錄中無聯絡人記錄! **\n");
printf("
*\n");
return;
}

	while(p)
	{
		printf("**********************************\n");
		printf("**          聯絡人%d資訊        **\n",++i);
		printf("**********************************\n");
		printf("$學    號:%s\n",p->stu.id);
		printf("$姓    名:%s\n",p->stu.name);
		printf("$手機號碼:%s\n",p->stu.tel);
		printf("$Q Q 號碼:%s\n",p->stu.qq);
		printf("**********************************\n");
		p=p->next;
	}

}

void Exit()
{
StuNode *p=book;
while§
{
book=p->next;
free§;
p=book;
}
}

void ReadFile()
{
StuNode *p;
char id[MAX_ID];
FILE pf=fopen(“book.txt”,“r”);
if(!pf)return;
while(fscanf(pf,"%s",id)!=EOF)
{
p=(StuNode
)malloc(sizeof(StuNode));
strcpy(p->stu.id,id);
fscanf(pf,"%s",p->stu.name);
fscanf(pf,"%s",p->stu.tel);
fscanf(pf,"%s",p->stu.qq);
p->next=book;
book=p;
p=NULL;
}
fclose(pf);
}

void WriteFile()
{
StuNode *p=book;
FILE *pf=fopen(“book.txt”,“w”);
if(!pf)return;
while§
{
fprintf(pf,"%s",p->stu.id);
fprintf(pf,"%s",p->stu.name);
fprintf(pf,"%s",p->stu.tel);
fprintf(pf,"%s",p->stu.qq);
p=p->next;
}
fclose(pf);
}

int FindStu(char *id)
{
StuNode *p=book;
while§
{
if(strcmp(id,p->stu.id)==0)
return 1;
p=p->next;
}
return 0;
}

int main()
{
ShowMenu();
return 0;
}