
在学生管理系统中 按成绩排序 按学号查找 请各位大侠帮忙看看问题出在哪里 (C语言编写)
/*排序与查找*/#include"stdio.h"#include"conio.h"#defineMAXSIZE100typedefintkeytype;typedef...
/*排序与查找*/
#include "stdio.h"
#include "conio.h"
#define MAXSIZE 100
typedef int keytype;
typedef struct st
{
char name[20];
char num[9]; /*学号*/
double score[4]; /*四门成绩*/
double gread; /*总分*/
}datatype;
typedef struct
{
datatype r[MAXSIZE+1];
int length;
}student,*pstudent;
pstudent creat(void)
/*建立*/
{
pstudent s;
int i;
i=1;
s=(pstudent)malloc(sizeof(student));
printf("Enter the length of the list:");
scanf("%d",&s->length);
while(i<=s->length)
{
printf("Enter the name,num,scores of the students:");
scanf("%s",s->r[i].name);
scanf("%s",s->r[i].num);
scanf("%lf%lf%lf",&s->r[i].score[0],&s->r[i].score[1],&s->r[i].score[2],&s->r[i].score[3]);
s->r[i].gread=s->r[i].score[0]+s->r[i].score[1]+s->r[i].score[2]+s->r[i].score[3];
i++;
}
return (s);
}
/*插入排序*/
void insert(pstudent *s)
{
int i,j;
for(i=2;i<=(*s)->length;i++)
{
(*s)->r[0]=(*s)->r[i];
j=i-1;
while((*s)->r[0].gread<(*s)->r[j].gread)
{
(*s)->r[j+1]=(*s)->r[j];
j--;
}
(*s)->r[j+1]=(*s)->r[0];
}
}
/*顺序表的查找*/
int search(pstudent s,char num[9])
{
int i,j;
for(i=0;i<s->length;i++)
{ for(j=0;j<9;j++)
{
if(s->r[i].num[i]!=num[i])
break;
}
if(j==9)
return (i);
}
return -1;
}
void input(pstudent s)
{
int i;
for(i=1;i<=s->length;i++)
{
printf("Enter the information of %dth student:",i);
printf("%s\t",s->r[i].name);
printf("%s\t",s->r[i].num);
printf("%5.2lf\t%5.2lf\t%5.2lf\t%5.2lf\t",s->r[i].score[0],s->r[i].score[1],s->r[i].score[2],s->r[i].score[3]);
printf("%lf\t",s->r[i].gread);
}
}
main()
{
pstudent s;
char num[10];
s=creat();
printf("The paixu based on the num:\n");
insert(&s);
input(s);
printf("\n");
printf("Enter num[10]:");
scanf("%s",num);
printf("The results are:");
printf("%s\n",s->r[search(s,num)].name);
printf("%s\n",s->r[search(s,num)].num);
printf("%5.2lf%5.2lf%5.2lf%5.2lf\t",s->r[search(s,num)].score[0],s->r[search(s,num)].score[1],s->r[search(s,num)].score[2],s->r[search(s,num)].score[3]);
printf("%5.2lf",s->r[search(s,num)].gread);
getch();
}
问题是可以编译 但是结果是不能连续输入信息 展开
#include "stdio.h"
#include "conio.h"
#define MAXSIZE 100
typedef int keytype;
typedef struct st
{
char name[20];
char num[9]; /*学号*/
double score[4]; /*四门成绩*/
double gread; /*总分*/
}datatype;
typedef struct
{
datatype r[MAXSIZE+1];
int length;
}student,*pstudent;
pstudent creat(void)
/*建立*/
{
pstudent s;
int i;
i=1;
s=(pstudent)malloc(sizeof(student));
printf("Enter the length of the list:");
scanf("%d",&s->length);
while(i<=s->length)
{
printf("Enter the name,num,scores of the students:");
scanf("%s",s->r[i].name);
scanf("%s",s->r[i].num);
scanf("%lf%lf%lf",&s->r[i].score[0],&s->r[i].score[1],&s->r[i].score[2],&s->r[i].score[3]);
s->r[i].gread=s->r[i].score[0]+s->r[i].score[1]+s->r[i].score[2]+s->r[i].score[3];
i++;
}
return (s);
}
/*插入排序*/
void insert(pstudent *s)
{
int i,j;
for(i=2;i<=(*s)->length;i++)
{
(*s)->r[0]=(*s)->r[i];
j=i-1;
while((*s)->r[0].gread<(*s)->r[j].gread)
{
(*s)->r[j+1]=(*s)->r[j];
j--;
}
(*s)->r[j+1]=(*s)->r[0];
}
}
/*顺序表的查找*/
int search(pstudent s,char num[9])
{
int i,j;
for(i=0;i<s->length;i++)
{ for(j=0;j<9;j++)
{
if(s->r[i].num[i]!=num[i])
break;
}
if(j==9)
return (i);
}
return -1;
}
void input(pstudent s)
{
int i;
for(i=1;i<=s->length;i++)
{
printf("Enter the information of %dth student:",i);
printf("%s\t",s->r[i].name);
printf("%s\t",s->r[i].num);
printf("%5.2lf\t%5.2lf\t%5.2lf\t%5.2lf\t",s->r[i].score[0],s->r[i].score[1],s->r[i].score[2],s->r[i].score[3]);
printf("%lf\t",s->r[i].gread);
}
}
main()
{
pstudent s;
char num[10];
s=creat();
printf("The paixu based on the num:\n");
insert(&s);
input(s);
printf("\n");
printf("Enter num[10]:");
scanf("%s",num);
printf("The results are:");
printf("%s\n",s->r[search(s,num)].name);
printf("%s\n",s->r[search(s,num)].num);
printf("%5.2lf%5.2lf%5.2lf%5.2lf\t",s->r[search(s,num)].score[0],s->r[search(s,num)].score[1],s->r[search(s,num)].score[2],s->r[search(s,num)].score[3]);
printf("%5.2lf",s->r[search(s,num)].gread);
getch();
}
问题是可以编译 但是结果是不能连续输入信息 展开
2个回答
2011-06-07
展开全部
在结构体中定义的浮点数,C语言没有加载浮点格式库,一种修改方法为:
scanf("%lf%lf%lf",&s->r[i].score[0],&s->r[i].score[1],&s->r[i].score[2],&s->r[i].score[3]);
改为
double score;
scanf("%lf",&score);
s->r[i].score[0]=score;
scanf("%lf%lf%lf",&s->r[i].score[1],&s->r[i].score[2],&s->r[i].score[3]);
// scanf("%lf%lf%lf",&s->r[i].score[0],&s->r[i].score[1],&s->r[i].score[2],&s->r[i].score[3]);
scanf("%lf%lf%lf",&s->r[i].score[0],&s->r[i].score[1],&s->r[i].score[2],&s->r[i].score[3]);
改为
double score;
scanf("%lf",&score);
s->r[i].score[0]=score;
scanf("%lf%lf%lf",&s->r[i].score[1],&s->r[i].score[2],&s->r[i].score[3]);
// scanf("%lf%lf%lf",&s->r[i].score[0],&s->r[i].score[1],&s->r[i].score[2],&s->r[i].score[3]);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询