| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1361 人关注过本帖
标题:求解惑告诉我这个程序哪有问题,自定义函数Calate2好像发生了越界访问
只看楼主 加入收藏
风说L
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2016-2-16
结帖率:100%
收藏
已结贴  问题点数:15 回复次数:5 
求解惑告诉我这个程序哪有问题,自定义函数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
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:11 
程序代码:
//别的不知道,看出两个逻辑错误
void Calate2(struct student *head,int m,int n)//3
{
    int sum,i,j;
    float averscore;
    struct student *p=head;
    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;
        printf("ID:%ld,NAME:%s,TOTAL:%d\n",p->studentID,p->studentname,p->total);
        p=p->next;  //先输出完再指向下一个
    }
}

   唯实惟新 至诚致志
2016-03-01 15:36
风说L
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2016-2-16
收藏
得分:0 
回复 2楼 qq1023569223
还不行
2016-03-03 07:43
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
你可以参考下面的代码写,你的逻辑很混乱。
程序代码:
#include <stdio.h>
#include <stdlib.h>

struct num
{
    int data;
    struct num *next;
};

struct num *create(int n);
void display(struct num *head);
void destory(struct num *head);
void calAnddisplay(struct num *head);

//issue:http://bbs.bccn.net/thread-461827-1-1.html
int main()
{
    struct num *head;
    int n;
    scanf("%d",&n);

    head=create(n);
    //display(head);
    calAnddisplay(head);

    destory(head);

    return 0;
}

struct num *create(int n)
{
    struct num *head,*p,*q;
    head=(struct num *)malloc(sizeof(struct num));
    p=(struct num *)malloc(sizeof(struct num));
    head->next=p;

    scanf("%d",&head->data);
    int i;
    for(i=1;i<n;i++)
    {
        scanf("%d",&p->data);
        q=(struct num *)malloc(sizeof(struct num));
        p->next=q;
        p=q;

        if(i==(n-1))
        {
            p->next=NULL;
        }
    }

    return head;
}

void display(struct num *head)
{
    struct num *p=head;
    do
    {
        printf("%d ",p->data);
        p=p->next;
    }while(p->next!=NULL);
}

void destory(struct num *head)
{
    struct num *p=head,*q;
    do
    {
        q=p->next;
        free(p);
        p=q;
    }while(p->next!=NULL);
    free(p);
}

void calAnddisplay(struct num *head)
{
    struct num *p=head;

    //A1
    int A1=0;
    do
    {
        if(p->data%10==0)
        {
            A1+=p->data;
        }
        p=p->next;
    }while(p->next!=NULL);

    //A2
    p=head;
    int A2=0,countA2=0;
    do
    {
        if(p->data%5==1)
        {
            countA2++;
            if(countA2%2==0)
            {
                A2-=p->data;
            }
            else
            {
                A2+=p->data;
            }
        }
        p=p->next;
    }while(p->next!=NULL);

    //A3
    p=head;
    int A3=0;
    do
    {
        if(p->data%5==2)
        {
            A3++;
        }
        p=p->next;
    }while(p->next!=NULL);

    //A4
    p=head;
    float A4=0,sum=0;
    int countA4=0;
    do
    {
        if(p->data%5==3)
        {
            countA4++;
            sum+=p->data;
        }
        p=p->next;
    }while(p->next!=NULL);
    if(countA4!=0)
    {
        A4=sum/countA4;
    }

    //A5
    p=head;
    int A5=0,flag=0,tmp=0;
    do
    {
        if(p->data%5==4)
        {
            if(flag==0)
            {
                A5=p->data;
                flag=1;
            }
            else
            {
                tmp=p->data;
                if(tmp>A5)
                {
                    A5=tmp;
                }
            }
        }
        p=p->next;
    }while(p->next!=NULL);

    //output
    if(A1==0)
    {
        printf("N ");
    }
    else
    {
        printf("%d ",A1);
    }

    if(countA2==0)
    {
        printf("N ");
    }
    else
    {
        printf("%d ",A2);
    }

    if(A3==0)
    {
        printf("N ");
    }
    else
    {
        printf("%d ",A3);
    }

    if(countA4==0)
    {
        printf("N ");
    }
    else
    {
        printf("%.1f ",A4);
    }

    if(A5==0)
    {
        printf("N");
    }
    else
    {
        printf("%d",A5);
    }
}

   唯实惟新 至诚致志
2016-03-03 08:23
风说L
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2016-2-16
收藏
得分:0 
回复 4楼 qq1023569223
恩恩  谢谢
2016-03-10 17:18
hzxsyzl
Rank: 2
等 级:论坛游民
威 望:1
帖 子:24
专家分:68
注 册:2016-3-8
收藏
得分:0 
学习了。谢谢。
2016-03-10 19:27
快速回复:求解惑告诉我这个程序哪有问题,自定义函数Calate2好像发生了越界访问
数据加载中...
 
   



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

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