Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
17 kB
3
Indexable
Never
#include<bits/stdc++.h>
#define ll long long
using namespace std;
//Bai 1, 2 : Rut gon 2 phan so, lam theo ham main co san, Bai 2 : tinh toan cong tru 2 phan so
//-----------------------------------------------------------------------------//
class PhanSo{
	private: 
		int tu, mau;
	public:
		PhanSo(int tu, int mau); // constructor : do trong main PhanSo p(1, 1);
		void rutgon();
		friend istream& operator >> (istream &in, PhanSo &a);
        friend ostream& operator << (ostream &out, PhanSo a);
        // Nap chong toan tu + bang member PhanSo
        PhanSo operator + (PhanSo another);
        // Nap chong toan tu - su dung friend
        friend PhanSo operator - (PhanSo, PhanSo);
};

PhanSo::PhanSo(int a, int b){
	this->tu = a;
	this->mau = b;
} 

istream& operator >> (istream& in, PhanSo &a){
    in >> a.tu >> a.mau;
    return in;
}

ostream& operator << (ostream& out, PhanSo a){
    out << a.tu << "/" << a.mau << endl;
    return out;
}

int gcd(int a, int b){
	if(b == 0) return a;
	return gcd(b, a % b);
}

int lcm(int a, int b){
	return a / gcd(a, b) * b;
}
void PhanSo::rutgon(){
	int chung = gcd(this->tu, this->mau);
	this->tu /= chung;
	this->mau /= chung;
}
// Ham main bai 1
//int main(){
//	PhanSo p(1, 1); // Khai bao 1 phan so bang constructor co 2 tham so (1, 1)
//	cin >> p;
//	p.rutgon();
//	cout << p;
//	return 0;
//}
//--------------------------------------------------------------------------------//

PhanSo PhanSo::operator + (PhanSo another){
	PhanSo tong(1, 1);// phai khai bao PhanSo tong nhu nay vi cai constructor ban dau minh no phai gan gia tri
	int mauChung = lcm(this->mau, another.mau);
	tong.tu = (mauChung / this->mau) * this->tu + (mauChung / another.mau) * another.tu;
	tong.mau = mauChung;
	tong.rutgon();
	return tong;
}

PhanSo operator - (PhanSo a, PhanSo b){
	PhanSo hieu(1, 1);
	int mauChung = lcm(a.mau, b.mau);
	hieu.mau = mauChung;
	hieu.tu = a.tu * (mauChung / a.mau) - b.tu * (mauChung / b.mau);
	hieu.rutgon();
	return hieu;
}
//Ham main bai 2
//int main(){
//	PhanSo p(1, 1), q(1, 1);
//	cin >> p >> q;
//	cout << p - q << p + q << endl;
//}
//-----------------------------------------------------------------------------//

//Bai 3 : 
class SinhVien{
	private:
		string maSV, ten, lop, ngaySinh;
		double gpa;
	public:
		SinhVien();
		friend void nhap(SinhVien &);
		friend void in(SinhVien);
		//--------------------------
		//Bai 4 :
		friend istream& operator >> (istream&, SinhVien &);
		friend ostream& operator << (ostream&, SinhVien);
		
};


SinhVien::SinhVien(){
	this->ten = this->lop = this->ngaySinh = "";
	this->gpa = 0;
}

void chuanHoaNgaySinh(string &s){
	if(s[1] == '/') s = "0" + s;
	if(s[4] == '/'){
		s.insert(3, "0");
	}
}
void nhap(SinhVien &a){
	a.maSV = "23736931";
	getline(cin, a.ten);
	cin >> a.lop >> a.ngaySinh >> a.gpa;
	chuanHoaNgaySinh(a.ngaySinh);
}

void in(SinhVien a){
	cout << a.maSV << " " << a.ten << " " << a.ngaySinh << " " << a.lop << " " << fixed << setprecision(2) << a.gpa << endl;
}

#define a() a
// Ham main bai 3
// ngay sinh duoc chuan hoa dang dd/mm/yyyy
/*
int main(){
	SinhVien a(); // SinhVien a
	nhap(a); // Chu y : 2 cai ham nhap va in khong phai la 2 member cua class SinhVien
	in(a); // Neu la member thi dung a.nhap(), a.in() || Con nhu nay thi minh cu khoi tao ham voi nhuw bth hay lam thoi
	return 0;
}
*/
//------------------------------------------------------------------------------------
//Bai 4 thi cung y vay nhung yeu cau nap chong toan tu cin vs cout

istream& operator >> (istream& in, SinhVien &a){
	a.maSV = "23736931";
	getline(in, a.ten);
	in >> a.lop >> a.ngaySinh >> a.gpa;
	if(a.ngaySinh[2] != '/') a.ngaySinh = "0" + a.ngaySinh;
	if(a.ngaySinh[5] != '/') a.ngaySinh.insert(3, "0");
	return in;
}

ostream& operator << (ostream& out, SinhVien a){
	out << a.maSV << " " << a.ten << " " << a.ngaySinh << " " << a.lop << " " << fixed << setprecision(2) << a.gpa << endl;
	return out;
}
/*
int main(){
	SinhVien a();
	cin >> a;
	cout << a;
	return 0;
}
*/
// Bai 5 : Hay nhap danh sach sinh vien gom maSv, Ten, Lop, Email
// Sap xep ds sinh vien theo lop, tang dan (theo thu tu tu dien)
/*
4
B16DCCN011
Nguyen Trong Duc Anh
D16CNPM1
sv1@stu.ptit.edu.vn
B15DCCN215
To Ngoc Hieu
D15CNPM3
sv2@stu.ptit.edu.vn
D15DCKT150
Nguyen Ngoc Son
D15CQKT02-B
sv3@stu.ptit.edu.vn
B15DCKT199
Nguyen Trong Tung
D15CQKT03-B
sv4@stu.ptit.edu.vn
*/
/*
-------------------------------------------------------------
B15DCCN215 To Ngoc Hieu D15CNPM3 sv2@stu.ptit.edu.vn
-------------------------------------------------------------
D15DCKT150 Nguyen Ngoc Son D15CQKT02-B sv3@stu.ptit.edu.vn
-------------------------------------------------------------
B15DCKT199 Nguyen Trong Tung D15CQKT03-B sv4@stu.ptit.edu.vn
-------------------------------------------------------------
B16DCCN011 Nguyen Trong Duc Anh D16CNPM1 sv1@stu.ptit.edu.vn
*/
class SinhVien1{
	private: 
		string maSv, ten, lop, email;
	public:
		SinhVien1(){
			this->maSv = this->ten = this->lop = this->email = "";
		}
		friend istream& operator >> (istream&, SinhVien1 &);
		friend ostream& operator << (ostream&, SinhVien1);
		string getLop();
		string getMaSv(){
			return this->maSv;
		}
		// Cach 2 : minh chi can nop chong cai toan tu < thoi, xong vo main : sort(a, a + n)
//		bool operator < (SinhVien1 another){
//			return this->lop < another.lop;
//		}
		// sap xep sinh vien theo ma
		bool operator < (SinhVien1 another){
			return this->maSv < another.maSv;
		}
		
};

istream& operator >> (istream& in, SinhVien1 &a){
	in >> a.maSv; in.ignore();
	getline(in, a.ten);
	in >> a.lop >> a.email;
	return in;
}

ostream& operator << (ostream& out, SinhVien1 a){
	out << "-------------------------------------------------------------\n";
	out << a.maSv << " " << a.ten << " " << a.lop << " " << a.email << endl;
	return out;
}
void nhapSV(SinhVien1 a[], int n){
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
}

void inSV(SinhVien1 a[], int n){
	for(int i = 0; i < n; i++){
		cout << a[i];
	}
}
string SinhVien1::getLop(){
	return this->lop;
}

bool cmp(SinhVien1 a, SinhVien1 b){
	return a.getLop() < b.getLop();
}
// Ham main bai 5 
/*
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	int n; cin >> n;
	SinhVien1 a[n];
	nhapSV(a, n);
	sort(a, a + n, cmp);
	inSV(a, n);
	return 0;
}
*/
//-------------------------------------------------------------------
// Bai 6 : Sap xep sinh vien theo ma so sv
// input no khac, bo cai so luong sinh vien di
/*
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	vector<SinhVien1> v;
	SinhVien1 tmp;
	while(cin >> tmp){
		v.push_back(tmp);
	}
	sort(v.begin(), v.end());
	for(int i = 0; i < v.size(); i++){
		cout << v[i];
	}
	return 0;
}
*/
//--------------------------------------------------------
//Bai 7 : Nhap danh sach sinh vien, xong moi truy van, nhap vo 1 lop, xong xuat ra man hinh 
//cac sinh vien thuoc lop do
/*
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	int n; cin >> n;
	SinhVien1 a[n];
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	int q; cin >> q;
	while(q--){
		string lopCanTim; cin >> lopCanTim;
		cout << "DANH SACH SINH VIEN LOP : " << lopCanTim << endl;
		for(int i = 0; i < n; i++){
			if(a[i].getLop() == lopCanTim){
				cout << a[i];
			}
		}
	}
	return 0;
}
4
B16DCCN011
Nguyen Trong Duc Anh
D16CNPM1
sv1@stu.ptit.edu.vn
B15DCCN215
To Ngoc Hieu
D15CNPM3
sv2@stu.ptit.edu.vn
B15DCKT150
Nguyen Ngoc Son
D15CQKT02-B
sv3@stu.ptit.edu.vn
B15DCKT199
Nguyen Trong Tung
D15CQKT02-B
sv4@stu.ptit.edu.vn
1
D15CQKT02-B
*/
//---------------------------------------------------------------------------
// Bai 8 : Tim sinh vien thuoc khoa nao do
//B16DCCN011 : B16 la khoa 2016
//D15CQKT02-B : Khoa 2015
/*
4
B16DCCN011
Nguyen Trong Duc Anh
D16CNPM1
sv1@stu.ptit.edu.vn
B15DCCN215
To Ngoc Hieu
D15CNPM3
sv2@stu.ptit.edu.vn
B15DCKT150
Nguyen Ngoc Son
D15CQKT02-B
sv3@stu.ptit.edu.vn
B15DCKT199
Nguyen Trong Tung
D15CQKT02-B
sv4@stu.ptit.edu.vn
1
2015
*/
// Ham main bai 8
/*
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	int n; cin >> n;
	SinhVien1 a[n];
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	int q; cin >> q;
	while(q--){
		string s; cin >> s;
		cout << "DANH SACH SINH VIEN KHOA : " << s << endl;
		for(int i = 0; i < n; i++){
			if(a[i].getLop()[1] == s[2] && a[i].getLop()[2] == s[3]){
				cout << a[i];
			}
		}
	}
	return 0;
}
*/
//--------------------------------------------------------------------------
// Bai 9 : Liet ke sinh vien theo nganh, chu E la sunh vien he clc
// DCKT : Ke toan
// DCCN : Cong Nghe Thong Tin - Tru di cac sinh vien lop bat dau bang chu E
// DCAT : An Toan Thong Tin - Tru di cac sinh vien lop bat dau bang chu E
// DCVT : Dien Tu Vien Thong
// DCDT : Dien Tu

void vietHoa(string &s){
	for(int i = 0; i < s.length(); i++){
		s[i] = toupper(s[i]);
	}
}

string convert(string s){
	string res = "";
	res += toupper(s[0]);
	for(int i = 1; i < s.length(); i++){
		if(s[i] != ' ' && s[i - 1] == ' '){
			res += toupper(s[i]);
			return res;
		}
	}
	return res;
}
/*
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	int n; cin >> n;
	map<string, vector<SinhVien1>> mp;
	for(int i = 0; i < n; i++){
		SinhVien1 tmp; cin >> tmp;
		mp[tmp.getMaSv().substr(5, 2)].push_back(tmp);
	}
	int q; cin >> q;
	cin.ignore();
	while(q--){
		string s; getline(cin, s);
		vietHoa(s);
		cout << "DANH SACH SINH VIEN NGANH : " << s << endl;
		string nganh = convert(s);
		if(nganh == "AT" || nganh == "CN"){
			for(SinhVien1 x : mp[nganh]){
				if(x.getMaSv().find("E") != string::npos){ // Neu khong tim thay chu E thi no tra ve string::npos
					cout << x;
				}
			}
		}
		else{
			for(SinhVien1 x : mp[nganh]){
				cout << x;
			}
		}
	}
	return 0;
}
4
B16DCCN011
Nguyen Trong Duc Anh
D16CNPM1
sv1@stu.ptit.edu.vn
B15DCCN215
To Ngoc Hieu
D15CNPM3
sv2@stu.ptit.edu.vn
B15DCKT150
Nguyen Ngoc Son
D15CQKT02-B
sv3@stu.ptit.edu.vn
B15DCKT199
Nguyen Trong Tung
D15CQKT02-B
sv4@stu.ptit.edu.vn
1
Ke toan
//--------------------out-----------------------------------
DANH SACH SINH VIEN NGANH : KE TOAN
-------------------------------------------------------------
B15DCKT150 Nguyen Ngoc Son D15CQKT02-B sv3@stu.ptit.edu.vn
-------------------------------------------------------------
B15DCKT199 Nguyen Trong Tung D15CQKT02-B sv4@stu.ptit.edu.vn
*/

//-------------------------------------------------------------------
/*
Bai 10 : Danh sach doanh nghiep nhan sinh vien thuc tap
Hay sap xep cac doanh nghiep theo so luong sinh vien co the nhan giam dan
Output : Ghi ra danh sach da duoc sap xep so luong giam dan. Trong truong hop cung so luong
thi sap xep theo ma doanh nghiep(thu tu tu dien tang dan)
*/
class DoanhNghiep{
	private:
		string maDoanhNghiep, tenDoanhNghiep;
		int soSinhVien;
	public:
		void nhap();
		void xuat();
		int getSoLuongSinhVien(){
			return this->soSinhVien;
		}
		string getMaDoanhNghiep(){
			return this->maDoanhNghiep;
		}
};

void DoanhNghiep::nhap(){
	cin >> this->maDoanhNghiep;
	cin.ignore();
	getline(cin, this->tenDoanhNghiep);
	cin >> soSinhVien;
}

void DoanhNghiep::xuat(){
	cout << "--------------------------------------\n";
	cout << this->maDoanhNghiep << "\n" << this->tenDoanhNghiep << "\n" << this->soSinhVien << endl;
}

bool cmp1(DoanhNghiep a, DoanhNghiep b){
	if(a.getSoLuongSinhVien() == b.getSoLuongSinhVien()){
		return a.getMaDoanhNghiep() < b.getMaDoanhNghiep();
	}
	return a.getSoLuongSinhVien() > b.getSoLuongSinhVien();
}

/*
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	int n; cin >> n;
	DoanhNghiep b[n];
	for(int i = 0; i < n; i++){
		b[i].nhap();
	}
	sort(b, b + n, cmp1);
	for(int i = 0; i < n; i++){
		b[i].xuat();
	}
	return 0;
}
*/
//-----------------------------------------------------------------------
/*
Bai 12: 
Khai bao 1 cai class NhanVien
Sap xep nhan vien theo do tuoi tu gia den tre
mm/dd/yyyy
ma nhan vien thi duoc danh so theo thu tu nhap vao, vd : 00001 00002 tang dan len
*/


class NhanVien{
	private:
		string maNhanVien, hoTen, gioiTinh, ngaySinh, diaChi, maSoThue, ngayKiHopDong;
	
	public:
		NhanVien();
		static int cnt;
		friend istream& operator >> (istream &in, NhanVien &);
	    friend ostream& operator << (ostream &out, NhanVien ); 
	    friend bool operator < (NhanVien a, NhanVien b){
	    	string s1 = a.ngaySinh, s2 = b.ngaySinh;
			int ngayA = (s1[0] - '0') * 10 + (s1[1] - '0'), thangA = (s1[3] - '0') * 10 + (s1[4] - '0'), namA = stoi(s1.substr(6));
			int ngayB = (s2[0] - '0') * 10 + (s2[1] - '0'), thangB = (s2[3] - '0') * 10 + (s2[4] - '0'), namB = stoi(s2.substr(6));
			if(namA != namB){
				return namA < namB;
			}
			if(thangA != thangB){
				return thangA < thangB;
			}
			return ngayA < ngayB;
		}
		int getCnt(){
			return this->cnt;
		}
};

NhanVien::NhanVien(){
	maNhanVien = hoTen = gioiTinh = ngaySinh = diaChi = maSoThue = ngayKiHopDong = "";
}

int NhanVien::cnt = 0;

istream& operator >> (istream& in, NhanVien &a){
	NhanVien::cnt++;
	a.maNhanVien = string(5 - to_string(NhanVien::cnt).length(), '0') + to_string(NhanVien::cnt);
	in.ignore();
	getline(in, a.hoTen);
    in >> a.gioiTinh >> a.ngaySinh;
    in.ignore();
	getline(in,a.diaChi);
	in >> a.maSoThue >> a.ngayKiHopDong;
    return in;
}

ostream& operator << (ostream& out, NhanVien a){
	// hoac nay dung bien maNhanVien la int thi : 
	// out << setfill('0') << setw(5) << a.maNhanVien
	out << a.maNhanVien << " | " << a.hoTen << " | " << a.gioiTinh << " | " << a.ngaySinh << " | " << a.diaChi << " | " << a.maSoThue << " | " << a.ngayKiHopDong << endl;
    return out;
}

void sapxep(NhanVien a[], int n){
	sort(a, a + n);
}


#define a() a
/*
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	NhanVien a[50];
	int n; cin >> n;
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	sapxep(a, n);
	for(int i = 0; i < n; i++){
		cout << a[i];
	}
	
	return 0;
}
4
Nguyen Van A
Nam
10/22/1982
Mo Lao-Ha Dong-Ha Noi
8333012345
31/12/2023
Ly Thi B
Nu
10/15/1988
Quang Nam
83330123456
22/08/2011
Hoang Thi C
Nu
04/02/1981
Gia Lai
833301234567
22/08/2011
Bui Trung Kien
Nam
03/03/2005
Quang Ngai
051205007969
03/03/2030
*/

//-----------------------------------------------------------
/*
Bai 18 : Sap xep giang vien theo ten, cung ten thi sap xep theo ma
In : 
3
Nguyen Manh Son
Cong nghe phan mem
Vu Hoai Nam
Khoa hoc may tinh
Dang Minh Tuan
An toan thong tin

Out :
GV02 Vu Hoai Nam KHMT
GV01 Nguyen Manh Son CNPM
GV03 Dang Minh Tuan ATTT
*/
string chuanHoaLop(string s){
	string res = "";
	stringstream ss(s);
	string token;
	while(ss >> token){
		res += toupper(token[0]);
	}
	return res;
}

string chuanTen(string s){
	string res;
	stringstream ss(s);
	string token;
	while(ss >> token){
		res = token;
	}
	return res;
}
class GiangVien{
	private:
		string maGV, tenGiangVien, boMon;
	public:
		static int dem;
		friend istream& operator >> (istream &in, GiangVien &a){
			++dem;
			string s = to_string(dem);
			if(s.length() < 2) s = "0" + s;
			a.maGV = "GV" + s;
			getline(in, a.tenGiangVien);
			getline(in, a.boMon);
			a.boMon = chuanHoaLop(a.boMon);
			return in;
		}
	    friend ostream& operator << (ostream &out, GiangVien a){
	    	out << a.maGV << " " << a.tenGiangVien << " " << a.boMon << endl;
	    	return out;
		}
	    friend bool operator < (GiangVien a, GiangVien b){
	    	string tenA = chuanTen(a.tenGiangVien), tenB = chuanTen(b.tenGiangVien);
	    	if(tenA != tenB){
	    		return tenA < tenB;
			}
			return a.maGV < b.maGV;
		}
		string getLop(){
			return this->boMon;
		}
};


int GiangVien::dem = 0;


int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	GiangVien a[50];
	int n; cin >> n; cin.ignore();
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	int q; cin >> q;
	cin.ignore();
	while(q--){
		string s; getline(cin, s);
		s = chuanHoaLop(s);
		cout << "DANH SACH GIANG VIEN BO MOM : " << s << endl;
		for(int i = 0; i < n; i++){
			if(s == a[i].getLop()){
				cout << a[i];
			}
		}
	}

	return 0;
}


Leave a Comment