| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 285 人关注过本帖
标题:【求助函数问题】请大神们帮看看这个程序问题出在哪里
只看楼主 加入收藏
more5039
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-3-24
结帖率:0
收藏
已结贴  问题点数:10 回复次数:2 
【求助函数问题】请大神们帮看看这个程序问题出在哪里
目的:(用于对各个学生的分数从小到大排列)键盘输入学生的个数 n,通过3个函数分别对n个学生的成绩进行输入,排列,输出。
下面是程序代码,请各位看看问题出在哪里。

# include <stdio.h>
# include <malloc.h>

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

//输入函数;

void Input(void)
{
    int i, len;
    struct Student * pArr;
    //char name[100];
    //float score;
    pArr = (struct Student *)malloc(len * sizeof (struct Student));
    for(i=0; i<len; i++)
    {
        printf("请输入第%d个学生的信息:\n", i);
        
        printf("age=");
        scanf("%d", &pArr[i].age);

        printf("score=");
        scanf("%f", &pArr[i].score);

        printf("name=");
        scanf("%s", pArr[i].name);
    }
}

//输出函数

void Output()
{
    int i, len;
    struct Student * pArr;
    //char name[100];
    //float score;
    pArr = (struct Student *)malloc(len * sizeof (struct Student));
    for(i=0; i<len; i++)
    {
        printf("第%d个学生的信息是:\n", i);
        
        printf("age = %d", pArr[i].age);
        printf("score = %f", pArr[i].score);
        printf("name = %s", pArr[i].name);        
    }
}

void change()
{
    int i, j, len, age;
    struct Student * pArr;
    struct Student t;
    char name[100];
    float score;
   
    for(i=0; i<len-1; i++)
    {
        for(j=0; j<len-1-i; j++)
        {
            if(pArr[j].score>pArr[j+1].score)
            {
                t = pArr[j];
                pArr[j] = pArr[j+1];
                pArr[j+1] = t;
            
            }
        }
    }
}


int main(void)
{
    int i, len;
    struct Student * pArr;
    pArr = (struct Student *)malloc(len * sizeof (struct Student));
    printf("请输入学生的个数:\n");
    printf("len = ");
    scanf("%d", &len);

    void Input(void);
    void change();
    void Output();
   

    return 0;
}
搜索更多相关主题的帖子: include 
2012-09-18 15:49
heishu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:42
专家分:131
注 册:2012-9-7
收藏
得分:0 
程序代码:
今天才看到这个帖子,首先:

void Input(void);
    void change();
    void Output();

这个是函数声明不是函数调用。
还有程序中出现很多不必要的代码,不知楼主对变量的作用域了解多少。
这是我改动后的程序:
程序代码:
# include <stdio.h>
# include <malloc.h>

struct Student
{
    int age;
    float score;
    char name[100];
};
int len;  //声明为全局变量
struct Student * pArr;

//输入函数;

void Input()
{
    int i;
    //struct Student * pArr;
    //char name[100];
    //float score;
    pArr = (struct Student *)malloc(len * sizeof (struct Student));
    for(i=0; i<len; i++)
    {
        printf("请输入第%d个学生的信息:\n", i);
       
        printf("age=");
        scanf("%d", &pArr[i].age);

        printf("score=");
        scanf("%f", &pArr[i].score);

        printf("name=");
        scanf("%s", pArr[i].name);
    }
}

//输出函数

void Output()
{
    int i;
   
    //char name[100];
    //float score;
    //pArr = (struct Student *)malloc(len * sizeof (struct Student));
    for(i=0; i<len; i++)
    {
        printf("第%d个学生的信息是:\n", i);
       
        printf("age = %d", pArr[i].age);
        printf("score = %f", pArr[i].score);
        printf("name = %s", pArr[i].name);       
    }
}

void change()
{
    int i, j,age;
    struct Student t;
    //char name[100];
    //float score;
   
    for(i=0; i<len-1; i++)
    {
        for(j=0; j<len-1-i; j++)
        {
            if(pArr[j].score>pArr[j+1].score)
            {
                t = pArr[j];
                pArr[j] = pArr[j+1];
                pArr[j+1] = t;
           
            }
        }
    }
}


int main(void)
{
    int i;
    //struct Student * pArr;
   // pArr = (struct Student *)malloc(len * sizeof (struct Student));
    printf("请输入学生的个数:\n");
    printf("len = ");
    scanf("%d", &len);
    Input();
    change();
    Output();
    

    return 0;
} 
运行后:
图片附件: 游客没有浏览图片的权限,请 登录注册

如图。
如有不对请高手指教

[qq]1402050187[/qq]
2012-09-19 11:49
heishu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:42
专家分:131
注 册:2012-9-7
收藏
得分:10 
没分

[qq]1402050187[/qq]
2012-09-21 10:37
快速回复:【求助函数问题】请大神们帮看看这个程序问题出在哪里
数据加载中...
 
   



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

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