结构体

客英 2019-05-17 03:02:22
#include "stdio.h" #include "stdlib.h" #define M 3 struct stu {int no; char name[20]; int score[3]; int sum; }; struct stu s[M]; input(struct stu a[]) /*输入学生数据*/ { int i,j; printf("请输入M个学生的学号、姓名、3门成绩\n"); for(i=0; i<M; i++) { scanf("%d",&a[i].no); scanf("%s",a[i].name); for(j=0;j<3;j++) scanf("%d",&a[i].score[j]); } } calc(struct stu a[]) /* 计算每个学生三门课总分 */ {/************found************/ int i,j; for(i=0;i<3;i++) { a[i].sum=0; for(j=0;j<3;j++) a[i].sum+=a[i].score[j];}; } max(struct stu a[]) /* 计算总分最高的学生的下标 */ {/************found************/ double m=a[0].sum; int i; for(i=1;i<3;i++) if(m<a[i].sum) m=a[i].sum; return i; } pnt(struct stu b) /* 在屏幕上输出总分最高的学生信息 */ {/************found************/ int i; printf("%6d\n",b.no); printf("%s\n",b.name); for(i=0;i<3;i++) printf("%5d",b.score[i]); printf("%6d",b.sum); } main() { int m; input(s); calc(s); m=max(s); pnt(s[m]); system("pause"); } 请问以上代码错在哪?没有得到正确的运行结果
...全文
86 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
客英 2019-05-28
  • 打赏
  • 举报
回复
请问?错了,为什,打不为什么打这么哪里
客英 2019-05-28
  • 打赏
  • 举报
回复
#include "stdio.h" #include "stdlib.h" struct list { int data; struct list *next; }; struct list *creatlist() { struct list *p,*q,*h; int a; h=(struct list *)malloc(sizeof(struct list)); /* 申请一个头指针 */ p=q=h; printf("Input an int number,enter -1 to end:\n"); scanf("%d",&a); while(a!=-1) { p=(struct list *)malloc(sizeof(struct list)); /* 申请一个结点空间 */ p->data=a; q->next=p; /************found************/ p->next =p; scanf("%d",&a); } p->next='\0' ; return(h); } void outlist(struct list *q) { struct list *p; p=q->next; while(p!=NULL) { printf("%5d\n",p->data); /************found************/ p=p->next ; } } int amax(struct list *head) { struct list *p; int max; p=head->next; max=p->data; p=p->next; while(p!=NULL) { if (max<p->data ) max=p->data; /************found************/ p=p->next; } return max; } main() { int mm; struct list *head; head=creatlist(); outlist(head); mm=amax(head); printf("max=%d\n",mm); system("pause"); }
客英 2019-05-28
  • 打赏
  • 举报
回复
程序填空题
源程序中的3个函数用于链表数据的输入输出及求结点的最大值,当输入数据为-1时输入结束。请完善程序,源程序为tk7_3.c。
CaptainXue 2019-05-17
  • 打赏
  • 举报
回复
你自定义的函数前面怎么没有加函数类型呢?函数的类型是根据是否有返回值,或者返回着的类型决定的。比如:如果你的函数只是纯粹的实现输出数据,或者不需要返回一个值,这个时候函数的类型就是void。如果有返回值,函数的类型一般就和返回值的数据类型一致。可以看看函数这个文章https://blog.csdn.net/weixin_43956598/article/details/90055036
周末ZhouMo 2019-05-17
  • 打赏
  • 举报
回复
引用 4 楼 jsx_SEVEN 的回复:
默认返回值类型不一定适合所有函数,应该按楼上说的加上返回值类型。 还有就是在计算总分最高的函数里加一句i-=1;循环结束时i成了3,在主函数里pnt(s[m]);m是3,越界了,C语言不会自动检测这种情况。

int mymax(struct stu a[])      /* 计算总分最高的学生的下标 */
{
	double m = a[0].sum;
	int i;
	for (i = 1; i < 3; i++)
		if (m < a[i].sum)
			m = a[i].sum;
	i -= 1;
	return i;
}
有点问题,i只是遍历用的,应该改成这样

int mymax(struct stu a[])      /* 计算总分最高的学生的下标 */
{
	double m = a[0].sum;
	int i,j=0;
	for (i = 1; i < 3; i++)
	{
		if (m < a[i].sum)
		{
			m = a[i].sum;
			j = i;               //用 j 保存最大值得下标
		}
	}
	return j;		
}
周末ZhouMo 2019-05-17
  • 打赏
  • 举报
回复
默认返回值类型不一定适合所有函数,应该按楼上说的加上返回值类型。 还有就是在计算总分最高的函数里加一句i-=1;循环结束时i成了3,在主函数里pnt(s[m]);m是3,越界了,C语言不会自动检测这种情况。

int mymax(struct stu a[])      /* 计算总分最高的学生的下标 */
{
	double m = a[0].sum;
	int i;
	for (i = 1; i < 3; i++)
		if (m < a[i].sum)
			m = a[i].sum;
	i -= 1;
	return i;
}
Italink 2019-05-17
  • 打赏
  • 举报
回复
给函数签名加上返回值,没有返回值用void

#include "stdio.h"
#include "stdlib.h"
#define  M  3 
struct stu
{
	int    no;
	char   name[20];
	int    score[3];
	int    sum;
};
struct stu s[M];
void input(struct stu a[])  /*输入学生数据*/
{
	int i, j;
	printf("请输入M个学生的学号、姓名、3门成绩\n");
	for (i = 0; i < M; i++)
	{
		scanf("%d", &a[i].no);
		scanf("%s", a[i].name);
		for (j = 0; j < 3; j++)
			scanf("%d", &a[i].score[j]);
	}
}
void calc(struct stu a[])  /* 计算每个学生三门课总分 */
{/************found************/
	int i, j;
	for (i = 0; i < 3; i++)
	{
		a[i].sum = 0;
		for (j = 0; j < 3; j++)
			a[i].sum += a[i].score[j];
	};



}
int max(struct stu a[])      /* 计算总分最高的学生的下标 */
{/************found************/
	double m = a[0].sum;
	int i;
	for (i = 1; i < 3; i++)
		if (m < a[i].sum)
			m = a[i].sum;
	return i;

}
void  pnt(struct stu b)   /* 在屏幕上输出总分最高的学生信息  */
{/************found************/
	int i;
	printf("%6d\n", b.no);
	printf("%s\n", b.name);
	for (i = 0; i < 3; i++)
		printf("%5d", b.score[i]);
	printf("%6d", b.sum);
}
int main()
{
	int m;
	input(s);
	calc(s);
	m = max(s);
	pnt(s[m]);
	system("pause");
	return 0;
}
客英 2019-05-17
  • 打赏
  • 举报
回复
这儿事回怎么灵像字。改怎么请问边
wshyhm 2019-05-17
  • 打赏
  • 举报
回复
函数都没有返回类型,你怎么可能运行通过的了?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧