em đang viết chương trình quản lý sinh viên nhưng em không biết viết hàm tìm kiếm tên sinh viên như thế nào mong anh chị giúp đỡ.
// quanlysinhvien.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"
typedef struct sv
{
char mssv[10];
char hoten[25];
char ngaysinh[10];
char diachi[20];
float Diem;
};
typedef struct cautruc
{
sv info;
struct cautruc*next;
}node;
typedef struct danhsach
{
node *head;
node *tail;
}list;
void khoitao(list &l)
{
l.head=l.tail=NULL;
}
sv * khoitaosinhvien()
{
sv * x = new sv;
float diem;
flushall();
printf("\n nhap vao ma so sinh vien ");
gets(x->mssv);
printf("\n nhap vao ho ten ");
gets(x->hoten);
printf("\n nhap vao ngay/thang/nam sinh ");
gets(x->ngaysinh);
printf("\n nhap dia chi ");
gets(x->diachi);
printf("\n nhap diem ");
scanf("%f",&diem);
x->Diem=diem;
return x;
}
node *taophantu(sv &pt)
{
node *p=new node;
if(p==NULL)
exit(1);
else
{
p->info=pt;
p->next=NULL;
}
return p;
}
void xuatds(list &l)
{
node *p=l.head;
printf("\nma so\t\t ho ten\t\t ngay sinh\t dia chi\t diem\n");
while(p!=NULL)
{
printf("%s",p->info.mssv);
printf("\t%s",p->info.hoten);
printf("\t%s",p->info.ngaysinh);
printf("\t%s",p->info.diachi);
printf("\t%.2f",p->info.Diem);
p=p->next;
printf("\n");
}
}
void themcuoi(list &l)
{
sv *t=khoitaosinhvien();
node *p=taophantu(*t);
if(l.head==NULL)
l.head=l.tail=p;
else
{
l.tail->next=p;
l.tail=p;
}
}
void themdau(list &l)
{
sv *t=khoitaosinhvien();
node *p=taophantu(*t);
if(l.head==NULL)
l.head=l.tail=p;
else
{
p->next=l.head;
l.head=p;
}
}
void main()
{
int n,a;
list l;
printf("\n nhap vao so sinh vien ");
scanf("%d",&n);
khoitao(l);
for(int i=1;i<=n;i++)
{
printf("\n nhap thong tin sinh vien thu %d ",i);
themcuoi(l);
}
printf("\n danh sach sinh vien vua nhap la ");
xuatds(l);
do
{
printf("\n 1-them dau danh sach ");
printf("\n 2-them cuoi danh sach ");
scanf("%d",&a);
switch(a)
{
case 1:
{
themdau(l);
xuatds(l);
break;
}
case 2:
{
themcuoi(l);
xuatds(l);
break;
}
}
}
while(a!=-1);
getch();
}
// quanlysinhvien.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"
typedef struct sv
{
char mssv[10];
char hoten[25];
char ngaysinh[10];
char diachi[20];
float Diem;
};
typedef struct cautruc
{
sv info;
struct cautruc*next;
}node;
typedef struct danhsach
{
node *head;
node *tail;
}list;
void khoitao(list &l)
{
l.head=l.tail=NULL;
}
sv * khoitaosinhvien()
{
sv * x = new sv;
float diem;
flushall();
printf("\n nhap vao ma so sinh vien ");
gets(x->mssv);
printf("\n nhap vao ho ten ");
gets(x->hoten);
printf("\n nhap vao ngay/thang/nam sinh ");
gets(x->ngaysinh);
printf("\n nhap dia chi ");
gets(x->diachi);
printf("\n nhap diem ");
scanf("%f",&diem);
x->Diem=diem;
return x;
}
node *taophantu(sv &pt)
{
node *p=new node;
if(p==NULL)
exit(1);
else
{
p->info=pt;
p->next=NULL;
}
return p;
}
void xuatds(list &l)
{
node *p=l.head;
printf("\nma so\t\t ho ten\t\t ngay sinh\t dia chi\t diem\n");
while(p!=NULL)
{
printf("%s",p->info.mssv);
printf("\t%s",p->info.hoten);
printf("\t%s",p->info.ngaysinh);
printf("\t%s",p->info.diachi);
printf("\t%.2f",p->info.Diem);
p=p->next;
printf("\n");
}
}
void themcuoi(list &l)
{
sv *t=khoitaosinhvien();
node *p=taophantu(*t);
if(l.head==NULL)
l.head=l.tail=p;
else
{
l.tail->next=p;
l.tail=p;
}
}
void themdau(list &l)
{
sv *t=khoitaosinhvien();
node *p=taophantu(*t);
if(l.head==NULL)
l.head=l.tail=p;
else
{
p->next=l.head;
l.head=p;
}
}
void main()
{
int n,a;
list l;
printf("\n nhap vao so sinh vien ");
scanf("%d",&n);
khoitao(l);
for(int i=1;i<=n;i++)
{
printf("\n nhap thong tin sinh vien thu %d ",i);
themcuoi(l);
}
printf("\n danh sach sinh vien vua nhap la ");
xuatds(l);
do
{
printf("\n 1-them dau danh sach ");
printf("\n 2-them cuoi danh sach ");
scanf("%d",&a);
switch(a)
{
case 1:
{
themdau(l);
xuatds(l);
break;
}
case 2:
{
themcuoi(l);
xuatds(l);
break;
}
}
}
while(a!=-1);
getch();
}
Comment