From a0109d2c99198d86d9c93d1182e54d4fc3ff6355 Mon Sep 17 00:00:00 2001 From: zyx <1029606625@qq.com> Date: Mon, 4 Jul 2022 00:31:52 +0800 Subject: [PATCH] 2022-07-04 00:31 --- CourseDAO.h | 5 +- CourseDAOImpl.cpp | 21 ++- GradeDAO.h | 14 +- GradeDAOImpl.cpp | 94 +++++++++- Menu.cpp | 431 ++++++++++++++++++++++++++++++++++++++++++++ Menu.h | 24 +++ MenuUtils.cpp | 29 +++ MenuUtils.h | 12 ++ SAS.vcxproj | 4 + SAS.vcxproj.filters | 12 ++ StudentDAO.h | 5 +- StudentDAOImpl.cpp | 29 ++- main.cpp | 16 +- 13 files changed, 669 insertions(+), 27 deletions(-) create mode 100644 Menu.cpp create mode 100644 Menu.h create mode 100644 MenuUtils.cpp create mode 100644 MenuUtils.h diff --git a/CourseDAO.h b/CourseDAO.h index 0a6e955..9328916 100644 --- a/CourseDAO.h +++ b/CourseDAO.h @@ -10,7 +10,8 @@ void addCourse(_ConnectionPtr connection, Course course); void deleteCourse(_ConnectionPtr connection, string id); void updateCourseName(_ConnectionPtr connection, string id, string name); -Course& getCourseById(_ConnectionPtr connection, string id); -std::vector& getCourseByName(_ConnectionPtr connection, string name); +Course* getCourseById(_ConnectionPtr connection, string id); +Course* getCourseByName(_ConnectionPtr connection, string name); +std::vector* getAllCourse(_ConnectionPtr connection); #endif // !COURSE_DAO_H \ No newline at end of file diff --git a/CourseDAOImpl.cpp b/CourseDAOImpl.cpp index 2b71c95..c3040d2 100644 --- a/CourseDAOImpl.cpp +++ b/CourseDAOImpl.cpp @@ -19,19 +19,32 @@ void updateCourseName(_ConnectionPtr connection, string id, string name) { connection->Execute(sql.c_str(), NULL, (long)0); } -Course& getCourseById(_ConnectionPtr connection, string id) { +Course* getCourseById(_ConnectionPtr connection, string id) { string sql = "select * from course where id = '" + id + "'"; _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; Course* course = new Course(); course->setId((char*)(_bstr_t)record->Fields->GetItem("id")->Value); course->setName((char*)(_bstr_t)record->Fields->GetItem("name")->Value); - return *course; + return course; } -std::vector& getCourseByName(_ConnectionPtr connection, string name) { +Course* getCourseByName(_ConnectionPtr connection, string name) { string sql = "select * from course where name = '" + name + "'"; _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; + + Course* course = new Course(); + course->setId((char*)(_bstr_t)record->Fields->GetItem("id")->Value); + course->setName((char*)(_bstr_t)record->Fields->GetItem("name")->Value); + return course; +} + +std::vector* getAllCourse(_ConnectionPtr connection) { + string sql = "select * from course"; + _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; std::vector* v = new std::vector(); Course* course; @@ -41,5 +54,5 @@ std::vector& getCourseByName(_ConnectionPtr connection, string name) { course->setName((char*)(_bstr_t)record->Fields->GetItem("name")->Value); v->push_back(*course); } - return *v; + return v; } \ No newline at end of file diff --git a/GradeDAO.h b/GradeDAO.h index 0a83362..9ce4289 100644 --- a/GradeDAO.h +++ b/GradeDAO.h @@ -12,8 +12,16 @@ void deleteOnesGrade(_ConnectionPtr connection, string stuId,string courseId); void deleteGradeByStuId(_ConnectionPtr connection, string stuId); void deleteGradeByCourseId(_ConnectionPtr connection, string courseId); void updateOnesGrade(_ConnectionPtr connection, string stuId, string courseId, string grade); -Grade& getOnesGrade(_ConnectionPtr connection, string stuId, string courseId); -std::vector& getGradeByStuId(_ConnectionPtr connection, string stuId); -std::vector& getGradeByCourseId(_ConnectionPtr connection, string courseId); +Grade* getOnesGrade(_ConnectionPtr connection, string stuId, string courseId); +std::vector* getGradeByStuId(_ConnectionPtr connection, string stuId); +std::vector* getGradeByCourseId(_ConnectionPtr connection, string courseId); +std::vector* getGradeByStuClass(_ConnectionPtr connection, string stuClass); +std::vector* getAllGrade(_ConnectionPtr connection); + +double getOnesAvgGrade(_ConnectionPtr connection, string stuId); +double getOnesAllGrade(_ConnectionPtr connection, string stuId); +double getClassAvgGrade(_ConnectionPtr connection, string stuClass, string courseName); +double getClassMaxGrade(_ConnectionPtr connection, string stuClass, string courseName); +double getClassMinGrade(_ConnectionPtr connection, string stuClass, string courseName); #endif // !GRADE_DAO_H diff --git a/GradeDAOImpl.cpp b/GradeDAOImpl.cpp index 716dc25..023db7e 100644 --- a/GradeDAOImpl.cpp +++ b/GradeDAOImpl.cpp @@ -32,20 +32,22 @@ void updateOnesGrade(_ConnectionPtr connection, string stuId, string courseId, s connection->Execute(sql.c_str(), NULL, (long)0); } -Grade& getOnesGrade(_ConnectionPtr connection, string stuId,string courseId) { +Grade* getOnesGrade(_ConnectionPtr connection, string stuId,string courseId) { string sql = "select * from grade where stuId = '" + stuId + "\' and courseId = \'" + courseId + "'"; _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; Grade* grade = new Grade(); grade->setStuId((char*)(_bstr_t)record->Fields->GetItem("stuId")->Value); grade->setCourseId((char*)(_bstr_t)record->Fields->GetItem("courseId")->Value); grade->setGrade((char*)(_bstr_t)record->Fields->GetItem("grade")->Value); - return *grade; + return grade; } -std::vector& getGradeByStuId(_ConnectionPtr connection, string stuId) { +std::vector* getGradeByStuId(_ConnectionPtr connection, string stuId) { string sql = "select * from grade where stuId = '" + stuId + "'"; _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; std::vector* v = new std::vector(); Grade* grade; @@ -56,12 +58,13 @@ std::vector& getGradeByStuId(_ConnectionPtr connection, string stuId) { grade->setGrade((char*)(_bstr_t)record->Fields->GetItem("grade")->Value); v->push_back(*grade); } - return *v; + return v; } -std::vector& getGradeByCourseId(_ConnectionPtr connection, string courseId) { +std::vector* getGradeByCourseId(_ConnectionPtr connection, string courseId) { string sql = "select * from grade where courseId = '" + courseId + "'"; _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; std::vector* v = new std::vector(); Grade* grade; @@ -72,5 +75,84 @@ std::vector& getGradeByCourseId(_ConnectionPtr connection, string courseI grade->setGrade((char*)(_bstr_t)record->Fields->GetItem("grade")->Value); v->push_back(*grade); } - return *v; + return v; +} + +std::vector* getGradeByStuClass(_ConnectionPtr connection, string stuClass) { + string sql = "select * from grade where stuId in(select id from student where stuClass = '" + stuClass + "')"; + _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; + + std::vector* v = new std::vector(); + Grade* grade; + for (; !record->EndOfFile; record->MoveNext()) { + grade = new Grade(); + grade->setStuId((char*)(_bstr_t)record->Fields->GetItem("stuId")->Value); + grade->setCourseId((char*)(_bstr_t)record->Fields->GetItem("courseId")->Value); + grade->setGrade((char*)(_bstr_t)record->Fields->GetItem("grade")->Value); + v->push_back(*grade); + } + return v; +} + +std::vector* getAllGrade(_ConnectionPtr connection) { + string sql = "select * from grade"; + _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; + + std::vector* v = new std::vector(); + Grade* grade; + for (; !record->EndOfFile; record->MoveNext()) { + grade = new Grade(); + grade->setStuId((char*)(_bstr_t)record->Fields->GetItem("stuId")->Value); + grade->setCourseId((char*)(_bstr_t)record->Fields->GetItem("courseId")->Value); + grade->setGrade((char*)(_bstr_t)record->Fields->GetItem("grade")->Value); + v->push_back(*grade); + } + return v; +} + +double getOnesAvgGrade(_ConnectionPtr connection, string stuId) { + string sql = "select count(*) cnt,sum(grade) all_grade from grade where stuId = '" + stuId + "'"; + _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; + + int cnt = atoi((char*)(_bstr_t)record->Fields->GetItem("cnt")->Value); + double all_grade = atof((char*)(_bstr_t)record->Fields->GetItem("all_grade")->Value); + return all_grade / cnt; +} + +double getOnesAllGrade(_ConnectionPtr connection, string stuId) { + string sql = "select sum(grade) all_grade from grade where stuId = '" + stuId + "'"; + _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; + + return atof((char*)(_bstr_t)record->Fields->GetItem("all_grade")->Value); +} + +double getClassAvgGrade(_ConnectionPtr connection, string stuClass, string courseName) { + string sql = "select avg(grade) avg_grade from grade where stuId in (select id from student where stuClass = '" + stuClass + "') "; + sql += "and courseId in (select id from course where name = '" + courseName + "')"; + _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; + + return atof((char*)(_bstr_t)record->Fields->GetItem("avg_grade")->Value); +} + +double getClassMaxGrade(_ConnectionPtr connection, string stuClass, string courseName) { + string sql = "select max(grade) max_grade from grade where stuId in (select id from student where stuClass = '" + stuClass + "') "; + sql += "and courseId in (select id from course where name = '" + courseName + "')"; + _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; + + return atof((char*)(_bstr_t)record->Fields->GetItem("max_grade")->Value); +} + +double getClassMinGrade(_ConnectionPtr connection, string stuClass, string courseName) { + string sql = "select min(grade) min_grade from grade where stuId in (select id from student where stuClass = '" + stuClass + "') "; + sql += "and courseId in (select id from course where name = '" + courseName + "')"; + _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; + + return atof((char*)(_bstr_t)record->Fields->GetItem("min_grade")->Value); } \ No newline at end of file diff --git a/Menu.cpp b/Menu.cpp new file mode 100644 index 0000000..9558828 --- /dev/null +++ b/Menu.cpp @@ -0,0 +1,431 @@ +#include +#include"Menu.h" +#include"MenuUtils.h" +#include"StudentDAO.h" +#include"GradeDAO.h" +#include"CourseDAO.h" +using namespace std; + +void mainMenu(_ConnectionPtr connection) { + system("cls"); + printf("1.显示所有学生\n"); + printf("2.增加学生\n"); + printf("3.删除学生\n"); + printf("4.修改学生\n"); + printf("5.从文件导入学生信息\n"); + printf("6.导出学生信息到文件\n"); + printf("7.学生成绩管理\n"); + printf("8.课程管理\n"); + printf("0.退出\n"); + + int opt = -1; + checkOptInput(opt, 0, 8); + + switch (opt){ + case 1: + showAllStudent(connection); + break; + case 2: + insertStudent(connection); + break; + case 3: + deleteStudent(connection); + break; + case 4: + changeStudent(connection); + break; + case 5: + importData(connection); + break; + case 6: + exportData(connection); + break; + case 7: + studentGradeMenu(connection); + break; + case 8: + courseMenu(connection); + break; + default: + exit(EXIT_SUCCESS); + } + printf("\n"); +} + +void studentGradeMenu(_ConnectionPtr connection) { + while (true) { + system("cls"); + printf("1.输入学生成绩\n"); + printf("2.获取学生平均成绩\n"); + printf("3.获取学生总成绩\n"); + printf("4.获取班级平均分\n"); + printf("5.获取班级最高分\n"); + printf("6.获取班级最低分\n"); + printf("7.获取班级成绩\n"); + printf("8.二级拓展菜单\n"); + printf("0.返回上一级\n"); + + int opt = -1; + checkOptInput(opt, 0, 8); + + printf("\n"); + + string stuId, courseId; + string stuClass, courseName; + double grade = -1; + int size; + vector *v; + switch (opt) { + case 1://输入学生成绩 + printf("请输入学号:"); + cin >> stuId; + printf("请输入课程名:"); + cin >> courseId; + printf("请输入分数(0-100):"); + while (!(cin >> grade) || (opt < 0) || (opt > 100)) { + cout << "输入错误!请重新输入:"; + cin.clear(); + while (cin.get() != '\n'); + } + addGrade(connection, *new Grade(stuId, courseId, doubleToString(grade))); + printf("添加成功!\n"); + break; + case 2://获取学生平均成绩 + printf("请输入学号:"); + cin >> stuId; + printf("学生平均成绩为:%.2lf\n", getOnesAvgGrade(connection, stuId)); + break; + case 3://获取学生总成绩 + printf("请输入学号:"); + cin >> stuId; + printf("学生总成绩为:%.2lf\n", getOnesAllGrade(connection, stuId)); + break; + case 4://获取班级平均分 + printf("请输入班级:"); + cin >> stuClass; + printf("请输入课程名:"); + cin >> courseName; + cout << stuClass << "班级" << courseName << "课程的平均分为:" << getClassAvgGrade(connection, stuClass, courseName) << "\n"; + break; + case 5://获取班级最高分 + printf("请输入班级:"); + cin >> stuClass; + printf("请输入课程名:"); + cin >> courseName; + cout << stuClass << "班级" << courseName << "课程的最高分为:" << getClassMaxGrade(connection, stuClass, courseName) << "\n"; + break; + case 6://获取班级最低分 + printf("请输入班级:"); + cin >> stuClass; + printf("请输入课程名:"); + cin >> courseName; + cout << stuClass << "班级" << courseName << "课程的最低分为:" << getClassMinGrade(connection, stuClass, courseName) << "\n"; + break; + case 7://获取班级成绩 + printf("请输入班级:"); + cin >> stuClass; + v = getGradeByStuClass(connection, stuClass); + if (v == NULL) { + printf("该班级不存在!\n"); + break; + } + size = v->size(); + cout << "学号\t" << "课程名\t" << "成绩\n"; + for (int i = 0; i < size; i++) { + cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n"; + } + break; + case 8://二级拓展菜单 + studentGradeExternMenu(connection); + break; + default://返回上一级 + return; + } + system("pause"); + } +} + +void studentGradeExternMenu(_ConnectionPtr connection) { + while (true) { + system("cls"); + printf("1.删除某学生某科成绩\n"); + printf("2.删除某学生成绩\n"); + printf("3.删除某科成绩\n"); + printf("4.更改某学生某科成绩\n"); + printf("5.查询某学生某科成绩\n"); + printf("6.查询某学生成绩\n"); + printf("7.查询某科成绩\n"); + printf("8.查询所有成绩\n"); + printf("0.返回上一级\n"); + + int opt = -1; + checkOptInput(opt, 0, 8); + + printf("\n"); + + string stuId, courseId; + string stuClass, courseName; + double grade = -1; + int size; + vector *v; + Grade* gra; + Course* course; + switch (opt) { + case 1://删除某学生某科成绩 + printf("请输入学号:"); + cin >> stuId; + printf("请输入课程名:"); + cin >> courseName; + course = getCourseByName(connection, courseName); + if (course == NULL) { + printf("该课程不存在!\n"); + break; + } + deleteOnesGrade(connection, stuId, course->getId()); + printf("删除成功!\n"); + break; + case 2://删除某学生成绩 + printf("请输入学号:"); + cin >> stuId; + deleteGradeByStuId(connection, stuId); + printf("删除成功!\n"); + break; + case 3://删除某科成绩 + printf("请输入课程名:"); + cin >> courseName; + course = getCourseByName(connection, courseName); + if (course == NULL) { + printf("该课程不存在!\n"); + break; + } + deleteGradeByCourseId(connection, course->getId()); + printf("删除成功!\n"); + break; + case 4://更改某学生某科成绩 + printf("请输入学号:"); + cin >> stuId; + printf("请输入课程名:"); + cin >> courseName; + course = getCourseByName(connection, courseName); + if (course == NULL) { + printf("该课程不存在!\n"); + break; + } + printf("请输入新分数(0-100):"); + while (!(cin >> grade) || (opt < 0) || (opt > 100)) { + cout << "输入错误!请重新输入:"; + cin.clear(); + while (cin.get() != '\n'); + } + updateOnesGrade(connection, stuId, course->getId(), doubleToString(grade)); + printf("更改成功!\n"); + break; + case 5://查询某学生某科成绩 + printf("请输入学号:"); + cin >> stuId; + printf("请输入课程名:"); + cin >> courseName; + course = getCourseByName(connection, courseName); + if (course == NULL) { + printf("该课程不存在!\n"); + break; + } + gra = getOnesGrade(connection, stuId, course->getId()); + if (gra == NULL) { + printf("该条记录不存在!\n"); + break; + } + cout << "学号\t" << "课程名\t" << "成绩\n"; + cout << (*gra).getStuId() << "\t" << getCourseById(connection, (*gra).getCourseId())->getName() << "\t" << (*gra).getGradeStr() << "\n"; + break; + case 6://查询某学生成绩 + printf("请输入学号:"); + cin >> stuId; + v = getGradeByStuId(connection, stuId); + if (v == NULL) { + printf("该学生不存在!\n"); + break; + } + size = v->size(); + cout << "学号\t" << "课程名\t" << "成绩\n"; + for (int i = 0; i < size; i++) { + cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n"; + } + break; + case 7://查询某科成绩 + printf("请输入课程名:"); + cin >> courseName; + course = getCourseByName(connection, courseName); + if (course == NULL) { + printf("该课程不存在!\n"); + break; + } + v = getGradeByCourseId(connection, course->getId()); + if (v == NULL) { + printf("该课程不存在对应成绩!\n"); + break; + } + size = v->size(); + cout << "学号\t" << "课程名\t" << "成绩\n"; + for (int i = 0; i < size; i++) { + cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n"; + } + break; + case 8://查询所有成绩 + v = getAllGrade(connection); + if (v == NULL) { + printf("无成绩记录!\n"); + break; + } + size = v->size(); + cout << "学号\t" << "课程名\t" << "成绩\n"; + for (int i = 0; i < size; i++) { + cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n"; + } + break; + default://返回上一级 + return; + } + system("pause"); + } +} + +void courseMenu(_ConnectionPtr connection) { + while (true) { + system("cls"); + printf("1.添加课程\n"); + printf("2.删除课程\n"); + printf("3.更改课程\n"); + printf("4.查询所有课程\n"); + printf("0.返回上一级\n"); + int opt = -1; + checkOptInput(opt, 0, 8); + + printf("\n"); + + int size; + string courseId, courseName; + vector* v; + switch (opt) { + case 1://添加课程 + printf("请输入课程号:"); + cin >> courseId; + printf("请输入课程名:"); + cin >> courseName; + addCourse(connection,*new Course(courseId, courseName)); + printf("添加成功!\n"); + break; + case 2://删除课程 + printf("请输入课程号:"); + cin >> courseId; + deleteCourse(connection, courseId); + printf("删除成功!\n"); + break; + case 3://更改课程 + printf("请输入课程号:"); + cin >> courseId; + printf("请输入课程名:"); + cin >> courseName; + updateCourseName(connection, courseId,courseName); + printf("更新成功!\n"); + break; + case 4://查询所有课程 + v = getAllCourse(connection); + if (v == NULL) { + printf("不存在课程记录!\n"); + break; + } + size = v->size(); + cout << "课程号\t" << "课程名\n"; + for (int i = 0; i < size; i++) { + cout << (*v)[i].getId() << "\t" << (*v)[i].getName() << "\n"; + } + break; + default://返回上一级 + return; + } + system("pause"); + } +} + +void showAllStudent(_ConnectionPtr connection) { + vector vs = *getAllStudent(connection); + int size = vs.size(); + for (int i = 0; i < size; i++) { + cout << vs[i].toString() << "\n"; + } +} + +void insertStudent(_ConnectionPtr connection) { + Student *stu = new Student(); + + string str; + printf("请输入学号:"); + cin >> str; + stu->setId(str); + printf("请输入姓名:"); + cin >> str; + stu->setName(str); + printf("请输入性别:"); + cin >> str; + stu->setSex(str); + printf("请输入班级:"); + cin >> str; + stu->setStuClass(str); + printf("请输入学生状态(在读、休学、退学):"); + cin >> str; + stu->setStatus(str); + + addStudent(connection, *stu); + printf("添加成功!\n"); +} + +void deleteStudent(_ConnectionPtr connection) { + string id; + printf("请输入待删除学生的学号:"); + cin >> id; + Student *stu = getStudentById(connection,id); + if (stu == NULL) { + printf("该学生不存在!\n"); + return; + } + deleteStudent(connection, id); + printf("删除成功!\n"); +} + +void changeStudent(_ConnectionPtr connection) { + string id; + printf("请输入待修改学生的学号:"); + cin >> id; + Student* stu = getStudentById(connection, id); + if (stu == NULL) { + printf("该学生不存在!\n"); + return; + } + + printf("请选择修改 班级(0) / 学生状态(1):"); + int opt = -1; + checkOptInput(opt, 0, 1); + + string input; + switch (opt) { + case 0: + printf("\n请输入新班级:"); + cin >> input; + updateStudentClass(connection,id,input); + break; + default: + printf("\n请输入新状态:"); + cin >> input; + updateStudentStatus(connection,id, input); + break; + } +} + +void exportData(_ConnectionPtr connection) { + +} + +void importData(_ConnectionPtr connection) { + +} \ No newline at end of file diff --git a/Menu.h b/Menu.h new file mode 100644 index 0000000..f105922 --- /dev/null +++ b/Menu.h @@ -0,0 +1,24 @@ +#ifndef MENU_H +#define MENU_H + +#import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile") + +#include +#include"Student.cpp" +#include"Course.cpp" +#include"Grade.cpp" + +void mainMenu(_ConnectionPtr connection); +void studentGradeMenu(_ConnectionPtr connection); +void studentGradeExternMenu(_ConnectionPtr connection); +void courseMenu(_ConnectionPtr connection); + +void showAllStudent(_ConnectionPtr connection); +void insertStudent(_ConnectionPtr connection); +void deleteStudent(_ConnectionPtr connection); +void changeStudent(_ConnectionPtr connection); + +void exportData(_ConnectionPtr connection); +void importData(_ConnectionPtr connection); + +#endif // !MENU_H \ No newline at end of file diff --git a/MenuUtils.cpp b/MenuUtils.cpp new file mode 100644 index 0000000..f81be97 --- /dev/null +++ b/MenuUtils.cpp @@ -0,0 +1,29 @@ +#include"MenuUtils.h" + +void checkOptInput(int& opt, int low, int high) { + printf(">>>请输入选项(%d-%d):", low, high); + while (!(cin >> opt) || (opt < low) || (opt > high)) { + cout << "输入选项错误!请重新输入:"; + cin.clear(); + while (cin.get() != '\n'); + } +} + +string doubleToString(double num) { + char* buf; + int dec, sign; + + buf = _fcvt(num, 2, &dec, &sign); + + string str; + if (sign) str += "-"; + int i; + for (i = 0; i < dec; i++) { + str += buf[i]; + } + str += "."; + for (; i < strlen(buf); i++) { + str += buf[i]; + } + return str; +} \ No newline at end of file diff --git a/MenuUtils.h b/MenuUtils.h new file mode 100644 index 0000000..435f756 --- /dev/null +++ b/MenuUtils.h @@ -0,0 +1,12 @@ +#ifndef MENU_UTILS_H +#define MENU_UTILS_H +#define _CRT_SECURE_NO_WARNINGS + +#include +#include +using namespace std; + +void checkOptInput(int& opt, int low, int high); +string doubleToString(double num); + +#endif // !MENU_UTILS_H diff --git a/SAS.vcxproj b/SAS.vcxproj index e80c9f9..1da7adf 100644 --- a/SAS.vcxproj +++ b/SAS.vcxproj @@ -133,6 +133,8 @@ + + @@ -141,6 +143,8 @@ + + diff --git a/SAS.vcxproj.filters b/SAS.vcxproj.filters index e4cc194..069ba5d 100644 --- a/SAS.vcxproj.filters +++ b/SAS.vcxproj.filters @@ -42,6 +42,12 @@ 婧愭枃浠 + + 婧愭枃浠 + + + 婧愭枃浠 + @@ -56,5 +62,11 @@ 澶存枃浠 + + 澶存枃浠 + + + 澶存枃浠 + \ No newline at end of file diff --git a/StudentDAO.h b/StudentDAO.h index 3a08fa8..40728ac 100644 --- a/StudentDAO.h +++ b/StudentDAO.h @@ -11,7 +11,8 @@ void addStudent(_ConnectionPtr connection, Student student); void deleteStudent(_ConnectionPtr connection, string id); void updateStudentClass(_ConnectionPtr connection, string id, string stuClass); void updateStudentStatus(_ConnectionPtr connection, string id, string status); -Student& getStudentById(_ConnectionPtr connection, string id); -std::vector& getStudentByName(_ConnectionPtr connection, string name); +Student* getStudentById(_ConnectionPtr connection, string id); +std::vector* getStudentByName(_ConnectionPtr connection, string name); +std::vector* getAllStudent(_ConnectionPtr connection); #endif // !STUDENT_DAO_H \ No newline at end of file diff --git a/StudentDAOImpl.cpp b/StudentDAOImpl.cpp index 6b9d986..de7c5d3 100644 --- a/StudentDAOImpl.cpp +++ b/StudentDAOImpl.cpp @@ -28,9 +28,10 @@ void updateStudentStatus(_ConnectionPtr connection, string id, string status) { connection->Execute(sql.c_str(), NULL, (long)0); } -Student& getStudentById(_ConnectionPtr connection, string id) { +Student* getStudentById(_ConnectionPtr connection, string id) { string sql = "select * from student where id = '" + id + "'"; _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; Student *student = new Student(); student->setId((char*)(_bstr_t)record->Fields->GetItem("id")->Value); @@ -38,12 +39,13 @@ Student& getStudentById(_ConnectionPtr connection, string id) { student->setSex((char*)(_bstr_t)record->Fields->GetItem("sex")->Value); student->setStuClass((char*)(_bstr_t)record->Fields->GetItem("stuClass")->Value); student->setStatus((char*)(_bstr_t)record->Fields->GetItem("status")->Value); - return *student; + return student; } -std::vector& getStudentByName(_ConnectionPtr connection, string name) { +std::vector* getStudentByName(_ConnectionPtr connection, string name) { string sql = "select * from student where name = '" + name + "'"; _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; std::vector *v = new std::vector(); Student* student; @@ -56,5 +58,24 @@ std::vector& getStudentByName(_ConnectionPtr connection, string name) { student->setStatus((char*)(_bstr_t)record->Fields->GetItem("status")->Value); v->push_back(*student); } - return *v; + return v; +} + +std::vector* getAllStudent(_ConnectionPtr connection) { + string sql = "select * from student"; + _RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0); + if (record->BOF == -1) return NULL; + + std::vector* v = new std::vector(); + Student* student; + for (; !record->EndOfFile; record->MoveNext()) { + student = new Student(); + student->setId((char*)(_bstr_t)record->Fields->GetItem("id")->Value); + student->setName((char*)(_bstr_t)record->Fields->GetItem("name")->Value); + student->setSex((char*)(_bstr_t)record->Fields->GetItem("sex")->Value); + student->setStuClass((char*)(_bstr_t)record->Fields->GetItem("stuClass")->Value); + student->setStatus((char*)(_bstr_t)record->Fields->GetItem("status")->Value); + v->push_back(*student); + } + return v; } \ No newline at end of file diff --git a/main.cpp b/main.cpp index b01f75a..dc2cd45 100644 --- a/main.cpp +++ b/main.cpp @@ -4,9 +4,10 @@ #include #include // setlocale #include -#include +#include #include "DBCUtils.h" #include "StudentDAO.h" +#include "Menu.h" using namespace std; //高级功能 直接输入sql语句 @@ -22,11 +23,14 @@ int main() { _ConnectionPtr pconnect(_uuidof(Connection)); if (getConnection(pwd, pconnect)) { - try { - - }catch (_com_error& err) { - wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage()); - wprintf(L"Description = %s\n", (wchar_t*)err.Description()); + while (true) { + try { + mainMenu(pconnect); + } + catch (_com_error& err) { + wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage()); + wprintf(L"Description = %s\n", (wchar_t*)err.Description()); + } } } CoUninitialize();