hoangduc17t
30-04-2009, 10:09 PM
Tớ vừa viết một đoạn code để làm bài tập thực hành CTDL và GT, debug không xảy ra vấn đề gì nhưng mà lúc chạy thì lại có chuyện
// Danh_sach_lien_ket_don.cpp : Defines the entry point for the console application.
//khoi tao danh sach
//them 1 node vao danh sach
//them n node vao danh sach: nhap n, lap n: nhap x, them node x vao ds
//in danh sach
//Nhap x, tim node co khoa bang x
//nhap y: xoa node co khoa y
//In ra khoa lon nhat trong danh sach
//sap xep cac node trong danh sach theo thu tu tang dan cua khoa
//huy danh sach
#include "stdafx.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
struct date
{
int day;
int month;
int year;
};
struct data
{
char ten[30],msv[10],gioitinh[3],lop[6];
date ngsinh;
float diemtb;
};
struct NODE
{
data info;
NODE* pnext;
};
struct LIST
{
NODE* phead;
NODE* ptail;
};
void Init(LIST &l)
{
l.phead=NULL;
l.ptail=NULL;
}
NODE* getNode(data x)
{
NODE* p= new NODE;
if(p!=NULL)
{
p->info=x;
p->pnext=NULL;
}
return p;
}
void insertfirst(LIST &l, NODE* p)
{
if(l.phead==NULL)
l.phead=l.ptail=p;
else
{
p->pnext=l.phead;
l.phead=p;
}
}
NODE* addfirst(LIST &l, data x)
{
NODE* p= getNode(x);
if(p==NULL) return NULL;
insertfirst(l,p);
return p;
}
void insertlast(LIST &l, NODE* p)
{
if(l.phead==NULL)
l.phead=l.ptail=p;
else
{
l.ptail->pnext=p;
l.ptail=p;
}
}
NODE* addlast(LIST &l, data x)
{
NODE* p=getNode(x);
if(p==NULL) return NULL;
insertlast(l,p);
return p;
}
void insertafter(LIST &l, NODE* q, NODE* p)
{
if(l.phead==NULL||q==NULL) insertfirst(l,p);
else
{
p->pnext=q->pnext;
q->pnext=p;
if(q==l.ptail) l.ptail=p;
}
}
NODE* addafter(LIST &l, NODE* q, data x)
{
NODE* p=getNode(x);
if(p==NULL) return NULL;
insertafter(l,q,p);
return p;
}
void importdata(data &x)
{
printf("\nNhap ten(Nhan phim enter de ket thuc): ");
fflush(stdin);
gets(x.ten);
if(x.ten[0]='\0') return;
printf("\nNhap ngay sinh: ");
fflush(stdin);
scanf("%d-%d-%d",&x.ngsinh.day,&x.ngsinh.month,&x.ngsinh.year);
printf("\nNhap gioi tinh: ");
fflush(stdin);
gets(x.gioitinh);
printf("\nNhap ten lop: ");
fflush(stdin);
gets(x.lop);
printf("\nNhap ma sinh vien: ");
fflush(stdin);
gets(x.msv);
printf("\nNhap diem trung binh: ");
float temp;
scanf("%f",&temp);
x.diemtb=temp;
}
void insertn(LIST &l, int n)
{ NODE* p;
data x;
while(n>0)
{
for(int i=0;i<n;i)
{ printf("Nhap du lieu cho node thu %d",i+1);
importdata(x);
p=getNode(x);
addfirst(l,x);
}
break;
}
}
void datain(LIST &l)
{
data x;
while(1)
{ importdata(x);
if(addfirst(l,x)==NULL) break;
};
}
void dataout(data x)
{
printf("|%30s|%2d-%2d-%4d|%3s|%6s|%10s|%2.2f|",x.ten,x.ngsinh.day,x.ngsinh.month,x.ngsinh.year,x.gioitinh,x.lop,x.msv,x.diemtb);
}
void printlist(LIST l)
{
NODE* p;
p=l.phead;
if(l.phead)
{
printf("\ndanh sach rong");
return;
}
while(p)
{
dataout(p->info);
p=p->pnext;
}
}
NODE* searchnode(LIST l, char* MSV)
{
NODE* p=l.phead;
while(p!=NULL&&strcmp(p->info.msv,MSV)!=0) p=p->pnext;
return p;
}
NODE* searchbehind(LIST l, char* MSV)
{
NODE* q=NULL;
NODE* p=l.phead;
while(p!=NULL&&strcmp(p->info.msv,MSV)!=0)
{
q=p;
p=p->pnext;
}
return q;
}
int removenode(LIST &l, char* MSV)
{
NODE* p=l.phead;
NODE* q=NULL;
while(p!=NULL)
{
if(strcmp(p->info.msv,MSV)==0) break;
q=p;
p=p->pnext;
}
if(p==NULL) return 0; //khong co node can xoa
if(p==l.phead) l.phead=p->pnext;//p la node dau
if(p==l.ptail) l.ptail=q; //p la node cuoi
if(q!=NULL) q->pnext=p->pnext;
delete p;
return 1;
}
void removelist(LIST &l)
{
NODE* p=l.phead;
while(p!=NULL)
{
l.phead=l.phead->pnext;
delete p;
p=l.phead;
}
l.ptail=l.phead=NULL;
}
void menu()
{
printf("\nO: thoat khoi chuong trinh");
printf("\n1: khoi tao mot danh sach rong");
printf("\n2: them chi tiet mot sinh vien vao danh sach");
printf("\n3: them chi tiet n sinh vien vao danh sach");
printf("\n4: in chi tiet mot sinh vien ra");
printf("\n5: in danh sach ra");
printf("\n6: Tim sinh vien qua ma sinh vien");
printf("\n7: Xoa chi tiet sinh vien qua ma sinh vien");
printf("\n8: sap xep chi tiet cac sinh vien theo thu tu tang dan cua ma sinh vien");
printf("\n9: huy danh sach");
}
void main()
{
data x;
LIST l;
char* MSV;
NODE* p;
menu();
int s;
while(1)
{
printf("\nSelect an menu or out(0): ");
scanf("%d",&s);
switch(s)
{
case 0: return;
case 1: Init(l); break;
case 2:
{ importdata(x);
addfirst(l,x);
break;
}
case 3:
{
int n;
printf("\nNhap so luong sinh vien can nhap vao chi tiet: ");
scanf("%d",&n);
insertn(l,n);
break;
}
case 4:
{ fflush(stdin);
gets(MSV);
p=searchnode(l,MSV);
dataout(p->info);
break;
}
case 5: printlist(l); break;
case 6:
{
gets(MSV);
p=searchnode(l,MSV);
dataout(p->info);
break;
}
case 7:
{
gets(MSV);
if(removenode(l,MSV)==0) printf("\nStudent not found!");
else printf("\nDeleted");
break;
}
case 9:
{
removelist(l);
printf("\nDeleted!"); break;
}
}
}
}
Vấn đề của tớ là lúc tớ nhập thử data cho một sinh viên xong và thử chức năng số 4 để in data đó ra thì chỉ nhập được mã sinh viên xong rồi chẳng làm được gì nữa.
Cái mình nghi ngờ nhất ở đây là cái cách tớ nhập và bắt cái mã sinh viên để đưa vào hàm searchnode()....
Ai đó có thể giúp tớ gỡ cái mớ này ra được không? Tớ quả thưc không biết cách nhập MSV sao cho nó đúng :(
Còn một chuyện nữa là các bạn coi thử các hàm importdata với dataout của tớ bị làm sao mà tớ lúc in thử ra thì gặp hai vấn đề:
+Chỗ in tên chỉ in được 30 khoảng trắng
+Chỗ in giới tính thì lại lẫn thêm cả cái lớp vào :-/
Quả thật là khó hiểu, mong các bạn giúp đỡ tận tình :|
P/s: lúc mình chạy nó có thông báo lỗi thế này: Access Violation, mình Ok thì nó báo cái này: http://i665.photobucket.com/albums/vv15/hoangduc17t/Loi.jpg
// Danh_sach_lien_ket_don.cpp : Defines the entry point for the console application.
//khoi tao danh sach
//them 1 node vao danh sach
//them n node vao danh sach: nhap n, lap n: nhap x, them node x vao ds
//in danh sach
//Nhap x, tim node co khoa bang x
//nhap y: xoa node co khoa y
//In ra khoa lon nhat trong danh sach
//sap xep cac node trong danh sach theo thu tu tang dan cua khoa
//huy danh sach
#include "stdafx.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
struct date
{
int day;
int month;
int year;
};
struct data
{
char ten[30],msv[10],gioitinh[3],lop[6];
date ngsinh;
float diemtb;
};
struct NODE
{
data info;
NODE* pnext;
};
struct LIST
{
NODE* phead;
NODE* ptail;
};
void Init(LIST &l)
{
l.phead=NULL;
l.ptail=NULL;
}
NODE* getNode(data x)
{
NODE* p= new NODE;
if(p!=NULL)
{
p->info=x;
p->pnext=NULL;
}
return p;
}
void insertfirst(LIST &l, NODE* p)
{
if(l.phead==NULL)
l.phead=l.ptail=p;
else
{
p->pnext=l.phead;
l.phead=p;
}
}
NODE* addfirst(LIST &l, data x)
{
NODE* p= getNode(x);
if(p==NULL) return NULL;
insertfirst(l,p);
return p;
}
void insertlast(LIST &l, NODE* p)
{
if(l.phead==NULL)
l.phead=l.ptail=p;
else
{
l.ptail->pnext=p;
l.ptail=p;
}
}
NODE* addlast(LIST &l, data x)
{
NODE* p=getNode(x);
if(p==NULL) return NULL;
insertlast(l,p);
return p;
}
void insertafter(LIST &l, NODE* q, NODE* p)
{
if(l.phead==NULL||q==NULL) insertfirst(l,p);
else
{
p->pnext=q->pnext;
q->pnext=p;
if(q==l.ptail) l.ptail=p;
}
}
NODE* addafter(LIST &l, NODE* q, data x)
{
NODE* p=getNode(x);
if(p==NULL) return NULL;
insertafter(l,q,p);
return p;
}
void importdata(data &x)
{
printf("\nNhap ten(Nhan phim enter de ket thuc): ");
fflush(stdin);
gets(x.ten);
if(x.ten[0]='\0') return;
printf("\nNhap ngay sinh: ");
fflush(stdin);
scanf("%d-%d-%d",&x.ngsinh.day,&x.ngsinh.month,&x.ngsinh.year);
printf("\nNhap gioi tinh: ");
fflush(stdin);
gets(x.gioitinh);
printf("\nNhap ten lop: ");
fflush(stdin);
gets(x.lop);
printf("\nNhap ma sinh vien: ");
fflush(stdin);
gets(x.msv);
printf("\nNhap diem trung binh: ");
float temp;
scanf("%f",&temp);
x.diemtb=temp;
}
void insertn(LIST &l, int n)
{ NODE* p;
data x;
while(n>0)
{
for(int i=0;i<n;i)
{ printf("Nhap du lieu cho node thu %d",i+1);
importdata(x);
p=getNode(x);
addfirst(l,x);
}
break;
}
}
void datain(LIST &l)
{
data x;
while(1)
{ importdata(x);
if(addfirst(l,x)==NULL) break;
};
}
void dataout(data x)
{
printf("|%30s|%2d-%2d-%4d|%3s|%6s|%10s|%2.2f|",x.ten,x.ngsinh.day,x.ngsinh.month,x.ngsinh.year,x.gioitinh,x.lop,x.msv,x.diemtb);
}
void printlist(LIST l)
{
NODE* p;
p=l.phead;
if(l.phead)
{
printf("\ndanh sach rong");
return;
}
while(p)
{
dataout(p->info);
p=p->pnext;
}
}
NODE* searchnode(LIST l, char* MSV)
{
NODE* p=l.phead;
while(p!=NULL&&strcmp(p->info.msv,MSV)!=0) p=p->pnext;
return p;
}
NODE* searchbehind(LIST l, char* MSV)
{
NODE* q=NULL;
NODE* p=l.phead;
while(p!=NULL&&strcmp(p->info.msv,MSV)!=0)
{
q=p;
p=p->pnext;
}
return q;
}
int removenode(LIST &l, char* MSV)
{
NODE* p=l.phead;
NODE* q=NULL;
while(p!=NULL)
{
if(strcmp(p->info.msv,MSV)==0) break;
q=p;
p=p->pnext;
}
if(p==NULL) return 0; //khong co node can xoa
if(p==l.phead) l.phead=p->pnext;//p la node dau
if(p==l.ptail) l.ptail=q; //p la node cuoi
if(q!=NULL) q->pnext=p->pnext;
delete p;
return 1;
}
void removelist(LIST &l)
{
NODE* p=l.phead;
while(p!=NULL)
{
l.phead=l.phead->pnext;
delete p;
p=l.phead;
}
l.ptail=l.phead=NULL;
}
void menu()
{
printf("\nO: thoat khoi chuong trinh");
printf("\n1: khoi tao mot danh sach rong");
printf("\n2: them chi tiet mot sinh vien vao danh sach");
printf("\n3: them chi tiet n sinh vien vao danh sach");
printf("\n4: in chi tiet mot sinh vien ra");
printf("\n5: in danh sach ra");
printf("\n6: Tim sinh vien qua ma sinh vien");
printf("\n7: Xoa chi tiet sinh vien qua ma sinh vien");
printf("\n8: sap xep chi tiet cac sinh vien theo thu tu tang dan cua ma sinh vien");
printf("\n9: huy danh sach");
}
void main()
{
data x;
LIST l;
char* MSV;
NODE* p;
menu();
int s;
while(1)
{
printf("\nSelect an menu or out(0): ");
scanf("%d",&s);
switch(s)
{
case 0: return;
case 1: Init(l); break;
case 2:
{ importdata(x);
addfirst(l,x);
break;
}
case 3:
{
int n;
printf("\nNhap so luong sinh vien can nhap vao chi tiet: ");
scanf("%d",&n);
insertn(l,n);
break;
}
case 4:
{ fflush(stdin);
gets(MSV);
p=searchnode(l,MSV);
dataout(p->info);
break;
}
case 5: printlist(l); break;
case 6:
{
gets(MSV);
p=searchnode(l,MSV);
dataout(p->info);
break;
}
case 7:
{
gets(MSV);
if(removenode(l,MSV)==0) printf("\nStudent not found!");
else printf("\nDeleted");
break;
}
case 9:
{
removelist(l);
printf("\nDeleted!"); break;
}
}
}
}
Vấn đề của tớ là lúc tớ nhập thử data cho một sinh viên xong và thử chức năng số 4 để in data đó ra thì chỉ nhập được mã sinh viên xong rồi chẳng làm được gì nữa.
Cái mình nghi ngờ nhất ở đây là cái cách tớ nhập và bắt cái mã sinh viên để đưa vào hàm searchnode()....
Ai đó có thể giúp tớ gỡ cái mớ này ra được không? Tớ quả thưc không biết cách nhập MSV sao cho nó đúng :(
Còn một chuyện nữa là các bạn coi thử các hàm importdata với dataout của tớ bị làm sao mà tớ lúc in thử ra thì gặp hai vấn đề:
+Chỗ in tên chỉ in được 30 khoảng trắng
+Chỗ in giới tính thì lại lẫn thêm cả cái lớp vào :-/
Quả thật là khó hiểu, mong các bạn giúp đỡ tận tình :|
P/s: lúc mình chạy nó có thông báo lỗi thế này: Access Violation, mình Ok thì nó báo cái này: http://i665.photobucket.com/albums/vv15/hoangduc17t/Loi.jpg