马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;
#include "student.h"
int readStructsIntoList( student* & stu ) {
char buffer[256]; //缓冲区
int k = 0;
for ( ; gets( buffer ); k++ ) { //逐行读入不超过n条记录
student* ta = new student;
sscanf( buffer, "%d\t%s\t%s\t%s\t%s\t%s\t%I64d",
&ta->id,
ta->name,
ta->dept,
ta->group,
ta->type,
ta->email,
&ta->cell);
ta->pts = 56 + rand() % 45; //成绩随机分布于(55, 100]
//print( *ta );
ta->next = stu; stu = ta; //串接到链表前端
}
return k;
}
void selectionsort( student* s ) { //按成绩(选择)排序
for ( student* ta = s; ta; ta = ta->next ) {
student* tm = ta;
for ( student* tb = ta; tb; tb = tb->next ) //选出下一个(最小者)
if ( tm->pts > tb->pts ) tm = tb;
swap( *ta, *tm ); //通过交换将其前移就位
student* next = tm->next; tm->next = ta->next; ta->next = next; //别忘了,next指针要再次交换以复原
}
}
void bubblesort( student* s ) { //按班级(起泡)排序:正向稳定
while ( 1 ) {
bool sorted = true; //有序标志
for ( student* ta = s; student* tb = ta->next; ta = ta->next )
if ( strcmp(ta->group, tb->group) > 0 ) {
sorted = false; //仍有逆序
swap( *ta, *tb ); //通过交换将其前移就位
student* next = ta->next; ta->next = tb->next; tb->next = next; //别忘了,next指针要再次交换以复原
}
if ( sorted ) break; //直至有序
}
}
void insertionsort( student* & s ) { //按班级(插入)排序:反向稳定
student* t = s;
while ( student* x = t->next ) { //sorted prefix = [s, t] = [0, x)
if ( strcmp(x->group, s->group) <= 0 ) { //[x] <= [s]
t->next = x->next; x->next = s; s = x; //太空舞步
} else {
student* p = s; //predecessor of x
while ( strcmp(x->group, p->next->group) > 0 ) p = p->next;
if ( p->next == x ) //[s, t] < [x]
t = x;
else {
t->next = x->next; x->next = p->next; p->next = x; //太空舞步
}
}
}
}
int main( int argc, char* argv[] ) {
srand ( ( unsigned int ) time ( NULL ) ); //设置随机种子
student* fpcStu = NULL; //灵活:既足够,也不浪费
int n = readStructsIntoList( fpcStu ); cout << n << " record(s) read\n";
selectionsort( fpcStu ); cout << "\n" << n << " record(s) sorted by score\n";
for ( student* ta = fpcStu; ta; ta = ta->next ) { //逐条输出,以验证排序无误
cout << "[" << --n << "]\t" << ta->name << "\t" << ta->group << "\t" << ta->pts << "\n";
}
//bubblesort( fpcStu ); cout << "Resorted by group\n";
insertionsort( fpcStu ); cout << "Resorted by group\n";
for ( student* ta = fpcStu; ta; ta = ta->next ) { //逐条输出,以验证排序无误
cout << "[" << n++ << "]\t" << ta->name << "\t" << ta->group << "\t" << ta->pts << "\n";
}
while ( fpcStu ) { //逐条释放各记录
student* ta = fpcStu; fpcStu = fpcStu->next; delete ta;
}
return 0;
}
我第一个问题就是:
1.#include "student.h"是什么意思? |