好细致,好耐心……
[qq]949654600[/qq]
#pragma once // 学生数据结构 struct StudentItem { char Code[7]; // 编码 char Name[21]; // 姓名 int Sex; // 性别 int SchoolCode; // 学院编码 double Scores[10]; // 课程成绩 }; // 学生数据表结构 struct StudentInfo { size_t Count; StudentItem Data[100]; }; extern StudentInfo Students; // 从磁盘文件载入性别编码方案 bool LoadSex(const char* fileName); // 从磁盘文件载入学生数据 bool LoadStudents(const char* fileName, StudentInfo* students); // 列出学生明细清单 void ListStudents(const StudentInfo* students, const char* outputFileName, bool outputAverage); // 按姓名排序 void StudentsSortByName(StudentInfo* students); // 按平均成绩排序 void StudentsSortByScore(StudentInfo* students);
#include <Windows.h> #include <stdio.h> #include <string.h> #include "MyTools.h" #include "School.h" #include "Student.h" // 性别数据结构及数据 struct SexItem { int Code; // 编码 char Name[3]; // 名称 } Sex[2]; extern CollegeInfo Colleges; // 从磁盘文件载入性别编码方案 bool LoadSex(const char* fileName) { FILE* file; if (fopen_s(&file, fileName, "rt") != 0) { HLOCAL message = GetSystemErrorMessageA(GetLastError()); if (message != NULL) { printf_s("文件%s无法打开: %s\n", fileName, message); LocalFree(message); } return false; } for (size_t index = 0; (index < _countof(Sex)) && (fscanf_s(file, "%d %s\n", &(Sex[index].Code), Sex[index].Name, _countof(Sex[index].Name))) == 2; ++index) { ; } fclose(file); return true; }; // 从磁盘文件载入学生数据 bool LoadStudents(const char* fileName, StudentInfo* students) { FILE* file; if (fopen_s(&file, fileName, "rt") != 0) { HLOCAL message = GetSystemErrorMessageA(GetLastError()); if (message != NULL) { printf_s("文件%s无法打开: %s\n", fileName, message); LocalFree(message); } return false; } for (size_t index = 0; ; ++index) { if (fscanf_s(file, "%s", students->Data[index].Code, _countof(students->Data[index].Code)) != 1) { break; } if (fscanf_s(file, "%s", students->Data[index].Name, _countof(students->Data[index].Name)) != 1) { break; } if (fscanf_s(file, "%d", &(students->Data[index].Sex)) != 1) { break; } if (fscanf_s(file, "%d", &(students->Data[index].SchoolCode)) != 1) { break; } bool success = true; for (size_t subjectIndex = 0; subjectIndex < _countof(students->Data[index].Scores); ++subjectIndex) { if (fscanf_s(file, "%lf", &(students->Data[index].Scores[subjectIndex])) != 1) { success = false; break; } } if (!success) { break; } ++(students->Count); } fclose(file); return true; } // 求指定学生的平均成绩 double AverageScore(const StudentItem* student) { double total = 0.0; for (size_t index = 0; index < _countof(student->Scores); ++index) { total += student->Scores[index]; } return total / _countof(student->Scores); } // 列出学生明细清单 void ListStudents(const StudentInfo* students, const char* outputFileName, bool outputAverage) { FILE* file = stdout; // 默认向控制台标准设备输出结果 if (outputFileName != NULL) { if (fopen_s(&file, outputFileName, "wt") != 0) { HLOCAL message = GetSystemErrorMessageA(GetLastError()); if (message != NULL) { printf_s("输出文件%s无法建立: %s,改向标准设备输出.\n", outputFileName, message); LocalFree(message); file = stdout; } } } for (size_t index = 0; index < students->Count; ++index) { fprintf_s(file, "%s, ", students->Data[index].Code); fprintf_s(file, "%s, ", students->Data[index].Name); fprintf_s(file, "%s, ", Sex[students->Data[index].Sex].Name); fprintf_s(file, "%s, ", Colleges.Data[students->Data[index].SchoolCode - 1].Name); // 注:这里假定学院数据以编码与数组下标挂钩,否则应编写检索函数 for (size_t subjectIndex = 0; subjectIndex < _countof(students->Data[index].Scores); ++subjectIndex) { fprintf_s(file, "%.0f ", students->Data[index].Scores[subjectIndex]); } if (outputAverage) { fprintf_s(file, "[%6.2f]\n", AverageScore(&(students->Data[index]))); } else { fputc('\n', file); } } if (file != stdout) { fclose(file); } } // 复制一个学生数据 StudentItem CopyStudent(StudentItem* source) { StudentItem target; strcpy_s(target.Code, source->Code); strcpy_s(target.Name, source->Name); target.Sex = source->Sex; target.SchoolCode = source->SchoolCode; memcpy_s(target.Scores, sizeof(target.Scores), source->Scores, sizeof(source->Scores)); return target; } // 交换两个学生的变量内容 void SwapStudent(StudentItem* s1, StudentItem* s2) { StudentItem temp = CopyStudent(s2); *s2 = CopyStudent(s1); *s1 = CopyStudent(&temp); } // 按姓名排序 void StudentsSortByName(StudentInfo* students) { // 用冒泡法进行排序 for (size_t i = 0; i < students->Count - 1; ++i) { for (size_t j = i; j < students->Count; ++j) { if (strcmp(students->Data[i].Name, students->Data[j].Name) < 0) // 升序 { SwapStudent(&(students->Data[i]), &(students->Data[j])); } } } } // 按平均成绩排序 void StudentsSortByScore(StudentInfo* students) { // 用冒泡法进行排序 for (size_t i = 0; i < students->Count - 1; ++i) { for (size_t j = i; j < students->Count; ++j) { if (AverageScore(&(students->Data[i])) > AverageScore(&(students->Data[j]))) // 降序 { SwapStudent(&(students->Data[i]), &(students->Data[j])); } } } }