【问题描述】
在以下问题中,定义学生的结构体student,录入学生信息,并存入到二进制文件grade.bin中,要求存入的是按照总成绩从大到小的顺序存储,数据可以分多次录入,每次录入前,都将显示文件中已有的数据,其具体形式如下:
在提示Input continue(y/n)?下输入'y',则可以继续输入,否则将退出。继续输入的内容将与现有的数据进行重新按总分从大到小的顺序排列后重新写入文件grade.bin中。
【输入形式】
【输出形式】文件
调试时的测试文件grade.bin压缩文件为
请将这个文件下载到与源代码相同的文件夹下并解压。
#include <iostream>
#include <fstream>
using namespace std;
/*
瀹氫箟缁撴瀯浣搒tudent鐢ㄤ簬琛ㄧず瀛︾敓鐨勫熀鏈俊鎭紝閲囩敤閾捐〃缁撴瀯
*/
struct student
{
int no; // 瀛﹀彿
char name[20], sex; // 濮撳悕浠ュ強鎬у埆, 璇锋€濊€冨鏋滃皢name[20]淇敼涓?name锛岀粨鏋滃皢浼氭€庢牱锛岃繕閫傚悎鏈鐨勫鐞嗘柟寮忓悧锛?
int age; // 骞撮緞
float chinese, computer; // 琛ㄧず涓ら棬璇剧▼鐨勬垚缁?
student *next;
bool operator<(const student &another) const // 閲嶈浇杩愮畻绗?< (灏忎簬)锛岀敤浜庣粨鏋勪綋瀵硅薄涔嬮棿鐨勬瘮杈冿紝鍦↙inkSort涓瘮杈冮摼琛ㄧ殑涓や釜鑺傜偣澶у皬
{
return (chinese + computer) < (another.chinese + another.computer);
}
};
student* ReadFile(ifstream& infile, student *head, int &n); // 浠庝簩杩涘埗鏂囦欢(鏂囦欢鎸囬拡涓篺p)涓皢鎵€鏈夋暟鎹鍏ワ紝瀛樺偍浜庝互head涓哄ご鎸囬拡鐨勯摼琛ㄤ腑
void display(student *head); // 鐢ㄤ簬鏄剧ず閾捐〃鐨勫唴瀹?
void WriteFile(ofstream& outfile, student *head); // 灏嗕互head涓哄ご鎸囬拡鐨勯摼琛ㄤ腑鐨勬暟鎹啓鍏ヤ簩杩涘埗鏂囦欢(鏂囦欢鎸囬拡涓篺p)
student* input(student *head, int &n); // 杈撳叆鏂扮殑鏁版嵁锛屽皢鏂版暟鎹繛鎺ュ埌閾捐〃鏈熬
student* LinkSort(student *head, const int &n); // 瀹炵幇瀵归摼琛ㄧ殑鎺掑簭锛屾寜鎬绘垚缁╀粠澶у埌灏忔帓鍒楋紝鍐掓场鎺掑簭
void swapNode(student *s, student *q); // 瀹炵幇瀵归摼琛ㄨ妭鐐圭殑鏁版嵁浜ゆ崲锛岀敤浜庢帓搴? 鍦ㄥ嚱鏁癓inkSort涓皟鐢?
int main()
{
int n = 0;
student *head = NULL;
ifstream getf;
getf.open("grade.bin",ios::in|ios::binary);
head = ReadFile(getf, head, n);
display(head);
getf.close();
head = input(head, n);
head = LinkSort(head, n);
ofstream putf;
putf.open("grade.bin",ios::in|ios::binary);
WriteFile(putf, head);
putf.close();
return 0;
}
student* ReadFile(ifstream& infile, student* head, int &n)
{
student *q = head;
if (head)
while(q->next) q = q->next;
while(!infile.eof())
{
student *p = new student;
if (!infile.read((char*)p,sizeof(student)))
{
delete p;
break;
}
p->next = NULL;
if (!head)
head = p;
else
q->next = p;
q = p;
n++;
}
return head;
}
void display(student* head)
{
student *p = head;
while(p)
{
printf("No: %d\n", p->no);
printf("Name: %s\n", p->name);
printf("Sex: %c\n", p->sex);
printf("Age: %d\n", p->age);
printf("Scores: %f %f\n\n", p->chinese, p->computer);
p = p->next;
}
}
student* input(student* head, int &n)
{
student *q = head;
if (head)
while(q->next) q = q->next;
while(1)
{
printf("Input continue(y/n)?");
if (getchar() != 'y')
break;
student *p = new student;
printf("No: ");
scanf("%d", &p->no);
getchar();
printf("Name: ");
gets(p->name);
printf("Sex(M/F): ");
p->sex = getchar();
printf("Age: ");
scanf("%d", &p->age);
printf("Chinese score: ");
scanf("%f", &p->chinese);
printf("Computer score: ");
scanf("%f", &p->computer);
p->next = NULL;
getchar();
if (!head)
head = p;
else
q->next = p;
q = p;
n++;
}
return head;
}
student* LinkSort(student* head, const int &n)
{
student *p;
for(int i = 1; i <= n - 1; i++)
{
p = head;
for(int j = 1; j <= n - i; j++)
{
if (*p < *p->next)
{
swapNode(p,p->next);
}
p = p->next;
}
}
return head;
}
void swapNode(student* s, student* q)
{
student *snext = s->next, *qnext = q->next, tmp;
tmp = *s;
*s = *q;
*q = tmp;
s->next = snext;
q->next = qnext;
}
void WriteFile(ofstream& outfile, student* head)
{
while(head)
{
outfile.write((char*)head,sizeof(student));
head = head->next;
}
}