主题:[讨论]排序
这是一个学生信息排序,学生信息有姓名和成绩,
用直接排序法和冒泡排序法,
我的程序排序无法输入正确的东西,请帮我看看.谢
#include"stdlib.h"
#include"stdio.h"
#include"string.h"
#define M 50
typedef struct stu
{
char name[20];
int mark;
}student;
int input(student *data)
{
int i=1;
printf("You can input the information\n");
printf("input the name of %d\n",i);
gets(data[i].name);
while(strcmp(data[i].name,"end")!=0)
{
printf("input the mark\n");
scanf("%d",data[i].mark);
i++;
if(i>M) printf("the data is full");
printf("input the name of %d\n",i);
getchar();
gets(data[i].name);
}
return i-1;
}
void straight(student *data)
{
int i,j;
for(i=2;i<=M;i++)
{
data[0]=data[i];
j=i-1;
while(data[0].mark<data[j].mark)
{
data[j+1]=data[j];
j--;
}
data[j+1]=data[0]
}
}
void bubblesort(student *data)
{
int i,j;
for(i=1;i<M;i++)
for(j=1;j<=M-i;j++)
if(data[j+1].mark<data[j].mark)
{
data[0]=data[j];
data[j]=data[j+1];
data[j+1]=data[0];
}
}
void print(student *data,int n)
{
int i;
for(i=1;i<=n;i++)
{
puts(data[i].name);
printf("%d \n",data[i].mark);
}
}
main()
{
student data[M];
int count;
clrscr();
count=input(data);/*输入学生信息*/
printf("count is %d\n",count);
if(count!=0)
{
printf("Straight:\n");
straight(data);
print(data,count);
}
if(count!=0)
{
printf("Bubblesort:\n");
bubblesort(data);
printf(data,count);
}
if(count==0)
printf("No data!");
}
用直接排序法和冒泡排序法,
我的程序排序无法输入正确的东西,请帮我看看.谢
#include"stdlib.h"
#include"stdio.h"
#include"string.h"
#define M 50
typedef struct stu
{
char name[20];
int mark;
}student;
int input(student *data)
{
int i=1;
printf("You can input the information\n");
printf("input the name of %d\n",i);
gets(data[i].name);
while(strcmp(data[i].name,"end")!=0)
{
printf("input the mark\n");
scanf("%d",data[i].mark);
i++;
if(i>M) printf("the data is full");
printf("input the name of %d\n",i);
getchar();
gets(data[i].name);
}
return i-1;
}
void straight(student *data)
{
int i,j;
for(i=2;i<=M;i++)
{
data[0]=data[i];
j=i-1;
while(data[0].mark<data[j].mark)
{
data[j+1]=data[j];
j--;
}
data[j+1]=data[0]
}
}
void bubblesort(student *data)
{
int i,j;
for(i=1;i<M;i++)
for(j=1;j<=M-i;j++)
if(data[j+1].mark<data[j].mark)
{
data[0]=data[j];
data[j]=data[j+1];
data[j+1]=data[0];
}
}
void print(student *data,int n)
{
int i;
for(i=1;i<=n;i++)
{
puts(data[i].name);
printf("%d \n",data[i].mark);
}
}
main()
{
student data[M];
int count;
clrscr();
count=input(data);/*输入学生信息*/
printf("count is %d\n",count);
if(count!=0)
{
printf("Straight:\n");
straight(data);
print(data,count);
}
if(count!=0)
{
printf("Bubblesort:\n");
bubblesort(data);
printf(data,count);
}
if(count==0)
printf("No data!");
}