修复了不能在15格以外放置的bug。
This commit is contained in:
97
OOP/03/Exercise02.cpp
Normal file
97
OOP/03/Exercise02.cpp
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Student {
|
||||||
|
private:
|
||||||
|
std::string name;
|
||||||
|
int studentNumber;
|
||||||
|
int grade;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Student();
|
||||||
|
Student(std::string& newName, int newNumber, int newGrade);
|
||||||
|
Student(Student &otherStu);
|
||||||
|
~Student();
|
||||||
|
|
||||||
|
std::string getName();
|
||||||
|
int getGrade();
|
||||||
|
int getStudentNumber();
|
||||||
|
void displayInfo();
|
||||||
|
};
|
||||||
|
|
||||||
|
Student::Student() {
|
||||||
|
name = "Undefined";
|
||||||
|
studentNumber = 0;
|
||||||
|
grade = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Student::Student(std::string& newName, int newNumber, int newGrade) {
|
||||||
|
name = newName;
|
||||||
|
this->studentNumber = newNumber;
|
||||||
|
this->grade = newGrade;
|
||||||
|
}
|
||||||
|
|
||||||
|
Student::Student(Student &otherStu) {
|
||||||
|
name = otherStu.getName();
|
||||||
|
studentNumber = otherStu.studentNumber;
|
||||||
|
grade = otherStu.grade;
|
||||||
|
}
|
||||||
|
|
||||||
|
Student::~Student() {
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Student::getName() {
|
||||||
|
return this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Student::getGrade() {
|
||||||
|
return this->grade;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Student::getStudentNumber() {
|
||||||
|
return this->studentNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Student::displayInfo() {
|
||||||
|
std::cout << "Name: " << this->name << std::endl
|
||||||
|
<< "Student Number: " << this->studentNumber << std::endl
|
||||||
|
<< "Grade: " << this->grade << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int max(Student* students[]) {
|
||||||
|
int max_studnum = 0;
|
||||||
|
int max = 0;
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
if (students[i]->getGrade() > max) {
|
||||||
|
max = students[i]->getGrade();
|
||||||
|
max_studnum = students[i]->getStudentNumber();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max_studnum;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Student* students[10];
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
std::string name;
|
||||||
|
int studnum;
|
||||||
|
int grade;
|
||||||
|
std::cout << "Name: ";
|
||||||
|
std::cin >> name;
|
||||||
|
std::cout << "Student No.: ";
|
||||||
|
std::cin >> studnum;
|
||||||
|
std::cout << "Grade: ";
|
||||||
|
std::cin >> grade;
|
||||||
|
students[i] = new Student(name, studnum, grade);
|
||||||
|
}
|
||||||
|
int maxStudNum = max(students);
|
||||||
|
Student maxStud;
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
if (students[i]->getStudentNumber() == maxStudNum) {
|
||||||
|
maxStud = *(students[i]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << "The max is: " << std::endl;
|
||||||
|
maxStud.displayInfo();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -53,8 +53,8 @@ int main() {
|
|||||||
std::cout << "Row No. ";
|
std::cout << "Row No. ";
|
||||||
std::cin >> row;
|
std::cin >> row;
|
||||||
if (std::cin.fail() ||
|
if (std::cin.fail() ||
|
||||||
(!(('1' <= row && row <= '9') || ('A' <= row && row <= 'F') ||
|
(!(('1' <= row && row <= '9') || ('A' <= row && row <= 'J') ||
|
||||||
('a' <= row && row <= 'f')))) {
|
('a' <= row && row <= 'j')))) {
|
||||||
std::cout << "Invalid input, try again." << std::endl;
|
std::cout << "Invalid input, try again." << std::endl;
|
||||||
std::cin.clear();
|
std::cin.clear();
|
||||||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
|
||||||
@@ -71,8 +71,8 @@ int main() {
|
|||||||
std::cout << "Column No. ";
|
std::cout << "Column No. ";
|
||||||
std::cin >> column;
|
std::cin >> column;
|
||||||
if (std::cin.fail() ||
|
if (std::cin.fail() ||
|
||||||
(!(('1' <= row && row <= '9') || ('A' <= row && row <= 'F') ||
|
(!(('1' <= row && row <= '9') || ('A' <= row && row <= 'J') ||
|
||||||
('a' <= row && row <= 'f')))) {
|
('a' <= row && row <= 'j')))) {
|
||||||
std::cout << "Invalid input, try again." << std::endl;
|
std::cout << "Invalid input, try again." << std::endl;
|
||||||
std::cin.clear();
|
std::cin.clear();
|
||||||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ int Player::toInt(char input) {
|
|||||||
if ('1' <= input && input <= '9') {
|
if ('1' <= input && input <= '9') {
|
||||||
return input - '1';
|
return input - '1';
|
||||||
}
|
}
|
||||||
else if ('A' <= input && input <= 'F') {
|
else if ('A' <= input && input <= 'J') {
|
||||||
return input - 'A' + 9;
|
return input - 'A' + 9;
|
||||||
}
|
}
|
||||||
else if ('a' <= input && input <= 'f') {
|
else if ('a' <= input && input <= 'j') {
|
||||||
return input - 'a' + 9;
|
return input - 'a' + 9;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
Reference in New Issue
Block a user