2020-07-07日报博客
1.今天完成的事情:
-
学习Java,听黑马系列的视频教程前30集。
-
完成面向对象程序设计小学期第八个练习:学生成绩管理程序(链表)
-
代码如下:
#include<iostream> #include<fstream> #include<iomanip> using namespace std; const int M = 1024; struct StudentInfo { public: long studentId; string name; float math; float Chinese; float English; StudentInfo* next = NULL; }; class StudentManage { public: StudentInfo* head = new StudentInfo; string createFile(); string importFile(); void show(); void inputInfo(); void deleteInfo(); void changeInfo(); void findInfo(); void summary(float sum[], float average[]); void sort(float sum[]); void exportFile(); void insert(); }; string StudentManage::createFile() { string fileName; cout << "请输入文件名:"; cin >> fileName; ofstream outfile(fileName, ios::out); if (!outfile) { cerr << "创建失败!" << endl; } outfile.close(); return fileName; } string StudentManage::importFile() { string fileName; cout << "请输入文件名: "; cin >> fileName; ifstream infile(fileName, ios::in || ios::_Noreplace); if (!infile) { cerr << "打开失败!" << endl; } head->next = NULL; StudentInfo* pre = this->head; while (!infile.eof()) { StudentInfo* p = new StudentInfo; infile >> p->studentId >> p->name >> p->math >> p->Chinese >> p->English; if (infile.eof()) { p->next = NULL; break; } pre->next = p; pre = p; p->next = NULL; } this->show(); infile.close(); return fileName; } void StudentManage::show() { cout << setw(10) << setiosflags(ios::left) << "学号 |" << setw(10) << setiosflags(ios::left) << "姓名 |" << setw(10) << setiosflags(ios::left) << "数学 |" << setw(10) << setiosflags(ios::left) << "语文 |" << setw(10) << setiosflags(ios::left) << "英语" << endl; StudentInfo* p = this->head->next; while (true) { if (p == NULL || p->math < 0 || p->math > 100) break; cout << setw(10) << setiosflags(ios::left) << p->studentId << setw(10) << setiosflags(ios::left) << p->name << setw(10) << setiosflags(ios::left) << p->math << setw(10) << setiosflags(ios::left) << p->Chinese << setw(10) << setiosflags(ios::left) << p->English << endl; if (p->next == NULL) break; p = p->next; } } void StudentManage::inputInfo() { int n; cout << "请输入需要输入的人数:"; cin >> n; cout << "开始输入学生信息:" << endl; StudentInfo* pEnd = this->head; while (true) { pEnd = pEnd->next; if (pEnd->next == NULL || pEnd->next->math < 0 || pEnd->next->math> 100) break; } StudentInfo* pre = pEnd; for (int i = 0; i < n; i++) { StudentInfo* p = new StudentInfo; cin >> p->studentId >> p->name >> p->math >> p->Chinese >> p->English; pre->next = p; pre = p; p->next = NULL; } } void StudentManage::deleteInfo() { if (head->next == NULL) { cout << "数据未录入!" << endl; return; } long id; cout << "请输入学生学号: " << endl; cin >> id; StudentInfo* p = this->head->next; while (true) { if (p == NULL || p->math < 0 || p->math > 100) break; if (p->next->studentId == id) { p->next = p->next->next; } p = p->next; } } void StudentManage::changeInfo() { if (head->next == NULL) { cout << "数据未录入!" << endl; return; } long id; cout << "请输入学生学号: " << endl; cin >> id; StudentInfo* p = this->head->next; while (true) { if (p == NULL || p->math < 0 || p->math > 100) break; if (p->studentId == id) { cout << "请重新输入该学生信息:" << endl; cout << setw(10) << setiosflags(ios::left) << "学号 |" << setw(10) << setiosflags(ios::left) << "姓名 |" << setw(10) << setiosflags(ios::left) << "数学 |" << setw(10) << setiosflags(ios::left) << "语文 |" << setw(10) << setiosflags(ios::left) << "英语" << endl; cin >> p->studentId >> p->name >> p->math >> p->Chinese >> p->English; } p = p->next; } } void StudentManage::findInfo() { if (head->next == NULL) { cout << "数据未录入!" << endl; return; } long id; cout << "请输入学生学号: " << endl; cin >> id; StudentInfo* p = this->head->next; while (true) { if (p == NULL || p->math < 0 || p->math > 100) break; if (p->studentId == id) { cout << "该生信息为:" << endl; cout << setw(10) << setiosflags(ios::left) << "学号 |" << setw(10) << setiosflags(ios::left) << "姓名 |" << setw(10) << setiosflags(ios::left) << "数学 |" << setw(10) << setiosflags(ios::left) << "语文 |" << setw(10) << setiosflags(ios::left) << "英语" << endl; cout << setw(10) << setiosflags(ios::left) << p->studentId << setw(10) << setiosflags(ios::left) << p->name << setw(10) << setiosflags(ios::left) << p->math << setw(10) << setiosflags(ios::left) << p->Chinese << setw(10) << setiosflags(ios::left) << p->English << endl; } p = p->next; } } void StudentManage::summary(float sum[M], float average[M]) { if (head->next == NULL) { cout << "数据未录入!" << endl; return; } StudentInfo* p = this->head->next; int i = 0; cout << setw(10) << setiosflags(ios::left) << "学号 |" << setw(10) << setiosflags(ios::left) << "姓名 |" << setw(10) << setiosflags(ios::left) << "数学 |" << setw(10) << setiosflags(ios::left) << "语文 |" << setw(10) << setiosflags(ios::left) << "英语 |" << "总分 |" << "平均分" << endl; while (true) { if (p == NULL || p->math < 0 || p->math > 100) break; sum[i] = p->math + p->Chinese + p->English; average[i] = sum[i] / 3.0; cout << setw(10) << setiosflags(ios::left) << p->studentId << setw(10) << setiosflags(ios::left) << p->name << setw(10) << setiosflags(ios::left) << p->math << setw(10) << setiosflags(ios::left) << p->Chinese << setw(10) << setiosflags(ios::left) << p->English << setw(10) << setiosflags(ios::left) << sum[i] << average[i] << endl; p = p->next; } } void Swap(StudentInfo& a, StudentInfo& b) { StudentInfo t; t.studentId = a.studentId; t.name = a.name; t.math = a.math; t.Chinese = a.Chinese; t.English = a.English; a.studentId = b.studentId; a.name = b.name; a.math = b.math; a.Chinese = b.Chinese; a.English = b.English; b.studentId = t.studentId; b.name = t.name; b.math = t.math; b.Chinese = t.Chinese; b.English = t.English; } void StudentManage::sort(float sum[]) { if (head->next == NULL) { cout << "数据未录入!" << endl; return; } int a, N = 0; StudentInfo* p = this->head->next; while (true) { if (p == NULL || p->math < 0 || p->math > 100) break; N++; p = p->next; } cout << N << endl; cout << "请选择按学号还是按总分排序(1.学号 2.总分):"; cin >> a; if (a == 1) { for (int i = 1; i < N - 1; i++) { StudentInfo* p = NULL; StudentInfo* q = NULL; StudentInfo* t = NULL; if (this->head == NULL || (this->head)->next == NULL) { return; } for (p = this->head; p != NULL; p = p->next) { t = p; for (q = p->next; q != NULL; q = q->next) { if (q->studentId < t->studentId) { t = q; } } if (t != p) { Swap(*t, *p); } } } this->show(); } if (a == 2) { StudentInfo* p1 = NULL; StudentInfo* q = NULL; StudentInfo* t = NULL; if (this->head == NULL || (this->head)->next == NULL) { return; } for (p1 = this->head->next; p1 != NULL; p1 = p1->next) { for (q = this->head->next; q->next != NULL; q = q->next) { if ((q->math + q->Chinese + q->English) < (q->next->math + q->next->Chinese + q->next->English)) { Swap(*q, *q->next); } } } StudentInfo* p = this->head->next; cout << setw(10) << setiosflags(ios::left) << "学号 |" << setw(10) << setiosflags(ios::left) << "姓名 |" << setw(10) << setiosflags(ios::left) << "数学 |" << setw(10) << setiosflags(ios::left) << "语文 |" << setw(10) << setiosflags(ios::left) << "英语 |" << "总分" << endl; while (true) { if (p == NULL || p->math < 0 || p->math > 100) break; double sum = p->math + p->Chinese + p->English; cout << setw(10) << setiosflags(ios::left) << p->studentId << setw(10) << setiosflags(ios::left) << p->name << setw(10) << setiosflags(ios::left) << p->math << setw(10) << setiosflags(ios::left) << p->Chinese << setw(10) << setiosflags(ios::left) << p->English << setw(10) << setiosflags(ios::left) << sum << endl; p = p->next; } } if (a != 1 && a != 2) { cout << "不存在该选项!" << endl; } } void StudentManage::exportFile() { string fileName; cout << "请输入文件名:"; cin >> fileName; ofstream outfile(fileName, ios::out); if (!outfile) { cerr << "Open error!" << endl; exit(1); } StudentInfo* p = this->head->next; while (true) { if (p == NULL || p->math < 0 || p->math > 100) break; outfile << setw(10) << setiosflags(ios::left) << p->studentId << setw(10) << setiosflags(ios::left) << p->name << setw(10) << setiosflags(ios::left) << p->math << setw(10) << setiosflags(ios::left) << p->Chinese << setw(10) << setiosflags(ios::left) << p->English << endl; if (p->next == NULL) break; p = p->next; } outfile.close(); cout << "写入完成!" << endl; } void StudentManage::insert() { int n, t, N; cout << "请输入要插入的学生数n:"; cin >> n; cout << "请输入位置:"; cin >> t; StudentInfo* p = this->head->next; for (int i = 0; i < t - 3; i++) { if (p == NULL || p->next == NULL || p->math < 0 || p->math > 100) break; p = p->next; } for (int i = 0; i < n; i++) { StudentInfo* t = p->next; p->next = new StudentInfo; cout << "请输入要插入学生的信息:" << endl; cin >> p->next->studentId >> p->next->name >> p->next->math >> p->next->Chinese >> p->next->English; p->next->next = t; p = p->next; } } int main() { StudentManage classA; string filename; float sum[M], average[M]; int a; cout << "学生成绩管理系统: 1 创建 2 导入 3 显示 4 输入 5 删除 6 修改 7 查询 8 汇总 9 排序 10 导出 11 插入 12 退出 "; while (true) { cout << "请选择:"; cin >> a; switch (a) { case 1:filename = classA.createFile(); continue; case 2:filename = classA.importFile(); continue; case 3:classA.show(); continue; case 4:classA.inputInfo(); continue; case 5:classA.deleteInfo(); continue; case 6:classA.changeInfo(); continue; case 7:classA.findInfo(); continue; case 8:classA.summary(sum, average); continue; case 9:classA.sort(sum); continue; case 10:classA.exportFile(); continue; case 11:classA.insert(); continue; case 12: { exit(0); } default:cout << "不存在该选项!" << endl; continue; } } return 0; }
2.遇到的问题:
- 链表的一些操作,如排序,已通过搜索引擎解决。
3.明天的计划:
- 继续完成小学期任务,继续学习Java。