cppHomework/Exam.cpp

135 lines
3.2 KiB
C++
Raw Permalink Normal View History

2023-09-02 07:36:45 +00:00
#pragma warning(disable:4996)
#include"Exam.h"
using namespace std;
Exam::Exam(Examsave* insave) {
this->save = *insave;
studentnum = insave->studentnum;
strcpy(name, insave->name);
strcpy(filename, insave->filename);
ifstream ibin(filename, ios::in, ios::binary);
Item tmp;
for (int i = 0; i < studentnum; i++) {
ibin.read((char*)&tmp, sizeof(Item));
items.push_back(tmp);
}
ibin.close();
if (items.size() > 0) {
sort(items.begin(), items.end());
reverse(items.begin(), items.end());
}
}
Exam::Exam(char inname[]) {
strcpy(name, inname);
studentnum = 0;
ifstream tmp(inname, ios::in, ios::binary);
if (tmp.good()) {
string nname;
nname += inname;
for (auto i = 1; i < 100; i++) {
nname += to_string(i);
ifstream tmp(nname, ios::in, ios::binary);
if (!tmp.good()) {
break;
}
}
strcpy(filename, nname.c_str());
}
else {
strcpy(filename, inname);
}
}
void Exam::addItem(long long int studentNo, double score[9]) {
rmvItem(studentNo);
Item tmp(studentNo, score);
items.push_back(tmp);
sort(items.begin(), items.end());
reverse(items.begin(), items.end());
studentnum++;
}
void Exam::addItem(const Item& item) {
rmvItem(item.getStuNo());
items.push_back(item);
sort(items.begin(), items.end());
reverse(items.begin(), items.end());
studentnum++;
}
void Exam::saveAll(ofstream& examsout) {
strcpy(save.filename, filename);
strcpy(save.name, name);
studentnum = items.size();
save.studentnum = studentnum;
ofstream obin(filename, ios::out, ios::binary);
Item tmp;
for (auto i = items.begin(); i < items.end(); i++) {
tmp = *i;
obin.write((char*)&tmp, sizeof(Item));
}
obin.close();
examsout.write((char*)&this->save, sizeof(Examsave));
}
void Exam::dspHeader() {
cout << setw(20) << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << setw(15) << "ѧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
}
void Exam::dsp() const {
cout << setw(20) << name << setw(15) << studentnum << endl;
}
void Exam::dspName() const {
cout << name;
}
void Exam::dspName(int w) const {
cout << setw(w) << name;
}
void Exam::dspItems() const {
for (auto i = items.begin(); i < items.end(); i++) {
i->dsp();
cout << setw(6) << distance(items.begin(), i) + 1 << endl;
}
}
void Exam::toCsvItems(ofstream& csv) const {
for (auto i = items.begin(); i < items.end(); i++) {
i->toCsv(csv);
csv << distance(items.begin(), i) + 1 << endl;
}
}
bool Exam::itemExists(long long int studentNo) const {
for (auto i = items.begin(); i < items.end(); i++) {
if (i->getStuNo()== studentNo) {
return true;
}
}
return false;
}
bool Exam::rmvItem(long long int studentNo) {
for (auto i = items.begin(); i < items.end(); i++) {
if (i->getStuNo() == studentNo) {
items.erase(i);
studentnum--;
return true;
}
}
return false;
}
void Exam::changeName(char name[]) {
strcpy(this->name, name);
}
const Item* Exam::StudentInquiry(long long int studentNo) const{
for (auto item = items.begin(); item < items.end(); item++) {
if (item->getStuNo() == studentNo) {
return &(*item);
}
}
return nullptr;
}
int Exam::getStuRanking(const Item& item) {
for (auto i = items.begin(); i < items.end(); i++) {
if (i->stuNoEqual(item)) {
return distance(items.begin(), i) + 1;
}
}
return -1;
}
void Exam::rm() const {
remove(filename);
}