|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
程序进行到一半出现
Unhandled exception in 001.exe:0xC0000005:Access Violation.
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)//student结构大小
#define STU sizeof(struct teacher)//teacher结构大小
struct student *stu();//创建链表
float average_number(int *p);//求平均数
struct student
{
float score;
struct student *next;
};
struct teacher
{
float average;
struct teacher *next;
};
int n,m;// n为有多少老师打分,m为有多少学生参加考试
struct student *stu()//构建链表
{
struct student *head = NULL;
struct student *p1,*p2;
float score;
n = 1;
do
{
printf("\n第%d位老师的分数:",n);
scanf("%f",&score);
if(score != 0) //输入0时结束输入
{
p1 = (struct student *)malloc(LEN);
p1->score = score;
p1->next = NULL;
if(head == NULL)
{
head = p1;
p2 = p1;
}
else
{
p2->next = p1;
p2 = p1;
}
n++;
}
else
{
break;
}
}while(1);
return head;
}
float average_number(struct student *head)
{
struct student *p1,*p2,*number;
int i;
float t,sum,average;
p1 = number = head;
p2 = head->next;
for(i = 1;i < (n-1);i++)
{
while(p1->next != NULL)
{
if(p1->score < p2->score)
{
t = p1->score;
p1->score = p2->score;
p2->score = t;
p1 = p2;
p2 = p2->next;
}
else
{
p2 = p1;
p2 = p2->next;
}
}
}
sum = 0;
for(i = 1;i <= (n-3);i++)
{
number = number->next;
sum = sum + number->score;
number = number->next;
}
average = sum/(n-3);
return average;
}
void main()
{
struct teacher *p1,*p2,*head = NULL;
int i;
float t,number;
do
{
m = 1;
printf("第%d位同学的得分:",m);
stu();
p1 = (struct teacher *)malloc(STU);
number = average_number(head);
p1->average = number;
p1->next = NULL;
if(head == NULL)
{
head = p1;
p2 = p1;
}
else
{
p2->next = p1;
p2 = p1;
}
m++;
}while(getchar() != '!');
p1 = head;
p2 = head->next;
for(i = 1;i < (m-1);i++)
{
while(p1->next != NULL)
{
if(p1->average < p2->average)
{
t = p1->average;
p1->average = p2->average;
p2->average = t;
p2 = p1;
p2 = p2->next;
}
else
{
p2 = p1;
p2 = p2->next;
}
}
}
p1 = head;
for(i = 1;i < (m-1);i++)
{
printf("%s",p1->average);
p1 = p1->next;
}
}
|
|