Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
6.3 kB
1
Indexable
Never
#include <iostream>
#include<regex>
#include<iomanip>
#include<string>
#include<fstream>
#include<map>

using namespace std;

unsigned int n = 0;

string chuyenString(char* s) {
	string t = "";
	for (int i = 0; i < strlen(s); i++) {
		t += tolower(s[i]);
	}
	return t;
}

struct QuanAo {
	char ma[7], ten[51], loai[31], size[3];
	unsigned int soLuong, giaBan;
};

void xuatMotDoiTuong(QuanAo& qa) {
	cout << "Ma san pham: " << qa.ma << '\n';
	cout << "Ten san pham: " << qa.ten << '\n';
	cout << "Loai san pham: " << qa.loai << '\n';
	cout << "Size: " << qa.size << '\n';
	cout << "So luong: " << qa.soLuong << '\n';
	cout << "Gia ban: " << qa.giaBan << "\n\n";
}

void nhapThongTin(QuanAo* qa) {
	regex maQA("[a-z]{3}[0-9]{3}", regex_constants::icase);
	regex tenQA("[a-z ]+", regex_constants::icase);
	regex soLuongQA("[a-z]{2}", regex_constants::icase);

	cout << "1. Nhap thong tin:\n\n";

	do {
		cout << "Nhap so luong quan ao:";
		cin >> n;
	} while (n <= 0);

	for (int i = 0; i < n; i++) {
		cout << "Nhap thong tin san pham thu :" << i + 1 << "\n";
		do {
			cout << "Nhap ma san pham:";
			cin >> qa[i].ma;
		} while (!regex_match(qa[i].ma, maQA));

		cin.ignore();

		do {
			cout << "Nhap ten san pham:";
			cin.getline(qa[i].ten, 51);
		} while (!regex_match(qa[i].ten, tenQA));

		do {
			cout << "Nhap loai san pham:";
			cin.getline(qa[i].loai, 31);
		} while (!regex_match(qa[i].loai, tenQA));

		do {
			cout << "Nhap size:";
			cin >> qa[i].size;
		} while (!regex_match(qa[i].size, soLuongQA));

		do {
			cout << "Nhap so luong:";
			cin >> qa[i].soLuong;
		} while (qa[i].soLuong < 0);

		do {
			cout << "Nhap gia ban:";
			cin >> qa[i].giaBan;
		} while (qa[i].giaBan < 0);
		cout << '\n';
	}
}

void xuatThongTin(QuanAo* qa) {
	if (n == 0) {
		cout << "Khong co du lieu:\n";
		return;
	}

	cout << "2. Xuat thong tin:\n\n";

	for (int i = 0; i < n; i++) {
		cout << "San pham thu :" << i + 1 << '\n';
		xuatMotDoiTuong(qa[i]);
	}



}

void xuatTheoLoaiVaKhoangTien(QuanAo* qa) {
	if (n == 0) {
		cout << "Khong co du lieu:\n";
		return;
	}

	char a[31]{};
	char loai[31];
	int dem = 0;
	unsigned int tien1, tien2;
	regex tenQA("[a-z ]+", regex_constants::icase);
	cin.ignore();
	do {
		cout << "Nhap loai san pham:";
		cin.getline(loai, 31);
	} while (!regex_match(loai, tenQA));

	//Chuyen loai ve chu thuong
	for (int i = 0; i < strlen(loai); i++)
		loai[i] = tolower(loai[i]);

	//Nhap khoang tien

	do {
		cout << "Nhap khoang tien 1:";
		cin >> tien1;

		cout << "Nhap khoang tien 2:";
		cin >> tien2;
	} while (tien1 < 0 || tien2 < 0);


	for (int i = 0; i < n; i++) {

		//Chuyen loai ve chu thuong
		for (int j = 0; j < strlen(qa[i].loai); j++)
			a[j] = tolower(qa[i].loai[j]);

		if (strcmp(loai, a) == 0 && qa[i].giaBan <= tien2 && qa[i].giaBan >= tien1) {
			xuatMotDoiTuong(qa[i]);
			dem++;
		}


	}

	if (dem == 0)
		cout << "Khong co du lieu:\n";

}

void demTheoLoaiVaSize(QuanAo* qa) {
	if (n == 0) {
		cout << "Khong co du lieu:\n";
		return;
	}

	map<string, map<string, unsigned int>> res;

	for (int i = 0; i < n; i++) {
		string loai = chuyenString(qa[i].loai);
		string size = chuyenString(qa[i].size);
		res[loai][size] += qa[i].soLuong;

	}

	for (auto it : res) {
		cout << "Loai :" << it.first << "\n";
		for (auto it1 : it.second)
			cout << "Size " << it1.first << ": " << it1.second << '\n';
	}
}

void doiCho(QuanAo& qa1, QuanAo& qa2) {
	QuanAo temp = qa1;
	qa1 = qa2;
	qa2 = temp;
}

void sapXep(QuanAo* qa) {
	if (n == 0) {
		cout << "Khong co du lieu:\n";
		return;
	}

	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) {
			if (strcmp(qa[i].loai, qa[j].loai) == 0) {
				if (qa[i].giaBan < qa[j].giaBan)
					doiCho(qa[i], qa[j]);

			}
			else {
				if (strcmp(qa[i].loai, qa[j].loai) > 0)
					doiCho(qa[i], qa[j]);
			}


		}
	}
	cout << "In ra man hinh\n";
	for (int i = 0; i < n; i++)
		xuatMotDoiTuong(qa[i]);
}

void ghiFile(QuanAo* qa)
{
	if (n == 0) {
		cout << "Khong co du lieu:\n";
		return;
	}

	string inp;
	cout << "Nhap ten file: ";
	cin >> inp;
	fstream fileIn(inp, ios::binary | ios::out);
	for (int i = 0; i < n; i++)
	{
		fileIn.write(reinterpret_cast<char*>(&qa[i]), sizeof(QuanAo));
	}
	fileIn.close();
	cout << "Da ghi vao file\n\n";
}

void docManHinh()
{
	if (n == 0) {
		cout << "Khong co du lieu:\n";
		return;
	}
	string inp;
	QuanAo ao;

	cout << "Nhap ten file de xuat ra man hinh: ";

	cin >> inp;
	fstream fileIn(inp, ios::binary | ios::in);
	if (!fileIn)
	{
		cout << "Khong the mo file nay\n";
		return;
	}
	while (fileIn.read(reinterpret_cast<char*>(&ao), sizeof(QuanAo)))
	{
		xuatMotDoiTuong(ao);
	}
}


void nhapMenu(QuanAo* qa) {
	int a;

	system("cls");
	cout << "*---------------------------*\n";
	cout << "1. Nhap thong tin quan ao:\n";
	cout << "2. Xuat danh sach quan ao:\n";
	cout << "3. Xuat danh sach quan ao theo loai va theo khoang tien:\n";
	cout << "4. Dem so luong quan ao theo loai va theo size:\n";
	cout << "5. Sap xep quan ao tang dan theo loai, cung loai thi sap xem giam theo gia:\n";
	cout << "6. Luu danh sach quan ao vao file. Doc va xuat file ra man hinh:\n ";
	cout << "Khac -> Thoat khoi chuong trinh:\n";
	cout << "*---------------------------*\n";

	cout << "Nhap so can chon:";
	cin >> a;
	switch (a) {
	case 1: {
		system("cls");
		nhapThongTin(qa);
		system("pause");
		break;
	}
	case 2: {
		system("cls");
		xuatThongTin(qa);
		system("pause");
		break;
	}
	case 3: {
		system("cls");
		xuatTheoLoaiVaKhoangTien(qa);
		system("pause");
		break;
	}
	case 4: {
		system("cls");
		demTheoLoaiVaSize(qa);
		system("pause");
		break;
	}
	case 5: {
		system("cls");
		sapXep(qa);
		system("pause");
		break;
	}
	case 6: {
		system("cls");
		ghiFile(qa);
		docManHinh();
		system("pause");
		break;
	}
	default: {
		system("cls");
		cout << "Thoat khoi chuong trinh:\n";
		exit(0);

	}

	}



}
int main()
{

	QuanAo* qa = new QuanAo[1000];
	while (true) {
		nhapMenu(qa);
	}
}