Guest 1K 11th Aug, 2020
#include<iostream> #include<string.h> #include <stdio.h> #include <iomanip> #include<string> #include<stdlib.h> using namespace std; struct Ngay{ int date, month, year; }; struct SinhVien{ char maSV[8]; char tenSV[50]; int gioiTinh; Ngay ngaySinh; char lop[12]; char khoa[7]; char diachi[100]; }; struct Node{ SinhVien data; Node *link; }; struct List{ Node *first, *last; }; int NhapSinhVien(SinhVien &s){ cout<<"Ma so sinh vien:"; cin.getline(s.maSV,10); if(strcmp(s.maSV,"0")==0) return 0; cout<<"Ho va ten: "; cin.getline(s.tenSV,20); cout<<"Gioi tinh: "; cin>>s.gioiTinh; cout<<"Ngay sinh: "; cin>>s.ngaySinh.date>>s.ngaySinh.month>>s.ngaySinh.year; getchar(); cout<<"Dia chi: "; cin.clear(); cin.getline(s.diachi,20); cout<<"Lop: "; cin.getline(s.lop,8); cout<<"Khoa: "; cin.getline(s.khoa,8); return 1; } void Init(List &l){ l.first = l.last = NULL; } Node *getNode(SinhVien x){ Node *p; p = new Node; if( p == NULL) return NULL; p->data = x; p->link = NULL; } void AddLast(List &l, Node *new_ele){ if(l.first == NULL ){ l.first = l.last = new_ele; } else{ l.last->link = new_ele; l.last = new_ele; } } void InsertLast(List &l, SinhVien s){ Node *p = getNode(s); if( p == NULL) return; AddLast(l,p); } void NhapDSSV(List &l){ cout<<"\n Bat da nhap danh sach sinh vien. Nhap maSV = 0 de dung \n"; SinhVien s; int flag = NhapSinhVien(s); while(flag){ InsertLast(l,s); flag = NhapSinhVien(s); } cout<<"Ket thuc nhap DSSV\n"; } void XuatSinhVien(SinhVien s){ char gt[4]; if(s.gioiTinh == 0){ strcpy(gt, "Nam"); } else{ strcpy(gt, "Nu"); } cout << setw(10) <<left<< s.maSV ; cout << setw(20) <<left << s.tenSV ; cout << s.ngaySinh.date<<"/"<<s.ngaySinh.month<<"/"<<s.ngaySinh.year ; cout << setw(5) << gt; cout << setw(40) << s.diachi; cout << setw(8) << s.khoa; cout << setw(8) <<s.lop<<endl; //cout<<"\n%10s %20s %5d/%d/%d %5s %40s %9s %8s",s.maSV,s.tenSV,s.ngaySinh.date,s.ngaySinh.month,s.ngaySinh.year,gt,s.diachi,s.khoa,s.lop); } void XuatDSSV(List l){ Node *p = l.first; while(p){ XuatSinhVien(p->data); p = p->link; } } int Search(List l, char ma[]){ Node *p = l.first; while(p){ if(strcmp(p->data.maSV,ma)==0) return 1; p = p->link; } } int InsertLast_KhongTrung(List &l, SinhVien s){ if(Search(l,s.maSV)){ cout<<"Ma sinh vien trung"; return 0; } else{ InsertLast(l,s); return 1; } } int main(){ List l; SinhVien s; Init(l); NhapDSSV(l); XuatDSSV(l); InsertLast_KhongTrung(l,s); XuatDSSV(l); }
No description