| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1244 人关注过本帖
标题:我写的这个代码能运行,就是不执行命令,还有输出的是乱码。有没有哪位大佬 ...
只看楼主 加入收藏
斯玥
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2022-1-3
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
我写的这个代码能运行,就是不执行命令,还有输出的是乱码。有没有哪位大佬能帮忙看一下怎么改才可以正常运行?
这是题目:
用C语言代码实现对一份成绩单的排序(成绩单包过姓名,成绩,年龄,学号4个内容)要求能根据用户的需要(选择),提供4种排序模式:
1,成绩由大到小。
2,姓名首字母由A到Z
3,年龄由小到大
4,学号由小到大
备注:
1,在能力范围内使代码更加简洁,代码执行更快。(程序优化)
2,合理备注(可读性)
3,可能用到的知识:动态数组定义(课本8.8)网络资料https://blog.

这是我写的代码:
#include <stdio.h>
#include <stdlib.h>

struct student
{
   char name[20];
   float score;
   int age;
   int num;
};
    void mode1( struct student stu[],int n)//参考书上307页(比较模式一)
{   n=4;
    struct student temp;//定义变量temp,排序时交换以便使用
    int i,j,k;
    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++ )
            if(stu[j].score>stu[k].score)//两两比较成绩,由高到低排列
            k=j;
        temp=stu[k];stu[k]=stu[i];stu[i]=temp;//进行交换比较
    }
}

    void mode2(struct student stu[],int n)//(比较模式二)
{   n=4;
    struct student temp;//定义变量temp,排序时交换以便使用
    int i,j,k;
    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++ )
            if(stu[j].name<stu[k].name)//两两比较姓名首字母,由A到Z排列
            k=j;
        temp=stu[k];stu[k]=stu[i];stu[i]=temp;//进行交换比较
    }
}

    void mode3(struct student stu[],int n)//(比较模式三)
{   n=4;
    struct student temp;//定义变量temp,排序时交换以便使用
    int i,j,k;
    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++ )
            if(stu[j].age<stu[k].age)//两两比较年龄,由小到大排列
            k=j;
        temp=stu[k];stu[k]=stu[i];stu[i]=temp;//进行交换比较
    }
}

    void mode4(struct student stu[],int n)//(比较模式四), int n=4;//定义变量n为学生人数4
{   n=4;
    struct student temp;//定义变量temp,排序时交换以便使用
    int i,j,k;
    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++ )
            if(stu[j].num<stu[k].num)//两两比较学号数字大小,由小到大排列
            k=j;
        temp=stu[k];stu[k]=stu[i];stu[i]=temp;//进行交换比较
    }
}
    int main()
{
    struct student stu[4]={{"zhangsan",65,18,211301},
    {"qiqi",89.5,17,211302},
    {"wanger",75.3,15,211303},
    {"sazi",93.7,19,211304}};//定义结构体数组,我理解为填写学生信息
    int m,i,n;
    n=4;
    scanf("%d",&m);
    for(i=0;i<n;i++)
       {printf("%8s  %6.2f  %d  %dn",stu[i].name,stu[i].score,stu[i].age,stu[i].num);}
       printf("n");
    if(m==1)
        mode1;
    else if(m==2)
           mode2;
    else if(m==3)
           mode3;
    else if(m==4)
           mode4;
    for(i=0;i<n;i++)
       {printf("%8s  %6.2f  %d  %dn",stu[i].name,stu[i].score,stu[i].age,stu[i].num);}

    return 0;


}
搜索更多相关主题的帖子: 比较 student int stu temp 
2022-01-03 22:48
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student
{
   char name[20];
   float score;
   int age;
   int num;
};

void student_print( const struct student stu[], size_t n )
{
    for( size_t i=0; i!=n; ++i )
        printf( "%8s  %6.2f  %d  %d\n", stu[i].name, stu[i].score, stu[i].age, stu[i].num );
}

int student_sort_cmp1( const void* a, const void* b ) // 成绩由大到小
{
    const struct student* p = (const struct student*)a;
    const struct student* q = (const struct student*)b;
    if( p->score > q->score ) return -1;
    if( p->score < q->score ) return +1;
    return 0;
}

int student_sort_cmp2( const void* a, const void* b ) // 姓名首字母由A到Z,听不懂是什么意思
{
    const struct student* p = (const struct student*)a;
    const struct student* q = (const struct student*)b;
    return strcmp(p->name,q->name);
}

int student_sort_cmp3( const void* a, const void* b ) // 年龄由小到大
{
    const struct student* p = (const struct student*)a;
    const struct student* q = (const struct student*)b;
    if( p->age < q->age ) return -1;
    if( p->age > q->age ) return +1;
    return 0;
}

int student_sort_cmp4( const void* a, const void* b ) // 学号由小到大
{
    const struct student* p = (const struct student*)a;
    const struct student* q = (const struct student*)b;
    if( p->num < q->num ) return -1;
    if( p->num > q->num ) return +1;
    return 0;
}

void student_sort( struct student stu[], size_t n, int (*comp)(const void*,const void*) )
{
    qsort( stu, n, sizeof(*stu), comp );
}

int main( void )
{
    struct student stu[] = { { "zhangsan", 65.0f, 18, 211301 }
                           , { "qiqi",     89.5f, 17, 211302 }
                           , { "wanger",   75.3f, 15, 211303 }
                           , { "sazi",     93.7f, 19, 211304 } };

    const size_t n = sizeof(stu)/sizeof(*stu);
    puts( "原始数据:" );
    student_print( stu, n );

    puts( "\n成绩由大到小:" );
    qsort( stu, n, sizeof(*stu), student_sort_cmp1 );
    student_print( stu, n );

    puts( "\n姓名首字母由A到Z:" );
    qsort( stu, n, sizeof(*stu), student_sort_cmp2 );
    student_print( stu, n );

    puts( "\n年龄由小到大:" );
    qsort( stu, n, sizeof(*stu), student_sort_cmp3 );
    student_print( stu, n );

    puts( "\n学号由小到大:" );
    qsort( stu, n, sizeof(*stu), student_sort_cmp4 );
    student_print( stu, n );
}


输出
原始数据:
zhangsan   65.00  18  211301
    qiqi   89.50  17  211302
  wanger   75.30  15  211303
    sazi   93.70  19  211304

成绩由大到小:
    sazi   93.70  19  211304
    qiqi   89.50  17  211302
  wanger   75.30  15  211303
zhangsan   65.00  18  211301

姓名首字母由A到Z:
    qiqi   89.50  17  211302
    sazi   93.70  19  211304
  wanger   75.30  15  211303
zhangsan   65.00  18  211301

年龄由小到大:
  wanger   75.30  15  211303
    qiqi   89.50  17  211302
zhangsan   65.00  18  211301
    sazi   93.70  19  211304

学号由小到大:
zhangsan   65.00  18  211301
    qiqi   89.50  17  211302
  wanger   75.30  15  211303
    sazi   93.70  19  211304

2022-01-04 12:57
斯玥
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2022-1-3
收藏
得分:0 
感谢感谢
2022-01-05 16:52
快速回复:我写的这个代码能运行,就是不执行命令,还有输出的是乱码。有没有哪位 ...
数据加载中...
 
   



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

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