| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2164 人关注过本帖
标题:一个有关指向结构体数组的指针的问题
只看楼主 加入收藏
haichuan
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2006-3-7
收藏
 问题点数:0 回复次数:6 
一个有关指向结构体数组的指针的问题
如下程序功能是:从主函数中输入5个学生的数据记录,从print函数输出这些纪录。 #include<stdio.h>
struct student
{
int num;
char name[20];
float score[3];
}stu[5];
main()
{

void print(struct student *);
int i;
printf("input 5 students' information:\n");
for(i=0;i<=4;i++)
scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
print(stu);
}
void print(struct student *p)
{
int i;
printf("No. name score1 score2 score3\n");
for(i=0;i<=4;i++,p++)
printf("%4d %-20s %f %f %f\n",(*p).num,(*p).name,(*p).score[0],(*p).score[1],(*p).score[2]);
} 编译连接都没有错,但是结果如下,不太正确
input 5 students' information:
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87 /*我懒省劲,重复输入12312,hanfang,78.5,98.5,87五次*/
No. name score1 score2 score3
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
Press any key to continue
搜索更多相关主题的帖子: 结构体 指针 
2006-04-14 15:38
haichuan
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2006-3-7
收藏
得分:0 
我的问题就是为什么出现那么多0.000000 0.000000 0.000000
2006-04-14 15:40
lj_860603
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:714
专家分:0
注 册:2006-1-25
收藏
得分:0 
程序大体没错``只是输入格式有问题:
for(i=0;i<=4;i++)
scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
//改为%d%s%f%f%f

我的原则很简单:不做不喜欢的事!
2006-04-14 16:29
rellorsky
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-4-14
收藏
得分:0 

查看一下scanf()的调用方法,按照lj_860603 的方法可以实现

2006-04-14 16:39
soft_wind
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:1430
专家分:0
注 册:2006-4-5
收藏
得分:0 
以下是引用haichuan在2006-4-14 15:38:00的发言:
如下程序功能是:从主函数中输入5个学生的数据记录,从print函数输出这些纪录。 #include<stdio.h>
struct student
{
int num;
char name[20];
float score[3];
}stu[5];
main()
{

void print(struct student *);
int i;
printf("input 5 students' information:\n");
for(i=0;i<=4;i++)
scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
print(stu);
}
void print(struct student *p)
{
int i;
printf("No. name score1 score2 score3\n");
for(i=0;i<=4;i++,p++)
printf("%4d %-20s %f %f %f\n",(*p).num,(*p).name,(*p).score[0],(*p).score[1],(*p).score[2]);
} 编译连接都没有错,但是结果如下,不太正确
input 5 students' information:
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87 /*我懒省劲,重复输入12312,hanfang,78.5,98.5,87五次*/
No. name score1 score2 score3
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
Press any key to continue

当你输入时,%s使得你输入的huangfeng,78.5,98.5,87都被当成一个字符串存到stu[i].name中,而后面的就没有进行赋值了,所以当然是0.0000000000000咯!
你可以这样改:scanfscanf("%d%s%f%f%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
在输入每个值的时候,都回车一下,估计就行了,我只是分析了原因,具体没去试,你可以再调试一下。


对不礼貌的女生收钱......
2006-04-14 17:15
百年不亮
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:789
专家分:0
注 册:2006-4-14
收藏
得分:0 

你的main()函数不够简洁,我增加了计算平均分并按成绩排序输出前5名的功能,
我修改后是:

#include<stdio.h>
#define SIZE 5
struct stud_score
{
long num;
char name[10];
double score[3];
double average;
};

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

void main()
{
struct stud_score student[SIZE];
input(student,SIZE);
sort(student,SIZE);
output(student);
}

void input(struct stud_score *student,int size)
{
int i;
printf("请按以下格式输入:\n\n"
"学号\t姓名\t分数1\t分数2\t分数3 \n");
for(i=0;i<size;i++)
{
printf("请输入第%d组数据\n>",i+1);
scanf(" %d %s %lf %lf %lf",
&student[i].num,&student[i].name,
&student[i].score[0],&student[i].score[1],&student[i].score[2]);
student[i].average=(student[i].score[0]+student[i].score[1]+student[i].score[2])/3;
}

}


void sort(struct stud_score *student,int size)
{
int i,j;
struct stud_score temp;
for(i=0;i<size-1;i++)
for(j=i+1;j<size;j++)
{
if(student[i].average<student[j].average)
{
temp=student[i];
student[i]=student[j];
student[j]=temp;
}
}
}


void output(struct stud_score *student)
{
int i;

printf("成绩前五是:\n");
printf("学号\t姓名\t分数1\t分数2\t分数3\t平均成绩\n");
for(i=0;i<5;i++)
printf(" %d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n",
student[i].num, student[i].name,
student[i].score[0], student[i].score[1], student[i].score[2],
student[i].average);
}

2006-04-14 17:16
流年226
Rank: 1
等 级:新手上路
帖 子:37
专家分:7
注 册:2013-4-27
收藏
得分:0 
同意楼上的。
2013-05-12 19:06
快速回复:一个有关指向结构体数组的指针的问题
数据加载中...
 
   



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

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