那位大神能告诉我为什么会出现乱码,在运行sort和insert时
乱码是这个11113 c 12 15 16 43
11112 b 13 14 15 42
11111 a 11 12 15 38
845702924 gh2 0 0 0 0
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<stdlib.h>
struct stu{
int num;
char name[10];
int china;
int math;
int english;
int sum;
struct stu *next;
};
struct stu *create(){
struct stu *head,*p1,*p2;
head=p2=NULL;
p1=(struct stu*)malloc(sizeof(struct stu));
scanf("%5d%6s%8d%8d%8d",&p1->num,p1->name,&p1->china,&p1->math,&p1->english);
p1->sum=p1->china+p1->math+p1->english;
p1->next=NULL;
if(p1->num!=0){head=p1;
}
else printf("errow");
while(p1->num!=0){
p2=p1;
p1=(struct stu*)malloc(sizeof(struct stu));
scanf("%5d%6s%8d%8d%8d",&p1->num,p1->name,&p1->china,&p1->math,&p1->english);
p1->sum=p1->china+p1->math+p1->english;
p2->next=p1;
p1->next=NULL;
}
p2->next=NULL;
free(p1);
return(head);
}
void display(struct stu *head){
FILE *fp;
char ch;
fp=fopen("D:\\stu.txt","r");
if(fp==NULL){printf("errow");exit(1);}
printf("\n");
while((ch=fgetc(fp))!=EOF){
putchar(ch);
}
fclose(fp);
}
void save(struct stu *head){
FILE *fp;
struct stu *p=head;
fp=fopen("D:\\stu.txt","w");
while(p!=NULL){
fprintf(fp,"%5d%6s%8d%8d%8d%8d\n",p->num,p->name,p->china,p->math,p->english,p->sum);
p=p->next;
}
fclose(fp);
system("pause");
}
struct stu *load(){
FILE *fp;
struct stu *head;
struct stu *p1,*p2;
fp=fopen("D:\\stu.txt","r");
if(fp==NULL){printf("errow");exit(1);}
p1=(struct stu*)malloc(sizeof(struct stu));
fscanf(fp,"%5d%6s%8d%8d%8d%8d\n",&p1->num,p1->name, &p1->china,&p1->math,&p1->english,&p1->sum);
if(p1!=NULL) head=p1;
else head=NULL;
while(!feof(fp)){
p2=p1;
p1=(struct stu*)malloc(sizeof(struct stu));
fscanf(fp,"%5d%6s%8d%8d%8d%8d",&p1->num,p1->name, &p1->china,&p1->math,&p1->english,&p1->sum);
p2->next=p1;
p1->next=NULL;
}
return(head);
}
void search(struct stu *head){
int num1;
struct stu *p1;
head=load();
display(head);
scanf("%5d",&num1);
p1=head;
while(num1!=p1->num){
if(p1->next!=NULL) p1=p1->next;
else break;
}
if(num1==p1->num){printf("%5d%6s%8d%8d%8d%8d\n",p1->num,p1->name,p1->china,p1->math,p1->english,p1->sum);}
else printf("errow");
getchar();
free(p1);
system("pause");
}
void sort(struct stu *head){
int flag=1;
struct stu *p1,*p2,*p3,*p;
struct stu p4;
head=load();
display(head);
p1=head;
p2=p1->next;
do{
do{
if(p1->sum<p2->sum){p3=p1->next;p1->next=p2->next;p2->next=p3;p4=*p1;*p1=*p2;*p2=p4;}
p2=p2->next;
}while(p2!=NULL);
if(flag) head=p1;
flag=0;
p1=p1->next;
p2=p1->next;
}while(p2!=NULL);
p=head;
while(p!=NULL){
printf("%5d%6s%8d%8d%8d%8d\n",p->num,p->name,p->china,p->math,p->english,p->sum);
p=p->next;
}
save(head);
//display(head);
free(p1);
free(p2);
//free(p3);
system("pause");
}
void free_t(struct stu *head){
struct stu *p1,*p2;
head=load();
p1=head;
while(p1!=NULL){
p2=p1;
p1=p1->next;
free(p2);
}
}
void insert(struct stu *head){
int a,num1;
struct stu *p1,*p2,*p3;
head=load();
display(head);
p3=(struct stu*)malloc(sizeof(struct stu));
p2=(struct stu*)malloc(sizeof(struct stu));
scanf("%5d%6s%8d%8d%8d",&p3->num,p3->name,&p3->china,&p3->math,&p3->english);
p3->sum=p3->china+p3->math+p3->english;
printf("%5d%6s%8d%8d%8d%8d\n",p3->num,p3->name,p3->china,p3->math,p3->english,p3->sum);
printf("放入首位--'1'");
printf("放入目的学号后--'2'");
scanf("%d",&a); clrscr();
if(a==1){p3->next=head;head=p3;}
if(a==2){
printf("输入目的学号");
scanf("%5d",&num1);
p1=head;
while(num1!=p1->num){
if(p1->next!=NULL) p1=p1->next;
else break;
}
if(p1->next!=NULL){p2=p1->next;p1->next=p3;p3->next=p2;}
if(p1->next==NULL){p1->next=p3;p3->next=NULL;}
}
save(head);
display(head);
system("pause");
}