Xem giúp mình muốn xuất thêm DSSV sau khi đã sắp xếp xong mà không bị trùng với file Ketqua.txt ban đầu
//Khai bai thu vien
#include <iostream>
#include <vector>
using namespace std;
#include <fstream>
#include <string>
#include <algorithm>
struct Sinhvien
{
string sMaSV, sHoten, nNamsinh;
float fDiemtb;
};
//Nguyen ham con
void docDSSV(vector <Sinhvien> &vec, int &nN);
void xuatDSSV(vector <Sinhvien> vec);
void timGhiSV(vector <Sinhvien> vec);
string xeploaiSV(float fDiem);
void chuanHoaChuoi(string &sChuoi);
void inHoaDauTu(string &s);
void xoaSV(vector <Sinhvien> &vec, string sKeyMaSV);
void sapxepDSSV(vector <Sinhvien> &vec);
void xuatDSSVSapXep(vector <Sinhvien> vec);
//Chuong trinh chinh
void main()
{
vector <Sinhvien> vec;
int nN = 0;
docDSSV(vec, nN);
xuatDSSV(vec);
string sKeyMaSV = "";
cout << " Nhap MaSV can xoa: ";
rewind(stdin);
getline(cin, sKeyMaSV);
xoaSV(vec, sKeyMaSV);
timGhiSV(vec);
docDSSV(vec, nN);
sapxepDSSV(vec);
xuatDSSVSapXep(vec);
system("pause");
}
//Dinh nghia nguyen ham con
void xuatDSSVSapXep(vector <Sinhvien> vec)
{
ofstream fcout;
fcout.open("DSSV_Sort.txt");
for (int i = 0; i < vec.size(); i++)
{
fcout << "Thong tin sinh vien thu: " << i << "\n";
fcout << "\tMaSV:" << vec[i].sMaSV << endl;
fcout << "\tHoTen: " << vec[i].sHoten << endl;
fcout << "\tNamSinh: " << vec[i].nNamsinh << endl;
fcout << "\tDiemTB: " << vec[i].fDiemtb << endl;
}
fcout.close();
}
void sapxepDSSV(vector <Sinhvien> &vec)
{
int i, j;
string tam;
for (int i = 0; i< vec.size() - 1; i++)
{
for (int j = i + 1; j < vec.size(); j++)
{
if (vec[i].sHoten > vec[j].sHoten)
{
tam = vec[i].sHoten;
vec[i].sHoten = vec[j].sHoten;
vec[j].sHoten = tam;
}
}
}
}
void xoaSV(vector <Sinhvien> &vec, string sKeyMaSV)
{
Sinhvien xA;
int flag = 0;
for (int i = 0; i < vec.size(); i++)
{
if (vec[i].sMaSV == sKeyMaSV)
{
flag = 1;
vec.erase(vec.begin() + i);
break;
}
}
if (flag == 0)
{
cout << "Khong tim thay ma SV\n";
}
}
void inHoaDauTu(string &s)
{
if (s[0] >= ‘a’&&s[0] <= ‘z’)
{
s[0] = s[0] - 32;
}
for (int i = 0; i < s.length(); i++)
{
if (s[i] == ’ '&& s[i + 1] >= ‘a’ && s[i + 1] <= ‘z’)
{
s[i + 1] = s[i + 1] - 32;
}
}
}
void chuanHoaChuoi(string &sChuoi)
{
//Xoa khoang trang du thua o dau chuoi:
int i = 0;
while (sChuoi[i] == ’ ')
{
sChuoi.erase(0, 1);
}
//Xoa khoang trang du thua o cuoi chuoi:
while (sChuoi[sChuoi.length() - 1] == ’ ')
{
sChuoi[sChuoi.length() - 1] = ‘\0’;
}
//Xoa khoang trang du thua o giua chuoi:
int j = 0;
while (j < sChuoi.length())
{
if (sChuoi[j] == ’ ’ && sChuoi[j + 1] == ’ ')
{
sChuoi.erase(j, 1);
}
else
j++;
}
}
string xeploaiSV(float fDiem)
{
string sLoai = "";
if (fDiem >= 0 && fDiem < 5)
{
sLoai = "Yeu";
}
else if (fDiem < 6.5)
{
sLoai = "Trung binh";
}
else if (fDiem < 8)
{
sLoai = "Kha";
}
else if (fDiem < 9)
{
sLoai = "Gioi";
}
else
{
sLoai = "Xuat sac";
}
return sLoai;
}
void timGhiSV(vector <Sinhvien> vec)
{
ofstream fcout;
fcout.open("KetQua.txt");
for (int i = 0; i < vec.size(); i++)
{
fcout << "Thong tin sinh vien ho Nguyen thu: " << i << "\n";
fcout << "\tMaSV:" << vec[i].sMaSV << endl;
fcout << "\tHoTen: " << vec[i].sHoten << endl;
fcout << "\tNamSinh: " << vec[i].nNamsinh << endl;
fcout << "\tDiemTB: " << vec[i].fDiemtb << endl;
}
fcout.close();
}
void xuatDSSV(vector <Sinhvien> vec)
{
cout << "\n **********XUAT DS SV********** \n";
for (int i = 0; i < vec.size(); i++)
{
cout << "Thong tin sinh vien thu: " << i << "\n";
cout << "\tMaSV:" << vec[i].sMaSV << endl;
cout << "\tHoTen: " << vec[i].sHoten << endl;
cout << "\tNamSinh: " << vec[i].nNamsinh << endl;
cout << "\tDiemTB: " << vec[i].fDiemtb << endl;
cout << "\tLoai: " << xeploaiSV(vec[i].fDiemtb) << endl;
}
}
void docDSSV(vector <Sinhvien> &vec, int &nN)
{
Sinhvien xA;
ifstream fcin;
fcin.open("DSSV.txt",ios::in);
if (fcin.is_open() == false)
{
cout << "Nhap file that bai";
return;
}
fcin >> nN;
fcin.ignore(1);
for (int i = 0; i < nN; i++)
{
rewind(stdin);
getline(fcin, xA.sMaSV,’#’);
getline(fcin, xA.sHoten,’#’);
chuanHoaChuoi(xA.sHoten);
inHoaDauTu(xA.sHoten);
getline(fcin, xA.nNamsinh,’#’);
fcin >> xA.fDiemtb;
fcin.ignore(1);
//Dua Sinhvien vao vector vec:
vec.push_back(xA);
}
fcin.close();
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?