标题:那位大神能告诉我为什么会出现乱码,在运行sort和insert时
取消只看楼主
爱夜夜夜夜
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2021-7-18
结帖率:0
已结贴  问题点数:20 回复次数:3 
那位大神能告诉我为什么会出现乱码,在运行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");
}
搜索更多相关主题的帖子: next NULL stu head struct 
2021-07-23 19:39
爱夜夜夜夜
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2021-7-18
得分:0 
回复 3楼 rjsp
//---------------------------------------------------------------------------

#pragma hdrstop

//---------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include"Unit2.h"
#pragma argsused
int main(int argc, char* argv[])
{  char ch;
   struct stu *head;
   FILE *fp;
   do{
     clrscr();
     gotoxy(40,2);printf("菜单");
     gotoxy(35,5); printf("1-登记学生信息");
     gotoxy(35,7); printf("2-读取文件建立链表");
     gotoxy(35,9); printf("3-查询学生信息");
     gotoxy(35,11);printf("4-打开文件");
     gotoxy(35,13);printf("5-保存学生信息");
     gotoxy(35,15);printf("6-学生成绩排序");
     gotoxy(35,17);printf("7-输出学生信息");
     gotoxy(35,19);printf("8-退出系统");
     gotoxy(33,21);printf("please choose:");
     gotoxy(25,23);printf("9-增加学生信息");
     gotoxy(48,23);printf("修改学生信息");
     gotoxy(47,21);
     ch=getch();
     switch(ch){
     case '1':clrscr();printf(" 学号    姓名    语文   数学   英语    总分");head=create();break;
     case '2':clrscr();printf("成功");head=load();system("pause");break;
     case '3':clrscr();printf(" 学号    姓名    语文   数学   英语    总分");search(head);break;
     case '4':clrscr();printf("errow");system("pause");break;
     case '5':clrscr();save(head);printf("保存成功");system("pause");break;
     case '6':clrscr();printf(" 学号    姓名    语文   数学   英语    总分");sort(head);break;
     case '7':clrscr();printf(" 学号    姓名    语文   数学   英语    总分");display(head);getchar();break;
     case '8':free_t(head);break;
     case '9':clrscr();printf(" 学号    姓名    语文   数学   英语    总分");insert(head);break;
     default:clrscr();printf("errow");
     }
     }while(ch!='8');
        getchar();
        return 0;
}
2021-08-25 13:59
爱夜夜夜夜
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2021-7-18
得分:0 
回复 2楼 apull
感谢大佬的回复。
我程序运行的时候并没有用create,我是直接用一个文本文档直接写数据,用load读取,然后再用sort。
学号     姓名    数学    英语    语文    总分
11113     c      12      15      16      43
11112     b      13      14      15      42
11111     a      11      12      15      38
845702924   gh2       0       0       0       0  最后一行是乱码 ,我将总分比较后用sort排序,虽然会排好,但会多出最后一行乱码
2021-08-25 14:15
爱夜夜夜夜
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2021-7-18
得分:0 
谢谢各位大佬
2021-09-04 12:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-506319-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.116708 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved