数据结构的一段程序,高手帮看看!

来源:百度知道 编辑:UC知道 时间:2024/07/03 05:01:02
本人新手,写了一段简单的数据结构程序,但是提示在第22行代码处有错误,高手帮忙看看!

求某班级学生分数由高到低,并输出学号和分数。

我这样写的:
#include<stdio.h>

#define MAXSIZE 10

typedef struct{
int NO;
int score;

}ElemType;

void sort(ElemType S[MAXSIZE],int n){
int t;
int i;
int j;

for(i=1;i<n;i++)
for(j=i;j<=n;j++){
scanf("%f,%f",&S[i],&S[j]);
if(S[i].score<S[j].score){
t=S[i];S[i]=S[j];S[j]=t;
}
for(i=1;i<=n;i++)
printf("%8d%8d%8d\n",i,S[i].NO,S[i].score);
}

}
就是再上面t=S[i];S[i]=S[j];S[j]=t;等号互换的地方提示等号错误,不知道错哪了帮忙解决下,谢谢!
谢谢,等号问题是解决了,可在添加t=(ElemType*)malloc(sizeof(ElemType))时却出现问题了,错误是binary '=' : no operator defined which takes a right-hand operand of type 'ElemType *' (or there is no acceptable conversion)关于分配地址的关键字malloc我还是不大会用,而且这个程序的main主函数我还是不会写,不知道在主函数里面应该写什么,该调用什么!帮忙解决后会追加15分!谢谢!

int t; 类型错误了 因为你右面有赋值语句t=S[i]; 而S类型是ElemType
你既然是用的静态结构 没必要用malloc了
我帮你写了段代码
你用用看吧
其实你要求的这段程序就用两个一维数组就足够了

#include<stdio.h>
#define MAXSIZE 10

typedef struct{
int NO;
int score;

}ElemType;

void sort(ElemType S[],int n)
{
ElemType t;
int i;
int flag=1;
while(flag==1)
{
flag=0;

for(i=n-1; i>0; i--)
if(S[i].score<S[i-1].score)
{
t=S[i];S[i]=S[i-1];S[i-1]=t; flag=1;
}

}
for(i=1;i<=n;i++)
printf("学号: %d 分数: %d\n",S[i-1].NO,S[i-1].score);
}

void main()
{
ElemType mm[MAXSIZE];
int n;
printf("输入人数n:");
scanf("%d",&n);
if(n<=MAXSIZE && n>0)
{

printf(" 输入学生信息\n\n");
int x=1;
for(;x<=n;x++)
{
printf("第%