该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
/************************************************************************
2013年6月3日 16:23:38
目的:输入若干个学生信息,按分数的升序进行排序后进行输出
************************************************************************/
#include
#include
struct Student
{
char name[10];
int age;
float score;
};
void inputStudent(struct Student ** point, int nTh); // 输入第 nTh 个学生信息函数
void outputStudent(struct Student ** point, int nTh); // 输出第 nTh 个学生信息函数
void sortStudent(struct Student ** point, int nLen); // 对 nLen 个学生按分数升序排序
int main(void)
{
struct Student * pArr;
int nLen;
int nCnt;
/************************************************************************
输入学生人数并为其分配内存空间
************************************************************************/
printf("请输入学生人数:\n");
scanf("%d", &nLen);
pArr = (struct Student *)malloc(nLen * sizeof(struct Student));
for (nCnt = 0; nCnt < nLen; ++nCnt)
{
printf("请输入第 %d 个学生信息:\n", nCnt + 1);
inputStudent(&pArr, nCnt);
}
sortStudent(&pArr, nLen);
printf(">>>>>>>>>>>输出学生信息>>>>>>>>>>>\n");
for (nCnt = 0; nCnt < nLen; ++nCnt)
{
outputStudent(&pArr, nCnt);
}
return 0;
}
void inputStudent(struct Student ** point, int nTh) // 输入第 nTh 个学生信息函数
{
printf("name: ");
scanf("%s", &(*point + nTh) -> name);
printf("age: ");
scanf("%d", &(*point + nTh) -> age);
printf("score: ");
scanf("%f", &(*point + nTh) -> score);
return ;
}
void outputStudent(struct Student ** point, int nTh) // 输出第 nTh 个学生信息函数
{
printf("第 %d 个学生生信息:\n", nTh + 1);
printf("name: ");
puts((*point + nTh) -> name);
printf("age: %d\n", (*point + nTh) -> age);
printf("score: %.2f\n", (*point + nTh) -> score);
printf("\n");
return ;
}
void sortStudent(struct Student ** point, int nLen) // 对 nLen 个学生按分数升序排序
{
int nCnt1, nCnt2;
// struct Student stTmp;
struct Student * pTmp;
for (nCnt1 = 0; nCnt1 < nLen -1; ++nCnt1)
{
for (nCnt2 = nCnt1 + 1; nCnt2 < nLen; ++nCnt2)
{
if ((*point + nCnt1) -> score > (*point + nCnt2) -> score)
{
/* stTmp = *(*point + nCnt1);
*(*point + nCnt1) = *(*point + nCnt2);
*(*point + nCnt2) = stTmp;
*/
pTmp = (*point + nCnt1);
(*point + nCnt1) = (*point + nCnt2); // error
(*point + nCnt2) = pTmp; // error
}
}
}
return ;
}
代码贴上,请各位领导批示...