第一课作业。
This commit is contained in:
36
OOP/01/Exercise01.cpp
Normal file
36
OOP/01/Exercise01.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
|
||||
enum CPU_RANK { P1 = 1, P2, P3, P4, P5, P6, P7 };
|
||||
|
||||
class CPU {
|
||||
private:
|
||||
CPU_RANK rank;
|
||||
int frequency;
|
||||
float voltage;
|
||||
|
||||
public:
|
||||
void enter() {
|
||||
int rankInt;
|
||||
std::cout << "Rank(1-7): ";
|
||||
std::cin >> rankInt;
|
||||
rank = (enum CPU_RANK)rankInt;
|
||||
|
||||
std::cout << "Frequency: ";
|
||||
std::cin >> frequency;
|
||||
|
||||
std::cout << "Voltage: ";
|
||||
std::cin >> voltage;
|
||||
};
|
||||
|
||||
void display() {
|
||||
std::cout << "Rank: " << rank << "; Frequency: " << frequency
|
||||
<< "MHz; Voltage: " << voltage << "V." << std::endl;
|
||||
};
|
||||
};
|
||||
|
||||
int main() {
|
||||
CPU a_cpu;
|
||||
a_cpu.enter();
|
||||
a_cpu.display();
|
||||
return 0;
|
||||
}
|
||||
239
OOP/01/Exercise02.cpp
Normal file
239
OOP/01/Exercise02.cpp
Normal file
@@ -0,0 +1,239 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#define BLACK false
|
||||
#define WHITE true
|
||||
|
||||
// 还没有实现判定输赢
|
||||
// todo: 棋盘满时判定和棋
|
||||
|
||||
// clang-format off
|
||||
std::string boardTitle = " 1 2 3 4 5 6 7 8 9 A B C D E F";
|
||||
char emptyBoardString[3][29][4] = {
|
||||
{
|
||||
"┏", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┓"
|
||||
},
|
||||
{
|
||||
"┠", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┨"
|
||||
},
|
||||
{
|
||||
"┗", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┛",
|
||||
}
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
enum putChessPieceResult { failed, success, win };
|
||||
|
||||
class ChessPiece {
|
||||
private:
|
||||
bool color;
|
||||
bool isEmptyStatus;
|
||||
|
||||
public:
|
||||
void init() {
|
||||
this->isEmptyStatus = true;
|
||||
}
|
||||
|
||||
void setColor(bool newColor) {
|
||||
this->isEmptyStatus = false;
|
||||
this->color = newColor;
|
||||
}
|
||||
|
||||
bool isEmpty() {
|
||||
return this->isEmptyStatus;
|
||||
}
|
||||
|
||||
bool getColor() {
|
||||
return this->color;
|
||||
}
|
||||
};
|
||||
|
||||
class ChessBoard {
|
||||
private:
|
||||
ChessPiece ChessPieces[15][15];
|
||||
|
||||
char toHex(int num) {
|
||||
if (num < 10) {
|
||||
return num + '0';
|
||||
}
|
||||
else {
|
||||
return num - 10 + 'A';
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void init() {
|
||||
for (int i = 0; i < 15; i++) {
|
||||
for (int j = 0; j < 15; j++) {
|
||||
this->ChessPieces[i][j].init();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void show() {
|
||||
std::cout << boardTitle << std::endl;
|
||||
for (int i = 0; i < 15; i++) {
|
||||
std::cout << this->toHex(i + 1) << ' ';
|
||||
char(*emptyLineRef)[29][4] = NULL;
|
||||
if (i == 0) {
|
||||
emptyLineRef = &emptyBoardString[0];
|
||||
}
|
||||
else if (i == 14) {
|
||||
emptyLineRef = &emptyBoardString[2];
|
||||
}
|
||||
else {
|
||||
emptyLineRef = &emptyBoardString[1];
|
||||
}
|
||||
int j = 0;
|
||||
for (; j < 15; j++) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
std::cout << (*emptyLineRef)[j * 2];
|
||||
}
|
||||
else {
|
||||
if (this->ChessPieces[i][j].getColor() == BLACK) {
|
||||
std::cout << "●";
|
||||
}
|
||||
else {
|
||||
std::cout << "○";
|
||||
}
|
||||
}
|
||||
if (j < 14) {
|
||||
std::cout << (*emptyLineRef)[j * 2 + 1];
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if success, returns false if not
|
||||
bool setChess(bool color, int row, int column) {
|
||||
if (row > 14 || row < 0 || column > 14 || column < 0) {
|
||||
std::cout << "Out of border!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (!this->ChessPieces[row][column].isEmpty()) {
|
||||
std::cout << "There already is a chess piece!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
this->ChessPieces[row][column].setColor(color);
|
||||
return true;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
this->init();
|
||||
}
|
||||
|
||||
bool checkWinner(int row, int column) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class Player {
|
||||
private:
|
||||
ChessBoard *board;
|
||||
std::string playerName;
|
||||
bool chessType;
|
||||
|
||||
int toInt(char input) {
|
||||
if ('1' <= input && input <= '9') {
|
||||
return input - '1';
|
||||
}
|
||||
else if ('A' <= input && input <= 'F') {
|
||||
return input - 'A' + 10;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void init(std::string name, bool type, ChessBoard *chessBoard) {
|
||||
this->playerName = name;
|
||||
this->chessType = type;
|
||||
this->board = chessBoard;
|
||||
}
|
||||
|
||||
std::string getName() {
|
||||
return this->playerName;
|
||||
}
|
||||
|
||||
bool getColor() {
|
||||
return this->chessType;
|
||||
}
|
||||
|
||||
putChessPieceResult setChess(char row, char column) {
|
||||
int rowCount, columnCount;
|
||||
rowCount = toInt(row);
|
||||
columnCount = toInt(column);
|
||||
if (!this->board->setChess(chessType, rowCount, columnCount)) {
|
||||
return failed;
|
||||
}
|
||||
if (this->board->checkWinner(rowCount, columnCount)) {
|
||||
std::cout << this->playerName << " wins!" << std::endl;
|
||||
return win;
|
||||
}
|
||||
else {
|
||||
return success;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
ChessBoard aBoard;
|
||||
aBoard.init();
|
||||
aBoard.show();
|
||||
Player players[2];
|
||||
{
|
||||
std::string temp = "";
|
||||
std::cout << "Player 1 name: ";
|
||||
std::cin >> temp;
|
||||
players[0].init(temp, BLACK, &aBoard);
|
||||
std::cout << "Player 2 name: ";
|
||||
std::cin >> temp;
|
||||
players[1].init(temp, WHITE, &aBoard);
|
||||
}
|
||||
|
||||
putChessPieceResult result;
|
||||
bool nowMovePlayerIndex = false;
|
||||
|
||||
do {
|
||||
std::cout << players[nowMovePlayerIndex].getName() << " ["
|
||||
<< (players[nowMovePlayerIndex].getColor() ? "White"
|
||||
: "Black")
|
||||
<< "]: "
|
||||
<< "Please enter the coordinate of your new chess piece."
|
||||
<< std::endl;
|
||||
char row, column;
|
||||
std::cout << "Row No. ";
|
||||
std::cin >> row;
|
||||
fflush(stdin);
|
||||
while (!(('1' <= row && row <= '9') || ('A' <= row) && (row <= 'F'))) {
|
||||
std::cout << "Not a valid row number. Try again." << std::endl;
|
||||
std::cout << "Row No. ";
|
||||
std::cin >> row;
|
||||
fflush(stdin);
|
||||
}
|
||||
|
||||
std::cout << "Column No. ";
|
||||
std::cin >> column;
|
||||
fflush(stdin);
|
||||
while (!(('1' <= row && row <= '9') || ('A' <= row) && (row <= 'F'))) {
|
||||
std::cout << "Not a valid column number. Try again." << std::endl;
|
||||
std::cout << "Column No. ";
|
||||
std::cin >> column;
|
||||
fflush(stdin);
|
||||
}
|
||||
result = players[nowMovePlayerIndex].setChess(row, column);
|
||||
if (result == failed) {
|
||||
continue;
|
||||
}
|
||||
else if (result == success) {
|
||||
nowMovePlayerIndex ^= 1;
|
||||
aBoard.show();
|
||||
continue;
|
||||
}
|
||||
else if (result == win) {
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
return 0;
|
||||
}
|
||||
13
OOP/01/Optional01/employee.h
Normal file
13
OOP/01/Optional01/employee.h
Normal file
@@ -0,0 +1,13 @@
|
||||
class Employee {
|
||||
private:
|
||||
int individualEmpNo;
|
||||
int grade;
|
||||
int accumPay;
|
||||
|
||||
public:
|
||||
void getInfo();
|
||||
void init(int, int, int);
|
||||
void setEmpNo(int);
|
||||
void setGrade(int);
|
||||
void setAccumPay(int);
|
||||
};
|
||||
35
OOP/01/Optional01/main.cpp
Normal file
35
OOP/01/Optional01/main.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
#include "employee.h"
|
||||
|
||||
void Employee::getInfo() {
|
||||
std::cout << "Employee No. " << this->individualEmpNo << ": grade " << this->grade << ", accumPay: " << this->accumPay << std::endl;
|
||||
}
|
||||
|
||||
void Employee::init(int number, int grade, int accumPay) {
|
||||
this->individualEmpNo = number;
|
||||
this->grade = grade;
|
||||
this->accumPay = accumPay;
|
||||
}
|
||||
|
||||
void Employee::setEmpNo(int newNo) {
|
||||
this->individualEmpNo = newNo;
|
||||
}
|
||||
|
||||
void Employee::setGrade(int newGrade) {
|
||||
this->grade = newGrade;
|
||||
}
|
||||
|
||||
void Employee::setAccumPay(int newAccumPay) {
|
||||
this->accumPay = newAccumPay;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Employee staff[4];
|
||||
staff[0].init(1, 1, 30);
|
||||
staff[1].init(2, 2, 60);
|
||||
staff[2].init(3, 4, 120);
|
||||
staff[3].init(5, 5, 150);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
staff[i].getInfo();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user