| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 416 人关注过本帖
标题:我自己做了一下,觉得我的比较麻烦,看看大家有木有简单点解法!
只看楼主 加入收藏
流星坠东湖
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-5-26
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
我自己做了一下,觉得我的比较麻烦,看看大家有木有简单点解法!
Please input the name, sex, number, and scores of 3 courses of all 5 students,
   then sort in ascending order the 5 students according to the following instructions (1)~(5) using Bubble Sort or Selection Sort.

   Problem:   programing the function <a> void bubblesort(struct stu *, int n) or <b> void selectsort(struct stu *, int n);
     (1) their average score of 3 courses;
     (2) the maximum value of 3 courses;
     (3) name;
     (4) number---sn;
     (5) the minimum value of 3 courses;
#include "stdio.h"
#include "string.h"

#define NUM 5  /*numbers of students*/
#define CRS 3  /*numbers of courses*/

struct stu
{
    char name[10];
    char sex;
    char sn[20];
    float score[CRS];
};

void bubblesort(struct stu *, int);
void selectsort(struct stu *, int);
main()
{
    int i,j;
    struct stu s[NUM];
    struct stu *p = s;


        strcpy(s[0].name,"zhangsan");
        s[0].sex = 'M';
        strcpy(s[0].sn,"jy1001005");
        s[0].score[0]=95.0;
        s[0].score[1]=78.0;
        s[0].score[2]=83.0;

        strcpy(s[1].name,"cuihua");
        s[1].sex = 'F';
        strcpy(s[1].sn,"sl1001003");
        s[1].score[0]=65.0;
        s[1].score[1]=98.0;
        s[1].score[2]=73.0;

        strcpy(s[2].name,"mazi");
        s[2].sex = 'M';
        strcpy(s[2].sn,"wy1002005");
        s[2].score[0]=64.0;
        s[2].score[1]=79.0;
        s[2].score[2]=58.0;

        strcpy(s[3].name,"ergou");
        s[3].sex = 'M';
        strcpy(s[3].sn,"jx1003002");
        s[3].score[0]=67.0;
        s[3].score[1]=89.0;
        s[3].score[2]=72.0;

        strcpy(s[4].name,"qiaofei");
        s[4].sex = 'M';
        strcpy(s[4].sn,"xj2004001");
        s[4].score[0]=95.0;
        s[4].score[1]=78.0;
        s[4].score[2]=83.0;
        
   

    printf("The information of students before sorting:\n");
    for (i=0; i<NUM; i++)
    {
        printf("The name of student %d \n",i);
        printf("%s\n",(p+i)->name);
        printf("The sex of student %d \n",i);
        printf("%c\n",(p+i)->sex);
        printf("The number of student %d \n",i);
        printf("%s\n",(p+i)->sn);
        for (j=0; j<CRS; j++)
        {
            printf("The score of the course %d of the student %d \n", j, i);
            printf("%f\n", s[i].score[j]);
        }
    }

    bubblesort(s,NUM);
    selectsort(p,NUM);

    printf("The information of students after sorting:\n");
    for (i=0; i<NUM; i++)
    {
        printf("The name of student %d \n",i);
        printf("%s\n",(p+i)->name);
        printf("The sex of student %d \n",i);
        printf("%c\n",(p+i)->sex);
        printf("The number of student %d \n",i);
        printf("%s\n",(p+i)->sn);
        for (j=0; j<CRS; j++)
        {
            printf("The score of the course %d of the student %d \n", j, i);
            printf("%f\n", s[i].score[j]);
        }
    }
    p = s;
}

/* To program*/
void bubblesort(struct stu * a, int n)
{
}

void selectsort(struct stu * a, int n)
{
}
(这是题目,我自己做的结果在下面,有木有同胞能帮我简化一下)
按(1)要求
双号
void bubblesort(struct stu * a, int n)
{
    int i,j,last;
    struct stu b;
    i=0;
    while(i<n-1)
    {
        last=n-1;
        for(j=n-1;j>i;j--)
        {    if(((a[j].score[0]+a[j].score[1]+a[j].score[2])/3)<((a[j-1].score[0]+a[j-1].score[1]+a[j-1].score[2])/3))
            {
                b=a[j-1];
                a[j-1]=a[j];
                a[j]=b;
                last=j;
               
            }
        }
        i=last;
    }
      

}
单号
void selectsort(struct stu * a, int n)
{
    int i,j,k;
    struct stu b;
    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++)
        {
            if(((a[j].score[0]+a[j].score[1]+a[j].score[2])/3)<((a[k].score[0]+a[k].score[1]+a[k].score[2])/3))
                k=j;
        }
        if(k!=i)
        {
            b=a[k];
            a[k]=a[i];
            a[i]=b;
        }
    }

}
按要求(2)
双号
void bubblesort(struct stu * a, int n)
{
    int i,j,last;
    float p,q;
    struct stu b;
    i=0;
    while(i<n-1)
    {
        last=n-1;
        for(j=n-1;j>i;j--)
        {
            p=a[j].score[0]>a[j].score[1]?(a[j].score[0]>a[j].score[2]?a[j].score[0]:a[j].score[2]):(a[j].score[1]>a[j].score[2]?a[j].score[1]:a[j].score[2]);
            q=a[j-1].score[0]>a[j-1].score[1]?(a[j-1].score[0]>a[j-1].score[2]?a[j-1].score[0]:a[j-1].score[2]):(a[j-1].score[1]>a[j-1].score[2]?a[j-1].score[1]:a[j-1].score[2]);
            if(p<q)
            {
                b=a[j-1];
                a[j-1]=a[j];
                a[j]=b;
                last=j;
               
            }
        }
        i=last;
    }
      
}

单号
void selectsort(struct stu * a, int n)
{
    int i,j,k;
    float p,q;
    struct stu b;
    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++)
        {
             p=a[j].score[0]>a[j].score[1]?(a[j].score[0]>a[j].score[2]?a[j].score[0]:a[j].score[2]):(a[j].score[1]>a[j].score[2]?a[j].score[1]:a[j].score[2]);
             q=a[k].score[0]>a[k].score[1]?(a[k].score[0]>a[k].score[2]?a[k].score[0]:a[k].score[2]):(a[k].score[1]>a[k].score[2]?a[k].score[1]:a[k].score[2]);
            if(p<q)
                k=j;
        }
        if(k!=i)
        {
            b=a[k];
            a[k]=a[i];
            a[i]=b;
        }
    }

}
按要求(5)
双号
void bubblesort(struct stu * a, int n)
{
    int i,j,last;
    float p,q;
    struct stu b;
    i=0;
    while(i<n-1)
    {
        last=n-1;
        for(j=n-1;j>i;j--)
        {
            p=a[j].score[0]<a[j].score[1]?(a[j].score[0]<a[j].score[2]?a[j].score[0]:a[j].score[2]):(a[j].score[1]<a[j].score[2]?a[j].score[1]:a[j].score[2]);
            q=a[j-1].score[0]<a[j-1].score[1]?(a[j-1].score[0]<a[j-1].score[2]?a[j-1].score[0]:a[j-1].score[2]):(a[j-1].score[1]<a[j-1].score[2]?a[j-1].score[1]:a[j-1].score[2]);
            if(p<q)
            {
                b=a[j-1];
                a[j-1]=a[j];
                a[j]=b;
                last=j;
               
            }
        }
        i=last;
    }
}
单号
void selectsort(struct stu * a, int n)
{
    int i,j,k;
    float p,q;
    struct stu b;
    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++)
        {
             p=a[j].score[0]<a[j].score[1]?(a[j].score[0]<a[j].score[2]?a[j].score[0]:a[j].score[2]):(a[j].score[1]<a[j].score[2]?a[j].score[1]:a[j].score[2]);
             q=a[k].score[0]<a[k].score[1]?(a[k].score[0]<a[k].score[2]?a[k].score[0]:a[k].score[2]):(a[k].score[1]<a[k].score[2]?a[k].score[1]:a[k].score[2]);
            if(p<q)
                k=j;
        }
        if(k!=i)
        {
            b=a[k];
            a[k]=a[i];
            a[i]=b;
        }
    }

}
按要求(3)
双号
void bubblesort(struct stu * a, int n)
{
    int i,j,last;
    struct stu b;
    i=0;
    while(i<n-1)
    {
        last=n-1;
        for(j=n-1;j>i;j--)
        {
            if(strcmp(a[j].name,a[j-1].name)<0)
            {
                b=a[j-1];
                a[j-1]=a[j];
                a[j]=b;
                last=j;
               
            }
        }
        i=last;
    }
}
单号
void selectsort(struct stu * a, int n)
{
    int i,j,k;
    struct stu b;
    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++)
        {
            if(strcmp(a[j].name,a[k].name)<0)
                k=j;
        }
        if(k!=i)
        {
            b=a[k];
            a[k]=a[i];
            a[i]=b;
        }
    }

}

按要求(4)
双号
void bubblesort(struct stu * a, int n)
{
    int i,j,last,x;
    char p[10],q[10];
    struct stu b;
    i=0;
    while(i<n-1)
    {
        last=n-1;
        for(j=n-1;j>i;j--)
        {
            for(x=0;x<7;x++)
            {
                p[x]=a[j].sn[x+2];
            }
            for(x=0;x<7;x++)
            {
                q[x]=a[j-1].sn[x+2];
            }
            if(strcmp(p,q)<0)
            {
                b=a[j-1];
                a[j-1]=a[j];
                a[j]=b;
                last=j;
               
            }
        }
        i=last;
    }
}
单号
void selectsort(struct stu * a, int n)
{
    int i,j,k,x;
    char p[10],q[10];
    struct stu b;
    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++)
        {
            for(x=0;x<7;x++)
            {
                p[x]=a[j].sn[x+2];
            }
            for(x=0;x<7;x++)
            {
                q[x]=a[k].sn[x+2];
            }
            if(strcmp(p,q)<0)
                k=j;
        }
        if(k!=i)
        {
            b=a[k];
            a[k]=a[i];
            a[i]=b;
        }
    }

}
搜索更多相关主题的帖子: following void function average 
2012-05-26 15:29
吴军旗
Rank: 5Rank: 5
等 级:职业侠客
帖 子:286
专家分:308
注 册:2011-9-14
收藏
得分:10 

最惨的不是忘不了悲伤的回忆,而是那些悲伤的回忆却开始记不清。。。
2012-05-26 19:12
善水盈渊
Rank: 2
等 级:论坛游民
帖 子:39
专家分:29
注 册:2011-11-16
收藏
得分:10 
好长
2012-05-27 00:20
快速回复:我自己做了一下,觉得我的比较麻烦,看看大家有木有简单点解法!
数据加载中...
 
   



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

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