第十周。

This commit is contained in:
unlockable
2023-04-26 19:00:24 +08:00
parent 99aa947a75
commit 5244ddb239
7 changed files with 496 additions and 14 deletions

261
OOP/10/Exercise01/main.cpp Normal file
View File

@@ -0,0 +1,261 @@
#include "teacher.h"
#include "tools.h"
#include <fstream>
#include <iomanip>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::flush;
enum workmode {
createFile = 1,
appendData,
changeData,
searchData,
displayAll,
quit
};
workmode displayMainMenu() {
int userSelection;
cout << setMiddle(62, "+", "") << endl;
cout << "+"
<< setMiddle(60, " ", "Contoso Company Employee Management System")
<< "+" << endl;
cout << setMiddle(62, "+", "") << endl;
cout << "\n";
cout << setMiddle(60, " ", "Main menu") << "\n";
cout << setMiddle(60, " ", "1. Import data") << "\n";
cout << setMiddle(60, " ", "2. Append data") << "\n";
cout << setMiddle(60, " ", "3. Change data") << "\n";
cout << setMiddle(60, " ", "4. Search data") << "\n";
cout << setMiddle(60, " ", "5. Display all") << "\n";
cout << setMiddle(60, " ", "6. Quit ") << endl;
do {
cout << "\nPlease select: ";
cin >> userSelection;
if (cin.fail()) {
cin.clear();
cin.ignore();
userSelection = 0;
}
if (1 <= userSelection && userSelection <= (int)quit) {
return (workmode)userSelection;
}
cout << "Unknown selection. Try again." << endl;
} while (true);
}
void doCreateFile() {
std::string fileName;
cout << "New file name (file with same name will be replaced): " << flush;
cin >> fileName;
std::fstream outfile(fileName, std::ios::out | std::ios::binary);
if (!outfile.is_open()) {
cout << "\033[1;31mOpen file failed. Quit to main menu.\033[0m" << endl;
return;
}
cout << "Start reading in new Teacher data. How many do you want to create?"
<< endl;
int count = 1, total = 0;
total = askValidInt();
while (count <= total) {
cout << "\033[1;32mTeacher " << count << "/" << total << ":\033[0m"
<< endl;
Teacher aTeacher(true);
outfile.write((char *)&aTeacher, sizeof(aTeacher));
cout << "Created " << count++ << " new teacher. (Total " << total << ")"
<< endl;
}
outfile.write((char *)&total, sizeof(total));
outfile.close();
}
void doAppendData() {
std::string fileName;
cout << "File name: " << flush;
cin >> fileName;
std::fstream fileObj(fileName,
std::ios::in | std::ios::out | std::ios::binary);
if (!fileObj.is_open()) {
cout << "\033[1;31mOpen file failed. Quit to main menu.\033[0m" << endl;
return;
}
int total;
fileObj.seekp(-sizeof(int), std::ios::end);
fileObj.read((char *)&total, sizeof(int));
cout << "\033[1;34mThere are already " << total
<< " teacher(s) recorded in this file.\033[0m" << endl;
fileObj.seekp(sizeof(Teacher) * total);
cout << "Start reading in new Teacher data. How many do you want to create?"
<< endl;
int newTeacherTotal, newTeacherCount = 1;
newTeacherTotal = askValidInt();
total += newTeacherTotal;
while (newTeacherCount <= newTeacherTotal) {
cout << "\033[1;32mNew teacher " << newTeacherCount << "/"
<< newTeacherTotal << ":\033[0m" << endl;
Teacher aTeacher(true);
fileObj.write((char *)&aTeacher, sizeof(aTeacher));
cout << "Created " << newTeacherCount++ << " new teacher. (Total "
<< newTeacherTotal << " new teachers)" << endl;
}
fileObj.write((char *)&total, sizeof(int));
fileObj.close();
}
void doChangeData() {
std::string fileName;
cout << "File name:" << flush;
cin >> fileName;
std::fstream fileObj(fileName,
std::ios::in | std::ios::out | std::ios::binary);
if (!fileObj.is_open()) {
cout << "\033[1;31mOpen file failed. Quit to main menu.\033[0m" << endl;
return;
}
int total;
fileObj.seekg(-sizeof(int), std::ios::end);
fileObj.read((char *)&total, sizeof(int));
cout << "\033[1;34mThere are " << total
<< " teacher(s) recorded in this file. Which one do you want to "
"change?\033[0m"
<< endl;
int userSelect = 0;
while (true) {
userSelect = askValidInt();
if (userSelect < 1 || userSelect > total) {
cout << "Out of bounds. Try again: " << flush;
continue;
}
break;
}
Teacher aTeacher;
fileObj.seekg(sizeof(Teacher) * (userSelect - 1));
fileObj.read((char *)&aTeacher, sizeof(Teacher));
cin >> aTeacher;
fileObj.seekg(sizeof(Teacher) * (userSelect - 1));
fileObj.write((char *)&aTeacher, sizeof(Teacher));
fileObj.close();
}
void doSearchData() {
std::string fileName;
cout << "File name: " << flush;
cin >> fileName;
std::fstream fileObj(fileName, std::ios::in | std::ios::binary);
if (!fileObj.is_open()) {
cout << "\033[1;31mOpen file failed. Quit to main menu.\033[0m" << endl;
return;
}
int total;
fileObj.seekg(-sizeof(int), std::ios::end);
fileObj.read((char *)&total, sizeof(int));
Teacher *allTeachers = new Teacher[total];
fileObj.seekg(0);
fileObj.read((char *)allTeachers, total * sizeof(Teacher));
fileObj.close();
int targetTeacherNum, matchedTeacherTotal = 0;
int *matchedIndexes = new int[total];
cout << "Input your target teacher number: ";
targetTeacherNum = askValidInt();
for (int i = 0; i < total; i++) {
if (allTeachers[i].getNumber() == targetTeacherNum) {
matchedIndexes[matchedTeacherTotal++] = i;
}
}
if (matchedTeacherTotal > 0) {
cout << "\033[1;34mThere are a total of " << matchedTeacherTotal
<< " teacher(s) matches your criteria.\033[0m" << endl;
for (int i = 0; i < matchedTeacherTotal; i++) {
cout << "\033[1;32mMatched teacher " << i + 1 << "/"
<< matchedTeacherTotal
<< " (Position in file: " << matchedIndexes[i] + 1
<< "):\033[0m\n"
<< allTeachers[matchedIndexes[i]];
}
cout << flush;
}
else {
cout << "\033[1;34mNo teacher matched your criteria.\033[0m" << endl;
}
}
void doDisplayAll() {
std::string fileName;
cout << "File name: " << flush;
cin >> fileName;
std::fstream fileObj(fileName, std::ios::in | std::ios::binary);
if (!fileObj.is_open()) {
cout << "\033[1;31mOpen file failed. Quit to main menu.\033[0m" << endl;
return;
}
int total;
fileObj.seekg(-sizeof(int), std::ios::end);
fileObj.read((char *)&total, sizeof(int));
Teacher *allTeachers = new Teacher[total];
fileObj.seekg(0);
fileObj.read((char *)allTeachers, total * sizeof(Teacher));
fileObj.close();
cout << "\033[1;34mThere are a total of " << total << " teacher(s).\033[0m"
<< endl;
for (int i = 0; i < total; i++) {
cout << "\033[1;32mTeacher " << i + 1 << "/" << total << ":\033[0m\n"
<< allTeachers[i];
}
cout << endl;
delete[] allTeachers;
};
int main() {
workmode mode;
bool running = true;
while (running) {
mode = displayMainMenu();
switch (mode) {
case createFile:
doCreateFile();
break;
case appendData:
doAppendData();
break;
case changeData:
doChangeData();
break;
case searchData:
doSearchData();
break;
case displayAll:
doDisplayAll();
break;
case quit:
running = false;
break;
}
}
return 0;
}

View File

@@ -0,0 +1,107 @@
#include "teacher.h"
#include "tools.h"
using std::cin;
using std::cout;
using std::endl;
using std::flush;
Teacher::Teacher(bool interactive, int _number, std::string _name,
std::string _sex)
: number(_number) {
if (interactive) {
std::string temp;
cout << "Number: " << flush;
this->number = askValidInt();
cout << "Name (max 50): " << flush;
cin >> temp;
this->setName(temp);
cout << "Sex (max 10): " << flush;
cin >> temp;
this->setSex(temp);
return;
}
this->setName(_name.c_str());
this->setSex(_name.c_str());
}
int Teacher::setNumber(int _num) {
this->number = _num;
return this->number;
}
std::string Teacher::setName(char _name[]) {
if (strlen(_name) > MAX_TEACHER_NAME_SIZE) {
cout << "\033[1;31mName is longer than " << MAX_TEACHER_NAME_SIZE
<< " characters. String will be cut.\033[0m" << endl;
}
strlcpy(Teacher::name, _name, MAX_TEACHER_NAME_SIZE + 1);
std::string result(_name);
return result;
}
std::string Teacher::setSex(char _sex[]) {
if (strlen(_sex) > MAX_TEACHER_SEX_SIZE) {
cout << "\033[1;31mSex is longer than " << MAX_TEACHER_SEX_SIZE
<< " characters. String will be cut.\033[0m" << endl;
}
strlcpy(Teacher::sex, _sex, MAX_TEACHER_NAME_SIZE + 1);
std::string result(_sex);
return result;
}
std::string Teacher::setName(std::string _name) {
if (_name.length() > MAX_TEACHER_NAME_SIZE) {
cout << "\033[1;31mName is longer than " << MAX_TEACHER_NAME_SIZE
<< " characters. String will be cut.\033[0m" << endl;
}
strlcpy(Teacher::name, _name.c_str(), MAX_TEACHER_NAME_SIZE);
return _name;
}
std::string Teacher::setSex(std::string _sex) {
if (_sex.length() > MAX_TEACHER_SEX_SIZE) {
cout << "\033[1;31mSex is longer than " << MAX_TEACHER_SEX_SIZE
<< " characters. String will be cut.\033[0m" << endl;
}
strlcpy(Teacher::sex, _sex.c_str(), MAX_TEACHER_NAME_SIZE);
return _sex;
}
int Teacher::getNumber() {
return this->number;
}
std::string Teacher::getName() {
std::string result(this->name);
return result;
}
std::string Teacher::getSex() {
std::string result(this->sex);
return result;
}
std::istream &operator>>(std::istream &input, Teacher &teacher) {
cout << "Number (Original: " << teacher.getNumber() << "): " << flush;
teacher.number = askValidInt();
cout << "Name (Original: " << teacher.getName() << "), max 50: " << flush;
std::string temp;
cin >> temp;
teacher.setName(temp);
cout << "Sex (Original: " << teacher.getSex() << "), max 10: " << flush;
cin >> temp;
teacher.setSex(temp);
return input;
}
std::ostream &operator<<(std::ostream &output, Teacher &teacher) {
cout << "Number: " << teacher.getNumber() << "\n";
cout << "Name: " << teacher.getName() << "\n";
cout << "Sex: " << teacher.getSex() << "\n";
return output;
}

View File

@@ -0,0 +1,42 @@
#include <iostream>
#define MAX_TEACHER_NAME_SIZE 50
#define MAX_TEACHER_SEX_SIZE 10
class Teacher {
private:
friend std::istream &operator>>(std::istream &input, Teacher &teacher);
friend std::ostream &operator<<(std::ostream &output, Teacher &teacher);
protected:
int number;
char name[MAX_TEACHER_NAME_SIZE + 1];
char sex[MAX_TEACHER_SEX_SIZE + 1];
public:
Teacher(bool interactive = false, int _number = 0, std::string _name = "Unknown",
std::string _sex = "Unknown");
// 返回更新后的num
int setNumber(int _num);
// 返回更新后的name
std::string setName(std::string _name);
// 返回更新后的name
std::string setName(char _name[]);
// 返回更新后的sex
std::string setSex(std::string _sex);
// 返回更新后的sex
std::string setSex(char _sex[]);
// 获取num
int getNumber();
// 获取name
std::string getName();
// 获取sex
std::string getSex();
};

View File

@@ -0,0 +1,38 @@
#include "tools.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int askValidInt() {
int result = 0;
while (true) {
cin >> result;
if (cin.fail()) {
cin.clear();
cin.ignore();
cout << "Please input a valid number." << endl;
continue;
}
break;
}
return result;
}
std::string setMiddle(int width, std::string fillChar,
std::string originString) {
int len = originString.length();
if (len >= width) {
return originString;
}
int fill = (width - len) / 2;
std::string result = "";
for (int i = 0; i < fill; i++) {
result += fillChar;
}
result += originString;
for (int i = result.length(); i < width; i++) {
result += fillChar;
}
return result;
}

View File

@@ -0,0 +1,3 @@
#include<iostream>
int askValidInt();
std::string setMiddle(int width, std::string fillChar, std::string originString);

27
OOP/10/Exercise02.cpp Normal file
View File

@@ -0,0 +1,27 @@
#include <strstream>
#include <iostream>
#include <fstream>
using std::cout;
using std::endl;
using std::ends;
struct student {
int num;
char name[20];
float score;
};
int main() {
student stud[3] = {{1001, "Li", 78},{1002, "Wang", 89.5}, {1004, "Fun", 90}};
char c[50];
std::strstream strio(c, sizeof(c), std::ios::in | std::ios::out);
for (int i = 0; i < 3; i++) {
strio << stud[i].num << stud[i].name << stud[i].score;
}
strio << ends;
std::fstream fileObj("content.dat", std::ios::out | std::ios::binary);
fileObj.write(c, sizeof(c));
fileObj.close();
return 0;
}

View File

@@ -1,4 +1,7 @@
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
class A {
protected:
@@ -38,23 +41,24 @@ public:
}
};
class D : public B, public C {
protected:
int d;
// class D : public B, public C {
// protected:
// int d;
public:
D(int _a, int _b, int _c, int _d) :A(_a), B(_a, _b), C(_a, _c), d(_d){};
// public:
// D(int _a, int _b, int _c, int _d) :A(_a), B(_a, _b), C(_a, _c), d(_d){};
void display() {
A::display();
B::display();
C::display();
std::cout << d << std::endl;
}
};
// void display() {
// A::display();
// B::display();
// C::display();
// std::cout << d << std::endl;
// }
// };
int main() {
D objD(1, 2, 3, 4);
objD.display();
// D objD(1, 2, 3, 4);
// objD.display();
cout << "---\n" << sizeof(A) << endl << sizeof(B) << endl << sizeof(C) << endl;
return 0;
}