code của em ko inSVrot voi inSVgioi được với ham tìm kiếm dựa vào tên và xóa sv dựa vào tên ko hoạt động.
Mấy anh chị giúp đỡ.
Thanks!
#include "stdafx.h"
#include<conio.h>
#include<stdio.h>
#include<iostream>
#include<string>
#include<iomanip>
#include <cstdlib>
using namespace std;
struct SinVien
{
string maSV;
string tenSV;
string lop;
string goitinh;
double diemcc;
double diemgk;
double diemthi;
double diemtb;
};
typedef struct tagNode
{
SinVien Info;
struct tagNode *pNext;
}Node;
typedef struct tagList
{
Node *pHead;
Node *pTail;
}List;
void CreateList(List &l)
{
l.pHead = NULL;
l.pTail = NULL;
}
Node *CreateNode(SinVien x)
{
Node *p;
p = new Node;
if (p == NULL)
exit(1);
p->Info = x;
p->pNext = NULL;
return p;
}
// them phan tu vao dau list
void AddHead(List &l, Node *p)
{
if (l.pHead == NULL)
{
l.pHead = p;
l.pTail = l.pHead;
}
else
{
p->pNext = l.pHead;
l.pHead = p;
}
}
// them phan tu vao cuoi danh sach
void AddTail(List &l, Node *p)
{
if (l.pHead == NULL)
{
l.pHead = p;
l.pTail = l.pHead;
}
else
{
l.pTail->pNext = p;
l.pTail = p;
}
}
// them phan tu p vao sau phan tu q
void InsertAfterQ(List &l, Node *p, Node *q)
{
if (q != NULL)
{
p->pNext = q->pNext;
q->pNext = p;
if (l.pTail == q)
l.pTail = p;
}
else
{
AddHead(l, q);
}
}
// Huy
int RemoveHead(List &l, SinVien x)
{
Node *p;
if (l.pHead != NULL)
{
p = l.pHead;
// x = p->Info;
l.pHead = l.pHead->pNext;
delete p;
if (l.pHead == NULL)
l.pTail = NULL;
return 1;
}
return 0;
}
// huy phan tu sau phan tu q trong list
int RemoveAfterQ(List &l, Node *q, SinVien &x)
{
Node *p;
if (q != NULL)
{
p = q->pNext;
if (p != NULL)
{
if (p == l.pTail)
l.pTail = q;
q->pNext = p->pNext;
x = p->Info;
delete p;
}
return 1;
}
else
return 0;
}
/*
int RemoveX(List &l, SinVien x)
{
Node *p, *q = NULL;
p = l.pHead;
while ((p != NULL) && (p->Info.tenSV != x.tenSV))
{
q = p;
p = p->pNext;
}
if (p == NULL)
return 0;
if (q != NULL)
RemoveAfterQ(l, q, x);
else
RemoveHead(l, x);
return 1;
}
*/
int RemoveX(List &l, SinVien x, string TenSV)
{
Node *p, *q = NULL;
p = l.pHead;
//TimTheoTen(l, ten);
while ((p != NULL) && (p->Info.tenSV != TenSV))
{
q = p;
p = p->pNext;
}
if (p == NULL)
return 0;
if (q != NULL)
RemoveAfterQ(l, q, x);
else
RemoveHead(l, x);
return 1;
}
/*
Node *Search(List l, SinVien x)
{
Node *p;
p = l.pHead;
while ((p != NULL) && (p->Info.tenSV != x.tenSV))
{
p = p->pNext;
}
return p;
}
*/
Node *TimTheoTen(List l, string tenSV)
{
Node *p, *q = NULL;
p = l.pHead;
while (p != NULL)
{
if (p->Info.tenSV != tenSV)
{
InDSSV(p);
}
else
{
cout << "\n Khong co du lieu ve sinh vien can tim";
}
q = p;
p = p->pNext;
}
return q;
}
void Nhapthongtin(List &l)
{
int n;
cout << "\nBan muon nhap bao nhieu sinh vien: ";
cin >> n;
CreateList(l); // Khởi tạo danh sách.
for (int i = 1; i <= n; i++)
{
// Mỗi lần vòng lặp chạy là ta nhập 1 Node
SinVien x;
cout << "\n----------() Nhap Thong Tin Sinh Vien ()------------\n";
cout << "\n-----------------------^_^--------------------------";
cout << "\n Nhap ma sinh vien : " ;
cin >> x.maSV;
cout << "\n Nhap ten sinh vien : " ;
cin >> x.tenSV;
cout << "\n Nhap lop cua sinh vien : " ;
cin >> x.lop;
cout << "\n Nhap goi tinh cua sinh vien: " ;
cin >> x.goitinh;
cout << "\n nhap diem cc : " ;
cin >> x.diemcc;
cout << "\n nhap diem giua ky : " ;
cin >> x.diemgk;
cout << "\n nhap diem thi cuoi ky : " ;
cin >> x.diemthi; cout << endl;
x.diemtb = x.diemcc*0.1 + x.diemgk*0.2 + x.diemthi*0.7;
Node *p =CreateNode (x); // Đưa data vào Node p, tạo ra node p
AddTail(l, p); // Thêm Node p vào cuối danh sách.
//AddHead(l, p);
}
}
void HienThi(List l)
{
cout << "_____________________________(DANH SACH SINH VIEN)_____________________________" << endl;
cout <<setw(3)<< "MaSV" << setw(8) << "Lop" << setw(10) << "Ten SV" << setw(20) << "■■■" << setw(8) << "DiemCC" << setw(8) << "DiemGK" << setw(9) << "DiemThi" << setw(9) << "DiemTB" << endl;
cout << "_______________________________________________________________________________" << endl;
for (Node *p = l.pHead; p != NULL; p = p->pNext)
{
cout << p->Info.maSV << setw(10) << p->Info.lop << setw(10) << p->Info.tenSV << setw(20) << p->Info.goitinh << setw(8) << p->Info.diemcc << setw(8) << p->Info.diemgk << setw(9) << p->Info.diemthi << setw(9) << p->Info.diemtb << endl;
cout << "_______________________________________________________________________________" << endl;
/*
cout << p->Info.maSV << endl;
cout << p->Info.tenSV << endl;
cout << p->Info.lop << endl;
cout << p->Info.diemcc << endl;
cout << p->Info.diemgk << endl;
cout << p->Info.diemthi << endl;
cout << p->Info.diemtb << endl;
*/
}
}
void InDSSV(Node *p)
{
cout << "_____________________________(DANH SACH SINH VIEN)_____________________________" << endl;
cout << setw(3) << "MaSV" << setw(8) << "Lop" << setw(10) << "Ten SV" << setw(20) << "■■■" << setw(8) << "DiemCC" << setw(8) << "DiemGK" << setw(9) << "DiemThi" << setw(9) << "DiemTB" << endl;
cout << "_______________________________________________________________________________" << endl;
cout << p->Info.maSV << setw(10) << p->Info.lop << setw(10) << p->Info.tenSV << setw(20) << p->Info.goitinh << setw(8) << p->Info.diemcc << setw(8) << p->Info.diemgk << setw(9) << p->Info.diemthi << setw(9) << p->Info.diemtb << endl;
cout << "_______________________________________________________________________________" << endl;
}
//int ReadFile(List &l, FILE *file)
void inSVrot(List l)
{
Node *p;
l.pHead;
while (p != NULL)
{
if (p->Info.diemtb < 4)
{
InDSSV(p);
}
else cout << "\n Khong co sinh vien co diem TB < 4 !";
p = p->pNext;
}
}
void inSVgoi(List l)
{
Node *p;
l.pHead;
while (p != NULL)
{
if (p->Info.diemtb >= 8)
{
InDSSV(p);
}
else cout << "\n Khong co sinh vien co diem TB >= 8 !";
p = p->pNext;
}
}
void menu()
{
cout << "____________________ Menu ____________________" << endl;
cout << "| 1. Them hoi so sinh vien. |" << endl;
cout << "| 2. Hien thi danh sach sinh vien. |" << endl;
cout << "| 3. Tim kiem sinh vien. |" << endl;
cout << "| 4. Hien thi sinh vien co diem TB < 4. |" << endl;
cout << "| 5. Hien thi sinh vien co diem TB > 8. |" << endl;
cout << "| 6. Xoa sinh vien. |" << endl;
cout << "| 0. Thoat. |" << endl;
cout << "|____________________________________________|" << endl;
}
int main()
{
SinVien x;
List l;
int luachon;
menu();
do
{
cout << "Nhap lua chon cua ban: ";
cin >> luachon;
if (luachon<0 || luachon>6)
{
cout << "lua chon khong hop le! vui long chon lai!" << endl;
}
switch (luachon)
{
case 1://nhap thong tin sinh vien
{
Nhapthongtin(l);
break;
}
case 2://hien thi tat ca sinh vien
{
HienThi(l);
break;
}
case 3://tim kiem sinh vien
{
string ten;
cout << "Nhap ten sinh vien can tim: ";
cin >> ten;
TimTheoTen(l, ten);
break;
}
case 4://Hien thi sinh vien co diem TB < 4.
{
inSVrot(l);
break;
}
case 5://Hien thi sinh vien co diem TB > 8.
{
inSVgoi(l);
break;
}
case 6://Xoa sinh vien.
{
string ten;
cout << "Nhap ten sinh vien can tim: ";
cin >> ten;
RemoveX(l, x, ten);
//Search(l, ten);
break;
}
case 0://thoat
{
return 0;
}
}
} while (luachon != 6);
return 0;
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?