#include<bits/stdc++.h>
//Phan Thanh Nhan k24
using namespace std;
#define MAX_SV 100
#define MAX_TAI_KHOAN 100
struct SINHVIEN {
string MaSV, GioiTinh, HoTenSV, NamSinh;
double TrungBinhHK;
};
string dang_nhap(const string& tenFile);
int nhap_TT(const string& tenFile, SINHVIEN danhSach[], int &soLuong);
void nhap_diem(const string& tenFile, SINHVIEN danhSach[], int soLuong);
void xuat(const SINHVIEN& sv);
void them_sinh_vien(SINHVIEN danhSach[], int &soLuong);//1
void cap_nhat_sinh_vien(SINHVIEN danhSach[], int soLuong);//2
void tim_kiem_sinh_vien(SINHVIEN danhSach[], int soLuong);//3
void xoa_sinh_vien(SINHVIEN danhSach[], int &soLuong);//4
void hien_thi_khong_canh_bao(SINHVIEN danhSach[], int soLuong);//5
void xuat_danh_sach_sinh_vien_ra_file(SINHVIEN danhSach[], int soLuong);//6
void menu_giang_vien(SINHVIEN danhSach[], int &soLuong); //menu + 7
int main() {
SINHVIEN danhSach[MAX_SV];
int soLuong = 0;
string fileDangNhap = "dangnhap.txt";
string fileDiem = "diemso.txt";
string fileThongTin = "thongtinSV.txt";
string inf="thongtinSV_export.txt"; //file output
string maSV = dang_nhap(fileDangNhap);
if (maSV.empty()) return 0;
soLuong = nhap_TT(fileThongTin, danhSach, soLuong);
nhap_diem(fileDiem, danhSach, soLuong);
if (maSV == "11377")
menu_giang_vien(danhSach, soLuong);
else{
for (int i = 0; i < soLuong; i++)
if (danhSach[i].MaSV == maSV){
xuat(danhSach[i]); break;
}
}
}
string dang_nhap(const string& tenFile) {
ifstream file(tenFile);
if (!file) {
cerr << "Khong the mo file dang nhap!" << endl;
return "";
}
string maSVs[MAX_TAI_KHOAN], matKhaus[MAX_TAI_KHOAN];
int soLuongTaiKhoan = 0;
while (file>>maSVs[soLuongTaiKhoan]>>matKhaus[soLuongTaiKhoan]){
soLuongTaiKhoan++;
}
file.close();
int soLanSai = 0;
while (soLanSai < 5) {
string nhapMaSV, nhapMatKhau;
cout<<"Nhap ma sinh vien: ";
cin>>nhapMaSV;
cout<<"Nhap mat khau: ";
cin>>nhapMatKhau;
for (int i=0;i<soLuongTaiKhoan;i++) {
if (maSVs[i] == nhapMaSV && matKhaus[i] == nhapMatKhau) {
cout << "Dang nhap thanh cong!\n";
return nhapMaSV;
}
}
cout << "Sai ma sinh vien hoac mat khau. Thu lai!\n";
soLanSai++;
}
cout << "Ban da nhap sai qua 5 lan. Thoat chuong trinh!\n";
return "";
}
int nhap_TT(const string& tenFile, SINHVIEN danhSach[], int &soLuong) {
ifstream file(tenFile);
if (!file) {
cerr << "Khong the mo file!" << endl;
return 0;
}
string maSV, gioiTinh, namSinh, hoTen;
soLuong = 0;
while (file>>maSV>>gioiTinh>>namSinh) {
getline(file>> ws, hoTen);
danhSach[soLuong].MaSV = maSV;
danhSach[soLuong].GioiTinh = gioiTinh;
danhSach[soLuong].NamSinh = namSinh;
danhSach[soLuong].HoTenSV = hoTen;
danhSach[soLuong].TrungBinhHK = 0.0;
soLuong++;
}
file.close();
return soLuong;
}
void nhap_diem(const string& tenFile, SINHVIEN danhSach[], int soLuong) {
ifstream file(tenFile);
if (!file) {
cerr << "Khong the mo file diem!" << endl;
return;
}
string maSV;
double ktlt, mmt, ctdl;
while (file >> maSV >> ktlt >> mmt >> ctdl) {
for (int i = 0; i < soLuong; i++) {
if (danhSach[i].MaSV == maSV) {
danhSach[i].TrungBinhHK = (ktlt * 4 + mmt * 3 + ctdl * 3) / 10.0;
break;
}
}
}
file.close();
}
void xuat(const SINHVIEN& sv) {
cout<< "MaSV: "<<sv.MaSV << "\n"
<< "GioiTinh: "<<sv.GioiTinh<< "\n"
<< "HoTenSV: "<<sv.HoTenSV<< "\n"
<< "NamSinh: "<<sv.NamSinh<< "\n"
<< fixed << setprecision(2)
<< "TrungBinhHK: " << sv.TrungBinhHK << "\n";
cout << "____________________________________\n";
}
void them_sinh_vien(SINHVIEN danhSach[], int &soLuong) {//1
if (soLuong >= MAX_SV) {
cout << "Danh sach da day, khong the them sinh vien!\n";
return;
}
cout << "Nhap MaSV: ";
cin >> danhSach[soLuong].MaSV;
for (int i = 0; i < soLuong; i++){
if (danhSach[i].MaSV == danhSach[soLuong].MaSV){
cout << "MaSV da ton tai! Them that bai.\n";
return;
}
}
cout << "Nhap ho ten: ";
cin.ignore();
getline(cin, danhSach[soLuong].HoTenSV);
cout<<"Nhap gioi tinh: "; cin>>danhSach[soLuong].GioiTinh;
cout<<"Nhap nam sinh: "; cin>>danhSach[soLuong].NamSinh;
double ktlt, mmt, ctdl;
cout<<"Nhap diem KTLT: "; cin>>ktlt;
cout<<"Nhap diem MMT: "; cin>>mmt;
cout<<"Nhap diem CTDL: "; cin>>ctdl;
danhSach[soLuong].TrungBinhHK = (ktlt*4 + mmt*3 + ctdl*3)/10.0;
soLuong++;
cout << "Them sinh vien thanh cong!\n";
}
void cap_nhat_sinh_vien(SINHVIEN danhSach[], int soLuong) {//2
string maSV;
cout << "Nhap MaSV can cap nhat: ";
cin >> maSV;
for (int i = 0; i < soLuong; i++) {
if (danhSach[i].MaSV == maSV) {
cout << "Nhap ho ten moi: ";
cin.ignore();
getline(cin, danhSach[i].HoTenSV);
cout << "Nhap gioi tinh moi: ";
cin >> danhSach[i].GioiTinh;
cout << "Nhap nam sinh moi: ";
cin >> danhSach[i].NamSinh;
double ktlt, mmt, ctdl;
cout << "Nhap diem KTLT moi: ";
cin >> ktlt;
cout << "Nhap diem MMT moi: ";
cin >> mmt;
cout << "Nhap diem CTDL moi: ";
cin >> ctdl;
danhSach[i].TrungBinhHK = (ktlt * 4 + mmt * 3 + ctdl * 3) / 10.0;
cout << "Cap nhat thanh cong!\n";
return;
}
}
cout << "Khong tim thay sinh vien voi MaSV: " << maSV << "\n";
}
void tim_kiem_sinh_vien(SINHVIEN danhSach[], int soLuong) {//3
string maSV;
cout<<"Nhap MaSV can tim: ";
cin>>maSV;
for (int i = 0; i < soLuong; i++)
if (danhSach[i].MaSV == maSV){
xuat(danhSach[i]); return;
}
cout << "Khong tim thay sinh vien voi MaSV: " << maSV << "\n";
}
void xoa_sinh_vien(SINHVIEN danhSach[], int &soLuong) {//4
string maSV;
cout<<"Nhap MaSV can xoa: "; cin>>maSV;
for (int i = 0; i < soLuong; i++){
if (danhSach[i].MaSV == maSV){
for (int j = i; j < soLuong - 1; j++){
danhSach[j] = danhSach[j + 1];
}
soLuong--;
cout<<"Xoa sinh vien thanh cong!\n";
return;
}
}
cout << "Khong tim thay sinh vien voi MaSV: " << maSV << "\n";
}
void hien_thi_khong_canh_bao(SINHVIEN danhSach[], int soLuong) {//5
bool coSV = false;
cout << "Danh sach sinh vien khong canh bao hoc tap:\n";
for (int i = 0; i < soLuong; i++){
if (danhSach[i].TrungBinhHK >= 4.0) {
xuat(danhSach[i]);
coSV = true;
}
}
if (!coSV)
cout << "Khong co sinh vien nao khong bi canh bao hoc tap!\n";
}
void xuat_danh_sach_sinh_vien_ra_file(SINHVIEN danhSach[], int soLuong){//6
ofstream file("thongtinSV_export.txt");
if (!file) {
cerr << "Khong the mo file de ghi!" << endl;
return;
}
for (int i = 0; i < soLuong; i++) {
file<<danhSach[i].MaSV << " "
<<danhSach[i].GioiTinh << " "
<<danhSach[i].NamSinh << " "
<<danhSach[i].HoTenSV << " "
<<fixed<<setprecision(2)<<danhSach[i].TrungBinhHK<< "\n";
}
file.close();
cout<<"Da xuat danh sach sinh vien ra file thongtinSV_export.txt thanh cong!\n";
}
void menu_giang_vien(SINHVIEN danhSach[], int &soLuong) {
int luaChon;
do {
cout<<"\n_____ MENU GIANG VIEN _____\n";
cout<<"1. Them sinh vien\n";
cout<<"2. Cap nhat thong tin sinh vien\n";
cout<<"3. Tim kiem sinh vien theo MaSV\n";
cout<<"4. Xoa sinh vien\n";
cout<<"5. Hien thi sinh vien khong canh bao hoc tap\n";
cout<<"6. Xuat danh sach sinh vien ra file.\n";
cout<<"7. Thoat\n";
cout<<"===========================\n";
cout<< "Nhap lua chon: ";
cin >> luaChon;
switch (luaChon){
case 1:
them_sinh_vien(danhSach, soLuong);
break;
case 2:
cap_nhat_sinh_vien(danhSach, soLuong);
break;
case 3:
tim_kiem_sinh_vien(danhSach, soLuong);
break;
case 4:
xoa_sinh_vien(danhSach, soLuong);
break;
case 5:
hien_thi_khong_canh_bao(danhSach, soLuong);
break;
case 6:
xuat_danh_sach_sinh_vien_ra_file(danhSach, soLuong);
break;
case 7:
cout << "THOAT\n";
break;
default:
cout << "Lua chon khong hop le. Vui long chon lai!\n";
}
} while (luaChon != 7);
}