2022-07-04 16:00

This commit is contained in:
zyx 2022-07-04 15:59:30 +08:00
parent a0109d2c99
commit 7851f2c38d
6 changed files with 471 additions and 149 deletions

View File

@ -18,11 +18,7 @@ public:
void setName(string name) { this->name = name; } void setName(string name) { this->name = name; }
string toString() { string toString() {
string toStr = "Course{"; return id + "\t" + name;
toStr += "id='" + id + '\'' +
", name='" + name + '\'' +
'}';
return toStr;
} }
private: private:
string id;//¿Î³ÌºÅ string id;//¿Î³ÌºÅ

View File

@ -22,12 +22,7 @@ public:
void setGrade(string grade) { this->grade = grade; } void setGrade(string grade) { this->grade = grade; }
string toString() { string toString() {
string toStr = "Grade{"; return stuId + "\t" + courseId + "\t" + grade;
toStr += "stuId='" + stuId + '\'' +
", courseId='" + courseId + '\'' +
", grade='" + grade + '\'' +
'}';
return toStr;
} }
private: private:
string stuId; string stuId;

357
Menu.cpp
View File

@ -1,12 +1,8 @@
#include<iostream>
#include"Menu.h" #include"Menu.h"
#include"MenuUtils.h"
#include"StudentDAO.h"
#include"GradeDAO.h"
#include"CourseDAO.h"
using namespace std;
void mainMenu(_ConnectionPtr connection) { void mainMenu(_ConnectionPtr connection) {
while (true) {
try {
system("cls"); system("cls");
printf("1.显示所有学生\n"); printf("1.显示所有学生\n");
printf("2.增加学生\n"); printf("2.增加学生\n");
@ -16,10 +12,11 @@ void mainMenu(_ConnectionPtr connection) {
printf("6.导出学生信息到文件\n"); printf("6.导出学生信息到文件\n");
printf("7.学生成绩管理\n"); printf("7.学生成绩管理\n");
printf("8.课程管理\n"); printf("8.课程管理\n");
printf("9.高级功能\n");
printf("0.退出\n"); printf("0.退出\n");
int opt = -1; int opt = -1;
checkOptInput(opt, 0, 8); checkOptInput(opt, 0, 9);
switch (opt) { switch (opt) {
case 1: case 1:
@ -46,14 +43,27 @@ void mainMenu(_ConnectionPtr connection) {
case 8: case 8:
courseMenu(connection); courseMenu(connection);
break; break;
case 9:
advancedFunction(connection);
break;
default: default:
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
printf("\n"); printf("\n");
system("pause");
}
catch (_com_error& err) {
wprintf(L"操作失败!\n");
wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n", (wchar_t*)err.Description());
system("pause");
}
}
} }
void studentGradeMenu(_ConnectionPtr connection) { void studentGradeMenu(_ConnectionPtr connection) {
while (true) { while (true) {
try {
system("cls"); system("cls");
printf("1.输入学生成绩\n"); printf("1.输入学生成绩\n");
printf("2.获取学生平均成绩\n"); printf("2.获取学生平均成绩\n");
@ -132,7 +142,7 @@ void studentGradeMenu(_ConnectionPtr connection) {
size = v->size(); size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n"; cout << "学号\t" << "课程名\t" << "成绩\n";
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n"; cout << (*v)[i].toString() << "\n";
} }
break; break;
case 8://二级拓展菜单 case 8://二级拓展菜单
@ -143,10 +153,18 @@ void studentGradeMenu(_ConnectionPtr connection) {
} }
system("pause"); system("pause");
} }
catch (_com_error& err) {
wprintf(L"操作失败!\n");
wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n", (wchar_t*)err.Description());
system("pause");
}
}
} }
void studentGradeExternMenu(_ConnectionPtr connection) { void studentGradeExternMenu(_ConnectionPtr connection) {
while (true) { while (true) {
try {
system("cls"); system("cls");
printf("1.删除某学生某科成绩\n"); printf("1.删除某学生某科成绩\n");
printf("2.删除某学生成绩\n"); printf("2.删除某学生成绩\n");
@ -236,7 +254,7 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
break; break;
} }
cout << "学号\t" << "课程名\t" << "成绩\n"; cout << "学号\t" << "课程名\t" << "成绩\n";
cout << (*gra).getStuId() << "\t" << getCourseById(connection, (*gra).getCourseId())->getName() << "\t" << (*gra).getGradeStr() << "\n"; cout << (*gra).toString() << "\n";
break; break;
case 6://查询某学生成绩 case 6://查询某学生成绩
printf("请输入学号:"); printf("请输入学号:");
@ -249,7 +267,7 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
size = v->size(); size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n"; cout << "学号\t" << "课程名\t" << "成绩\n";
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n"; cout << (*v)[i].toString() << "\n";
} }
break; break;
case 7://查询某科成绩 case 7://查询某科成绩
@ -268,7 +286,7 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
size = v->size(); size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n"; cout << "学号\t" << "课程名\t" << "成绩\n";
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n"; cout << (*v)[i].toString() << "\n";
} }
break; break;
case 8://查询所有成绩 case 8://查询所有成绩
@ -280,7 +298,7 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
size = v->size(); size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n"; cout << "学号\t" << "课程名\t" << "成绩\n";
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n"; cout << (*v)[i].toString() << "\n";
} }
break; break;
default://返回上一级 default://返回上一级
@ -288,10 +306,18 @@ void studentGradeExternMenu(_ConnectionPtr connection) {
} }
system("pause"); system("pause");
} }
catch (_com_error& err) {
wprintf(L"操作失败!\n");
wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n", (wchar_t*)err.Description());
system("pause");
}
}
} }
void courseMenu(_ConnectionPtr connection) { void courseMenu(_ConnectionPtr connection) {
while (true) { while (true) {
try {
system("cls"); system("cls");
printf("1.添加课程\n"); printf("1.添加课程\n");
printf("2.删除课程\n"); printf("2.删除课程\n");
@ -299,7 +325,7 @@ void courseMenu(_ConnectionPtr connection) {
printf("4.查询所有课程\n"); printf("4.查询所有课程\n");
printf("0.返回上一级\n"); printf("0.返回上一级\n");
int opt = -1; int opt = -1;
checkOptInput(opt, 0, 8); checkOptInput(opt, 0, 4);
printf("\n"); printf("\n");
@ -338,7 +364,7 @@ void courseMenu(_ConnectionPtr connection) {
size = v->size(); size = v->size();
cout << "课程号\t" << "课程名\n"; cout << "课程号\t" << "课程名\n";
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
cout << (*v)[i].getId() << "\t" << (*v)[i].getName() << "\n"; cout << (*v)[i].toString() << "\n";
} }
break; break;
default://返回上一级 default://返回上一级
@ -346,13 +372,71 @@ void courseMenu(_ConnectionPtr connection) {
} }
system("pause"); system("pause");
} }
catch (_com_error& err) {
wprintf(L"操作失败!\n");
wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n", (wchar_t*)err.Description());
system("pause");
}
}
} }
void advancedFunction(_ConnectionPtr connection) {
system("cls");
_RecordsetPtr record(_uuidof(Recordset));
string sql;
cin.clear();
while (cin.get() != '\n');
while (true) {
try {
wprintf(L"sql>");
//获取sql命令及种类
getline(cin, sql);
while (sql == "" || sql[sql.size() - 1] != ';') {
wprintf(L" >");
string tmp;
getline(cin, tmp);
sql += " " + tmp;
}
string comType = "";
getSqlType(sql, comType);
//执行sql命令
if (comType == "select") {//查询
record = connection->Execute(sql.c_str(), NULL, (long)0);
for (long i = 0; i < record->Fields->Count; i++)
wprintf(L"%s\t", (wchar_t*)record->Fields->GetItem(i)->Name);
printf("\n");
wprintf(L"%s", (wchar_t*)record->GetString(adClipString, long(-1), "\t", "\n", "Null"));
//showRecordInfo(record);
}else if (comType == "exit" || comType == "exit;") {
system("pause");
break;
}else {
connection->Execute(sql.c_str(), NULL, (long)0);
}
printf("\n");
}
catch (_com_error& err) {
wprintf(L"\nThe application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n\n", (wchar_t*)err.Description());
}
}
}
void showAllStudent(_ConnectionPtr connection) { void showAllStudent(_ConnectionPtr connection) {
vector<Student> vs = *getAllStudent(connection); vector<Student>* vs = getAllStudent(connection);
int size = vs.size(); if (vs == NULL) {
printf("学生信息为空!\n");
return;
}
int size = vs->size();
printf("学号\t姓名\t性别\t班级\t状态\n");
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
cout << vs[i].toString() << "\n"; cout << (*vs)[i].toString() << "\n";
} }
} }
@ -423,9 +507,248 @@ void changeStudent(_ConnectionPtr connection) {
} }
void exportData(_ConnectionPtr connection) { void exportData(_ConnectionPtr connection) {
while (true) {
try {
system("cls");
printf("1.导出学生信息\n");
printf("2.导出课程信息\n");
printf("3.导出成绩信息\n");
printf("4.导出所有信息\n");
printf("0.返回上一级\n");
int opt = -1;
checkOptInput(opt, 0, 4);
printf("\n");
switch (opt) {
case 1://导出学生信息
exportStudentData(connection);
break;
case 2://导出课程信息
exportCourseData(connection);
break;
case 3://导出成绩信息
exportGradeData(connection);
break;
case 4://导出所有信息
exportAllData(connection);
break;
default://返回上一级
return;
} }
system("pause");
}
catch (_com_error& err) {
wprintf(L"操作失败!\n");
wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n", (wchar_t*)err.Description());
system("pause");
}
}
}
void exportStudentData(_ConnectionPtr connection) {
ofstream outFile("student.txt", ios::out);
if (!outFile.is_open()) {
printf("导出student.txt文件失败\n");
return;
}
int size;
vector<Student>* vs;
vs = getAllStudent(connection);
if (vs == NULL) {
printf("学生信息为空!\n");
outFile.close();
return;
}
outFile << "学号\t姓名\t性别\t班级\t状态\n";
size = vs->size();
for (int i = 0; i < size; i++) {
outFile << (*vs)[i].toString() << "\n";
}
printf("成功导出student.txt文件!\n");
outFile.close();
}
void exportCourseData(_ConnectionPtr connection) {
ofstream outFile("course.txt", ios::out);
if (!outFile.is_open()) {
printf("导出course.txt文件失败\n");
return;
}
int size;
vector<Course>* vc;
vc = getAllCourse(connection);
if (vc == NULL) {
printf("课程信息为空!\n");
outFile.close();
return;
}
outFile << "课程号\t课程名\n";
size = vc->size();
for (int i = 0; i < size; i++) {
outFile << (*vc)[i].toString() << "\n";
}
printf("成功导出course.txt文件!\n");
outFile.close();
}
void exportGradeData(_ConnectionPtr connection) {
ofstream outFile("grade.txt", ios::out);
if (!outFile.is_open()) {
printf("导出grade.txt文件失败\n");
return;
}
int size;
vector<Grade>* vg;
vg = getAllGrade(connection);
if (vg == NULL) {
printf("成绩信息为空!\n");
outFile.close();
return;
}
outFile << "学号\t课程号\t成绩\n";
size = vg->size();
for (int i = 0; i < size; i++) {
outFile << (*vg)[i].toString() << "\n";
}
printf("成功导出grade.txt文件!\n");
outFile.close();
}
void exportAllData(_ConnectionPtr connection) {
exportStudentData(connection);
exportCourseData(connection);
exportGradeData(connection);
}
void importData(_ConnectionPtr connection) { void importData(_ConnectionPtr connection) {
while (true) {
try {
system("cls");
printf("1.导入学生信息\n");
printf("2.导入课程信息\n");
printf("3.导入成绩信息\n");
printf("4.导入所有信息\n");
printf("0.返回上一级\n");
int opt = -1;
checkOptInput(opt, 0, 4);
printf("\n");
switch (opt) {
case 1://导入学生信息
importStudentData(connection);
break;
case 2://导入课程信息
importCourseData(connection);
break;
case 3://导入成绩信息
importGradeData(connection);
break;
case 4://导入所有信息
importAllData(connection);
break;
default://返回上一级
return;
}
system("pause");
}
catch (_com_error& err) {
wprintf(L"操作失败!\n");
wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n", (wchar_t*)err.Description());
system("pause");
}
}
}
void importStudentData(_ConnectionPtr connection) {
ifstream inFile("student.txt", ios::in);
if (!inFile.is_open()) {
printf("读取student.txt文件失败\n");
return;
}
string title[5];
inFile >> title[0] >> title[1] >> title[2] >> title[3] >> title[4];
if (!(title[0] == "学号" && title[1] == "姓名" && title[2] == "性别" && title[3] == "班级" && title[4] == "状态")) {
printf("文件格式异常,读取失败!\n");
return;
}
Student* student = new Student();
while (!inFile.eof()) {
inFile >> title[0] >> title[1] >> title[2] >> title[3] >> title[4];
student->setId(title[0]);
student->setName(title[1]);
student->setSex(title[2]);
student->setStuClass(title[3]);
student->setStatus(title[4]);
addStudent(connection, *student);
}
printf("成功导入student.txt文件!\n");
inFile.close();
}
void importCourseData(_ConnectionPtr connection) {
ifstream inFile("course.txt", ios::in);
if (!inFile.is_open()) {
printf("读取course.txt文件失败\n");
return;
}
string title[2];
inFile >> title[0] >> title[1];
if (!(title[0] == "课程号" && title[1] == "课程名")) {
printf("文件格式异常,读取失败!\n");
return;
}
Course* course = new Course();
while (!inFile.eof()) {
inFile >> title[0] >> title[1];
course->setId(title[0]);
course->setName(title[1]);
addCourse(connection, *course);
}
printf("成功导入course.txt文件!\n");
inFile.close();
}
void importGradeData(_ConnectionPtr connection) {
ifstream inFile("grade.txt", ios::in);
if (!inFile.is_open()) {
printf("读取grade.txt文件失败\n");
return;
}
string title[3];
inFile >> title[0] >> title[1] >> title[2];
if (!(title[0] == "学号" && title[1] == "课程号" && title[2] == "成绩")) {
printf("文件格式异常,读取失败!\n");
return;
}
Grade* grade = new Grade();
while (!inFile.eof()) {
inFile >> title[0] >> title[1] >> title[2];
grade->setStuId(title[0]);
grade->setCourseId(title[1]);
grade->setGrade(title[2]);
addGrade(connection, *grade);
}
printf("成功导入grade.txt文件!\n");
inFile.close();
}
void importAllData(_ConnectionPtr connection) {
importStudentData(connection);
importCourseData(connection);
importGradeData(connection);
} }

18
Menu.h
View File

@ -3,15 +3,24 @@
#import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile") #import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include<iostream>
#include<fstream>
#include<vector> #include<vector>
#include"Student.cpp" #include"Student.cpp"
#include"Course.cpp" #include"Course.cpp"
#include"Grade.cpp" #include"Grade.cpp"
#include"DBCUtils.h"
#include"MenuUtils.h"
#include"StudentDAO.h"
#include"GradeDAO.h"
#include"CourseDAO.h"
using namespace std;
void mainMenu(_ConnectionPtr connection); void mainMenu(_ConnectionPtr connection);
void studentGradeMenu(_ConnectionPtr connection); void studentGradeMenu(_ConnectionPtr connection);
void studentGradeExternMenu(_ConnectionPtr connection); void studentGradeExternMenu(_ConnectionPtr connection);
void courseMenu(_ConnectionPtr connection); void courseMenu(_ConnectionPtr connection);
void advancedFunction(_ConnectionPtr connection);
void showAllStudent(_ConnectionPtr connection); void showAllStudent(_ConnectionPtr connection);
void insertStudent(_ConnectionPtr connection); void insertStudent(_ConnectionPtr connection);
@ -19,6 +28,15 @@ void deleteStudent(_ConnectionPtr connection);
void changeStudent(_ConnectionPtr connection); void changeStudent(_ConnectionPtr connection);
void exportData(_ConnectionPtr connection); void exportData(_ConnectionPtr connection);
void exportStudentData(_ConnectionPtr connection);
void exportCourseData(_ConnectionPtr connection);
void exportGradeData(_ConnectionPtr connection);
void exportAllData(_ConnectionPtr connection);
void importData(_ConnectionPtr connection); void importData(_ConnectionPtr connection);
void importStudentData(_ConnectionPtr connection);
void importCourseData(_ConnectionPtr connection);
void importGradeData(_ConnectionPtr connection);
void importAllData(_ConnectionPtr connection);
#endif // !MENU_H #endif // !MENU_H

View File

@ -26,14 +26,7 @@ public:
void setStatus(string status) { this->status = status; } void setStatus(string status) { this->status = status; }
string toString() { string toString() {
string toStr = "Student{"; return id + "\t" + name + "\t" + sex + "\t" + stuClass + "\t" + status;
toStr += "id='" + id + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", stuClass='" + stuClass + '\'' +
", status='" + status + '\'' +
'}';
return toStr;
} }
private: private:
string id;//学号 string id;//学号

View File

@ -10,29 +10,26 @@
#include "Menu.h" #include "Menu.h"
using namespace std; using namespace std;
//高级功能 直接输入sql语句
int main() { int main() {
setlocale(LC_ALL, "chs"); setlocale(LC_ALL, "chs");
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
string pwd; string pwd;
while (true) {
try {
wprintf(L"Enter password:"); wprintf(L"Enter password:");
getline(cin, pwd); getline(cin, pwd);
_ConnectionPtr pconnect(_uuidof(Connection)); _ConnectionPtr pconnect(_uuidof(Connection));
if (getConnection(pwd, pconnect)) { if (getConnection(pwd, pconnect)) {
while (true) {
try {
mainMenu(pconnect); mainMenu(pconnect);
} }
}
catch (_com_error& err) { catch (_com_error& err) {
wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage()); wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n", (wchar_t*)err.Description()); wprintf(L"Description = %s\n", (wchar_t*)err.Description());
} }
} }
}
CoUninitialize(); CoUninitialize();
return 0; return 0;
} }