鱼C论坛

 找回密码
 立即注册
查看: 2630|回复: 2

[已解决]语法没有问题,但是实现不了功能,请大家帮我看看有什么问题,谢谢大家

[复制链接]
发表于 2017-10-5 22:20:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 50
#define NAMESIZE 10
typedef struct node
{
    long studentID;
    char studentname[NAMESIZE];
    int score;
    int class;
}node;

struct link
{
    node data;
    struct link *next;
};

struct link *head=NULL,*head1=NULL,*head2=NULL,*head3=NULL,*head4=NULL;    /*定义四个链表的头节点指针和总表的头节点指针*/

/*新建一个链表,并设置头指针*/
struct link *AppendNode(struct link *head)
{
    struct link *p=NULL;
    node data;
    p=(struct link *)malloc(sizeof(struct link));
    if(p==NULL)
    {
        printf("No enough memory to allocate!\n");
        exit(0);
    }
    head=p;
    return head;
}

/*插入一个节点,返回插入后链表的头指针*/
struct link *InsertNode(struct link *head,node e)
{
    struct link *pr=head,*p=head,*temp=NULL;
    p=(struct link *)malloc(sizeof(struct link));          /*p指向带插入节点*/
    if(p==NULL)
    {
        printf("No enough memory!\n");
        exit(0);
    }
    p->next=NULL;
    p->data=e;
    if(head==NULL)
    {
        head=p;
    }
    else
    {
        while(pr->data.score>e.score&&pr->next!=NULL)
        {
            temp=pr;
            pr=pr->next;
        }
        if(pr->data.score<=e.score)
        {
            pr=temp;
            p->next=pr->next;
            pr->next=p;
        }
        else                   /*若在表尾插入新节点*/
        {
            pr->next=p;
        }
    }
    return head;
}

struct link *Init_List(struct link *head)
{
    struct link *q,*p=head;
    for(;p->next!=NULL;)
    {
        q=p;
        free(p);
        p=q->next;
    }
    head=NULL;
    return head;
}

void Init(void)
{
    Init_List(head1);
    Init_List(head2);
    Init_List(head3);
    Init_List(head4);
    Init_List(head);
    printf("Lists have been initialized!\n");
}

struct link *Merge_List(struct link *head1,struct link *head2,struct link *head)
{
    struct link *p,*p1,*p2;
    p1=head1;
    p2=head2;
    head=p=head1;
    while(p1&&p2)
    {
        if(p1->data.score>=p2->data.score)
        {
            p->next=p1;
            p=p1;
            p1=p1->next;
        }
        else
        {
            p->next=p2;
            p=p2;
            p2=p2->next;
        }
    }
    p->next=p1?p1:p2;
    free(head2);
    return head;
}

void Merge(void)
{
    struct link *head5,*head6;
    Init_List(head);
    head5=Merge_List(head1,head2,head5);
    head6=Merge_List(head3,head4,head6);
    head=Merge_List(head5,head6,head);
}

void SearchID(long ID)
{
    Merge();
    struct link *p=head;
    for(;p!=NULL;p=p->next)
    {
        if(p->data.studentID==ID)
        {
            printf("class:\t%d\nstudentname:\t%s\nscore:\t%d\n",p->data.class,p->data.studentname,p->data.score);
            break;
        }
    }
    if(p==NULL) printf("Input error!\n");
}

void Searchscore(int score)
{
    Merge();
    struct link *p=head;
    for(;p!=NULL;p=p->next)
    {
        if(p->data.score==score)
        {
            printf("class:\t%d\nstudentID:\t%ld\nstudentname:\t%s\nscore:\t%d\n",p->data.class,p->data.studentID,p->data.studentname,p->data.score);
            break;
        }
    }
    if(p==NULL) printf("Input error!\n");
}

void output(struct link *head)
{
    Merge();
    int counter=1;
    struct link *p=head;
    for(p=p->next;p!=NULL;p=p->next)
    {
        printf("%d\t%s\t%d\t%d\n",p->data.class,p->data.studentname,p->data.score,counter);
        counter++;
    }
}

int main()
{
    struct link *head=NULL,*head1=NULL,*head2=NULL,*head3=NULL,*head4=NULL;
    head=AppendNode(head);
    printf("1.Initialize a list.\n2.Insert score.\n3.Merge list.\n4.Search for someone.\n5.Output the result.\n");
    int op;
    while(scanf("%d",&op)&&op)
    {
        if(op==1)
        {
            Init();
            break;
        }
        if(op==2)
        {
            int score,c;
            char n[NAMESIZE];
            node e;
            printf("Input the class,studentID,studentname and score.(input -1 as end)\n");
            while(scanf("%d",&c)&&c!=-1)
            {
                e.class=c;
                scanf("%ld",&e.studentID);
                fgets(e.studentname,sizeof(e.studentname),stdin);
                scanf("%d",&e.score);
                if(c==1)
                {
                    head1=InsertNode(head1,e);
                }
                if(c==2)
                {
                    head2=InsertNode(head2,e);
                }
                if(c==3)
                {
                    head3=InsertNode(head3,e);
                }
                if(c==4)
                {
                    head4=InsertNode(head4,e);
                }
                else
                {
                    printf("Input error!.");
                }
            }
            printf("Inserting has been completed!\n");
            break;
        }
        if(op==3)
        {
            Merge();
            break;
        }
        if(op==4)
        {
            int ch,score;
            long ID;
            printf("Press 1 to search through ID and press 2 to search through score:");
            scanf("%d",&ch);
            switch(ch)
            {
                case'1':
                    printf("Input ID:");
                    scanf("%ld",&ID);
                    SearchID(ID);break;
                case'2':
                    printf("Input score:");
                    scanf("%d",score);
                    Searchscore(score);break;
                default:printf("Input error!");
            }
            break;
        }
        if(op==5)
        {
            if(head1!=NULL)printf("Class1 result:\nClass   Name    Grade   Ranking\n");
            output(head1);
            if(head2!=NULL)printf("Class2 result:\nClass   Name    Grade   Ranking\n");
            output(head2);
            if(head3!=NULL)printf("Class3 result:\nClass   Name    Grade   Ranking\n");
            output(head3);
            if(head4!=NULL)printf("Class4 result:\nClass   Name    Grade   Ranking\n");
            output(head4);
            if(head!=NULL)printf("Totally result:\nClass   Name    Grade   Ranking\n");
            output(head);
            break;
        }
        else
        {
            printf("Input error!\n");
        }
    }
    return 0;
}
最佳答案
2017-10-28 15:39:21
int class;   关键字不能做为变量名

“score”: 未引用的局部变量
“n”: 未引用的局部变量
使用了未初始化的局部变量“head5”
使用了未初始化的局部变量“head6”
使用了未初始化的局部变量“score”
========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-10-27 16:40:49 | 显示全部楼层
老铁,这种太长的代码可以用代码格式发,这样方便别人拷贝和查看
而且请给出你的目的以及运行后的结果差异,这样才能方便别人理清你的思路
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-10-28 15:39:21 | 显示全部楼层    本楼为最佳答案   
int class;   关键字不能做为变量名

“score”: 未引用的局部变量
“n”: 未引用的局部变量
使用了未初始化的局部变量“head5”
使用了未初始化的局部变量“head6”
使用了未初始化的局部变量“score”
========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 17:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表