| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1509 人关注过本帖
标题:指向结构体指针变量做形参
只看楼主 加入收藏
浅暗花璃
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2016-3-31
结帖率:77.78%
收藏
已结贴  问题点数:10 回复次数:1 
指向结构体指针变量做形参
#include <stdio.h>
#include <stdlib.h>
struct student
{
    char num[9];
    char name[50];
    char sex;
    double score[4];
    double aver;
}stu[2];
    void input(struct student stu[]);

    void f(struct student stu[]);
     void output(struct student *p);

void main()
{
    struct student stu[2];
     
    input(stu);
    f(stu);
   
   
    output(stu);

}

    void input(struct student stu[])
    {
        int i,j;
        printf("plase input the information:\n");
    for(i=0;i<2;i++)
    {
        printf("plase input the %d num:",i);
        scanf("%s",&stu[i].num );
        printf("plase input the %d name:",i);
        scanf("%s",&stu[i].name);
          fflush(stdin);
        printf("plase input the %d sex  m-女,n-男:",i);
        scanf("%c",&stu[i].sex);
         fflush(stdin);
      
        for(j=0;j<4;j++)
        {
            printf("plase input the %d score:",i);
              scanf("%lf",&stu[i].score[j]);
        }
    }
    }
    void f(struct student stu[] )
    {   int i,j,k;   
        double sum=0;
        struct student t;

        for(i=0;i<2;i++)
        {
            for(j=0;j<4;j++)
                sum+=stu[i].score[j];
            stu[i].aver=sum/4.0;
            printf("%lf",stu[i].aver);
        }
        for(i=0;i<2;i++)
        {
            k=i;
            for(j=i+1;j<2;j++)
                if(stu[k].aver>stu[j].aver)
                    k=j;
            
               
                if(k!=i)
                {  
                    t=stu[k];
                    stu[k]=stu[j];
                    stu[j]=t;
                }
            
        }
    }
        void output(struct student *p)
        {
            
            int j;
            //int struct student stu[2];
               p=malloc(sizeof(struct student));
            for(p=stu;p<stu+2;p++)
            
            {   
             printf("%s\n",p->num);
             printf("%s\n",p->name);
             printf("%c\n",p->sex);
             for(j=0;j<4;j++)
                printf("%lf\n",p->score[j]);
               printf("%lf\n",p->aver);
            }
        }
         
求指教,输出函数哪里不对了

[此贴子已经被作者于2016-5-22 16:05编辑过]

搜索更多相关主题的帖子: include double 结构体 
2016-05-22 16:03
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:10 
程序代码:
#include <stdio.h>
#include <stdlib.h>

struct student
{
    char num[9];
    char name[50];
    char sex;
    double score[4];
    double aver;
} stu[2];

void input(struct student stu[]);
void f(struct student stu[]);
void output(struct student *p);

main()
{
    input(stu);
    f(stu);
    output(stu);
}

void input(struct student stu[])
{
    int i,j;
    printf("plase input the information:\n");
    
    for(i=0; i<2; i++)
    {
        printf("\nplase input the %d num:",i);
        scanf("%s",&stu[i].num );
        printf("plase input the %d name:",i);
        scanf("%s",&stu[i].name);
        fflush(stdin);
        printf("plase input the %d sex  m-女,n-男:",i);
        scanf("%c",&stu[i].sex);
        fflush(stdin);

        for(j=0; j<4; j++)
        {
            printf("plase input the %d score:",i);
            scanf("%lf",&stu[i].score[j]);
        }
    }
}

void f(struct student stu[])
{
    int i,j;
    double sum=0;
    struct student t;
    
    for(i=0; i<2; i++)
    {
        sum = 0;
        for(j=0; j<4; j++)
            sum += stu[i].score[j];
        stu[i].aver = sum/4.0;
        printf("%lf\n",stu[i].aver);
    }
    
    for(i=0; i<2; i++)
    {
        for(j=i+1; j<2; j++)
        {
            if(stu[i].aver > stu[j].aver)
            {
                t = stu[i];
                stu[i] = stu[j];
                stu[j] = t;
            }
        }
    }
}

void output(struct student *p)
{
    for(int i=0; i<2; i++,p++)
    {
        printf("%s\n",p->num);
        printf("%s\n",p->name);
        printf("%c\n",p->sex);
        for(int j=0; j<4; j++)
            printf("%lf\n",p->score[j]);
        printf("%lf\n",p->aver);
    }
}
2016-05-22 17:14
快速回复:指向结构体指针变量做形参
数据加载中...
 
   



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

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