当前位置: 代码迷 >> C语言 >> 伤残程序求救!!!!
  详细解决方案

伤残程序求救!!!!

热度:271   发布时间:2008-04-09 17:47:48.0
伤残程序求救!!!!
#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  

----------------解决方案--------------------------------------------------------
第一眼看到的时候。。。以为是脑残。。。


----------------解决方案--------------------------------------------------------
水平很菜
版主能指出哪里出问题了吗
----------------解决方案--------------------------------------------------------
scanf("%d,%s,%f",&s->num,&s->name,&s->score);
%s??
c中好想不可一这样读入字符串吧
----------------解决方案--------------------------------------------------------
for(p=head;p!=NULL;p=p->next){
     small=p;
   for(q=p->next;q!=NULL;q=q->next)
      if(q->num>p->num)     //p=head;p->num, head中存有数据?其余函数也一样。
       small=q
----------------解决方案--------------------------------------------------------
回复 4# 的帖子
scanf("%s",...);//读入一字符串;

[[it] 本帖最后由 now 于 2008-4-9 21:34 编辑 [/it]]
----------------解决方案--------------------------------------------------------
  相关解决方案