| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 295 人关注过本帖
标题:C语言结构体输出的是分数低的,哪里错了
只看楼主 加入收藏
yuanxiubao
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-8-25
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
C语言结构体输出的是分数低的,哪里错了
#include<stdio.h>
#define N 3
struct xx
{
    char name[20];
    char sex;
    float score[4];
    float peraver;
} stu[N];
void input (struct xx *pstu, int n);
void jisuan (struct xx *pstu, int n, float *pall);
void compare (struct xx *pstu, int n);
void output (struct xx *pstu, int n, float *pall);
int main ()
{
    float allaver;
    input (stu, N);
    jisuan (stu, N, &allaver);
    compare (stu, N);
    output (stu, N, &allaver);

    return 0;
}
计算某班N个学生4门课程的平均成绩,输入姓名,性别,成绩,输出平均成绩高于N个学生总平均成绩的男生的信息
void input (struct xx *pstu, int n)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        printf ("请输入姓名:\n");
        scanf ("%s", pstu[i].name);
       getchar ();
        printf ("请输入性别:\n");
        scanf ("%c", &pstu[i].sex);
        getchar ();
        for (j = 0; j < 4; j++)
        {
            printf ("请输入成绩:\n");
            scanf ("%f", &pstu[i].score[j]);
        }
    }
}
void jisuan (struct xx *pstu, int n, float *pall)
{
    int i, j;
    float a = 0, b = 0;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < 4; j++)
        {
            a += pstu[i].score[j];
        }
        pstu[i].peraver = a / 4;
        b += pstu[i].peraver;
    }
    *pall = b / 4;
}

void compare (struct xx *pstu, int n)
{
    int i, j;
    struct xx temp;
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (pstu[i].peraver > pstu[j].peraver)
            {
                temp = pstu[i];
                pstu[i] = pstu[j];
                pstu[j] = temp;
            }
        }
    }
}
void output (struct xx *pstu, int n, float *pall)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        if (pstu[i].peraver < *pall && pstu[i].sex == 'm')
        {
            printf ("%s\t%c\t", pstu[i].name, pstu[i].sex);
            for (j = 0; j < 4; j++)
            {
                printf ("%f\t", pstu[i].score[j]);
            }
            printf ("\n");
        }
    }
}
搜索更多相关主题的帖子: 课程 结构体 C语言 
2011-08-25 21:39
A13433758072
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:广东潮州
等 级:小飞侠
威 望:1
帖 子:1182
专家分:2784
注 册:2010-7-22
收藏
得分:0 
回复 楼主 yuanxiubao


[ 本帖最后由 A13433758072 于 2011-8-26 03:05 编辑 ]

一步一个脚印...............................默默地前进.....
诚邀乐于解答c菜鸟问题,的热心网友加入,  QQ群38490319
2011-08-25 22:10
yuanxiubao
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-8-25
收藏
得分:0 
回复 2楼 A13433758072
if (pstu[i].peraver < *pall && pstu[i].sex == 'm')   我是说这样,运行结果是输出的比平均分高的分数,为什么?
2011-08-25 22:30
QQ346957135
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:148
专家分:658
注 册:2011-8-9
收藏
得分:20 
根据题意,compare函数没必要使用;jisuan函数内两处错误,另外就是那个if语句的错误,修改如下:
程序代码:
//C语言结构体输出的是分数低的,哪里错了
#include<stdio.h>
#define N 3
struct xx
{
    char name[20];
    char sex;
    float score[4];
    float peraver;
} stu[N];
void input (struct xx *pstu, int n);
void jisuan (struct xx *pstu, int n, float *pall);
void compare (struct xx *pstu, int n);
void output (struct xx *pstu, int n, float *pall);
int main ()
{
    float allaver;
    input (stu, N);
    jisuan (stu, N, &allaver);
    //compare (stu, N);
    output (stu, N, &allaver);

    return 0;
}
//计算某班N个学生4门课程的平均成绩,输入姓名,性别,成绩,输出平均成绩高于N个学生总平均成绩的男生的信息
void input (struct xx *pstu, int n)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        printf ("请输入姓名:\n");
        scanf ("%s", pstu[i].name);
        getchar ();
        printf ("请输入性别:\n");
        scanf ("%c", &pstu[i].sex);
        getchar ();
        for (j = 0; j < 4; j++)
        {
            printf ("请输入成绩:\n");
            scanf ("%f", &pstu[i].score[j]);
        }
    }
}
void jisuan (struct xx *pstu, int n, float *pall)
{
    int i, j;
    float a = 0, b = 0;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < 4; j++)
        {
            a += pstu[i].score[j];
        }
        pstu[i].peraver = a / 4;
        b += pstu[i].peraver;
        a=0;//a没有初始化
    }
    *pall = b / n;//b应该除以n
}

/*void compare (struct xx *pstu, int n)
{
    int i, j;
    struct xx temp;
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (pstu[i].peraver > pstu[j].peraver)
            {
                temp = pstu[i];
                pstu[i] = pstu[j];
                pstu[j] = temp;
            }
        }
    }
}*/
void output (struct xx *pstu, int n, float *pall)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        if (pstu[i].peraver> *pall && pstu[i].sex == 'm')//此处为>
        {
            printf ("%s\t%c\t", pstu[i].name, pstu[i].sex);
            for (j = 0; j < 4; j++)
            {
                printf ("%f\t", pstu[i].score[j]);
            }
            printf ("\n");
        }
    }
}

A real warrior never quits.
2011-08-25 23:08
zusjun
Rank: 1
等 级:新手上路
帖 子:8
专家分:5
注 册:2011-8-14
收藏
得分:0 
#include<stdio.h>
#define N 3
#include <malloc.h>
struct student
{
    char name[20];
    char sex;
    float score[4];
    float peraver;
} stu[N];
void input (struct student *pstu, int n);
float *jisuan (struct student *pstu, int n);
//void compare (struct student *pstu, int n);
void output (struct student *pstu, int n, float *pall);
int main ()
{
    //float allaver;
    float *p1;
    input (stu, N);
    p1=jisuan (stu, N);
    printf("%f\n",*p1);
    //compare (stu, N);
    output (stu, N, p1);

    return 0;
}
//计算某班N个学生4门课程的平均成绩,输入姓名,性别,成绩,输出平均成绩高于N个学生总平均成绩的男生的信息
void input (struct student *pstu, int n)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        printf ("请输入姓名:\n");
        scanf ("%s", &pstu[i].name);
       getchar ();
        printf ("请输入性别:\n");
        scanf ("%c", &pstu[i].sex);
        getchar ();
        for (j = 0; j < 4; j++)
        {
            printf ("请输入成绩:\n");
            scanf ("%f", &pstu[i].score[j]);
        }
    }
}
float *jisuan (struct student *pstu, int n)
{
    int i, j;
    float a = 0, b = 0;
    float *p;
    for (i = 0; i < n; i++)
    {
        a=0;
        for (j = 0; j < 4; j++)
        {
            a += pstu[i].score[j];
        }
         //a=0;
         pstu[i].peraver = a / 4;
    }
    for(i=0;i<n;i++)
    {
        b += pstu[i].peraver;
    }
    p=(float*)malloc (100);
    *p=b/3;
    return(p);
}

/*void compare (struct student *pstu, int n)
{
    int i, j;
    struct student temp;
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (pstu[i].peraver > pstu[j].peraver)
            {
                temp = pstu[i];
                pstu[i] = pstu[j];
                pstu[j] = temp;
            }
        }
    }
}*/
void output (struct student *pstu, int n, float *pall)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        if (pstu[i].peraver < (*pall) && pstu[i].sex == 'm')
        {
            printf ("%s\t%c\t", pstu[i].name, pstu[i].sex);
            for (j = 0; j < 4; j++)
            {
                printf ("%f\t", pstu[i].score[j]);
            }
            printf ("\n");
        }
    }
}
我给写好了,你写的程序里边compare根本在这里边没什么作用,不知道你写了这个干什么,我给改好了,我的这个完全可以运行。
2011-08-26 00:06
快速回复:C语言结构体输出的是分数低的,哪里错了
数据加载中...
 
   



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

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