it4k53
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
struct TTSach{
int MaSach;
char TenSach[50];
char TacGia[35];
int NamXB, SoTrang;
} Sach[100];
//Khai bao DS lien ket
struct node{
TTSach data;
struct node *next;
};
struct node *head, *cur, *new_node;
int n = 0;
int i,j;
int KtraTrungMaSach(int MaSach, int soluongnhap)
{
int d;
for(d=0;d<soluongnhap; d++)
if(Sach[d].MaSach==MaSach)
return 1; //trung ma
return 0; //ko trung
}
//cau b
void NhapSach()
{
TTSach tmpS;
printf("So quyen sach nhap vao: ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Thong tin quyen sach thu %d:
", i+1);
printf("Ma sach: ");scanf("%d", &tmpS.MaSach);
if(KtraTrungMaSach(tmpS.MaSach, i+1))
{
printf("Trung ma sach !
");
i--; //Nhap lai quyen sach vua roi
continue;
}
fflush(stdin);
printf("Ten sach: ");gets(tmpS.TenSach);
fflush(stdin);
printf("Tac gia: ");gets(tmpS.TacGia);
printf("Nam xuat ban: ");scanf("%d", &tmpS.NamXB);
printf("So trang: ");scanf("%d", &tmpS.SoTrang);
//Kiem tra trung ma sach
Sach[i]=tmpS;
}
}
//cau c
void InSach()
{
for(i=0;i<n;i++)
{
printf("Thong tin quyen sach thu %d:
", i+1);
printf("- Ma sach: %d
", Sach[i].MaSach);
printf("- Ten sach: %s
", Sach[i].TenSach);
printf("- tac gia: %s
", Sach[i].TacGia);
printf("- Nam xuat ban: %d
", Sach[i].NamXB);
printf("- So trang: %d
", Sach[i].SoTrang);
}
}
//cau d
void SapXepSach()
{
TTSach tmp;
for(i=0;i<=n;i++)
for(j=i+1;j<n;j++)
{
if((Sach[i].NamXB>Sach[j].NamXB) || (Sach[i].NamXB==Sach[j].NamXB && Sach[i].SoTrang>Sach[j].SoTrang))
{
tmp=Sach[i];
Sach[i]=Sach[j];
Sach[j]=tmp;
}
}
}
//cau e
void TimKiem(char TenSach[50])
{
for(i=0;i<n;i++)
{
if(strcmp(Sach[i].TenSach, TenSach) == 0)
{
//In ra thong tin sach
printf("- Ma sach: %d
", Sach[i].MaSach);
printf("- Ten sach: %s
", Sach[i].TenSach);
printf("- tac gia: %s
", Sach[i].TacGia);
printf("- Nam xuat ban: %d
", Sach[i].NamXB);
printf("- So trang: %d
", Sach[i].SoTrang);
return;
}
}
//Neu duyet het khong co ket qua thong bao
printf("Khong co quyen sach nao nhu vay !");
}
//cau f
void ChuyenDSLienKet()
{
struct node *p;
//Khoi tao DS
head=NULL;
cur=head;
//Chuye ndu lieu tu mang sang danh sach
for(i=0;i<n;i++)
{
//Tao nut moi
new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=Sach[i];
new_node->next=NULL;
//Chen nut moi
if(head==NULL)
{
head=new_node;
cur=head;
}
else
{
new_node->next=cur->next;
cur->next=new_node;
cur=cur->next;
}
}
//Hien thi danh sach
p=head;
while(p!=NULL)
{
//In ra ket qua
printf("- Ma sach: %d
", p->data.MaSach);
printf("- Ten sach: %s
", p->data.TenSach);
p=p->next; //Tro p den phan tu tiep theo
}
//Giai phong bo nho
p=head;
while(p!=NULL)
{
head=head->next;
free(p);
p=head;
}
}
int main()
{
char TenSachTK[50];
NhapSach();
InSach();
printf("Thuc hien sap xep sach:
");
SapXepSach();
printf("Sau khi sap xep:
");
InSach();
fflush(stdin);
printf("Nhap ten sach muon tim: ");gets(TenSachTK);
TimKiem(TenSachTK);
printf("Chuyen sang DS lien ket don:
");
ChuyenDSLienKet();
return 0;
}
/*
============================================================================
Name : QuanLiThuVien.c
Author : nhtera
Version : 1.0
Copyright : copyright by nhtera
Description : Quan Li Thu Vien in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
// a. Khai bao thong tin ten sach
typedef struct {
int masach;
char tensach[50];
char tacgia[35];
int namxuatban;
int sotrang;
} SACH;
typedef struct pointer {
SACH info;
struct pointer *next;
} POINTER;
// b. Viet ham nhap n cuon sach luu vao mot mang sao cho khong cuon sach nao trung ma
void nhapsach(SACH[], int);
// c. Viet thong tin xuat thong tin mang ra man hinh
void hienthisach(SACH[], int);
// d. Viet ham sap xep sach theo thu tu nam xuat ban, neu cung nam thi sap xep theo thu tu tang dan cua so trang
void sapxepsach(SACH[], int);
// e. Viet ham tim kiem theo ten sach
void timkiem(SACH[], int);
// f. chuyen thong tin trong mang ra danh sach lien ket don
void chuyenradanhsachlienket(SACH[], int);
void hienthidanhsachlienket();
POINTER *sList;
int main() {
int n;
sList = NULL;
SACH sachs[100];
printf("Nhap so sach: ");
scanf("%d", &n);
nhapsach(sachs, n);
hienthisach(sachs, n);
sapxepsach(sachs, n);
printf("Sau khi sap xep
");
hienthisach(sachs, n);
timkiem(sachs, n);
chuyenradanhsachlienket(sachs, n);
printf("
Duyet danh sach lien ket
");
hienthidanhsachlienket();
return 0;
}
void nhapsach(SACH sachs[], int n) {
int i, j, ms, flag;
for (i = 0; i < n; i++) {
do {
flag = 0;
printf("
Nhap ma sach: ");
scanf("%d", &ms);
for (j = 0; j < i; j++) {
if (sachs[j].masach == ms) {
printf(
"
Ma sach %d da ton tai, ban hay nhap vao ma sach khac!!!",
ms);
flag = 1;
}
}
} while (flag == 1);
sachs[i].masach = ms;
fflush(stdin);
printf("
Nhap ten sach: ");
gets(sachs[i].tensach);
printf("
Nhap tac gia: ");
gets(sachs[i].tacgia);
printf("
Nhap nam xuat ban: ");
scanf("%d", &sachs[i].namxuatban);
printf("
Nhap so trang: ");
scanf("%d", &sachs[i].sotrang);
}
}
void hienthisach(SACH sachs[], int n) {
int i;
printf("Ma sach\tTen sach\ttac gia\tnam xuat ban\tso trang
");
for (i = 0; i < n; i++) {
printf(" %d \t %s \t %s \t %d \t %d
", sachs[i].masach,
sachs[i].tensach, sachs[i].tacgia, sachs[i].namxuatban,
sachs[i].sotrang);
}
}
void sapxepsach(SACH sachs[], int n) {
int i, j;
SACH temp;
// su dung thuat toan bubble sort - noi bot
for (i = n; i > 0; i--) {
for (j = 1; j < i; j++) {
if (sachs[j - 1].namxuatban > sachs[j].namxuatban) {
temp = sachs[j - 1];
sachs[j - 1] = sachs[j];
sachs[j] = temp;
} else if (sachs[j - 1].namxuatban == sachs[j].namxuatban) {
if (sachs[j - 1].sotrang > sachs[j].sotrang) {
temp = sachs[j - 1];
sachs[j - 1] = sachs[j];
sachs[j] = temp;
}
}
}
}
}
void timkiem(SACH sachs[], int n) {
int i, flag = 0;
char ts[50];
fflush(stdin);
printf("
Nhap ten sach can tim: ");
gets(ts);
for (i = 0; i < n; i++) {
if (strcmp(sachs[i].tensach, ts) == 0) {
printf(" %d \t %s \t %s \t %d \t %d
", sachs[i].masach,
sachs[i].tensach, sachs[i].tacgia, sachs[i].namxuatban,
sachs[i].sotrang);
flag = 1;
}
}
if (flag == 0) {
printf("
Khong tim thay cuon sach nao!!!");
}
}
void chuyenradanhsachlienket(SACH sachs[], int n) {
int i;
POINTER *p, *newItem;
for (i = 0; i < n; i++) {
newItem = (POINTER *) malloc(sizeof(POINTER));
newItem->info = sachs[i];
newItem->next = NULL;
if (sList == NULL) {
sList = newItem;
p = sList;
} else {
p->next = newItem;
p = p->next;
}
}
}
void hienthidanhsachlienket() {
POINTER *p;
p = sList;
while (p != NULL) {
printf(" %d \t %s \t %s \t %d \t %d
", p->info.masach,
p->info.tensach, p->info.tacgia, p->info.namxuatban,
p->info.sotrang);
p = p->next;
}
}
Bạn đang đọc truyện trên: AzTruyen.Top