#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>//用system函数为清除屏幕命令。
#include <string.h>
#include <math.h>
struct _PACKAGE
{
char no[10];//快递单号
char name[20];//收货人
char tel[20];//收货人号码
char add[40];//收货人地址
char cate[10];//运单种类
char com[10];//承运公司
char time[20];//到达时间
struct _PACKAGE next;
};
FILE fp;
struct _PACKAGE p1, p2,* head;
void creat()
{
FILE *fp;
int a;
struct _PACKAGE *s,head,tail;
head=NULL;
tail=NULL;
system("cls");
if((fp=fopen("record.txt","a"))==NULL)
{
printf("打开文件失败");
}
do
{
s=(struct _PACKAGE )malloc(sizeof(struct _PACKAGE));//开辟新链表
//fread(s,sizeof(struct _PACKAGE),1,fp);
printf("快递单号:");
scanf("%s",&s->no);
printf("\n收货人:");
scanf("%s",s->name);
printf("\n收货人号码:");
scanf("%s",s->tel);
printf("\n收货人地址:");
scanf("%s",s->add);
printf("\n运单种类:");
scanf("%s",s->cate);
printf("\n乘运公司:");
scanf("%s",s->com);
printf("\n到达时间:");
scanf("%s",s->time);
if(head==NULL) head=s;
else tail->next=s;
tail=s;
fwrite(s,sizeof(struct _PACKAGE ),1,fp);
//fprintf(fp,"%d\t%s\t%s\t\t%s\t\t%s\t%s\t%s\n",s->no,s->name,s->tel,s->add,s->cate,s->com,s->time);
printf("继续请输入任意非零数字||退出请输入0\n");
scanf("%d",&a);
system("cls");
}while(a!=0);
fclose(fp);
}
void interface()
{
int i;
do
{
system("cls");
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 * 5:显示所有信息 *\n");
printf("\t\t * *\n");
printf("\t\t * 0:退出 *\n");
printf("\t\t * *\n");
printf("\t\t ***********************************************\n");
printf("\t 请选择所要进行的操作(0-5):");
scanf("%d",&i);
printf("\n");
switch(i)
{
case 1:creat();break;
case 2:;break;
case 3:delmax();break;
case 4:;break;
case 5: Print();break;
default:printf("选择有误.\n");break;
}
}while(i);
}
/显示包裹信息页面/
int Print()
{
system("cls");
if ((fp=fopen("record.txt","rb"))==NULL)
{
printf("不能打开文件,按任意键退出......\n");
system("pause");
}
printf("单号 | 收货人 |收货人号码|收货人地址|运单种类| 承运公司 | 到达时间\n");
printf("------------------------------------------------------------------------\n");
p1=(struct _PACKAGE *)malloc(sizeof(struct _PACKAGE));
do
{
if(fread(p1,sizeof(struct _PACKAGE),1,fp)!=1)
{
if(feof(fp))
{
printf("输出结束!");
system("pause");
break;
}
}
printf("%s%10s%10s%10s%15s%10s%10s\n",p1->no,p1->name,p1->tel,p1->add,p1->cate,p1->com,p1->time);
}while(1);
fclose(fp);
return 0;
}
/删除界面/
void getnum()
{
char no[15];
int a=0;
struct _PACKAGE *p1=NULL,*p2,*head=NULL,*q;
if((fp=fopen("E:\程序\快递包裹管理系统作业\record.txt","wb"))==NULL)
{
printf("文件打开错误\n");
return ;
}
p2=p1;
printf("请输入快递单号:");
scanf("%s",no);
while(p1!=NULL)
{
if(strcmp(p1->no,no)==0)
{
q=p1;
head=q->next;
p1=p2=q->next;
continue;
}
else
{
p2->next=p1->next;
q=p1;
p1=p1->next;
continue;
}
p2=p1;
p1=p2->next;
}
p1=p2=head;
while(p1!=NULL)
{
if(fwrite(p1,sizeof(struct _PACKAGE),1,fp)!=1)
{
printf("输入错误\n");break;
}
p1=p1->next;
}
fclose(fp);
}
void getname()
{
FILE *fp;
char b[20];
struct _PACKAGE *p1,*p2,*head,*q;
if((fp=fopen("record.txt","wb"))==NULL)
{
printf("文件打开错误\n");
return ;
}
//p2=p1;
printf("请输入收货人名字:");
scanf("%s",b);
while(p1!=NULL)
{
if(strcmp(p1->name,b)==0)
{
q=p1;
head=q->next;
p1=p2=q->next;
continue;
}
else
{
p2->next=p1->next;
q=p1;
p1=p1->next;
continue;
}
p2=p1;
p1=p2->next;
}
p1=p2=head;
while(p1!=NULL)
{
if(fwrite(p1,sizeof(struct _PACKAGE),1,fp)!=1)
{
printf("输入错误\n");break;
}
p1=p1->next;
}
fclose(fp);
}
int delmax()
{
int i;
CreatLinkTa();
while(1)
{
system("cls");
printf("**********************************\n");
printf("\t1.按快递单号删除\n");
printf("\t*2.按收货人名字删除\n");
printf("\t3.退出\n");
printf("***************************************\n");
printf("请输入:");
scanf("%d",&i);
switch(i)
{
case 1:getnum();break;
case 2:getname();break;
case 3:return 0;
default:break;
}
}
return 0;
}
int CreatLinkTa()
{
char n=0;
system("cls");
if((fp=fopen("record.txt","rb"))==NULL)
{
printf("打开文件错误,按任意键退出......");
system("pause");
return 0;
}
do
{
p1=( struct _PACKAGE *)malloc(sizeof(struct _PACKAGE ));
//p2=p1;
if((fread(p1,sizeof(struct _PACKAGE),1,fp)!=1)&&(feof(fp)))
{
printf("文件读入结束!按任意键退出......\n");
system("pause");
break;
}
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
}while(!feof(fp));
p1->next=NULL;
p1=head;
fclose(fp);
printf("链表产生完毕\n");
system("pause");
p1=p2=head;
printf("链表显示完毕,按任意键继续……\n");
return 0;
}
int main()
{
system("cls");
interface();
system("pause");
return 0;
}
检查no的值是否正确,前面有很多换行缓存,🤔是否用getchar()处理
修改如下,供参考:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>//用system函数为清除屏幕命令。
#include <string.h>
#include <math.h>
#include <windows.h>
struct _PACKAGE
{
char no[10];//快递单号
char name[20];//收货人
char tel[20];//收货人号码
char add[40];//收货人地址
char cate[10];//运单种类
char com[10];//承运公司
char time[20];//到达时间
struct _PACKAGE* next;
};
struct _PACKAGE* p1 = NULL, * p2 = NULL, * head = NULL;
void creat()
{
int a;
FILE* fp;
struct _PACKAGE* s;
system("cls");
if ((fp = fopen("record.txt", "a")) == NULL)
{
printf("打开文件失败");
}
else{
for (p2 = NULL, p1 = head; p1 != NULL; p2 = p1, p1 = p1->next);
do{
s = (struct _PACKAGE*)malloc(sizeof(struct _PACKAGE));//开辟新链表
s->next = NULL;
printf("快递单号:");
scanf("%s", s->no);//scanf("%s", &s->no);
printf("\n收货人:");
scanf("%s", s->name);
printf("\n收货人号码:");
scanf("%s", s->tel);
printf("\n收货人地址:");
scanf("%s", s->add);
printf("\n运单种类:");
scanf("%s", s->cate);
printf("\n乘运公司:");
scanf("%s", s->com);
printf("\n到达时间:");
scanf("%s", s->time);
fwrite(s, sizeof(struct _PACKAGE), 1, fp);
if (p2 == NULL)
head = s;
else
p2->next = s;
p2 = s;
printf("继续请输入任意非零数字||退出请输入0\n");
scanf("%d", &a);
system("cls");
} while (a != 0);
fclose(fp);
}
}
//显示包裹信息页面
void Print() //int Print()
{
system("cls");
if (head == NULL)
printf("未找到记录,按任意键退出......\n");
else{
p1 = head;
printf("单号 | 收货人 |收货人号码|收货人地址|运单种类| 承运公司 | 到达时间\n");
printf("------------------------------------------------------------------------\n");
while(p1)
{
printf("%s%10s%10s%10s%15s%10s%10s\n", p1->no, p1->name, p1->tel,
p1->add, p1->cate, p1->com, p1->time);
p1 = p1->next;
}
}
system("pause");
}
void Delete_record(char* s)
{
FILE* fp;
int flg = 0;
if ((fp = fopen("record.txt", "r")) == NULL)
printf("文件打开错误\n");
else {
fclose(fp);
p1 = head; p2 = NULL;
while (p1 != NULL)
{
if (strcmp(p1->no, s) == 0 || strcmp(p1->name, s) == 0)
{
if (p1 == head)
{
head = p1->next;
free(p1);
p1 = head;
}
else {
p2->next = p1->next;
free(p1);
p1 = p2;
}
flg = 1;
}
else {
p2 = p1;
p1 = p1->next;
}
}
if (!flg) {
printf("未找到删除记录!\n");
}
else {
p1 = head;
fp = fopen("record.txt", "w");
while (p1 != NULL)
{
fwrite(p1, sizeof(struct _PACKAGE), 1, fp);
p1 = p1->next;
}
fclose(fp);
printf("删除成功!\n");
}
}
}
//删除界面
void getnum()
{
char no[15];
printf("请输入快递单号:");
scanf("%s", no);
Delete_record(no);
system("pause");
}
void getname()
{
char b[20];
printf("请输入收货人名字:");
scanf("%s", b);
Delete_record(b);
system("pause");
}
void CreatLinkTa()
{
int n = 0;
FILE* fp;
system("cls");
if ((fp = fopen("record.txt", "r")) == NULL)
printf("打开文件错误......");
else{
do {
p1 = (struct _PACKAGE*)malloc(sizeof(struct _PACKAGE));
p1->next = NULL;
if ((fread(p1, sizeof(struct _PACKAGE), 1, fp) != 1))
{
free(p1);
break;
}
n++;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
} while (1);
fclose(fp);
}
}
void delmax()
{
int i = 1;
while (i)
{
system("cls");
printf("**********************************\n");
printf("\t1.按快递单号删除\n");
printf("\t2.按收货人名字删除\n");
printf("\t3.退出\n");
printf("***************************************\n");
printf("请输入:");
scanf("%d", &i);
switch (i)
{
case 1:getnum(); break;
case 2:getname();break;
case 3:i = 0; break;//return 0;
default:break;
}
}
}
void inter_face()
{
int i;
CreatLinkTa();
do{
system("cls");
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 * 5:显示所有信息 *\n");
printf("\t\t * *\n");
printf("\t\t * 0:退出 *\n");
printf("\t\t * *\n");
printf("\t\t ***********************************************\n");
printf("\t 请选择所要进行的操作(0-5):");
scanf("%d", &i);
printf("\n");
switch (i)
{
case 1:creat();break;
case 2:break;
case 3:delmax();break;
case 4:break;
case 5:Print();break;
case 0:i = 0; break;
default:printf("选择有误.\n"); break;
}
} while (i);
}
int main()
{
system("cls");
inter_face();
system("pause");
return 0;
}