| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1368 人关注过本帖
标题:求解惑告诉我这个程序哪有问题,自定义函数Calate2好像发生了越界访问
取消只看楼主 加入收藏
风说L
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2016-2-16
结帖率:100%
收藏
已结贴  问题点数:15 回复次数:2 
求解惑告诉我这个程序哪有问题,自定义函数Calate2好像发生了越界访问
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 30
#define M 6
struct student
{
    long studentID;
    char studentname[10];
    int score[6];
    int total;
    float aver;
    struct student *next;
};
struct student *Input_record(struct student *head,int n,int m);                   //1   
void Calate1(struct student *head,int m,int n);                       //2   
void Calate2(struct student *head,int n,int m);  //3
void main()
{
    int n,m;
    struct student *head=NULL;
    printf("how many subject?\n");
    scanf("%d",&m);
    printf("how many student?\n");
    scanf("%d",&n);
    head=Input_record(head,m,n);
    Calate1(head,m,n);
    Calate2(head,m,n);
}
struct student *Input_record(struct student *head,int m,int n)//1
{
    int i,j;
    struct student *p,*pr;
    for(i=1;i<=n;i++)
    {
        p=NULL;
        pr=head;
        p=(struct student *)malloc(sizeof(struct student));
        if(p==NULL)
        {
            printf("No enough memory to allocate!\n");
            exit(0);
        }
        if(head==NULL)
            head=p;
        else
        {
            while(pr->next!=NULL)
            {
                pr=pr->next;
            }
            pr->next=p;
        }
        printf("第%d个人的信息\n",i);
        printf("Input studentID:");
        scanf("%d",&p->studentID);
        printf("Input name:");
        scanf("%s",p->studentname);
        printf("Input score:");
        for(j=0;j<m;j++)
        {
        scanf("%d",&p->score[j]);
        }
        p->next=NULL;
    }
        return head;
}
void Calate1(struct student *head,int m,int n)//2
{
    int j,sum;
    float aver;
    struct student *p;
    for(j=0;j<m;j++)
    {
        p=head;
        sum=0;
        while(p!=NULL)
        {
            sum=sum+p->score[j];
            p=p->next;
        }   
        aver=(float)sum/n;
        printf("第%d门课程的总分和平均分为%5d%5.1f\n",j+1,sum,aver);
    }
}
void Calate2(struct student *head,int m,int n)//3
{
    int sum,i,j;
    float averscore;
    struct student *p;
    for(i=1;i<=n;i++)
    {   p=head;
        sum=0;
        for(j=0;j<m;j++)
        {
            sum=sum+p->score[j];
        }
        averscore=(float)sum/m;
        p->total=sum;
        p->aver=averscore;
        p=p->next;
        printf("ID:%ld,NAME:%s,TOTAL:%d\n",p->studentID,p->studentname,p->total);
    }
}
搜索更多相关主题的帖子: include 
2016-03-01 15:23
风说L
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2016-2-16
收藏
得分:0 
回复 2楼 qq1023569223
还不行
2016-03-03 07:43
风说L
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2016-2-16
收藏
得分:0 
回复 4楼 qq1023569223
恩恩  谢谢
2016-03-10 17:18
快速回复:求解惑告诉我这个程序哪有问题,自定义函数Calate2好像发生了越界访问
数据加载中...
 
   



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

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