概述
使用单链表进行排序见上一篇文章,这两篇文章相辅相承;
传送门:点我即达(。・ω・。)
****************************************************************************************************************************************
★首先附上一系列运行结果截图:
1:学生信息的初始化
2:排序功能列表
——使用了system("cls") 和 gotoxy() 等函数;
3:排序界面-姓名
4:排序界面-学号
5:排序界面-分数
*文件会被写到同级目录下的这四个文件中,下面代码中也可以分析得出。
★源代码:
★ 由于代码基于某些要求进行编写,可能有些复杂,但是基本原理仍然未变,只是多了些表示方法;
// 学生排序(函数作为参数传入).cpp : 定义控制台应用程序的入口点。
// 需要使用文件进行输出
// 要求函数结构void student_sort(STUDENT *pStu, int n, int(*pFunc)(STUDENT &, STUDENT &));
#include <string.h>
#include <iostream>
//文件的操作
#include <fstream>
//调用Sleep()/system()函数
#include <windows.h>
using namespace std;
typedef struct stu
{
char name[20];
int id;
float score;
}STUDENT;
STUDENT * Init_Stu( int number)
{
ofstream out("学生信息.txt");
out << "姓名 学号 分数n";
STUDENT *pStu = (STUDENT *)malloc(sizeof(STUDENT) * number); //开辟number个空间
cout << "Please Enter the stu'infomation: " << endl;
cout << "姓名 学号 分数" << endl;;
for (int i = 0; i < number; i++)
{
cin >> (pStu + i)->name;
cin >> (pStu + i)->id;
cin >> (pStu + i)->score;
}
cout << "n开始写入文件" << endl;
for (int i = 0; i < number; i++)
{
out << (pStu + i)->name << " ";
out << (pStu + i)->id << " ";
out << (pStu + i)->score << "n";
}
out.close();
cout << "……" << endl; Sleep(1000);
cout << "……" << endl; Sleep(1000);
cout << "文件成功写入!" << endl;
cout << "文件名为“Student_by_name.txt”" << endl << endl;
system("pause");
return pStu;
}
int pFunc_name(STUDENT *x, STUDENT *y)
{
if (strcmp(x->name, y->name) < 0)
{
return 0;
}
else
{
return 1;
}
}
int pFunc_id(STUDENT *x, STUDENT *y)
{
if (x->id > y->id)
{
return 1;
}
else
{
return 0;
}
}
int pFunc_score(STUDENT *x, STUDENT *y)
{
if (x->score > y->score)
{
return 1;
}
else
{
return 0;
}
}
void gotoxy(short x, short y) //自定义光标定位
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void student_sort_name(STUDENT *pStu, int n, int (*pFunc)(STUDENT *x, STUDENT *y))
{
STUDENT *p;
p = pStu;
STUDENT *temp;
temp = (STUDENT*)malloc(sizeof(STUDENT));
int k,j; //循环使用
for (int i = 1; i < n; i++)
{
p = pStu;
for (j = n - 1, k = 0; j >= i; j--,k++)
{
if (pFunc_name((p+k), (p + k+1)))
{
strcpy(temp->name, (p + k)->name);
strcpy((p + k)->name, (p + k+1)->name);
strcpy((p + k + 1)->name,temp->name);
p = pStu;
temp->id = (p + k)->id;
(p + k)->id = (p + k + 1)->id;
(p + k + 1)->id = temp->id;
p = pStu;
temp->score = (p + k)->score;
(p + k)->score = (p + k + 1)->score;
(p + k + 1)->score = temp->score;
}
}
}
cout << "是否要将排序结果写入文件?(文件名为“Student_by_name.txt”)" << endl;
for (int i = 0; i < n; i++)
{
cout << (pStu + i)->name << " ";
cout << (pStu + i)->id << " ";
cout << (pStu + i)->score << "n";
}
cout << "是否要将排序结果写入文件?(文件名为“Student_by_sno.txt”)" << endl;
if (getchar() == 'y')
{
ofstream out("Student_by_name.txt");
cout << "n开始写入文件" << endl;
for (int i = 0; i < n; i++)
{
out << (pStu + i)->name << " ";
out << (pStu + i)->id << " ";
out << (pStu + i)->score << "n";
}
out.close();
cout << "……" << endl; Sleep(1000);
cout << "……" << endl; Sleep(1000);
cout << "文件成功写入!" << endl << endl;
}
else
{
cout << "感谢使用,Bye~" << endl;
}
}
void student_sort_id(STUDENT *pStu, int n, int(*pFunc)(STUDENT *x, STUDENT *y))
{
STUDENT *p;
p = pStu;
STUDENT *temp;
temp = (STUDENT*)malloc(sizeof(STUDENT));
int k, j; //循环使用
for (int i = 1; i < n; i++)
{
p = pStu;
for (j = n - 1, k = 0; j >= i; j--, k++)
{
if (pFunc_id((p + k), (p + k + 1)))
{
strcpy(temp->name, (p + k)->name);
strcpy((p + k)->name, (p + k + 1)->name);
strcpy((p + k + 1)->name, temp->name);
p = pStu;
temp->id = (p + k)->id;
(p + k)->id = (p + k + 1)->id;
(p + k + 1)->id = temp->id;
p = pStu;
temp->score = (p + k)->score;
(p + k)->score = (p + k + 1)->score;
(p + k + 1)->score = temp->score;
}
}
}
for (int i = 0; i < n; i++)
{
cout << (pStu + i)->name << " ";
cout << (pStu + i)->id << " ";
cout << (pStu + i)->score << "n";
}
cout << "是否要将排序结果写入文件?(文件名为“Student_by_sno.txt”)" << endl;
if (getchar() == 'y')
{
ofstream out ("Student_by_sno.txt");
cout << "n开始写入文件" << endl;
for (int i = 0; i < n; i++)
{
out << (pStu + i)->name << " ";
out << (pStu + i)->id << " ";
out << (pStu + i)->score << "n";
}
out.close();
cout << "……" << endl; Sleep(1000);
cout << "……" << endl; Sleep(1000);
cout << "文件成功写入!" << endl << endl;
}
else
{
cout << "感谢使用,Bye~" << endl;
}
}
void student_sort_score(STUDENT *pStu, int n, int(*pFunc)(STUDENT *x, STUDENT *y))
{
STUDENT *p;
p = pStu;
STUDENT *temp;
temp = (STUDENT*)malloc(sizeof(STUDENT));
int k, j; //循环使用
for (int i = 1; i < n; i++)
{
p = pStu;
for (j = n - 1, k = 0; j >= i; j--, k++)
{
if (pFunc_score((p + k), (p + k + 1)))
{
strcpy(temp->name, (p + k)->name);
strcpy((p + k)->name, (p + k + 1)->name);
strcpy((p + k + 1)->name, temp->name);
p = pStu;
temp->id = (p + k)->id;
(p + k)->id = (p + k + 1)->id;
(p + k + 1)->id = temp->id;
p = pStu;
temp->score = (p + k)->score;
(p + k)->score = (p + k + 1)->score;
(p + k + 1)->score = temp->score;
}
}
}
cout << "是否要将排序结果写入文件?(文件名为“Student_by_score.txt”)" << endl;
for (int i = 0; i < n; i++)
{
cout << (pStu + i)->name << " ";
cout << (pStu + i)->id << " ";
cout << (pStu + i)->score << "n";
}
cout << "是否要将排序结果写入文件?(文件名为“Student_by_sno.txt”)" << endl;
if (getchar() == 'y')
{
ofstream out("Student_by_score.txt");
cout << "n开始写入文件" << endl;
for (int i = 0; i < n; i++)
{
out << (pStu + i)->name << " ";
out << (pStu + i)->id << " ";
out << (pStu + i)->score << "n";
}
out.close();
cout << "……" << endl; Sleep(1000);
cout << "……" << endl; Sleep(1000);
cout << "文件成功写入!" << endl << endl;
}
else
{
cout << "感谢使用,Bye~" << endl;
}
}
void run(STUDENT *pStu,int number)
{
system("title 学生信息排序");
gotoxy(35, 2); cout << "★功能列表★";
gotoxy(30, 3); cout << "1: 按照学生姓名排序";
gotoxy(30, 4); cout << "2:按照学生学号排序";
gotoxy(30, 5); cout << "3:按照学生分数排序";
gotoxy(0, 12);
int choice = 0;
cout << "★请输入您要执行的功能:";
cin >> choice;
getchar(); //吸收回车
switch (choice)
{
case 1:
student_sort_name(pStu, number, pFunc_name);
break;
case 2:
student_sort_id(pStu, number, pFunc_id);
break;
case 3:
student_sort_score(pStu, number, pFunc_score);
break;
default:
cout << "选择失败,感谢使用,再见!" << endl << endl;
}
}
int main()
{
cout << "***学生信息的初始化***" << endl;
STUDENT * pStu;
int number;
cout << "How many people? " << endl;
cin >> number;
pStu = Init_Stu(number);
system("cls");
run(pStu,number);
return 0;
}
*附加:
其实上面的运行结果排序的那几张图片还是有一些问题的,细心观察就会发现,好好我及时发现了这些问题,并在代码中进行了相应的修改。
问题来源:
交换的只是结构体中指定的两个数据,而不能保证在该结构体的其他数据一并交换,故我在每一段交换的代码中将其他数据也一并进行了交换。
(运行结果图片并未做修改~)
☆仅仅记录日常编写代码 与 疑问(`・ω・´)
****************************************************************************************************************************************
最快的脚步不是跨越,而是继续,最慢的步伐不是小步,而是徘徊。
****************************************************************************************************************************************
最后
以上就是爱撒娇砖头为你收集整理的☆ C/C++中使用结构体数组->排序(姓名+学号+分数)的全部内容,希望文章能够帮你解决☆ C/C++中使用结构体数组->排序(姓名+学号+分数)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复