| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1154 人关注过本帖
标题:那位大神能告诉我为什么会出现乱码,在运行sort和insert时
只看楼主 加入收藏
爱夜夜夜夜
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2021-7-18
结帖率:0
收藏
已结贴  问题点数:20 回复次数:6 
那位大神能告诉我为什么会出现乱码,在运行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
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1479
专家分:9035
注 册:2010-3-16
收藏
得分:10 
struct stu *p1
不需要free(p1);
只有malloc了又不需要保留的才需要free。
你这里是指针就free了一下,create里最后一个p1被free了,也就是最后一条数据被丢弃了。
2021-07-23 22:08
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
既然代码已经这么长了,却连个main函数都不给?!
2021-07-26 08:45
爱夜夜夜夜
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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
由代码猜测你的题目,我写了一个,供参考

程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

struct stu
{
    int num;
    char name[10];
    int china;
    int math;
    int english;
    struct stu* next;
};

struct stu* load( FILE* fp )
{
    if( fp != stdin )
        rewind( fp );

    struct stu* head;
    for( struct stu** p=&head; ; p=&(*p)->next )
    {
        *p = NULL;

        int num;
        if( fscanf(fp,"%d",&num)!=1 )
        {
            fscanf( fp, "%*[^\n]%*c" );
            break;
        }

        if( num == 0 )
            break;
#ifdef __cplusplus
        *p = (struct stu*)malloc( sizeof(struct stu) );
#else
        *p = malloc( sizeof(struct stu) );
#endif
        if( !*p )
            break;

        int n = fscanf( fp, "%9s%d%d%d", (*p)->name, &(*p)->china, &(*p)->math, &(*p)->english );
        if( n != 4 )
        {
            fscanf( fp, "%*[^\n]%*c" );
            free( *p );
            *p = NULL;
            break;
        }

        (*p)->num = num;
    }
    return head;
}

void save( const struct stu* head, int num, FILE* fp )
{
    if( fp != stdout )
        rewind( fp );

    for( const struct stu* p=head; p; p=p->next )
    {
        if( num==0 || num==p->num )
        {
            fprintf( fp, "%d\t%s\t%d\t%d\t%d", p->num, p->name, p->china, p->math, p->english );
            if( fp == stdout )
                fprintf( fp, "\t%d", p->china + p->math + p->english );
            fputc( '\n', fp );
        }
    }

    if( fp != stdout )
        fprintf( fp, "0\n" );
}

void save_by_order( const struct stu* head, FILE* fp )
{
    if( fp != stdout )
        rewind( fp );
    
    for( int cur_value=INT_MAX; ; )
    {
        int next_value = INT_MIN;
        for( const struct stu* p=head; p; p=p->next )
        {
            int sum = p->china + p->math + p->english;
            if( sum == cur_value )
            {
                fprintf( fp, "%d\t%s\t%d\t%d\t%d", p->num, p->name, p->china, p->math, p->english );
                if( fp == stdout )
                    fprintf( fp, "\t%d", p->china + p->math + p->english );
                fputc( '\n', fp );
            }
            else if( sum<cur_value && sum>next_value )
                next_value = sum;
        }
        if( next_value == cur_value )
            break;
        cur_value = next_value;
    }

    if( fp != stdout )
        fprintf( fp, "0\n" );
}

void erase( struct stu** phead, int num )
{
    struct stu** prev = phead;
    for( struct stu* p=*phead; p; )
    {
        if( num==0 || num==p->num )
        {
            *prev = p->next;
            free( p );
            p = *prev;
        }
        else
        {
            prev = &p->next;
            p = p->next;
        }
    }
}

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

int main( void )
{
    FILE* fp = fopen( "D:\\stu.txt", "r+" );
    if( !fp )
    {
        fp = fopen( "D:\\stu.txt", "w+" );
        if( !fp )
        {
            puts( "存取文件 D:\\stu.txt 失败." );
            return 1;
        }
    }
    struct stu* head = load( fp );

    for( int ch=0; ch!='7'; ch=_getch() )
    {
        puts( "--------------------------------------" );
        switch( ch )
        {
        default:
            {
                puts( "1 - 输出所有学生信息" );
                puts( "2 - 输出所有指定学号的学生信息" );
                puts( "3 - 以总分从高到低的顺序输出所有学生信息" );
                puts( "4 - 增添学生信息" );
                puts( "5 - 删除所有指定学号的学生信息" );
                puts( "6 - 删除所有学生信息" );
                puts( "7 - 退出系统" );
            }
            break;
        case '1':
            {
                puts( "学号\t姓名\t语文\t数学\t英语\t总分" );
                save( head, 0, stdout );
            }
            break;
        case '2':
            {
                printf( "输入欲查询的学号: " );
                int num;
                if( scanf("%d",&num)==1 || num!=0 )
                {
                    puts( "学号\t姓名\t语文\t数学\t英语\t总分" );
                    save( head, num, stdout );
                }
            }
            break;
        case '3':
            {
                puts( "学号\t姓名\t语文\t数学\t英语\t总分" );
                save_by_order( head, stdout );
            }
            break;
        case '4':
            {
                puts( "依次输入\"学号\t姓名\t语文\t数学\t英语\",当学号为0时结束输入." );
                struct stu* app = load( stdin );
                struct stu** p;
                for( p=&head; *p; p=&(*p)->next );
                *p = app;
                save( head, 0, fp );
            }
            break;
        case '5':
            {
                printf( "输入欲被删除的学生的学号: " );
                int num;
                if( scanf("%d",&num)==1 && num!=0 )
                    erase( &head, num );
                save( head, 0, fp );
            }
            break;
        case '6':
            {
                erase( &head, 0 );
                save( head, 0, fp );
            }
            break;
        }
        puts( "--------------------------------------" );
    }

    erase( &head, 0 );
    fclose( fp );
    return 0;
}
2021-08-26 09:20
爱夜夜夜夜
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2021-7-18
收藏
得分:0 
谢谢各位大佬
2021-09-04 12:19
快速回复:那位大神能告诉我为什么会出现乱码,在运行sort和insert时
数据加载中...
 
   



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

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