Cai Dat Danh Sach Lien Ket Don
// DSLKD.cpp : Defines the entry point for the console application.
//
//File:quanliSV.c
//Chuong trinh quan li sinh vien so cap
//Input: cac thong tin cua sinh vien nhap vao tu ban phim
//Ouput: in ra danh sach sinh vien va thuc hien mot so chuc nang
//------------------------------------------------------------------------------
//Class: Instructor:
//Assignment: Date assigned:
//Programmer: Date completed:
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
#include <iostream>
using namespace std;
/*dinh nghia cac gia tri ma nguoi dung co the chon*/
#define kiHieu (1,2,3,4,5,0)
//Cau truc cua mot sinh vien
typedef struct pp{
char ht[20];
char que[20];
char maSoSV[20];
struct pp *next;
}sinhVien;
//Ham nhap danh sach sinh vien
void nhapSV(sinhVien **pdau,int *n);
//Ham sua thong tin cua mot sinh vien trong danh sach
void suaSV(sinhVien *pdau,int *n);
//Ham them sinh vien vao trong danh sach
void themSV(sinhVien *pdau,int *n);
//Ham xoa sinh vien trong danh sach
void xoaSV(sinhVien *pdau,int *n);
//Ham tim kiem thong tin ve 1 sinh vien trong danh sach
void timSV(sinhVien *pdau,int *n);
//Ham in ra danh sach cac sinh vien duoc quan li trong danh sach
void inSV(sinhVien *pdau,int *n);
//Ham main
void main()
{
//Khai bao dia chi cua sinh vien dau tien
sinhVien *pdau = NULL;
int n;
int chon;
//Nhap danh sach sinh vien ban dau
nhapSV(&pdau,&n);
printf("HAY AN 1 PHIM CHUC NANG SAU:\n\
Phim 1 de sua thong tin cua mot sv trong danh sach\n\
Phim 2 de them 1 sv vao trong danh sach\n\
Phim 3 de xoa 1 sv trong danh sach\n\
Phim 4 de tim kiem 1 sv trong danh sach\n\
Phim 5 de in ra thong tin cac sv trong danh sach\n\
Phim 0 de thoat khoi chuong trinh\n");
printf("Chon: "); scanf("%d",&chon);
//Kiem tra so ma nguoi dung nhap vao co hop le hay khong
while(chon > 6|| chon < 0){
printf("So ban nhap khong hop le!\n Xin ban nhap lai: ");
scanf("%d",&chon);
}
//Xoa luong du lieu vao
fflush(stdin);
//Cac chuc nang tuong ung voi cac so
switch(chon){
case 0:
exit(0);
case 1:
suaSV(pdau,&n);
case 2:
themSV(pdau,&n);
case 3:
xoaSV(pdau,&n);
case 4:
timSV(pdau,&n);
case 5:
inSV(pdau,&n);
}
getch();
}
//Ham nhap danh sach sinh vien
void nhapSV(sinhVien **pdau,int *n)
{
sinhVien *p;
char ht[20];
//tao danh sach sinh vien
while(1){
//Nhap ten sinh vien roi kiem tra
printf("\n Hay nhap ten cua sinh vien: ");
//Kiem tra xem ten sinh vien co the them vao danh sach duoc hay khong
gets(ht);
if(ht[0]==0){
break;
}
//Kiem tra xem dau danh sach da co phan tu nao chua
//Neu chua co thi se tao ra phan tu dau tien
if(*pdau==NULL){
//Cap phat bo nho cho phan tu dau tien
*pdau=(sinhVien*)malloc(sizeof(sinhVien));
p=*pdau;
strcpy(p->ht,ht);
printf("\n Nhap que cua sinh vien: ");
gets(p->que);
printf("\n Ma so sinh vien: ");
gets(p->maSoSV);
p->next = NULL;
*n++;
}else{
//Thuc hien tao cac phan tu tiep theo cua danh sach
//Den khi nguoi dung khong nhap nua
p->next=(sinhVien*)malloc(sizeof(sinhVien));
p=p->next;
/*printf("Hay nhap ten cua sinh vien nay: ");
gets(ht);
if(ht==0){
break;
}*/
strcpy(p->ht,ht);
printf("Que quan: ");
gets(p->que);
printf("Ma so sinh vien: ");
gets(p->maSoSV);
p->next = NULL;
*n++;
}
}
return;
}
//Ham sua thong tin cua mot sinh vien
void suaSV(sinhVien *pdau,int *n)
{
//Con tro chua vi tri cua sinh vien dang kiem tra
sinhVien *p;
int kiemTra=1;
char ht[20];
char c;
printf("Hay nhap ten cua sinh vien can sua thong tin: ");
gets(ht);
while(1){
if(ht==0){
return;
}
if(strcmp(p->ht,ht)==0){
break;
}
if(p->next!=NULL){
p=p->next;
kiemTra=1;
}else{
kiemTra=0;
break;
}
}
if(kiemTra=0){
printf("Khong so sinh vien nao co ten nhu ban vua nhap!");
}else{
printf("Ban muon sau thong tin gi cua sinh vien nay:\n\
Chon 1 de sua ten\n\
Chon 2 de sua que quan\n\
Chon 3 de sua ma so sinh vien.");
c=getch();
while(c!=kiHieu){
printf("So ban nhap khong hop le!\n\
Xin hay nhap lai hoac an 0 de thoat");
}
while(c!='0'){
switch(c){
case 0:
exit(0);
case 1:
printf("Hay nhap ten moi ban muon sua:");
gets(p->ht);
case 2:
printf("Hay nhap que moi ban muon sua:");
gets(p->que);
case 3:
printf("Hay nhap ma so sinh vien moi:");
gets(p->maSoSV);
}
printf("Ban muon thuc hien sua thong tin gi nua khong\n\
Hay chon so tuong ung muon thuc hien hoac an so 0 de thoat");
c=getch();
}
}
}
//Ham them 1 sinh vien vao danh sach
void themSV(sinhVien *pdau,int *n)
{
sinhVien *p;
char ht[20];
printf("Ban hay nhap ten cua sinh vien muon them vao danh sach:");
gets(ht);
if(ht==0){
exit(0);
}
p=pdau;
while(p!=NULL){
p=p->next;
}
p->next=(sinhVien*)malloc(sizeof(sinhVien));
p=p->next;
strcpy(p->ht,ht);
printf("Nhap que cua sinh vien moi: ");
gets(p->que);
printf("Nhap ma so cua sinh vien moi: ");
gets(p->maSoSV);
}
//Ham xoa mot sinh vien trong danh sach
void xoaSV(sinhVien *pdau,int *n)
{
sinhVien *p=pdau,*luu;
char ht[20];
printf("Ban hay nhap ten cua sinh vien can xoa khoi danh sach: ");
gets(ht);
if(ht==0){
exit(0);
}
if(pdau==NULL){
printf("Danh sach khong co ai nen tim kiem khong thanh cong!");
exit(0);
}
while(p!=NULL){
if(strcmp(p->ht,ht)==0){
break;
}else{
if(p->next!=NULL){
luu=p;
p=p->next;
}else{
exit(0);
}
}
}
if(p->next==NULL){
luu->next=NULL;
}else{
luu->next=p->next;
}
}
//Ham tim kiem 1 sinh vien trong danh sach theo ten
void timSV(sinhVien *pdau,int *n)
{
sinhVien *p=pdau;
char ht[20];
printf("Nhap ten sinh vien ban muon tim trong danh sach: ");
gets(ht);
if(pdau==NULL){
printf("Danh sach chong nen tim kiem khong thuc hien!");
exit(0);
}
while(p!=NULL){
if(strcmp(p->ht,ht)==0){
break;
}else{
if(p->next!=NULL){
p=p->next;
}else{
printf("Khong co sinh vien nao ten nhu ban vua nhap!");
exit(0);
}
}
}
printf("Thong tin cua sinh vien ban vua nhap vao la: ");
printf("ten: %s \nQue quan: %s\nMa so sinh vien: %s",p->ht,p->que,p->maSoSV);
}
//Ham in ra danh sach cac sinh sien
void inSV(sinhVien *pdau,int *n)
{
sinhVien *p=pdau;
printf("%-30s%-30s%-30s\n","Ten sinh vien","Que quan","Ma so sinh vien");
while(p!=NULL){
printf("%-30s%-30s%-30s\n",p->ht,p->que,p->maSoSV);
p=p->next;
}
}
//End
0 nhận xét:
Đăng nhận xét