| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 562 人关注过本帖
标题:[求助]为什么这个程序运算不正确
只看楼主 加入收藏
liqingyang
Rank: 2
等 级:论坛游民
帖 子:47
专家分:39
注 册:2010-2-24
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:4 
[求助]为什么这个程序运算不正确
计算一组学生的平均成绩和不及格人数。用结构指针变量作函数参数编程
struct stu
{
    int    num;
    char*    name;
    char    sex;
    float    score;
}boy[5]={
                {101,"li ming",'B',88.5},
                {102,"wang lu",'G',92.3},
                {103,"zhang tian",'B',58},
                {104,"lu ming",'B',34},
                {105,"fu li",'G',84},
            };
            
main()
{
    int i;
    struct stu* ps;
    void avge(struct stu* ps);
    ps=boy;
    for(i=0;i<5;i++,ps++)
        printf("%d\t%s\t%c\t%f\n",ps->num,ps->name,ps->sex,ps->score);   
   
    avge(ps);   
    getch();
    return;
}

void avge(struct stu* ps)
{
    int i,c=0;
    float k=0,sum;
    for(i=0;i<5;i++,ps++);
    {
        sum+=ps->score;
        if(ps->score<60)
            c++;
    }
    printf("sum=%f\n",sum);
    k=sum/5;
    printf("avge=%f\tc=%d\n",k,c);
}
为什么输出的结果不是我想要的。
搜索更多相关主题的帖子: 运算 
2010-04-20 17:05
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:7 
struct stu
{
    int    num;
    char    *name; //如果只用作只读的,可以定义成这样,如果要可写,要给它申请空间,或者定义成数组形式
    char    sex;
    float    score;
}boy[5]={
                {101,"li ming",'B',88.5},
                {102,"wang lu",'G',92.3},
                {103,"zhang tian",'B',58},
                {104,"lu ming",'B',34},
                {105,"fu li",'G',84},
            };
int main()
{
    int i;
    struct stu* ps;
    void avge(struct stu* ps);
    ps=boy;
    for(i=0;i<5;i++,ps++)
        printf("%d\t%s\t%c\t%f\n",ps->num,ps->name,ps->sex,ps->score);

    ps=boy;//重新使ps指向第一个元素
    avge(ps);
    getchar();
    return 0;
}
void avge(struct stu* ps)
{
    int i,c=0;
    float k=0,sum = 0;                                                                                            
    for(i=0;i<5;i++,ps++)//; 这里多了个分号                                                            
    {                                                                                                            
        sum+=ps->score;                                                                                          
        if(ps->score<60)                                                                                          
            c++;                                                                                                  
    }                                                                                                            
    printf("sum=%f\n",sum);                                                                                       
    k=sum/5;                                                                                                      
    printf("avge=%f\tc=%d\n",k,c);                                                                                
}      
2010-04-21 09:02
hanzhenddd
Rank: 5Rank: 5
等 级:职业侠客
帖 子:90
专家分:361
注 册:2010-3-4
收藏
得分:3 
闲的无聊把你的程序修改了一下  试运行正常!!
程序代码:
#include <stdio.h>

struct stu
{
    int    num;
    char    name[20];
    char    sex;
    float    score;
}boy[5]={
                {101,"li ming",'B',88.5},
                {102,"wang lu",'G',92.3},
                {103,"zhang tian",'B',58},
                {104,"lu ming",'B',34},
                {105,"fu li",'G',84},
            };

float  show (const struct stu *p)
{
    int i;
    float sum = 0;

    for (i = 0; i < 5; i++,p++)
       {
            sum+= p->score;
            printf ("%5d%10s%3c  %.3f\n",p->num, p->name, p->sex, p->score);
       }

    return sum;
}
int main (int argc, char *argv[])
{
    float sum, everage;
    struct stu *p;

    p = boy;
    sum = show(p);
    everage = sum/5;
    //my_sum(p);

    printf ("\nsum = %.3f , everage = %.3f\n", sum, everage);

    return 0;
}
=================
  101   li ming  B  88.500
  102   wang lu  G  92.300
  103zhang tian  B  58.000
  104   lu ming  B  34.000
  105     fu li  G  84.000

sum = 356.800 , everage = 71.360

Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.

2010-04-21 12:11
b001lcp
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:45
专家分:157
注 册:2010-3-17
收藏
得分:0 
LS的程序中漏了计算不及格人数了,其它的都正确
2010-04-21 12:43
hanzhenddd
Rank: 5Rank: 5
等 级:职业侠客
帖 子:90
专家分:361
注 册:2010-3-4
收藏
得分:0 
以下是引用b001lcp在2010-4-21 12:43:04的发言:

LS的程序中漏了计算不及格人数了,其它的都正确
我看楼主没写。。我也就没弄。。其实也就是帮他找找错误而已。。
2010-04-21 13:16
快速回复:[求助]为什么这个程序运算不正确
数据加载中...
 
   



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

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