1、设计一个成绩管理系统。包括 添加(insert),修改(update),删除(delete),查找(SQL)四个功能 成绩管理系统3个表 :学生(学号,姓名,性别);课程(课程号,课程名);成绩(学号,课程号,分数) 学生.学号与成绩.学号关联,课程.课程号与成绩.课程号关联。数据库相关知识点:表结构的操作 表记录的操作 表的查询
头文件student.h
#pragma once //student.h
#include<iostream>
#include<string>
using namespace std;
class Student //定义student类 ,其中有学号、名字、性别的数据成员
{
public:
int studentID;
string name, sex;
int LessonNumber;
string LessonName;
int score;
Student(int sID,string na,string se ,int LNum,string LNa ,int sco );
void showsth();
};
头文件studentscore.h
#pragma once
#include<iostream>
using namespace std;
#include"student.h"
#include<fstream>
#define FILENAME "stuFile.txt"
class StudentScore
{
public:
StudentScore();
void Show_Menu();//菜单
void ExitSystem();//退出
int m_StuNum;//学生人数
Student** m_StuArray;
void Add_Stu();//添加
//保存文件
void save();
//判断是否为空
bool m_FileIsEmpty;
//统计人数
int get_StuNum();
//初始化学生
void init_Stu();
//显示学生
//删除学生
void Del_Stu();
//判断有无
int IsExist(int studentId);
//修改学生
void Mod_Stu();
//查找学生
void Find_Stu();
~StudentScore();
};
源文件student.cpp
#include<iostream>
using namespace std;
#include"student.h"
Student::Student(int sID, string na, string se, int LNum, string LNa, int sco)
{
this->studentID = sID;
this->name = na;
this->sex = se;
this->LessonNumber = LNum;
this->LessonName = LNa;
this->score = sco;
}
void Student::showsth()
{
cout << "学号" << this->studentID
<< "\t名字" << this->name
<< "\t性别" << this->sex
<< "\t课程号" << this->LessonNumber
<< "\t课程名" << this->LessonName
<< "\t分数" << this->score;
}
源文件studentscore.cpp
#include"studentscore.h"
StudentScore::StudentScore()
{
//文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
//测试代码cout << "文件不存在" << endl;
this->m_StuNum = 0;
this->m_StuArray = NULL;
this->m_FileIsEmpty = true;
ifs.close();
return;
}
//文件存在,但为空。
char ch;
ifs >> ch;
if (ifs.eof())
{
//测试代码cout << "文件为空" << endl;
this->m_StuNum = 0;
this->m_StuArray = NULL;
this->m_FileIsEmpty = true;
ifs.close();
return;
}
//初始化
//文件存在,有内容
int num = this->get_StuNum();
//测试代码cout << "现在有" << num << "人" << endl;
this->m_StuNum = num;
this->m_StuArray = new Student * [this->m_StuNum];
this->init_Stu();
//测试代码
/*for (int i = 0; i < this->m_StuNum; i++)
{
cout << "学号:" << this->m_StuArray[i]->studentID
<< " 名字:" << this->m_StuArray[i]->name
<< " 性别:" << this->m_StuArray[i]->sex
<< " 课程号:" << this->m_StuArray[i]->LessonNumber
<< " 课程名:" << this->m_StuArray[i]->LessonName
<< " 分数:" << this->m_StuArray[i]->score << endl;
}*/
}
//文件添加
void StudentScore::Add_Stu()
{
cout << "请输入添加的学生数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
//添加
//计算添加新空间的大小
int newSize = this->m_StuNum + addNum;
//开辟新空间
Student** newSpace = new Student * [newSize];
if (this->m_StuArray != NULL)
{
for (int i = 0; i < this->m_StuNum; i++)
{
newSpace[i] = this->m_StuArray[i];
}
}
for (int i = 0; i < addNum; i++)
{
int studentID;
string name, sex;
int LessonNumber;
string LessonName;
int score;
cout << "请输入第" << i+1 << "个的学号" << endl;
cin >> studentID;
cout << "请输入第" << i + 1 << "个的名字" << endl;
cin >> name;
cout << "请输入第" << i + 1 << "个的性别" << endl;
cin >> sex;
cout << "请输入第" << i + 1 << "个的课程号" << endl;
cin >> LessonNumber;
cout << "请输入第" << i + 1 << "个的课程名" << endl;
cin >> LessonName;
cout << "请输入第" << i + 1 << "个的分数" << endl;
cin >> score;
Student* student = NULL;
student =new Student(studentID, name, sex, LessonNumber, LessonName, score);
newSpace[this->m_StuNum + i] = student;
}
delete [] this->m_StuArray;
this->m_StuArray = newSpace;
this->m_StuNum = newSize;
this->m_FileIsEmpty = false;
cout << "成功添加" << addNum << "名"<<endl;
this->save();
}
else {
cout << "输入有误" << endl;
}
system("pause");
system("cls");
}
//文件保存
void StudentScore::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0; i < this->m_StuNum; i++)
{
ofs << this->m_StuArray[i]->studentID << " "
<< this->m_StuArray[i]->name << " "
<< this->m_StuArray[i]->sex << " "
<< this->m_StuArray[i]->LessonNumber << " "
<< this->m_StuArray[i]->LessonName << " "
<< this->m_StuArray[i]->score << endl;
}
ofs.close();
}
int StudentScore::get_StuNum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int studentID;
string name, sex;
int LessonNumber;
string LessonName;
int score;
int num = 0;
while (ifs >> studentID && ifs >> name && ifs >> sex && ifs >> LessonNumber && ifs >> LessonName && ifs >> score)
{
num++;//统计人数
}
return num;
}
//初始化员工
void StudentScore::init_Stu()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int studentID;
string name, sex;
int LessonNumber;
string LessonName;
int score;
int index=0;
while (ifs >> studentID && ifs >> name && ifs >> sex && ifs >> LessonNumber && ifs >> LessonName && ifs >> score)
{
Student* student = NULL;
student = new Student(studentID, name, sex, LessonNumber, LessonName, score);
this->m_StuArray[index] = student;
index++;
}
ifs.close();
}
//删除学生
void StudentScore::Del_Stu()
{
if (this->m_FileIsEmpty)
{
cout << "不存在" << endl;
}
else
{
cout<<"请输入学号" << endl;
int studentID = 0;
cin >> studentID;
int index = this->IsExist(studentID);
if (index != -1)
{
for (int i = index; i < this->m_StuNum - 1; i++)
{
this->m_StuArray[i] = this->m_StuArray[i + 1];
}
this->m_StuNum--;
this->save();
cout << "删除成功" << endl;
}
else
{
cout<<"删除失败,无该学号" << endl;
}
}
system("pause");
system("cls");
}
//判断有无
int StudentScore::IsExist(int studentId)
{
int index = -1;
for (int i = 0; i < this->m_StuNum; i++)
{
if (this->m_StuArray[i]->studentID == studentId)
{
index = i;
break;
}
}
return index;
}
//修改学生
void StudentScore::Mod_Stu()
{
if (this->m_FileIsEmpty)
{
cout << "不存在" << endl;
}
else
{
cout << "请输入修改学号" << endl;
int studentID = 0;
cin >> studentID;
int ret = this->IsExist(studentID);
if (ret != -1)
{
delete this->m_StuArray[ret];
int newstudentID;
string newname, newsex;
int newLessonNumber;
string newLessonName;
int newscore;
cout << "请输入该的学号" << endl;
cin >> newstudentID;
cout << "请输入该的名字" << endl;
cin >> newname;
cout << "请输入该的性别" << endl;
cin >> newsex;
cout << "请输入该的课程号" << endl;
cin >> newLessonNumber;
cout << "请输入该的课程名" << endl;
cin >> newLessonName;
cout << "请输入该的分数" << endl;
cin >> newscore;
Student* student = NULL;
student = new Student(newstudentID, newname, newsex, newLessonNumber, newLessonName, newscore);
this->m_StuArray[ret] = student;
cout << "修改成功" << endl;
this->save();
}
else
{
cout << "查无此人" << endl;
}
}
system("pause");
system("cls");
}
//查找学生
void StudentScore::Find_Stu()
{
if (this->m_FileIsEmpty)
{
cout << "不存在" << endl;
}
else
{
cout << "输入查找方式:" << endl;
cout << "1.学号查找" << endl;
cout << "2.名字查找" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
int studentID=0;
cout << "请输入学号" << endl;
cin >> studentID;
int ret = IsExist(studentID);
if (ret != -1)
{
cout << "查找成功,如下:" << endl;
this->m_StuArray[ret]->showsth();
studentID = 0;
ret = 0;
}
else
{
cout << "查找失败" << endl;
}
}
else if (select==2 )
{
string name;
cout << "请输入名字:" << endl;
cin >> name;
bool flag = false;
for (int i = 0; i <m_StuNum;i++)
{
if (this->m_StuArray[i]->name == name)
{
cout << "查找成功" << endl;
flag = true;
this->m_StuArray[i]->showsth();
}
}
if (flag == false)
{
cout << "查找失败" << endl;
}
}
else
{
cout << "输入有误" << endl;
}
}
system("pause");
system("cls");
}
//文件菜单
void StudentScore::Show_Menu()
{
cout << "欢迎使用成绩管理系统:" << endl
<< "0.退出" << endl
<< "1.添加学生信息" << endl
<< "2.修改学生信息" << endl
<< "3.删除学生信息" << endl
<< "4.查找学生信息" << endl;
}
void StudentScore::ExitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
StudentScore::~StudentScore()
{
if (this->m_StuArray != NULL)
{
delete[]this->m_StuArray;
this->m_StuArray = NULL;
}
}
主源文件 :成绩管理系统.cpp
#include<iostream>
using namespace std;
#include"studentscore.h"
#include"student.h"
int main()
{
/*Student* student = NULL;
student = new Student(123, "zhangsan", "nan", 1234, "yuwen", 100);
student->showsth();*/
StudentScore ss;
int choice=0;
while (true)
{
ss.Show_Menu();
cout << "请输入你的选项" << endl;
cin >> choice;
switch (choice)
{
case 0:
ss.ExitSystem();
break;
case 1:
ss.Add_Stu();
break;
case 2:
ss.Mod_Stu();
break;
case 3:
{
/*int ret = ss.IsExist(1);
if (ret != -1)
{
cout << "存在该学号" << endl;
}
else
{
cout << "不存在该学号" << endl;
}*/
ss.Del_Stu();
break;
}
case 4:
ss.Find_Stu();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}