标题:伤残程序求救!!!!
取消只看楼主
xshj1025
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-3-31
 问题点数:0 回复次数:1 
伤残程序求救!!!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
    int num;
    char name[10];
    float score;
    struct student *next;
}link;

link *listcreat(int n)
{
    link *head;
    link *p,*s;
    int i;
    if((head=(link *)malloc(sizeof(link)))==NULL){
            printf("内存分配失败!\n");
                exit(0);
                }
        head->next=NULL;
    p=head;
    
for(i=0;i<n;i++){
    if((s=(link *)malloc(sizeof(link)))==NULL){
        printf("内存分配失败!\n");
            exit(0);
        }
            p->next=s;
                printf("第%d个人:\n学号     姓名     成绩:\n",i+1);
                    scanf("%d,%s,%f",&s->num,&s->name,&s->score);
                s->next=NULL;
                    p=s;        
                }
    return head;
}/*创建链表,输入数据*/

void num_sort(link *head)
{
    link *p,*q,*small;
    int numtemp;
    float scoretemp;
    char nametemp[10];
        for(p=head;p!=NULL;p=p->next){
                small=p;
                
                    for(q=p->next;q!=NULL;q=q->next)
                        if(q->num>p->num)
                        small=q;
                    if(p!=small){
                    numtemp=p->num;
                    p->num=q->num;
                    q->num=numtemp;
                    scoretemp=p->score;
                    p->score=q->score;
                    q->score=scoretemp;
                    strcpy(nametemp,p->name);
                    strcpy(p->name,q->name);
                    strcpy(q->name,nametemp);
                    }            
          }
}/*选择排序,以num的大小作为判断条件numtemp
    float scoretemp
    char nametemp[10]为相应的中间交换变量*/

void score_sort(link *head)
{
    link *p,*q,*small;
    int numtemp;
    float scoretemp;
    char nametemp[10];
        for(p=head;p!=NULL;p=p->next){
                small=p;
                
                    for(q=p->next;q!=NULL;q=q->next)
                        if(q->score>p->score)
                        small=q;
                    if(p!=small){
                    numtemp=p->num;
                    p->num=q->num;
                    q->num=numtemp;
                    scoretemp=p->score;
                    p->score=q->score;
                    q->score=scoretemp;
                    strcpy(nametemp,p->name);
                    strcpy(p->name,q->name);
                    strcpy(q->name,nametemp);
                    }
                    
          }
    
}/*选择排序,以score的大小作为判断条件numtemp
    float scoretemp
    char nametemp[10]为相应的中间交换变量*/

void name_sort(link *head)
{
    link *p,*q,*small;
    int numtemp;
    float scoretemp;
    char nametemp[10];
        for(p=head;p!=NULL;p=p->next){
                small=p;
                
                    for(q=p->next;q!=NULL;q=q->next)
                        if(strcmp(q->name,p->name)>0)
                        small=q;
                    if(p!=small){
                    numtemp=p->num;
                    p->num=q->num;
                    q->num=numtemp;
                    scoretemp=p->score;
                    p->score=q->score;
                    q->score=scoretemp;
                    strcpy(nametemp,p->name);
                    strcpy(p->name,q->name);
                    strcpy(q->name,nametemp);
                    }
                    
          }
    
}/*选择排序,用strcmp(name1,name2)大于0与否作为判断条件numtemp
    float scoretemp
    char nametemp[10]为相应的中间交换变量*/

void print(link *head)
{
    link *p;
    p=head;
    while(p->next!=NULL){
        printf("%d,%s,%f",p->num,p->name,p->score);
        p=p->next;
       }
}/*输出函数*/

void main()/*主程序*/
{
    link *head;
    int n;
    printf("********学生信息管理系统*********\n");
        printf("请输入人数:");
        scanf("%d",&n);
    head=listcreat(n);
        printf("以学号排名:\n");
    num_sort(head);
    print(head);
    printf("以成绩排名:\n");
    score_sort(head);
    print(head);
    printf("以姓名排名:\n");
    name_sort(head);
    print(head);
}
搜索更多相关主题的帖子: 伤残 link 内存 head 
2008-04-09 17:47
xshj1025
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-3-31
得分:0 
水平很菜
版主能指出哪里出问题了吗
2008-04-09 18:11



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




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

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