您的位置:首页 > 其它

主函数创建5个学生的数组,写一个排序函数,让学生按姓名从小到大排序,主函数输出排序后的结果

2014-07-28 21:24 363 查看
//1.主函数创建5个学生的数组,写一个排序函数,让学生按姓名从小到大排序,主函数输出排序后的结果。
void sortStuByName(Student *pStu, int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (strcmp((pStu + j)->name, (pStu + j + 1)->name) > 0) {
char temp[20] = {0};
strcpy(temp, pStu + j + 1);
strcpy(pStu + j + 1, pStu + j);
strcpy(pStu + j, temp);
}
}
}
}
void outputStu(Student *pStu, int count) {
for (int i = 0; i < count; i++) {
printf("%s %d %d\n", (pStu + i)->name, (pStu + i)->age, (pStu + i)->score);
}
}
//2、写一个整型数组排序的函数,并测试效果。
void assignArr(int *pArr, int count) {
for (int i = 0; i < count; i++) {
*(pArr + i) = arc4random() % 50;
}
}
void sortArr(int *pArr, int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (*(pArr + j) > *(pArr + j + 1)) {
int temp = *(pArr + j + 1);
*(pArr + j + 1) = *(pArr + j);
*(pArr + j) = temp;
}
}
}

}
void outputArr(int *pArr, int count) {
for (int i = 0; i < count; i++) {
printf("%d ", *(pArr + i));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐