Một công ty đa cấp nọ có ba loại nhân viên: Nhân viên văn phòng và Nhân viên sản xuất và…
- User
Một công ty đa cấp nọ có ba loại nhân viên: Nhân viên văn phòng và Nhân viên sản xuất và Sếp. Mỗi người cần quản lý các thông tin sau: Họ tên (+ thông tin phụ để tính lương hàng tháng). Công ty cần tính lương như sau: OOP C++: Đối với nhân viên sản xuất (Loại 1, thông tin phụ là lương căn bản và số sản phẩm): Lương = lương căn bản + số sản phẩm * 5. Đối với nhân viên văn phòng (Loại 2, thông tin phụ là số ngày làm việc): Lương = số ngày làm việc * 100. Đối với sếp (Loại 3, không có thông tin phụ): Lương = 100 + số sản phẩm của tất cả nhân viên sản xuất * 2 + số ngày làm việc của tất cả nhân viên văn phòng * 40. Nhập vào số lượng n, sau đó cho nhập loại nhân viên, tên và thông tin phụ từng người. Ví dụ nhập:
1 NguyenVanA 300000 10 Sau khi nhập xong xuất ra kết quả dưới dịnh dạng:
Tên người 1: lương người 1 Tên người 2: lương người 2 ... Input Format
int String float int (for worker)
int String int (for officer)
int String (for manager)
Constraints
No constraint
Output Format
[String: float]
Sample Input 0
4 1 nvquenA 2000 10 1 nvquenB 3000 20 2 nvquenC 40 3 sep Sample Output 0
nvquenA: 2050 nvquenB: 3100 nvquenC: 4000 sep: 1760
- Assistant
#include <iostream> #include <vector> #include <string> #include <sstream> using namespace std; class Employee { protected: string name; public: virtual void input() = 0; virtual float getSalary() = 0; }; class Worker : public Employee { private: float baseSalary; int numProducts; public: void input() { cin >> name >> baseSalary >> numProducts; } float getSalary() { return baseSalary + numProducts * 5; } }; class Officer : public Employee { private: int numDays; public: void input() { cin >> name >> numDays; } float getSalary() { return numDays * 100; } }; class Manager : public Employee { private: int totalProducts = 0; int totalDays = 0; public: void input() { cin >> name; } void getInfo(vector<Employee*> employees) { for (const auto& employee : employees) { if (auto worker = dynamic_cast<Worker*>(employee)) { totalProducts += worker->getSalary(); } else if (auto officer = dynamic_cast<Officer*>(employee)) { totalDays += officer->getSalary(); } } } float getSalary() { return 100 + (totalProducts * 2) + (totalDays * 40); } }; int main() { int n; cin >> n; vector<Employee*> employees; Manager* boss = nullptr; for (int i = 0; i < n; ++i) { int type; cin >> type; Employee* employee; if (type == 1) { employee = new Worker(); } else if (type == 2) { employee = new Officer(); } else { employee = new Manager(); boss = (Manager*)employee; } employee->input(); employees.push_back(employee); } if (boss) { boss->getInfo(employees); } for (const auto& employee : employees) { cout << employee->getName() << ": " << employee->getSalary() << endl; } return 0; } - User
Nhập số lượng chai n, sau đó nhập loại chai và thông tin tương ứng (hình trụ tròn - 1 - chiều cao x bán kính đáy - lấy PI = 3.14, hình hộp chữ nhật - 2 - chiều cao x chiều dài x chiều rộng, hình lập phương - 3 - độ dài cạnh). Chai hình trụ thì chỉ tính diện tích xung quanh trong khi đó hai chai còn lại tính diện tích toàn phần. Xuất ra thông tin sau: OOP C++: Dien tich chai vo: [Diện tích] Luong nuoc tren mat dat: [Thể tích nước trên mặt đất] Input Format
int
[[int]]
Constraints
No constraint
Output Format
string
string
Sample Input 0
3 2 2 3 5 3 4 1 1 1 Sample Output 0
Dien tich chai vo: 164.28 Luong nuoc tren mat dat: 9.714
- Assistant
#include <iostream> #include <vector> #include <string> #include <sstream> using namespace std; const float PI = 3.14; class Bottle { public: virtual float getArea() = 0; virtual float getGroundWater() = 0; }; class Cylinder : public Bottle { private: float height; float radius; public: Cylinder(float height, float radius) : height(height), radius(radius) {} float getArea() { return 2 * PI * radius * height; } float getGroundWater() { return PI * radius * radius * height; } }; class Rectangular : public Bottle { private: float height; float width; float length; public: Rectangular(float height, float length, float width) : height(height), length(length), width(width) {} float getArea() { return 2 * (length * width + length * height + width * height); } float getGroundWater() { return height * width * length; } }; class Cube : public Bottle { private: float edgeLength; public: Cube(float edgeLength) : edgeLength(edgeLength) {} float getArea() { return 6 * edgeLength * edgeLength; } float getGroundWater() { return edgeLength * edgeLength * edgeLength; } }; int main() { int n; cin >> n; vector<Bottle*> bottles; for (int i = 0; i < n; ++i) { int type; cin >> type; Bottle* bottle; if (type == 1) { float height, radius; cin >> height >> radius; bottle = new Cylinder(height, radius); } else if (type == 2) { float width, height, length; cin >> height >> length >> width; bottle = new Rectangular(height, length, width); } else { float edgeLength; cin >> edgeLength; bottle = new Cube(edgeLength); } bottles.push_back(bottle); } float totalArea = 0; float totalGroundWater = 0; for (const auto& bottle : bottles) { totalArea += bottle->getArea(); totalGroundWater += bottle->getGroundWater(); } cout << "Dien tich chai vo: " << totalArea << endl; cout << "Luong nuoc tren mat dat: " << totalGroundWater << endl; return 0; } - User
Nhập số lượng chai n, sau đó nhập loại chai và thông tin tương ứng (hình trụ tròn - 1 - chiều cao x bán kính đáy - lấy PI = 3.14, hình hộp chữ nhật - 2 - chiều cao x chiều dài x chiều rộng, hình lập phương - 3 - độ dài cạnh). Chai hình trụ thì chỉ tính diện tích xung quanh trong khi đó hai chai còn lại tính diện tích toàn phần. Xuất ra thông tin sau: OOP C++: Dien tich chai vo: [Diện tích] Luong nuoc tren mat dat: [Thể tích nước trên mặt đất] Input Format
int
[[int]]
Constraints
No constraint
Output Format
string
string
Sample Input 0
3 2 2 3 5 3 4 1 1 1 Sample Output 0
Dien tich chai vo: 164.28 Luong nuoc tren mat dat: 9.714
- Assistant
#include <iostream> #include <vector> #include <string> #include <sstream> using namespace std; const float PI = 3.14; class Bottle { public: virtual float getArea() = 0; virtual float getGroundWater() = 0; }; class Cylinder : public Bottle { private: float height; float radius; public: Cylinder(float height, float radius) : height(height), radius(radius) {} float getArea() { return 2 * PI * radius * height; } float getGroundWater() { return PI * radius * radius * height; } }; class Rectangular : public Bottle { private: float height; float width; float length; public: Rectangular(float height, float length, float width) : height(height), length(length), width(width) {} float getArea() { return 2 * (length * width + length * height + width * height); } float getGroundWater() { return height * width * length; } }; class Cube : public Bottle { private: float edgeLength; public: Cube(float edgeLength) : edgeLength(edgeLength) {} float getArea() { return 6 * edgeLength * edgeLength; } float getGroundWater() { return edgeLength * edgeLength * edgeLength; } }; int main() { int n; cin >> n; vector<Bottle*> bottles; for (int i = 0; i < n; ++i) { int type; cin >> type; Bottle* bottle; if (type == 1) { float height, radius; cin >> height >> radius; bottle = new Cylinder(height, radius); } else if (type == 2) { float width, height, length; cin >> height >> length >> width; bottle = new Rectangular(height, length, width); } else { float edgeLength; cin >> edgeLength; bottle = new Cube(edgeLength); } bottles.push_back(bottle); } float totalArea = 0; float totalGroundWater = 0; for (const auto& bottle : bottles) { totalArea += bottle->getArea(); totalGroundWater += bottle->getGroundWater(); } cout << "Dien tich chai vo: " << totalArea << endl; cout << "Luong nuoc tren mat dat: " << totalGroundWater << endl; return 0; } - User
fix Solution.cpp: In constructor ‘Rectangular::Rectangular(float, float, float)’: Solution.cpp:37:11: warning: ‘Rectangular::length’ will be initialized after [-Wreorder] float length; ^~~~~~ Solution.cpp:36:11: warning: ‘float Rectangular::width’ [-Wreorder] float width; ^~~~~ Solution.cpp:40:5: warning: when initialized here [-Wreorder] Rectangular(float height, float length, float width) : height(height), length(length), width(width) {} ^~~~~~~~~~~
- Assistant
#include <iostream> #include <vector> #include <string> #include <sstream> using namespace std; const float PI = 3.14; class Bottle { public: virtual float getArea() = 0; virtual float getGroundWater() = 0; }; class Cylinder : public Bottle { private: float height; float radius; public: Cylinder(float height, float radius) : height(height), radius(radius) {} float getArea() { return 2 * PI * radius * height; } float getGroundWater() { return PI * radius * radius * height; } }; class Rectangular : public Bottle { private: float height; float length; float width; public: Rectangular(float height, float length, float width) : height(height), width(width), length(length) {} float getArea() { return 2 * (length * width + length * height + width * height); } float getGroundWater() { return height * width * length; } }; class Cube : public Bottle { private: float edgeLength; public: Cube(float edgeLength) : edgeLength(edgeLength) {} float getArea() { return 6 * edgeLength * edgeLength; } float getGroundWater() { return edgeLength * edgeLength * edgeLength; } }; int main() { int n; cin >> n; vector<Bottle*> bottles; for (int i = 0; i < n; ++i) { int type; cin >> type; Bottle* bottle; if (type == 1) { float height, radius; cin >> height >> radius; bottle = new Cylinder(height, radius); } else if (type == 2) { float height, length, width; cin >> height >> length >> width; bottle = new Rectangular(height, length, width); } else { float edgeLength; cin >> edgeLength; bottle = new Cube(edgeLength); } bottles.push_back(bottle); } float totalArea = 0; float totalGroundWater = 0; for (const auto& bottle : bottles) { totalArea += bottle->getArea(); totalGroundWater += bottle->getGroundWater(); } cout << "Dien tich chai vo: " << totalArea << endl; cout << "Luong nuoc tren mat dat: " << totalGroundWater << endl; return 0; }