OOP HomeWork
This commit is contained in:
BIN
oop_hw2/02_面向对象程序设计上机实验二.docx
Normal file
BIN
oop_hw2/02_面向对象程序设计上机实验二.docx
Normal file
Binary file not shown.
BIN
oop_hw2/02_面向对象程序设计上机实验二_参考答案.docx
Normal file
BIN
oop_hw2/02_面向对象程序设计上机实验二_参考答案.docx
Normal file
Binary file not shown.
61
oop_hw2/oop_hw2_1/Q1.cpp
Normal file
61
oop_hw2/oop_hw2_1/Q1.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
class TRandom {
|
||||
private:
|
||||
unsigned long seed;
|
||||
public:
|
||||
TRandom(unsigned long s) : seed(s) {}
|
||||
unsigned char NextByte() {
|
||||
seed = seed * 1103515245 + 12345;
|
||||
return static_cast<unsigned char>((seed / 65536) % 256);
|
||||
}
|
||||
};
|
||||
|
||||
void Coder(unsigned char data[], int len, unsigned long key) {
|
||||
TRandom rand(key);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
data[i] ^= rand.NextByte();
|
||||
}
|
||||
}
|
||||
|
||||
void Coder(unsigned char data[], int len, TRandom& rand, unsigned long key) {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
data[i] ^= rand.NextByte();
|
||||
}
|
||||
}
|
||||
|
||||
class Crypter {
|
||||
private:
|
||||
TRandom rand;
|
||||
public:
|
||||
Crypter(unsigned long seed) : rand(seed) {}
|
||||
void Process(unsigned char data[], int len) {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
data[i] ^= rand.NextByte();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void Test() {
|
||||
unsigned char msg[] = "Hello World";
|
||||
int len = sizeof(msg);
|
||||
unsigned long key = 12345;
|
||||
std::cout << "Original: " << msg << std::endl;
|
||||
Coder(msg, len, key);
|
||||
std::cout << "Encrypted: "; for (int i = 0; i < len; ++i) std::cout << msg[i]; std::cout << std::endl;
|
||||
Coder(msg, len, key);
|
||||
std::cout << "Decrypted: " << msg << std::endl;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD> rand <20><>Ϊ TRandom rand<6E><64><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>ᶪʧԭ״̬<D7B4><CCAC><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// <20><><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD>Ϊ const TRandom& rand<6E><64><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>Ϊ NextByte <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> const
|
BIN
oop_hw2/oop_hw2_1/Q1.exe
Normal file
BIN
oop_hw2/oop_hw2_1/Q1.exe
Normal file
Binary file not shown.
BIN
oop_hw2/oop_hw2_1/测试截图1.png
Normal file
BIN
oop_hw2/oop_hw2_1/测试截图1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 101 KiB |
27
oop_hw2/oop_hw2_2/Q2.cpp
Normal file
27
oop_hw2/oop_hw2_2/Q2.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Book {
|
||||
private:
|
||||
int totalPages;
|
||||
int pagesRead;
|
||||
public:
|
||||
Book(int total) : totalPages(total), pagesRead(0) {}
|
||||
void Read(int pages) { pagesRead += pages; if (pagesRead > totalPages) pagesRead = totalPages; }
|
||||
int GetTotalPages() const { return totalPages; }
|
||||
int GetPagesRead() const { return pagesRead; }
|
||||
int GetPagesLeft() const { return totalPages - pagesRead; }
|
||||
};
|
||||
|
||||
void Test() {
|
||||
Book b(100);
|
||||
b.Read(30);
|
||||
std::cout << "Read: " << b.GetPagesRead() << ", Left: " << b.GetPagesLeft() << std::endl;
|
||||
b.Read(80);
|
||||
std::cout << "Read: " << b.GetPagesRead() << ", Left: " << b.GetPagesLeft() << std::endl;
|
||||
}
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
BIN
oop_hw2/oop_hw2_2/Q2.exe
Normal file
BIN
oop_hw2/oop_hw2_2/Q2.exe
Normal file
Binary file not shown.
BIN
oop_hw2/oop_hw2_2/测试截图2.png
Normal file
BIN
oop_hw2/oop_hw2_2/测试截图2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 104 KiB |
43
oop_hw2/oop_hw2_3/Q3.cpp
Normal file
43
oop_hw2/oop_hw2_3/Q3.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
enum Suit { Spade, Heart, Diamond, Club };
|
||||
enum Rank { Two = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace };
|
||||
|
||||
class Card {
|
||||
private:
|
||||
int id;
|
||||
Suit suit;
|
||||
Rank rank;
|
||||
int backId;
|
||||
int width, height;
|
||||
std::pair<int, int> topLeft;
|
||||
public:
|
||||
Card(int _id, Suit _suit, Rank _rank, int _backId, int _w, int _h, std::pair<int, int> _tl)
|
||||
: id(_id), suit(_suit), rank(_rank), backId(_backId), width(_w), height(_h), topLeft(_tl) {}
|
||||
|
||||
Card(const Card& other) : id(other.id), suit(other.suit), rank(other.rank), backId(other.backId),
|
||||
width(other.width), height(other.height), topLeft(other.topLeft) {}
|
||||
|
||||
int GetBackId() const { return backId; }
|
||||
void SetBackId(int id) { backId = id; }
|
||||
bool IsSameSuit(const Card& other) const { return suit == other.suit; }
|
||||
bool IsSameRank(const Card& other) const { return rank == other.rank; }
|
||||
bool IsSuit(Suit s) const { return suit == s; }
|
||||
bool IsRank(Rank r) const { return rank == r; }
|
||||
void SetPosition(int x, int y) { topLeft = {x, y}; }
|
||||
std::pair<int, int> GetBottomRight() const { return {topLeft.first + width, topLeft.second + height}; }
|
||||
};
|
||||
|
||||
void Test() {
|
||||
Card c1(0, Spade, Ace, 1, 100, 150, {0, 0});
|
||||
Card c2(1, Spade, King, 2, 100, 150, {100, 0});
|
||||
std::cout << "Same Suit: " << c1.IsSameSuit(c2) << std::endl;
|
||||
std::cout << "Bottom Right: (" << c1.GetBottomRight().first << ", " << c1.GetBottomRight().second << ")\n";
|
||||
}
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
BIN
oop_hw2/oop_hw2_3/Q3.exe
Normal file
BIN
oop_hw2/oop_hw2_3/Q3.exe
Normal file
Binary file not shown.
BIN
oop_hw2/oop_hw2_3/测试截图3.png
Normal file
BIN
oop_hw2/oop_hw2_3/测试截图3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 122 KiB |
51
oop_hw2/oop_hw2_4/Q4.cpp
Normal file
51
oop_hw2/oop_hw2_4/Q4.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
class Hero {
|
||||
private:
|
||||
int charm, fame, attack, defense, magic;
|
||||
int items[5] = {0};
|
||||
public:
|
||||
Hero() : charm(0), fame(0), attack(0), defense(0), magic(0) {}
|
||||
|
||||
void Equip(int pos, int item) {
|
||||
if (pos >= 0 && pos < 5) items[pos] = item;
|
||||
ApplyItem(item);
|
||||
}
|
||||
|
||||
void Remove(int pos) {
|
||||
if (pos >= 0 && pos < 5) items[pos] = 0;
|
||||
}
|
||||
|
||||
void ApplyItem(int item) {
|
||||
switch(item) {
|
||||
case 1: charm += 2; break;
|
||||
case 2: fame += 3; break;
|
||||
case 3: attack += 1; break;
|
||||
case 4: defense += 2; break;
|
||||
case 5: magic += 4; break;
|
||||
case 6: charm += 1; magic += 1; break;
|
||||
}
|
||||
}
|
||||
|
||||
void ShowStats() const {
|
||||
std::cout << "Charm: " << charm << ", Fame: " << fame
|
||||
<< ", Attack: " << attack << ", Defense: " << defense
|
||||
<< ", Magic: " << magic << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
void Test() {
|
||||
Hero h;
|
||||
h.Equip(0, 1);
|
||||
h.Equip(1, 3);
|
||||
h.Equip(2, 5);
|
||||
h.ShowStats();
|
||||
h.Remove(1);
|
||||
h.ShowStats();
|
||||
}
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
BIN
oop_hw2/oop_hw2_4/Q4.exe
Normal file
BIN
oop_hw2/oop_hw2_4/Q4.exe
Normal file
Binary file not shown.
BIN
oop_hw2/oop_hw2_4/测试截图4.png
Normal file
BIN
oop_hw2/oop_hw2_4/测试截图4.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 97 KiB |
27
oop_hw2/oop_hw2_5/Q5.cpp
Normal file
27
oop_hw2/oop_hw2_5/Q5.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
class Demo {
|
||||
private:
|
||||
int mNum;
|
||||
static Demo* instance;
|
||||
Demo() : mNum(0) {}
|
||||
public:
|
||||
static Demo* GetInstance() {
|
||||
if (!instance) instance = new Demo();
|
||||
return instance;
|
||||
}
|
||||
void AddValue(int value) { mNum += value; }
|
||||
void ShowValue() const { std::cout << "Value=" << mNum << std::endl; }
|
||||
};
|
||||
Demo* Demo::instance = NULL;
|
||||
|
||||
void Test() {
|
||||
Demo::GetInstance()->AddValue(10);
|
||||
Demo::GetInstance()->AddValue(5);
|
||||
Demo::GetInstance()->ShowValue();
|
||||
}
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
BIN
oop_hw2/oop_hw2_5/Q5.exe
Normal file
BIN
oop_hw2/oop_hw2_5/Q5.exe
Normal file
Binary file not shown.
BIN
oop_hw2/oop_hw2_5/测试截图5.png
Normal file
BIN
oop_hw2/oop_hw2_5/测试截图5.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 93 KiB |
46
oop_hw2/oop_hw2_6/Q6.cpp
Normal file
46
oop_hw2/oop_hw2_6/Q6.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Monster {
|
||||
private:
|
||||
int speed, hitpoint, damage, defense;
|
||||
public:
|
||||
Monster(int s, int h, int d, int def) : speed(s), hitpoint(h), damage(d), defense(def) {}
|
||||
|
||||
bool operator>(const Monster& other) const {
|
||||
if (speed != other.speed) return speed > other.speed;
|
||||
if (hitpoint != other.hitpoint) return hitpoint > other.hitpoint;
|
||||
if (damage != other.damage) return damage > other.damage;
|
||||
return defense > other.defense;
|
||||
}
|
||||
|
||||
void Attack(Monster& other) const {
|
||||
int harm = std::max(2 * damage - other.defense, 1);
|
||||
other.hitpoint -= harm;
|
||||
std::cout << "Monster attacks for " << harm << " damage. Opponent HP: " << other.hitpoint << std::endl;
|
||||
}
|
||||
|
||||
void Fight(Monster& other) {
|
||||
Monster* first = this;
|
||||
Monster* second = &other;
|
||||
if (!(*this > other)) std::swap(first, second);
|
||||
|
||||
while (first->hitpoint > 0 && second->hitpoint > 0) {
|
||||
first->Attack(*second);
|
||||
if (second->hitpoint <= 0) break;
|
||||
second->Attack(*first);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void Test() {
|
||||
Monster m1(10, 30, 5, 2);
|
||||
Monster m2(8, 35, 4, 3);
|
||||
m1.Fight(m2);
|
||||
}
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
||||
|
BIN
oop_hw2/oop_hw2_6/Q6.exe
Normal file
BIN
oop_hw2/oop_hw2_6/Q6.exe
Normal file
Binary file not shown.
BIN
oop_hw2/oop_hw2_6/测试截图6.png
Normal file
BIN
oop_hw2/oop_hw2_6/测试截图6.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 146 KiB |
BIN
oop_hw2/上机测试2.pdf
Normal file
BIN
oop_hw2/上机测试2.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user