2022-07-04 23:38 增加对学生、课程、成绩等是否存在的检测

This commit is contained in:
zyx 2022-07-04 23:39:46 +08:00
parent 81603a64b5
commit 0ddf037a96
2 changed files with 162 additions and 9 deletions

View File

@ -17,7 +17,7 @@ bool getConnection(std::string& pwd, _ConnectionPtr& connection) {
wprintf(L"Description = %s\n", (wchar_t*)err.Description()); wprintf(L"Description = %s\n", (wchar_t*)err.Description());
} }
if (!isPwdTrue) {//密码错误连接失败打开连接失败跳出try块执行 if (!isPwdTrue) {//密码错误连接失败打开连接失败跳出try块执行
wprintf(L"密码错误!"); wprintf(L"密码错误!\n");
return false; return false;
} }
} }
@ -34,7 +34,7 @@ bool getConnection(std::string& pwd, _ConnectionPtr& connection) {
wprintf(L"Description = %s\n", (wchar_t*)err.Description()); wprintf(L"Description = %s\n", (wchar_t*)err.Description());
} }
if (!isPwdTrue) {//验证失败连接失败打开连接失败跳出try块执行 if (!isPwdTrue) {//验证失败连接失败打开连接失败跳出try块执行
wprintf(L"验证失败!"); wprintf(L"验证失败!\n");
return false; return false;
} }
} }

167
Menu.cpp
View File

@ -80,55 +80,120 @@ void studentGradeMenu(_ConnectionPtr connection) {
printf("\n"); printf("\n");
string stuId, courseId; string stuId, courseId;
string stuClass, courseName; string stuClass, courseName;
double grade = -1; double grade = -1;
size_t size; size_t size;
Student* student;
Course* course;
vector<Grade>* v; vector<Grade>* v;
switch (opt) { switch (opt) {
case 1://输入学生成绩 case 1://输入学生成绩
printf("请输入学号:"); printf("请输入学号:");
cin >> stuId; cin >> stuId;
student = getStudentById(connection, stuId);
if (student == NULL) {
printf("该学生不存在!\n");
break;
}
printf("请输入课程名:"); printf("请输入课程名:");
cin >> courseId; cin >> courseName;
course = getCourseByName(connection, courseName);
if (course == NULL) {
printf("该课程不存在!\n");
break;
}
printf("请输入分数(0-100)"); printf("请输入分数(0-100)");
while (!(cin >> grade) || (opt < 0) || (opt > 100)) { while (!(cin >> grade) || (grade < 0) || (grade > 100)) {
cout << "输入错误!请重新输入:"; cout << "输入错误!请重新输入:";
cin.clear(); cin.clear();
while (cin.get() != '\n'); while (cin.get() != '\n');
} }
addGrade(connection, *new Grade(stuId, courseId, doubleToString(grade)));
addGrade(connection, *new Grade(stuId, course->getId(), doubleToString(grade)));
printf("添加成功!\n"); printf("添加成功!\n");
break; break;
case 2://获取学生平均成绩 case 2://获取学生平均成绩
printf("请输入学号:"); printf("请输入学号:");
cin >> stuId; cin >> stuId;
student = getStudentById(connection, stuId);
if (student == NULL) {
printf("该学生不存在!\n");
break;
}
printf("学生平均成绩为:%.2lf\n", getOnesAvgGrade(connection, stuId)); printf("学生平均成绩为:%.2lf\n", getOnesAvgGrade(connection, stuId));
break; break;
case 3://获取学生总成绩 case 3://获取学生总成绩
printf("请输入学号:"); printf("请输入学号:");
cin >> stuId; cin >> stuId;
student = getStudentById(connection, stuId);
if (student == NULL) {
printf("该学生不存在!\n");
break;
}
printf("学生总成绩为:%.2lf\n", getOnesAllGrade(connection, stuId)); printf("学生总成绩为:%.2lf\n", getOnesAllGrade(connection, stuId));
break; break;
case 4://获取班级平均分 case 4://获取班级平均分
printf("请输入班级:"); printf("请输入班级:");
cin >> stuClass; cin >> stuClass;
v = getGradeByStuClass(connection, stuClass);
if (v == NULL) {
printf("该班级不存在或无成绩记录!\n");
break;
}
printf("请输入课程名:"); printf("请输入课程名:");
cin >> courseName; cin >> courseName;
course = getCourseByName(connection, courseName);
if (course == NULL) {
printf("该课程不存在!\n");
break;
}
cout << stuClass << " 班级 " << courseName << " 课程的平均分为:" << getClassAvgGrade(connection, stuClass, courseName) << "\n"; cout << stuClass << " 班级 " << courseName << " 课程的平均分为:" << getClassAvgGrade(connection, stuClass, courseName) << "\n";
break; break;
case 5://获取班级最高分 case 5://获取班级最高分
printf("请输入班级:"); printf("请输入班级:");
cin >> stuClass; cin >> stuClass;
v = getGradeByStuClass(connection, stuClass);
if (v == NULL) {
printf("该班级不存在或无成绩记录!\n");
break;
}
printf("请输入课程名:"); printf("请输入课程名:");
cin >> courseName; cin >> courseName;
course = getCourseByName(connection, courseName);
if (course == NULL) {
printf("该课程不存在!\n");
break;
}
cout << stuClass << " 班级 " << courseName << " 课程的最高分为:" << getClassMaxGrade(connection, stuClass, courseName) << "\n"; cout << stuClass << " 班级 " << courseName << " 课程的最高分为:" << getClassMaxGrade(connection, stuClass, courseName) << "\n";
break; break;
case 6://获取班级最低分 case 6://获取班级最低分
printf("请输入班级:"); printf("请输入班级:");
cin >> stuClass; cin >> stuClass;
v = getGradeByStuClass(connection, stuClass);
if (v == NULL) {
printf("该班级不存在或无成绩记录!\n");
break;
}
printf("请输入课程名:"); printf("请输入课程名:");
cin >> courseName; cin >> courseName;
course = getCourseByName(connection, courseName);
if (course == NULL) {
printf("该课程不存在!\n");
break;
}
cout << stuClass << " 班级 " << courseName << " 课程的最低分为:" << getClassMinGrade(connection, stuClass, courseName) << "\n"; cout << stuClass << " 班级 " << courseName << " 课程的最低分为:" << getClassMinGrade(connection, stuClass, courseName) << "\n";
break; break;
case 7://获取班级成绩 case 7://获取班级成绩
@ -136,9 +201,10 @@ void studentGradeMenu(_ConnectionPtr connection) {
cin >> stuClass; cin >> stuClass;
v = getGradeByStuClass(connection, stuClass); v = getGradeByStuClass(connection, stuClass);
if (v == NULL) { if (v == NULL) {
printf("该班级不存在\n"); printf("该班级不存在或无成绩记录\n");
break; break;
} }
size = v->size(); size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n"; cout << "学号\t" << "课程名\t" << "成绩\n";
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
@ -188,10 +254,17 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
vector<Grade>* v; vector<Grade>* v;
Grade* gra; Grade* gra;
Course* course; Course* course;
Student* student;
switch (opt) { switch (opt) {
case 1://删除某学生某科成绩 case 1://删除某学生某科成绩
printf("请输入学号:"); printf("请输入学号:");
cin >> stuId; cin >> stuId;
student = getStudentById(connection, stuId);
if (student == NULL) {
printf("该学生不存在!\n");
break;
}
printf("请输入课程名:"); printf("请输入课程名:");
cin >> courseName; cin >> courseName;
course = getCourseByName(connection, courseName); course = getCourseByName(connection, courseName);
@ -199,12 +272,31 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
printf("该课程不存在!\n"); printf("该课程不存在!\n");
break; break;
} }
gra = getOnesGrade(connection, stuId, course->getId());
if (gra == NULL) {
printf("该学生不存在该课程的成绩记录!\n");
break;
}
deleteOnesGrade(connection, stuId, course->getId()); deleteOnesGrade(connection, stuId, course->getId());
printf("删除成功!\n"); printf("删除成功!\n");
break; break;
case 2://删除某学生成绩 case 2://删除某学生成绩
printf("请输入学号:"); printf("请输入学号:");
cin >> stuId; cin >> stuId;
student = getStudentById(connection, stuId);
if (student == NULL) {
printf("该学生不存在!\n");
break;
}
v = getGradeByStuId(connection, stuId);
if (v == NULL) {
printf("该学生不存在成绩记录!\n");
break;
}
deleteGradeByStuId(connection, stuId); deleteGradeByStuId(connection, stuId);
printf("删除成功!\n"); printf("删除成功!\n");
break; break;
@ -216,12 +308,25 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
printf("该课程不存在!\n"); printf("该课程不存在!\n");
break; break;
} }
v = getGradeByCourseId(connection, course->getId());
if (v == NULL) {
printf("该课程不存在成绩记录!\n");
break;
}
deleteGradeByCourseId(connection, course->getId()); deleteGradeByCourseId(connection, course->getId());
printf("删除成功!\n"); printf("删除成功!\n");
break; break;
case 4://更改某学生某科成绩 case 4://更改某学生某科成绩
printf("请输入学号:"); printf("请输入学号:");
cin >> stuId; cin >> stuId;
student = getStudentById(connection, stuId);
if (student == NULL) {
printf("该学生不存在!\n");
break;
}
printf("请输入课程名:"); printf("请输入课程名:");
cin >> courseName; cin >> courseName;
course = getCourseByName(connection, courseName); course = getCourseByName(connection, courseName);
@ -229,18 +334,32 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
printf("该课程不存在!\n"); printf("该课程不存在!\n");
break; break;
} }
gra = getOnesGrade(connection, stuId, course->getId());
if (gra == NULL) {
printf("该学生不存在该课程的成绩记录!\n");
break;
}
printf("请输入新分数(0-100)"); printf("请输入新分数(0-100)");
while (!(cin >> grade) || (opt < 0) || (opt > 100)) { while (!(cin >> grade) || (opt < 0) || (opt > 100)) {
cout << "输入错误!请重新输入:"; cout << "输入错误!请重新输入:";
cin.clear(); cin.clear();
while (cin.get() != '\n'); while (cin.get() != '\n');
} }
updateOnesGrade(connection, stuId, course->getId(), doubleToString(grade)); updateOnesGrade(connection, stuId, course->getId(), doubleToString(grade));
printf("更改成功!\n"); printf("更改成功!\n");
break; break;
case 5://查询某学生某科成绩 case 5://查询某学生某科成绩
printf("请输入学号:"); printf("请输入学号:");
cin >> stuId; cin >> stuId;
student = getStudentById(connection, stuId);
if (student == NULL) {
printf("该学生不存在!\n");
break;
}
printf("请输入课程名:"); printf("请输入课程名:");
cin >> courseName; cin >> courseName;
course = getCourseByName(connection, courseName); course = getCourseByName(connection, courseName);
@ -248,22 +367,31 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
printf("该课程不存在!\n"); printf("该课程不存在!\n");
break; break;
} }
gra = getOnesGrade(connection, stuId, course->getId()); gra = getOnesGrade(connection, stuId, course->getId());
if (gra == NULL) { if (gra == NULL) {
printf("条记录不存在\n"); printf("学生不存在该课程的成绩记录\n");
break; break;
} }
cout << "学号\t" << "课程名\t" << "成绩\n"; cout << "学号\t" << "课程名\t" << "成绩\n";
cout << (*gra).toString() << "\n"; cout << (*gra).toString() << "\n";
break; break;
case 6://查询某学生成绩 case 6://查询某学生成绩
printf("请输入学号:"); printf("请输入学号:");
cin >> stuId; cin >> stuId;
v = getGradeByStuId(connection, stuId); student = getStudentById(connection, stuId);
if (v == NULL) { if (student == NULL) {
printf("该学生不存在!\n"); printf("该学生不存在!\n");
break; break;
} }
v = getGradeByStuId(connection, stuId);
if (v == NULL) {
printf("该学生不存在成绩记录!\n");
break;
}
size = v->size(); size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n"; cout << "学号\t" << "课程名\t" << "成绩\n";
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
@ -278,11 +406,13 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
printf("该课程不存在!\n"); printf("该课程不存在!\n");
break; break;
} }
v = getGradeByCourseId(connection, course->getId()); v = getGradeByCourseId(connection, course->getId());
if (v == NULL) { if (v == NULL) {
printf("该课程不存在对应成绩!\n"); printf("该课程不存在对应成绩!\n");
break; break;
} }
size = v->size(); size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n"; cout << "学号\t" << "课程名\t" << "成绩\n";
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
@ -295,6 +425,7 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
printf("无成绩记录!\n"); printf("无成绩记录!\n");
break; break;
} }
size = v->size(); size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n"; cout << "学号\t" << "课程名\t" << "成绩\n";
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
@ -332,10 +463,17 @@ void courseMenu(_ConnectionPtr connection) {
size_t size; size_t size;
string courseId, courseName; string courseId, courseName;
vector<Course>* v; vector<Course>* v;
Course* course;
switch (opt) { switch (opt) {
case 1://添加课程 case 1://添加课程
printf("请输入课程号:"); printf("请输入课程号:");
cin >> courseId; cin >> courseId;
course = getCourseById(connection,courseId);
if (course != NULL) {
printf("该课程已存在!\n");
break;
}
printf("请输入课程名:"); printf("请输入课程名:");
cin >> courseName; cin >> courseName;
addCourse(connection, *new Course(courseId, courseName)); addCourse(connection, *new Course(courseId, courseName));
@ -344,12 +482,24 @@ void courseMenu(_ConnectionPtr connection) {
case 2://删除课程 case 2://删除课程
printf("请输入课程号:"); printf("请输入课程号:");
cin >> courseId; cin >> courseId;
course = getCourseById(connection, courseId);
if (course == NULL) {
printf("该课程不存在!\n");
break;
}
deleteCourse(connection, courseId); deleteCourse(connection, courseId);
printf("删除成功!\n"); printf("删除成功!\n");
break; break;
case 3://更改课程 case 3://更改课程
printf("请输入课程号:"); printf("请输入课程号:");
cin >> courseId; cin >> courseId;
course = getCourseById(connection, courseId);
if (course == NULL) {
printf("该课程不存在!\n");
break;
}
printf("请输入课程名:"); printf("请输入课程名:");
cin >> courseName; cin >> courseName;
updateCourseName(connection, courseId, courseName); updateCourseName(connection, courseId, courseName);
@ -361,6 +511,7 @@ void courseMenu(_ConnectionPtr connection) {
printf("不存在课程记录!\n"); printf("不存在课程记录!\n");
break; break;
} }
size = v->size(); size = v->size();
cout << "课程号\t" << "课程名\n"; cout << "课程号\t" << "课程名\n";
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
@ -455,6 +606,7 @@ void insertStudent(_ConnectionPtr connection) {
printf("该学生已存在!\n"); printf("该学生已存在!\n");
return; return;
} }
stu->setId(str); stu->setId(str);
printf("请输入姓名:"); printf("请输入姓名:");
cin >> str; cin >> str;
@ -482,6 +634,7 @@ void deleteStudent(_ConnectionPtr connection) {
printf("该学生不存在!\n"); printf("该学生不存在!\n");
return; return;
} }
deleteStudent(connection, id); deleteStudent(connection, id);
printf("删除成功!\n"); printf("删除成功!\n");
} }