Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.9 kB
1
Indexable
Never
#include <iostream>
#include <string>



using namespace std;

class MyAttr
{
public:
	MyAttr() { maso = -1; };
	MyAttr(int x) { maso = x; };
	void input() {
		cout << "Nhap ma so:"<<endl;
		cin >> maso;
		cin.ignore();
		while (maso<=0)
		{
			cout << "Nhap lai ma so, ma so > 0:" << endl;
			cin >> maso;
			cin.ignore();


		}
	}
	void output() {
		cout << "ma so: " << maso << endl;

	}

	

private:
	int maso;

};

class Mydate
{
public:
	Mydate() { ngay = thang = nam = -1; }
	Mydate(int x) { ngay = x; thang = nam = -1; }
	Mydate(int x, int y) { ngay = x; thang = y; nam = -1; }
	Mydate(int x, int y,int z) { ngay = x; thang = y; nam = z; }
	void input() {
		cout << "Nhap thoi gian ( ngay / thang / nam):" << endl;
		cin >> ngay>>thang>>nam;
		cin.ignore();

		while (ngay<=0|| thang <= 0 || nam<=0)
		{
			cout << "Nhap lai thoi gian:" << endl;
			cin >> ngay >> thang >> nam;
			cin.ignore();

		}
	}
	void output() {
		cout << "thoi gian: " << ngay<<"/"<<thang<<"/"<<nam << endl;

	}

	friend bool operator > (Mydate x1, Mydate x2) {
		if (x1.nam > x2.nam)return true;
		if (x1.thang > x2.thang)return true;
		if (x1.ngay > x2.ngay)return true;
	}
private:
	int ngay, thang, nam;

};

class Myfile : public MyAttr , public Mydate
{
public:
	Myfile() { filename = ""; filesize = -1; }
	Myfile(string name) { filename = name; filesize = -1; }
	Myfile(string name,int size) { filename = name; filesize = size; }
	void input() {
		MyAttr::input();
		Mydate::input();

			cout << "Nhap ten file:" << endl;
			getline(cin, filename);
			while (filename.length()<=0)
			{
				cout << "Nhap lai ten:" << endl;
				getline(cin, filename);

			}
			cout << "Nhap kich thuoc file:" << endl;
			cin >> filesize;
			cin.ignore();

			while (filesize <= 0)
			{
				cout << "Nhap lai kich thuoc file:" << endl;
				cin >> filesize;
				cin.ignore();


			}

		

	}
	void output() {
		MyAttr::output();
		Mydate::output();
		cout << "name: " << filename << endl;
		cout << "size: " << filesize << endl;
	}
	friend void sort(Myfile files[],int n) {
		for (int i = 0; i < n-1; i++)
		{
			for (int j = i+1; j < n; j++)
			{
				if (files[i] > files[j]) {
					Myfile temp = files[i];
					files[i] = files[j];
					files[j] = temp;
				}

			}
		}
	}


	

private:
	string filename;
	int filesize;


};

int main() {
	Myfile* files = NULL; int n;
	cout << "Nhap so file muon nhap:" << endl;
	cin >> n;
	cin.ignore();
	files = new Myfile[n];
	while (n<=0)
	{
		cout << "Nhap so file muon nhap ( > 0 ):" << endl;
		cin.ignore();
	}
	for (int i = 0; i < n; i++)
	{
		files[i].input();
		cout << "-----------" << endl;

	}
	sort(files, n);
	for (int i = 0; i < n; i++)
	{
		files[i].output();


	}
}