郁闷,这程序崩了,我也快崩了,大家帮忙找找原因

ylwdi 2009-12-20 12:09:36
有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生数据,要求打印出3门课总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课的成绩、平均分数)。
要求用input函数输入10个学生数据;用average函数求总平均分;用max函数找出最高分的学生数据;总平均分和最高分学生的数据都在主函数中输出。
#include"malloc.h"
#include"stdio.h"
#include"string.h"
#include"math.h"
#define SIZE 10
#define NUM 3
typedef struct student /*define one struct*/
{
char sno[SIZE];
char name[SIZE];
float score[NUM];
float s_sum;
}stu;
void main(void) /*main function*/
{
void input(stu*p_one); /*input_func instruction*/
stu*max(stu*p_two,float s_sum,float record);/*max_func instruction*/
float average(float s_sum);/*.....*/
int i;
int j=0;
float sum=0;
float record=0;
stu*q;
stu*p;
p=(stu*)malloc(SIZE*sizeof(stu));/*Dynamic allocation ten space*/
if(p=NULL)printf("can't");
input(p);
p->s_sum=0;
while(j<SIZE)
{for(i=0;i<NUM;i++) /*find everyone's sum total score*/
p->s_sum+=p->score[i];
sum+=p->s_sum; /*total score*/
q=max(p,p->s_sum,record);/*the max's adress*/
record=q->s_sum;
p++;
j++;
}
printf("please output the max_score_student's information:\n");
printf("sno name score1 score2 score3 s_sum\n");
printf("%-8s%-9s",q->sno,q->name);
for(j=0;j<NUM;j++)
printf("%-6.1f",q->score[j]);
printf("%f",q->s_sum);
printf("please output the total_average:%f",average(sum));
}
void input(stu*p_one)
{
int i,j;
for(i=1;i<=SIZE;i++,p_one++)
{
printf("please input the %d student's information:\n",i);
printf("sno:");
scanf("%s",&(p_one->sno));
scanf("name:");
printf("%s",&(p_one->name));
for(j=0;j<NUM;j++)
{ printf("score%d:",j);
scanf("%f",&(p_one->score[j]));
}
printf("\n");
}
}
stu*max(stu*p_two,float s_sum,float record)
{
stu*flag;
if(record<s_sum)
flag=p_two;
return flag;
}
float average(float s_sum)
{
return s_sum/SIZE;
}
刚输入一个学号就崩了,。。郁闷
...全文
496 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
dingxueshu 2010-01-13
  • 打赏
  • 举报
回复

for(j=0;j <NUM;j++)
scanf("%f",&p->scor[j]);
总是这句出现问题?
dingxueshu 2010-01-13
  • 打赏
  • 举报
回复
#include <stdio.h>
#define NUM 3
typedef struct
{
char name[10];
float scor[NUM];
}stu;

void input(stu *p)
{
int i,j;
for(i=0;i<NUM;i++,p++)
{
printf("Name:\n");
scanf("%s",&p->name);
printf("Scor:\n");
for(j=0;j<NUM;j++)
scanf("%f",&p->scor[j]);
}
}
void main()
{
stu*p[NUM],*q;
q=p;
input(p);
}

26楼的方法很好,但是如果不用数组,而用指针我在调试这个程序是也总是一闪而过...
smallbear_2008 2010-01-13
  • 打赏
  • 举报
回复
调着调着就进步了,呵呵,vc或者gdb单步跑一下!增强对程序的亲切感!
gww009 2010-01-13
  • 打赏
  • 举报
回复
呵呵 加油 刚刚我也调试一个程序 调试了2个小时
vanchristin 2010-01-13
  • 打赏
  • 举报
回复
楼主很幸运,找到了CSDN~
许文君 2010-01-12
  • 打赏
  • 举报
回复
看你的程序我也崩溃了。。。

if(常量==变量) 最好是这么写
dingxueshu 2010-01-12
  • 打赏
  • 举报
回复
该程序崩溃在main函数中调用input(p);

在input函数中,第二句中for(i=1;i <=SIZE;i++,p_one++)这个p_one++就是一个普通的指针它不是一个数组指针,你应该在实参中p就应该是个指针数组,,,
Ps_kandy 2010-01-12
  • 打赏
  • 举报
回复
楼主。~
给你个建议。~
程序越简单越好。~
还有。~ 你的排版实在太好了。~
如果换你是我老师的学生的话,就惨了。~。!
ylwdi 2010-01-12
  • 打赏
  • 举报
回复

修改之后能运行,也不崩溃了,但是输出的个人总成绩和总平均成绩都是错误的.
代码如下,请再帮忙找下错误,实在找不到哪里有错了,,,,,
#include"malloc.h"
#include"stdio.h"
#include"string.h"
#include"math.h"
#define SIZE 10
#define NUM 3
typedef struct student /*define one struct*/
{
char sno[SIZE];
char name[SIZE];
float score[NUM];
float s_sum;
}stu;
stu *p;
void input(stu*p_one); /*input_func instruction*/
stu*max(stu*p_two,float record);/*max_func instruction*/
float average(float s_sum);/*.....*/
void main(void) /*main function*/
{

int j,i;
float sum=0;
float record=0;
stu*q,*p1;
stu*stub;
p1=stub=(stu*)malloc(SIZE*sizeof(stu));
printf("plese input the data:\n");
input(stub);
p=stub;
for(j=0;j<SIZE;j++,p1++)
{for(i=0;i<NUM;i++)
p1->s_sum+=p1->score[i];
sum+=p1->s_sum;
q=max(p1,record);/*the max's adress*/
record=q->s_sum;

}

printf("please output the max_score_student's information:\n");
printf("sno name score1 score2 score3 s_sum\n");
printf("%-8s%-9s",q->sno,q->name);
for(j=0;j <NUM;j++)
printf("%-6.1f",q->score[j]);
printf("%f",q->s_sum);
printf("please output the total_average:%f",average(sum));
}
void input(stu*p_one)
{
int i,j;
for(i=1;i<=SIZE;i++,p_one++)
{
printf("please input the %d student's information:\n",i);
printf("sno:");
scanf("%s",p_one->sno);
printf("name:");
scanf("%s",p_one->name);
for(j=0;j<NUM;j++)
{ printf("score%d:",j);
scanf("%f",&(p_one->score[j]));
}

printf("\n");
}

}
stu*max(stu*p1, float record)
{

if(record<(p1->s_sum))
p=p1 ;
return p;
}
float average(float s_sum)
{
return (s_sum/SIZE);
}
jernymy 2010-01-12
  • 打赏
  • 举报
回复
简单测试了一下,使用全局的数组


#include "malloc.h"
#include "stdio.h"
#include "string.h"
#include "math.h"

#define STR_LEN 10 // sno or name max len
#define MAX_NUM 10 // student num
#define SCORE_NUM 3

typedef struct student /*define one struct*/
{
char sno[STR_LEN];
char name[STR_LEN];
float score[SCORE_NUM];
float s_sum;
}stu;

stu tStudent[MAX_NUM] = {0}; // stu *p;此处如果使用指针,那么内存分配与
// 释放问题要考虑好了,要不然内存泄露或越界的了
void input(void); /*input_func instruction*/
//stu *max(void); /*max_func instruction*/
int max(void); /*max_func instruction*/
float average(int nMaxIdx); /*.....*/

void main(void) /*main function*/
{
int nLoop;
int nMaxIdx;

input();
nMaxIdx = max(); /* the max's adress */

printf("please output the max_score_student's information:\n");
printf("sno name score1 score2 score3 s_sum\n");
printf("%-8s%-9s", tStudent[nMaxIdx].sno, tStudent[nMaxIdx].name);
for (nLoop = 0; nLoop < SCORE_NUM; nLoop++)
{
printf("%-6.1f",tStudent[nMaxIdx].score[nLoop]);
}
printf("%f", tStudent[nMaxIdx].s_sum);
printf("\n");
printf("please output the total_average:%f", (float)average(nMaxIdx));
}

void input(void)
{
int i,j;

printf("plese input the data:\n");
for (i=1; i<=MAX_NUM; i++)
{
printf("please input the %d student's information:\n",i);
printf("sno:");
scanf("%s", tStudent[i].sno);
printf("name:");
scanf("%s", tStudent[i].name);
for (j=0; j<SCORE_NUM; j++)
{
printf("score%d:", j);
scanf("%f", &(tStudent[i].score[j]));
tStudent[i].s_sum += tStudent[i].score[j];
}
printf("\n");
}
}

int max(void)
{
int nIdx;
int nMaxIdx = 0;
float fMaxSum = tStudent[0].s_sum;

for (nIdx = 1; nIdx < MAX_NUM; nIdx++)
{
if (fMaxSum < tStudent[nIdx].s_sum)
{
fMaxSum = tStudent[nIdx].s_sum;
nMaxIdx = nIdx;
}
}

return nMaxIdx;
}

float average(int nIdx)
{
return (tStudent[nIdx].s_sum/SCORE_NUM);
}


ajuncgpcqz 2010-01-12
  • 打赏
  • 举报
回复
路过学习一下
gelu1040 2010-01-12
  • 打赏
  • 举报
回复
放下屠刀,立地成佛
cavalier_man 2009-12-22
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 happy_yw 的回复:]
stu*max(stu*p_two,float s_sum,float record)
{
    stu*flag;
    if(record <s_sum)
    flag=p_two;
    return flag;
}

明显错误,不能返回栈中的指针flag;
这个函数重新写一下吧

[/Quote]

恩,这个函数你写的有问题,我给你改成了这样的,可以运行
stu* max(stu* p_two,float record)
{
if(record < p_two->s_sum)
{
record=p_two->s_sum;
return p_two;
}

}
会飞的老鱼 2009-12-21
  • 打赏
  • 举报
回复
你这个问题貌似指针越界导致程序崩溃,你把你那句

p=(stu*)malloc(SIZE*sizeof(stu));/*Dynamic allocation ten space*/
if(p=NULL)printf("can't");

改成

p=(stu*)malloc((SIZE+1)*sizeof(stu));/*Dynamic allocation ten space*/
if(p=NULL)printf("can't");

即多分配一个结构体,再试试,输入10个学生数据应该不会崩溃了吧
selooloo 2009-12-21
  • 打赏
  • 举报
回复
改成这样,可保程序不崩
stu p;
input(&p);
后面的形式也要相应改变
happy_yw 2009-12-21
  • 打赏
  • 举报
回复
程序又仔细看了一下:
感觉可读性很差,兄弟写程序最好用标准的变量命名形式
程序崩溃的地方应该就是我上边说的:
在函数中不能返回指向栈的指针,因为退出函数的时候你申请的空间已经释放了,这样你在函数外边跟本就找不到它了,指针乱指一气,导致崩溃
happy_yw 2009-12-21
  • 打赏
  • 举报
回复
stu*max(stu*p_two,float s_sum,float record)
{
stu*flag;
if(record<s_sum)
flag=p_two;
return flag;
}

明显错误,不能返回栈中的指针flag;
这个函数重新写一下吧
zhangwenlang 2009-12-21
  • 打赏
  • 举报
回复
敢问楼主你不会F10吗?
jack_wq 2009-12-21
  • 打赏
  • 举报
回复
debug一下难道查不出来?
  • 打赏
  • 举报
回复
楼主,要补基础啊,我都补了一学期了!,,,,!
加载更多回复(12)

69,371

社区成员

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

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