OOP HomeWork

This commit is contained in:
e2hang
2025-08-11 00:01:30 +08:00
commit e8a5ca2363
119 changed files with 3187 additions and 0 deletions

70
oop_hw6/hw4/box.cpp Normal file
View File

@@ -0,0 +1,70 @@
#include "box.h"
#include <iostream>
#include <cstring>
#include <typeinfo>
std::string demangle(const std::string name) {
std::string s = name;
std::string prefix = "class ";
if (s.find(prefix) == 0) {
s = s.substr(prefix.length());
}
return s;
}
Box& Box::inBox(Fruit& x)
{
fruit[num] = &x /*static_cast<int*> ()*/;
num++;
return *this;
// TODO: <20>ڴ˴<DAB4><CBB4><EFBFBD><EFBFBD><EFBFBD> return <20><><EFBFBD><EFBFBD>
}
int Box::numApple()
{
int numA = 0;
for (int i = 0;i < num;i++) {
//std::string x = typeid(fruit[i]).name();
//std::cout << demangle(x) << std::endl;
Fruit* tmp = (Fruit*)fruit[i];
if (tmp->checkLoss() == 4) numA++;
}
return numA;
}
int Box::numOrange()
{
int numA = 0;
for (int i = 0;i < num;i++) {
Fruit* tmp = (Fruit*)fruit[i];
if (tmp->checkLoss() == 3) numA++;
}
return numA;
}
int Box::WeightLoss()
{
int WS = 0;
for (int i = 0;i < num;i++) {
if ( ((Fruit*)fruit[i])->isDryed() == false) {
WS += ((Fruit*)fruit[i])->checkLoss();
}
}
return WS;
}
int Box::totalWeight()
{
int TotalW = 0;
for (int i = 0;i < num;i++) {
TotalW += ((Fruit*)fruit[i])->checkWeight();
}
return TotalW;
}
void Box::UPDATE_ALL()
{
for (int i = 0;i < num;i++) {
((Fruit*)fruit[i])->upDate();
}
}

21
oop_hw6/hw4/box.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include <iostream>
#include "fruit.h"
class Box {
private:
void* fruit[8]{};
int num = 0;
int TotalDate = 0;
int lossTotal = 0;
public:
int returnDate();
Box& inBox(Fruit& x);
int numApple();
int numOrange();
int WeightLoss();
int totalWeight();
void UPDATE_ALL();
friend Fruit;
};

60
oop_hw6/hw4/fruit.cpp Normal file
View File

@@ -0,0 +1,60 @@
#include "fruit.h"
Fruit::Fruit()
{
weight = 0;
loss = 0;
}
Fruit::Fruit(int w, int l)
{
weight = w;
loss = l;
}
int& Fruit::checkWeight()
{
return weight;
}
int Fruit::checkLoss()
{
return loss;
}
void Fruit::upDate()
{
this->DatePassed++;
}
void Fruit::Dryed()
{
this->Dry = true;
}
bool Fruit::isDryed()
{
return Dry;
}
void Apple::losewater()
{
if (this->checkWeight() > 30) {
this->checkWeight() -= 4;
}
else
{
this->Dryed();
}
}
void Orange::losewater()
{
if (this->checkWeight() > 18) {
this->checkWeight() -= 3;
}
else
{
this->Dryed();
}
}

39
oop_hw6/hw4/fruit.h Normal file
View File

@@ -0,0 +1,39 @@
#pragma once
#include <iostream>
class Fruit {
private:
int weight;
int loss;
int DatePassed = 0;
bool Dry = false;
public:
Fruit();
Fruit(int w, int l);
int& checkWeight();
int checkLoss();
void upDate();
void Dryed();
bool isDryed();
virtual void losewater() = 0;
};
class Apple : public Fruit {
private:
public:
Apple() : Fruit(50, 4) {};
//Apple(int, int) = delete;
Apple(const Apple& x) = delete;
void losewater();
};
class Orange : public Fruit {
private:
public:
Orange() : Fruit(30, 3) {};
//Orange(int, int) = delete;
Orange(const Apple& x) = delete;
void losewater();
};

56
oop_hw6/hw4/main.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include <iostream>
#include "box.h"
#include "fruit.h"
using namespace std;
int main() {
Apple A[8];
Orange O[8];
Box B;
cout << "Day 1" << endl;
B.inBox(A[0]);
B.inBox(O[0]);
cout << "Apple : " << B.numApple() << endl;
cout << "Orange : " << B.numOrange() << endl;
cout << "WeightLoss : " << B.WeightLoss() << endl;
B.UPDATE_ALL();
cout << "Day 2" << endl;
B.inBox(A[1]);
B.inBox(O[1]);
cout << "Apple : " << B.numApple() << endl;
cout << "Orange : " << B.numOrange() << endl;
cout << "WeightLoss : " << B.WeightLoss() << endl;
cout << "Total Weight : " << B.totalWeight() << endl;
B.UPDATE_ALL();
cout << "Day 3" << endl;
B.inBox(A[2]);
cout << "Apple : " << B.numApple() << endl;
cout << "Orange : " << B.numOrange() << endl;
cout << "WeightLoss : " << B.WeightLoss() << endl;
cout << "Total Weight : " << B.totalWeight() << endl;
B.UPDATE_ALL();
cout << "Day 4" << endl;
B.inBox(O[2]);
cout << "Apple : " << B.numApple() << endl;
cout << "Orange : " << B.numOrange() << endl;
cout << "WeightLoss : " << B.WeightLoss() << endl;
cout << "Total Weight : " << B.totalWeight() << endl;
B.UPDATE_ALL();
cout << "Day 5" << endl;
B.inBox(A[3]);
B.inBox(O[3]);
cout << "Apple : " << B.numApple() << endl;
cout << "Orange : " << B.numOrange() << endl;
cout << "WeightLoss : " << B.WeightLoss() << endl;
cout << "Total Weight : " << B.totalWeight() << endl;
B.UPDATE_ALL();
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 356 KiB