| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 802 人关注过本帖
标题:伤残程序求救!!!!
只看楼主 加入收藏
xshj1025
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-3-31
收藏
 问题点数:0 回复次数:5 
伤残程序求救!!!!
#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
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
第一眼看到的时候。。。以为是脑残。。。

" border="0" />
2008-04-09 17:50
xshj1025
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-3-31
收藏
得分:0 
水平很菜
版主能指出哪里出问题了吗
2008-04-09 18:11
闪闪4521
Rank: 1
等 级:新手上路
帖 子:196
专家分:0
注 册:2007-11-30
收藏
得分:0 
scanf("%d,%s,%f",&s->num,&s->name,&s->score);
%s??
c中好想不可一这样读入字符串吧
2008-04-09 20:58
now
Rank: 1
来 自:广州
等 级:新手上路
帖 子:544
专家分:0
注 册:2007-11-9
收藏
得分:0 
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

GIS
Geographic Information System
你在哪里?——》你的坐标?
2008-04-09 21:30
now
Rank: 1
来 自:广州
等 级:新手上路
帖 子:544
专家分:0
注 册:2007-11-9
收藏
得分:0 
回复 4# 的帖子
scanf("%s",...);//读入一字符串;

[[it] 本帖最后由 now 于 2008-4-9 21:34 编辑 [/it]]

GIS
Geographic Information System
你在哪里?——》你的坐标?
2008-04-09 21:33
快速回复:伤残程序求救!!!!
数据加载中...
 
   



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

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