By GokiSoft.com| 08:57 28/12/2020|
Lập Trình C

[Share Code] Quản lý sách 2 -Lập trình C


Quản lý sách 2 - Lập trình C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct Book_ST {
	char ten[30];
	char theloai [30];
	int giatien;
	int namxuatban;
} Book;

void showMenu();
void inputData(Book *p);
void sort(Book *p);
void display(Book *p);
void statistic(Book *p);
void search(Book *p);
void saveFile(Book *p);

int main(int argc, char *argv[]) {
	Book bookList[3];
	int choose;
	
	do {
		showMenu();
		scanf("%d", &choose);
		
		switch(choose) {
			case 1:
				inputData(bookList);
				break;
			case 2:
				sort(bookList);
				display(bookList);
				statistic(bookList);
				break;
			case 3:
				search(bookList);
				break;
			case 4:
				saveFile(bookList);
				break;
			case 5:
				printf("\nGoodbye!!!");
				break;
			default:
				printf("\nNhap lai!!!");
				break;
		}
	} while(choose != 5);
	
	return 0;
}

void showMenu() {
	printf("\n1. Nhap du lieu");
	printf("\n2. Sap xep, thong ke, hien thi");
	printf("\n3. Tim kiem");
	printf("\n4. Luu File");
	printf("\n5. Thoat");
	printf("\nChon: ");
}

void inputData(Book *p) {
	int i;
	for(i=0;i<3;i++) {
		printf("\nNhap quyen sach thu %d:", i + 1);
		printf("\nTen: ");
		fflush(stdin);fflush(stdout);
		gets(p[i].ten);
		
		printf("\nThe loai: ");
		fflush(stdin);fflush(stdout);
		gets(p[i].theloai);
		
		printf("\nGia tien: ");
		fflush(stdin);fflush(stdout);
		scanf("%d", &p[i].giatien);
		
		printf("\nNam xuat ban: ");
		fflush(stdin);fflush(stdout);
		scanf("%d", &p[i].namxuatban);
	}
}

void sort(Book *p) {
	int i, j, cmp;
	for(i=0;i<2;i++) {
		for(j=i+1;j<3;j++) {
			cmp = strcmp(p[i].theloai, p[j].theloai);
			if(cmp < 0) {
				Book tmp = p[i];
				p[i] = p[j];
				p[j] = tmp;
			}
		}
	}
}

void display(Book *p) {
	int i;
	printf("\n%5s||%10s||%10s||%6s||%10s", "STT", "Ten", "The Loai", "Gia Tien", "Nam Xuat Ban");
	for(i=0;i<3;i++) {
		printf("\n%5d||%10s||%10s||%6d||%10d", i+1, p[i].ten, p[i].theloai, p[i].giatien, p[i].namxuatban);
	}
}

void statistic(Book *p) {
	//Thong ke so quyen sach theo tung nam
	//Mang p gom cac quan sach nhu sau: 2010, 2012, 2010, 2018, 2018, 2010
	//Ket qua: 2010: 3, 2012: 1, 2018: 2
	//Giai thuat
	//int count[6] = {1, 1, 1, 1, 1, 1} => don mang => {3, 1, 0, 2, 0, 0}
	//Cach giai quyet
	int count[3] = {1, 1, 1};//so phan tu cu the => neu so phan tu nhieu 10, 20, 1000 phan tu => for set du lieu
	//Thuc hien giai thuat don mang
	int i, j;
	for(i=0;i<2;i++) {
		if(count[i] == 0) {
			continue;//Ko thuc hien don tai phan tu gia tri = 0 => da dc su dung cho lan truoc roi.
		}
		
		for(j=i+1;j<3;j++) {
			if(count[j] == 0) continue;
			
			if(p[i].namxuatban == p[j].namxuatban) {
				count[i]++;
				count[j]--;
			}
		}
	}
	
	//Hien thi
	for(i=0;i<3;i++) {
		if(count[i] > 0) {
			printf("\nNam %d co %d quan sach", p[i].namxuatban, count[i]);
		}
	}
}

void search(Book *p) {
	int i, count = 0;
	char theloai[30];
	printf("\nNhap the loai: ");
	fflush(stdin);fflush(stdout);
	gets(theloai);
	
	for(i=0;i<3;i++) {
		int cmp = strcmp(p[i].theloai, theloai);
		
		if(cmp == 0) {
			//Tim thay
			if(count == 0) {
				//Hien thi header trong TH chua hien thi lan nao => tuong ung vs count = 0
				printf("\n%5s||%10s||%10s||%6s||%10s", "STT", "Ten", "The Loai", "Gia Tien", "Nam Xuat Ban");
			}
			count++;
			printf("\n%5d||%10s||%10s||%6d||%10d", i+1, p[i].ten, p[i].theloai, p[i].giatien, p[i].namxuatban);
		}
	}
	
	if(count == 0) {
		printf("\nKhong co quyen sach thuoc loai nay.");
	}
}

void saveFile(Book *p) {
	//Mo file
	FILE *fp = fopen("book.dat", "wb");
	
	//Luu file
	fwrite(p, sizeof(Book), 3, fp);
	
	//Dong file
	fclose(fp);
}




Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)