| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 571 人关注过本帖
标题:输入学生信息后,然后进行排序,然后输入,为什么会出错
只看楼主 加入收藏
laotianshun
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-11-5
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
输入学生信息后,然后进行排序,然后输入,为什么会出错
#include <stdio.h>
#include <malloc.h>
struct Student//定义一个结构体
{
        int age;
        char name[100];
        float score;
        
};

        
int main (void)
{
        int len;
        int i,j;
        struct Student t;                                           //parr[j]不是int类型,所以t不能是int类型
        struct Student *parr;                                       //定义一个struct Student *类型的指针变量 parr
        printf("请输入有多少个学生\n");
        printf("个数:");
        scanf("%d",&len);                                         
        parr=(struct Student *)malloc(len*sizeof(struct Student));   //定义一个动态数组
                                                                                                                          
        for (i=0;i<len;i++)                                         //for循环输入学生的信息
        {
                printf("请输入第%d个学生的信息:\n",i+1);

                printf("age=");
                scanf("%d",&parr.age);

                printf("name=");
                scanf("%s",parr.name);

                printf("score=");
                scanf("%f",&parr.score);
        }


        for (i=0;i<len-1;i++)     //冒泡排序
        {
                for (j=0;i<len-1-i;j++)
                {
                        if (parr[j].score > parr[j+1].score)     //比较的是struct Student里的成绩,不是整个struct Student里的内容
                                {
                                                t = parr[j];          //parr[j]不是int类型,所以t不能是int类型
                                  parr[j] = parr[j+1];
                                        parr[j+1] = t;               //当成绩比较完后,把分数高的“整体”和分数低的“整体”互换
                                }                                 
                        }
                }
        
        printf("\n\n学生的信息:\n");


        for (i=0;i<len;i++)                  //for循环输出学生的信息
        {
                printf("以下是第%d学生的信息\n",i+1);
                printf("age\n",parr.age);
                printf("name\n",parr.name);
                printf("score\n",parr.score);
        }

return 0;
}

  
图片附件: 游客没有浏览图片的权限,请 登录注册



能不能把“for循环输入输出学生的信息”还有“排序”用函数去写?
变成
#include <stdio.h>
#include<malloc.h>
void for1()
{
}
void paixu()
{
}
void for2()
{
}
int main (void)
{
……………………
  for1();
  paixu();
  for2();
  return 0;
}
搜索更多相关主题的帖子: 学生 信息 include 结构体 
2013-04-26 20:08
罗庇鹏ksq
Rank: 5Rank: 5
来 自:太平洋
等 级:职业侠客
帖 子:220
专家分:310
注 册:2012-6-30
收藏
得分:10 
  for (i=0;i<len;i++)                                         //for循环输入学生的信息
        {
                printf("请输入第%d个学生的信息:\n",i+1);

                printf("age=");
                scanf("%d",&parr.age);

                getchar();

                printf("name=");
                scanf("%s",parr.name);
                 
                printf("score=");
                scanf("%f",&parr.score);

                getchar();
        }

从来都是无所谓,现在也该学着有所谓。✿咱们一个人,别坐井观天❀
2013-04-26 23:07
罗庇鹏ksq
Rank: 5Rank: 5
来 自:太平洋
等 级:职业侠客
帖 子:220
专家分:310
注 册:2012-6-30
收藏
得分:0 
还有      
//当成绩比较完后,把分数高的“整体”和分数低的“整体”互换……那里错了
        parr是指针,循环输入里面咋没看到偏移呀?一直在原地互换!!

从来都是无所谓,现在也该学着有所谓。✿咱们一个人,别坐井观天❀
2013-04-26 23:15
hsjjgm
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:106
专家分:189
注 册:2013-4-27
收藏
得分:10 
都是新手,交流下,下面是我写的,可以运行:
#include<stdio.h>
#include<malloc.h>

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

int main(void)
{
    int len,i,j,k,n;

    printf("请输入学生人数:");
    scanf("%d", &len);
    struct student * parr = (struct student *)malloc(sizeof(struct student)*len);

    for(i = 0; i < len; i++)
    {
        printf("请输入第%d个学生的信息", i+1);

        printf("age= ");
        scanf("%d", &((parr+i)->age));
        printf("name= ");
        scanf("%s", &((parr+i)->name));
        printf("score= ");
        scanf("%f", &((parr+i)->score));
    }

    for(j = 0; j < len-1; j++)
    {
        for(k = 0; k < len-1-j; k++)
        {
            if((parr+k)->score < (parr+k+1)->score)
            {
                struct student t;

                t = *(parr+k);
                *(parr+k) = *(parr+k+1);
                *(parr+k+1) = t;

            }
        }
    }

    for(n = 0;n < len; n++)
    {
        printf("以下是%s的成绩", (parr+n)->name);
        printf("年龄是%d\n", (parr+n)->age);
        printf("名字是%s\n", (parr+n)->name);
        printf("分数是%f\n", (parr+n)->score);
    }

    return 0;

}
我还写了个函数的,功能扩充了一点点:
#include<stdio.h>
#include<malloc.h>

struct student
{
    int ID;
    char name[50];
    int age;
    float score;
};

void input(struct student **, int);
void output(struct student **, int);
void sort(struct student **, int);

int main(void)
{
    int num;
    printf("请输入学生个数:num =");
    scanf("%d", &num);
   
    struct student * pArra =(struct student *)malloc(num * sizeof(struct student));
   
    input(&pArra, num);
    sort(&pArra, num);
    output(&pArra, num);
    return 0;
}

void input(struct student ** pArra,int num)
{
    int i;
    for(i = 0; i < num; i++)
    {
        printf("请输入第%d个学生的信息:\n", i+1);
        printf("学号");
        scanf("%d", &((*pArra+i)->ID));
        printf("姓名:\n");
        scanf("%s", &((*pArra+i)->name));
        printf("年龄:\n");
        scanf("%d", &((*pArra+i)->age));
        printf("分数:\n");
        scanf("%f", &((*pArra+i)->score));
    }
   
}

void output(struct student ** pArra,int num)
{
    int ID,i,j;
    char ch;
    do
    {
        printf("按1查询全部,按0精确查询");
        scanf("%d", &j);
        if(j == 1)
        {
            for(i = 0; i < num; i++)
            {
                printf("学号: %d\n", (*pArra+i)->ID);
                printf("姓名: %s\n", (*pArra+i)->name);
                printf("年龄:%d\n", (*pArra+i)->age);
                printf("分数: %f\n", (*pArra+i)->score);
                printf("\n");
               
            }
        }
        else if(j == 0)
        {
            printf("请输入要查询的学生学号:");
            scanf("%d", &ID);
            for(i = 0;i < num; i++)
            {
                if(ID == (*pArra+i)->ID)
                {
                    printf("以下是该学生的信息:\n");
                    printf("学号: %d\n", (*pArra+i)->ID);
                    printf("姓名: %s\n", (*pArra+i)->name);
                    printf("年龄:%d\n", (*pArra+i)->age);
                    printf("分数: %f\n", (*pArra+i)->score);
                    
                }
            }
        }
        else
            printf("请输入 0 或 1 查询\n");
        
        printf("是否要继续查询 Y/N\n");
        scanf(" %c", &ch);
        
    }while('Y' == ch||'y' == ch);
}

void sort(struct student ** pArra,int num)
{
    int i,j;
    float t;
    for(i = 0; i < num-1; i++)
    {
        for(j=0; j < num-1-i; j++)
        {
            if(((*pArra+j)->score) < ((*pArra+j+1)->score))
            {
                t = (*pArra+j)->score;
                (*pArra+j)->score = (*pArra+j+1)->score;
                ((*pArra+j+1)->score) = t;
            }
        }
        
    }
}
希望对你有帮助
2013-04-27 17:10
快速回复:输入学生信息后,然后进行排序,然后输入,为什么会出错
数据加载中...
 
   



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

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