By GokiSoft.com| 21:23 04/10/2023|
Lập Trình C

Quản lý tranh - quản lý gallery - Lập trình C

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

5

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

Nguyễn đình quân [T2008A]
Nguyễn đình quân

2020-11-02 06:40:34



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

typedef struct tranhtheu_st{
	char ten[30];
	int chieudai;
	int chieurong;
	char tacgia[30];
	int giaban;
} tranhTheu;

void showMenu();
void inputData(tranhTheu *p);
void sortData(tranhTheu *p);
void displayData(tranhTheu *p);
int dienTich(int a, int b);
void findPrice(tranhTheu *p);
void ghiFile(tranhTheu *p);

int main(){
	tranhTheu dsTranh[3];
	int select;
	
	do{
		showMenu();
		printf("\nNhap lua chon: ");
		scanf("%d", &select);
		
		switch(select){
			case 1:{
				inputData(dsTranh);
				break;
			}
			
			case 2:{
				sortData(dsTranh);
				displayData(dsTranh);
				break;
			}
			
			case 3:{
				findPrice(dsTranh);
				break;
			}
			
			case 4:{
				ghiFile(dsTranh);
				printf("\nDa luu thanh cong!!\n");
				break;
			}
			
			case 5:{
				printf("\nThoat!!\n");
				break;
			}
			
			default:{
				printf("\nLoi cu phap, vui long thu lai!!\n");
				break;
			}
		}
	}
	while(select != 5);
	return 0;
}

void showMenu(){
	printf("\n1.Nhap danh sach cac buc tranh.");
	printf("\n2.Sap xep, thong ke va hien thi thong tin chi tiet cua tung buc tranh theo dien tich giam dan.");
	printf("\n3.Tim buc tranh co theo gia ban.");
	printf("\n4.Ghi vao tap tin nhi phan tranhtheu.dat.");
	printf("\n5.Thoat.");
}

void inputData(tranhTheu *p){
	int i;
	for(i=0;i<3;i++){
		printf("\nNhap thong tin cho buc tranh so %d", i+1);
		
		printf("\nTen: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].ten);
		
		printf("\nTac gia: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].tacgia);
		
		printf("\nChieu dai: ");
		scanf("%d", &p[i].chieudai);
		
		printf("\nChieu rong: ");
		scanf("%d", &p[i].chieurong);
		
		printf("\nGia ban (USD): ");
		scanf("%d", &p[i].giaban);	
	}
}

int dienTich(int a, int b){
	return a*b;
}

void sortData(tranhTheu *p){
	int i, j;

	for(i=0;i<2;i++){
		for(j=0;j<3;j++){
			int a = dienTich(p[i].chieudai, p[i].chieurong);
			int b = dienTich(p[j].chieudai, p[j].chieurong);
			
			if(a>b){
				int tmp = a;
				a = b;
				b = tmp;
			}
		}
	}
}

void displayData(tranhTheu *p){
	int i;
	
	//Hien Thi
	for(i=0;i<100;i++){
		printf("=");
	}
	printf("\nDanh sach tranh");
	printf("\n");
	for(i=0;i<100;i++){
		printf("=");
	}
	printf("\n%-5s||%-30s||%-30s||%-5s||%-5s||%-15s||%-15s","STT","Ten","Tac gia","Dai","Rong","Gia","Kho");

	char kho[100];
		
	for(i=0;i<3;i++){
		int s = dienTich(p[i].chieudai, p[i].chieurong);
		if(s>=10000){
			strcpy(kho,"Lon");
		}
		else if(s>=2000){
			strcpy(kho,"Trung binh");
		}
		else if(s<2000){
			strcpy(kho,"Nho");
		}
		printf("\n00%d  ||%-30s||%-30s||%-5d||%-5d||%-15d||%-10s",i+1,p[i].ten,p[i].tacgia,p[i].chieudai,p[i].chieurong,p[i].giaban,kho);
	}
	printf("\n");
	for(i=0;i<100;i++){
		printf("=");
	}
	
	//Thong ke
	printf("\nThong ke: ");
	
	int count=0;
	for(i=0;i<3;i++){
		int s = dienTich(p[i].chieudai, p[i].chieurong);
		if(s>=10000){
			count++;
		}
	}
	if(count!=0){
		printf("\n+ Kho lon co %d buc tranh",count);
	}

	count=0;
	for(i=0;i<3;i++){
		int s = dienTich(p[i].chieudai, p[i].chieurong);
		if(s<10000 && s>=2000){
			count++;
		}
	}
	if(count!=0){	
		printf("\n+ Kho trung binh co %d buc tranh",count);
	}
	
	count=0;
	for(i=0;i<3;i++){
		int s = dienTich(p[i].chieudai, p[i].chieurong);
		if(s<2000){
			count++;
		}
	}
	if(count!=0){
		printf("\n+ Kho nho co %d buc tranh",count);
	}

	printf("\n");
}

void findPrice(tranhTheu *p){
	int i,a;
	int count=0;	
	char kho[100];
		
	printf("\nNhap gia ban toi da: ");
	scanf("%d",&a);
	
	for(i=0;i<3;i++){
		if(p[i].giaban<=a){
			count++;
		}
	}
	
	if(count!=0){
		printf("\n");
		for(i=0;i<100;i++){
		   printf("=");
	    }  
	    printf("\nDanh sach tranh");
	    printf("\n");
	    for(i=0;i<100;i++){
		   printf("=");
	    }
	    printf("\n%-5s||%-30s||%-30s||%-5s||%-5s||%-15s||%-15s","STT","Ten","Tac gia","Dai","Rong","Gia","Kho");
	}
	
	for(i=0;i<3;i++){
		if(p[i].giaban<=a){
			int s = dienTich(p[i].chieudai, p[i].chieurong);
			if(s>=10000){
				strcpy(kho,"Lon");
			}
			else if(s>=2000){
				strcpy(kho,"Trung binh");
			}
			else if(s<2000){
				strcpy(kho,"Nho");
			}
			printf("\n00%d  ||%-30s||%-30s||%-5d||%-5d||%-15d||%-10s",i+1,p[i].ten,p[i].tacgia,p[i].chieudai,p[i].chieurong,p[i].giaban,kho);
		}
	}
	
	count=0;
	for(i=0;i<3;i++){
		if(p[i].giaban<=a){
			count++;
		}
	}
	
	if(count!=0){
		printf("\n");
		for(i=0;i<100;i++){
		   printf("=");
	    }  
    }
    
    if(count==0){
    	printf("\nKhong co buc tranh nao ban voi gia nay");
	}
	printf("\n");
}

void ghiFile(tranhTheu *p){
	FILE *fp;
	fp=fopen("tranhtheu.dat","wb");
	fwrite(p, sizeof(tranhTheu), 3, fp);
	fclose(fp);
}



Trung Hiếu [community]
Trung Hiếu

2020-10-31 12:31:19

C2002L

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

typedef struct tranhtheu_st{
	char ten[30];
	int chieudai;
	int chieurong;
	char tacgia[30];
	int giaban;
} tranhTheu;

void showMenu();
void inputData(tranhTheu *p);
void sortData(tranhTheu *p);
void displayData(tranhTheu *p);
int dienTich(int a, int b);
void findPrice(tranhTheu *p);
void ghiFile(tranhTheu *p);

int main(){
	tranhTheu dsTranh[3];
	int select;
	
	do{
		showMenu();
		printf("\nNhap lua chon: ");
		scanf("%d", &select);
		
		switch(select){
			case 1:{
				inputData(dsTranh);
				break;
			}
			
			case 2:{
				sortData(dsTranh);
				displayData(dsTranh);
				break;
			}
			
			case 3:{
				findPrice(dsTranh);
				break;
			}
			
			case 4:{
				ghiFile(dsTranh);
				printf("\nDa luu thanh cong!!\n");
				break;
			}
			
			case 5:{
				printf("\nThoat!!\n");
				break;
			}
			
			default:{
				printf("\nLoi cu phap, vui long thu lai!!\n");
				break;
			}
		}
	}
	while(select != 5);
	return 0;
}

void showMenu(){
	printf("\n1.Nhap danh sach cac buc tranh.");
	printf("\n2.Sap xep, thong ke va hien thi thong tin chi tiet cua tung buc tranh theo dien tich giam dan.");
	printf("\n3.Tim buc tranh co theo gia ban.");
	printf("\n4.Ghi vao tap tin nhi phan tranhtheu.dat.");
	printf("\n5.Thoat.");
}

void inputData(tranhTheu *p){
	int i;
	for(i=0;i<3;i++){
		printf("\nNhap thong tin cho buc tranh so %d", i+1);
		
		printf("\nTen: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].ten);
		
		printf("\nTac gia: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].tacgia);
		
		printf("\nChieu dai: ");
		scanf("%d", &p[i].chieudai);
		
		printf("\nChieu rong: ");
		scanf("%d", &p[i].chieurong);
		
		printf("\nGia ban (USD): ");
		scanf("%d", &p[i].giaban);	
	}
}

int dienTich(int a, int b){
	return a*b;
}

void sortData(tranhTheu *p){
	int i, j;

	for(i=0;i<2;i++){
		for(j=0;j<3;j++){
			int a = dienTich(p[i].chieudai, p[i].chieurong);
			int b = dienTich(p[j].chieudai, p[j].chieurong);
			
			if(a>b){
				int tmp = a;
				a = b;
				b = tmp;
			}
		}
	}
}

void displayData(tranhTheu *p){
	int i;
	
	//Hien Thi
	for(i=0;i<100;i++){
		printf("=");
	}
	printf("\nDanh sach tranh");
	printf("\n");
	for(i=0;i<100;i++){
		printf("=");
	}
	printf("\n%-5s||%-30s||%-30s||%-5s||%-5s||%-15s||%-15s","STT","Ten","Tac gia","Dai","Rong","Gia","Kho");

	char kho[100];
		
	for(i=0;i<3;i++){
		int s = dienTich(p[i].chieudai, p[i].chieurong);
		if(s>=10000){
			strcpy(kho,"Lon");
		}
		else if(s>=2000){
			strcpy(kho,"Trung binh");
		}
		else if(s<2000){
			strcpy(kho,"Nho");
		}
		printf("\n00%d  ||%-30s||%-30s||%-5d||%-5d||%-15d||%-10s",i+1,p[i].ten,p[i].tacgia,p[i].chieudai,p[i].chieurong,p[i].giaban,kho);
	}
	printf("\n");
	for(i=0;i<100;i++){
		printf("=");
	}
	
	//Thong ke
	printf("\nThong ke: ");
	
	int count=0;
	for(i=0;i<3;i++){
		int s = dienTich(p[i].chieudai, p[i].chieurong);
		if(s>=10000){
			count++;
		}
	}
	if(count!=0){
		printf("\n+ Kho lon co %d buc tranh",count);
	}

	count=0;
	for(i=0;i<3;i++){
		int s = dienTich(p[i].chieudai, p[i].chieurong);
		if(s<10000 && s>=2000){
			count++;
		}
	}
	if(count!=0){	
		printf("\n+ Kho trung binh co %d buc tranh",count);
	}
	
	count=0;
	for(i=0;i<3;i++){
		int s = dienTich(p[i].chieudai, p[i].chieurong);
		if(s<2000){
			count++;
		}
	}
	if(count!=0){
		printf("\n+ Kho nho co %d buc tranh",count);
	}

	printf("\n");
}

void findPrice(tranhTheu *p){
	int i,a;
	int count=0;	
	char kho[100];
		
	printf("\nNhap gia ban toi da: ");
	scanf("%d",&a);
	
	for(i=0;i<3;i++){
		if(p[i].giaban<=a){
			count++;
		}
	}
	
	if(count!=0){
		printf("\n");
		for(i=0;i<100;i++){
		   printf("=");
	    }  
	    printf("\nDanh sach tranh");
	    printf("\n");
	    for(i=0;i<100;i++){
		   printf("=");
	    }
	    printf("\n%-5s||%-30s||%-30s||%-5s||%-5s||%-15s||%-15s","STT","Ten","Tac gia","Dai","Rong","Gia","Kho");
	}
	
	for(i=0;i<3;i++){
		if(p[i].giaban<=a){
			int s = dienTich(p[i].chieudai, p[i].chieurong);
			if(s>=10000){
				strcpy(kho,"Lon");
			}
			else if(s>=2000){
				strcpy(kho,"Trung binh");
			}
			else if(s<2000){
				strcpy(kho,"Nho");
			}
			printf("\n00%d  ||%-30s||%-30s||%-5d||%-5d||%-15d||%-10s",i+1,p[i].ten,p[i].tacgia,p[i].chieudai,p[i].chieurong,p[i].giaban,kho);
		}
	}
	
	count=0;
	for(i=0;i<3;i++){
		if(p[i].giaban<=a){
			count++;
		}
	}
	
	if(count!=0){
		printf("\n");
		for(i=0;i<100;i++){
		   printf("=");
	    }  
    }
    
    if(count==0){
    	printf("\nKhong co buc tranh nao ban voi gia nay");
	}
	printf("\n");
}

void ghiFile(tranhTheu *p){
	FILE *fp;
	fp=fopen("tranhtheu.dat","wb");
	fwrite(p, sizeof(tranhTheu), 3, fp);
	fclose(fp);
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2020-10-03 01:58:46



//Assignment Quan li tranh(checked)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct tranhtheu{
	char ten[30],tacgia[30];
	int chieudai, chieurong, giaban;
}Tranh;

void Menu(){
	printf("\n1.Nhap danh sach cac buc tranh.");
	printf("\n2.Sap xep, thong ke va hien thi thong tin chi tiet cua tung buc tranh theo dien tich giam dan.");
	printf("\n3.Tim buc tranh co theo gia ban.");
	printf("\n4.Ghi vao tap tin nhi phan tranhtheu.dat.");
	printf("\n5.Thoat.");
}

void nhaptranh(Tranh *p){
	int i;
	for(i=0;i<3;i++){
		fflush(stdin);fflush(stdout);
		printf("\nNhap thong tin cho buc tranh thu %d: ",i+1);
		fflush(stdin);fflush(stdout);
		printf("\nTen: ");
		gets(p[i].ten);
		fflush(stdin);fflush(stdout);
		printf("\nTac gia: ");
		gets(p[i].tacgia);
		fflush(stdin);fflush(stdout);
		printf("\nChieu dai: ");
		scanf("%d",&p[i].chieudai);
		fflush(stdin);fflush(stdout);
		printf("\nChieu rong: ");
		scanf("%d",&p[i].chieurong);
		fflush(stdin);fflush(stdout);
		printf("\nGia ban (USD) : ");
		scanf("%d",&p[i].giaban);
		fflush(stdin);fflush(stdout);
	}
}

void sapxeptranh(Tranh *p){
	int i,j;
	for(i=0;i<2;i++){
		for(j=i+1;j<3;j++){
			if(p[i].chieudai*p[i].chieurong<p[j].chieudai*p[j].chieurong){
				Tranh temp=p[i];
				p[i]=p[j];
				p[j]=temp;
			}
		}
	}
}

void hienthitranh(Tranh *p){
	int i;
	for(i=0;i<117;i++){
		printf("=");
	}
	printf("\nDanh sach tranh");
	printf("\n");
	for(i=0;i<117;i++){
		printf("=");
	}
	printf("\n%-5s||%-30s||%-30s||%-5s||%-5s||%-15s||%-15s","STT","Ten","Tac gia","Dai","Rong","Gia","Kho");
	for(i=0;i<3;i++){
		if(p[i].chieudai*p[i].chieurong>=10000){
			printf("\n00%d  ||%-30s||%-30s||%-5d||%-5d||%-15d||Lon            ",i+1,p[i].ten,p[i].tacgia,p[i].chieudai,p[i].chieurong,p[i].giaban);
		}
		else if(p[i].chieudai*p[i].chieurong>=2000){
			printf("\n00%d  ||%-30s||%-30s||%-5d||%-5d||%-15d||Trung binh     ",i+1,p[i].ten,p[i].tacgia,p[i].chieudai,p[i].chieurong,p[i].giaban);
		}
		else if(p[i].chieudai*p[i].chieurong<2000){
			printf("\n00%d  ||%-30s||%-30s||%-5d||%-5d||%-15d||Nho            ",i+1,p[i].ten,p[i].tacgia,p[i].chieudai,p[i].chieurong,p[i].giaban);
		}
	}
	printf("\n");
	for(i=0;i<117;i++){
		printf("=");
	}
	printf("\nThong ke:");
	int count=0;
	for(i=0;i<3;i++){
		if(p[i].chieudai*p[i].chieurong>=10000){
			count++;
		}
	}
	if(count!=0)
	printf("\n+ Kho lon co %d buc tranh",count);
	count=0;
	for(i=0;i<3;i++){
		if(p[i].chieudai*p[i].chieurong<10000 && p[i].chieudai*p[i].chieurong>=2000){
			count++;
		}
	}
	if(count!=0)
	printf("\n+ Kho trung binh co %d buc tranh",count);
	count=0;
	for(i=0;i<3;i++){
		if(p[i].chieudai*p[i].chieurong<2000){
			count++;
		}
	}
	if(count!=0)
	printf("\n+ Kho nho co %d buc tranh",count);
	printf("\n");
}

void timtranh(Tranh *p){
	int i,a;
	int count=0;
	fflush(stdin);fflush(stdout);
	printf("\nNhap gia ban toi da: ");
	scanf("%d",&a);
	fflush(stdin);fflush(stdout);
	for(i=0;i<3;i++){
		if(p[i].giaban<=a){
			count++;
		}
	}
	if(count!=0){
		printf("\n");
		for(i=0;i<117;i++){
		   printf("=");
	    }  
	    printf("\nDanh sach tranh");
	    printf("\n");
	    for(i=0;i<117;i++){
		   printf("=");
	    }
	    printf("\n%-5s||%-30s||%-30s||%-5s||%-5s||%-15s||%-15s","STT","Ten","Tac gia","Dai","Rong","Gia","Kho");
	}
	for(i=0;i<3;i++){
		if(p[i].giaban<=a){
			if(p[i].chieudai*p[i].chieurong>=10000){
			    printf("\n00%d  ||%-30s||%-30s||%-5d||%-5d||%-15d||Lon            ",i+1,p[i].ten,p[i].tacgia,p[i].chieudai,p[i].chieurong,p[i].giaban);
		    }
		    else if(p[i].chieudai*p[i].chieurong>=2000){
			    printf("\n00%d  ||%-30s||%-30s||%-5d||%-5d||%-15d||Trung binh     ",i+1,p[i].ten,p[i].tacgia,p[i].chieudai,p[i].chieurong,p[i].giaban);
		    }
		    else if(p[i].chieudai*p[i].chieurong<2000){
			    printf("\n00%d  ||%-30s||%-30s||%-5d||%-5d||%-15d||Nho            ",i+1,p[i].ten,p[i].tacgia,p[i].chieudai,p[i].chieurong,p[i].giaban);
		    }
		}
	}
	count=0;
	for(i=0;i<3;i++){
		if(p[i].giaban<=a){
			count++;
		}
	}
	if(count!=0){
		printf("\n");
		for(i=0;i<117;i++){
		   printf("=");
	    }  
    }
    if(count==0){
    	printf("\nKhong co buc tranh nao ban voi gia nay");
	}
	printf("\n");
}

void ghifile(Tranh *p){
	FILE *fp;
	fp=fopen("tranhtheu.dat","wb");
	fwrite(p, sizeof(Tranh), 3, fp);
	fclose(fp);
}

int main(){
	Tranh dsTranh[3];
	int choose;
	while(1){
		Menu();
		printf("\nLua chon chuong trinh: ");
		scanf("%d",&choose);
		switch(choose){
			case 1:
				nhaptranh(dsTranh);
			break;
			case 2:
				sapxeptranh(dsTranh);
				hienthitranh(dsTranh);
			break;
			case 3:
				timtranh(dsTranh);
			break;
			case 4:
				ghifile(dsTranh);
				printf("\nDa luu thanh cong!!\n");
			break;
			case 5:
				printf("\nTam biet!!\n");
				exit(0);
			break;
			default:
				printf("\nLoi cu phap, vui long thu lai!!\n");
			break;	
		}
	}
}



Đặng Trần Nhật Minh [T2008A]
Đặng Trần Nhật Minh

2020-10-02 09:56:45



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

int n = 3;

typedef struct tranhtheu {
	
	char ten[30], tacgia[30], kho[15];
	int chieudai, chieurong, giaban;
	
} tranh;

void swap(tranh &a, tranh &b) {
	
	tranh temp = a;
	a = b;
	b = temp;
	
}

void inputTranh(tranh *a, int n) {
	
	for (int i = 0; i < n; i++) {
		
		printf("\nNhap thong tin cho buc tranh so %d:", i + 1);
					
		printf("\nTen: ");
		fflush(stdin); fflush(stdout);
		gets(a[i].ten);
					
		printf("\nTac gia: ");
		fflush(stdin); fflush(stdout);
		gets(a[i].tacgia);
					
		printf("\nChieu dai: ");
		fflush(stdin); fflush(stdout);
		scanf("%d", &a[i].chieudai);
		
		printf("\nChieu rong: ");
		fflush(stdin); fflush(stdout);
		scanf("%d", &a[i].chieurong);
		
		printf("\nGia ban (USD): ");
		fflush(stdin); fflush(stdout);
		scanf("%d", &a[i].giaban);
								
	}
	
}

void sortTranh (tranh *a, int n) {
	
	int index_MAX, cL = 0, cTB = 0, cN = 0;
	
	for (int i = 0; i < n; i++) {
		
		if ((a[i].chieudai * a[i].chieurong) >= 10000) strcpy(a[i].kho, "LON"), cL++;
		else if ((a[i].chieudai * a[i].chieurong) >= 2000) strcpy(a[i].kho, "TRUNG BINH"), cTB++;
		else if ((a[i].chieudai * a[i].chieurong) < 2000) strcpy(a[i].kho, "NHO"), cN++;
		
	}
	
	for (int i = 0; i < n - 1; i++) {
					
		index_MAX = i;
				
		for (int j = i + 1; j < n; j++)
			if ((a[index_MAX].chieudai * a[index_MAX].chieurong) < (a[j].chieudai * a[j].chieurong)) index_MAX = j;
				
		swap(a[index_MAX], a[i]);
					
	}
	
	printf("\n===============================================================================================");
	printf("\n|STT|Ten                           |Tac Gia             |Dai  |Rong |Gia         |Kho          |"); //|3|30|20|5|5|12|13
	printf("\n===============================================================================================");
	
	for (int i = 0; i < n; i++)
		printf("\n|00%d|%-30s|%-20s|%-5d|%-5d|%-12d|%-13s|", i + 1, a[i].ten, a[i].tacgia, a[i].chieudai, a[i].chieurong, a[i].giaban, a[i].kho);
		
	printf("\n===============================================================================================");
	
	printf("\nThong Ke: ");
	
	if (cL > 0) printf("\n+ Kho Lon co %d buc tranh.", cL);
	if (cTB > 0) printf("\n+ Kho Trung Binh co %d buc tranh.", cTB);
	if (cN > 0) printf("\n+ Kho Nho co %d buc tranh.", cN);
	
}

void searchTranh (tranh *a, int n) {
	
	printf("\nNhap gia ban toi da: ");
	fflush(stdin); fflush(stdout);
	int s, stt = 0; scanf("%d", &s);
	
	bool check = false;
	
	for (int i = 0; i < n; i++) 
		if (a[i].giaban <= s) {
			
			printf("\n===============================================================================================");
			printf("\n|STT|Ten                           |Tac Gia             |Dai  |Rong |Gia         |Kho          |"); //|3|30|20|5|5|12|13
			printf("\n===============================================================================================");
			printf("\n|00%d|%-30s|%-20s|%-5d|%-5d|%-12d|%-13|", stt + 1, a[i].ten, a[i].tacgia, a[i].chieudai, a[i].chieurong, a[i].giaban, a[i].kho);
			printf("\n===============================================================================================");
			check = true;
			
		}
		
	if (!check) printf("\nKhong co buc tranh nao nhu vay.");
	
}

void outputTranh(tranh *a, int n) {
	
	FILE *fp;
	
	fp = fopen("tranhtheu.dat", "wb");
	
	fwrite(a, sizeof(tranh), n, fp);
	
	fclose(fp);
	
}

int main() {

	tranh dsTranh[n];
	
	int sel;
	
	while (true) {
		
		printf("\n==========Menu=========");
		printf("\n1. Nhap danh sach cac buc tranh.");
		printf("\n2. Sap xep, thong ke va hien thi thong tin chi tiet cua tung buc tranh theo dien tich giam dan.");
		printf("\n3. Tim buc tranh co theo gia ban.");
		printf("\n4. Ghi vao tap tin nhi phan tranhtheu.dat.");
		printf("\n5. Thoat.");	
		printf("\n========================");
		printf("\nNhap lua chon cua ban: "); 
		fflush(stdin); fflush(stdout);
		scanf("%d", &sel);
		
		switch (sel) {
			
			case 1:
				
				inputTranh(dsTranh, n);
				
				break;
				
			case 2:
				
				sortTranh(dsTranh, n);
				
				break;
				
			case 3:
				
				searchTranh(dsTranh, n);
				
				break;
				
			case 4:
				
				outputTranh(dsTranh, n);
				
				break;
				
			case 5:
				
				printf("EXIT!");
				
				exit(0);
				
			default:
				
				printf("\nNHAP SAI!");
				
				break;
			
		}
		
	}
	
	return 0;

}




Nguyễn Hữu Hiếu [T2008A]
Nguyễn Hữu Hiếu

2020-10-02 03:59:56



#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 tranhtheuST {
	char ten[30], tacgia[30], kho[20];
	int chieudai, chieurong, giaban;
} tranhtheu;

void showMenu();
void input(tranhtheu *p);
void sapxep(tranhtheu *p);
void hienthi(tranhtheu *p);
char *laykhotranh(tranhtheu t);
void thongke(tranhtheu *p);
void timtranh(tranhtheu *p);
void ghidulieu(tranhtheu *p);

int main(int argc, char *argv[]) {
	
	tranhtheu dsTranh[3];
	int choose;
	FILE *fptr;
	
	do {
		showMenu();
		scanf("%d", &choose);
		switch(choose) {
			case 1:
				input(dsTranh);
				
				break;
			case 2:			
				sapxep(dsTranh);
				hienthi(dsTranh);
				thongke(dsTranh);
						
				break;
			case 3:
				timtranh(dsTranh);
				break;
			case 4:
				
   				ghidulieu(dsTranh);
				break;
			case 5:
				printf("\nGood bye!!!");
				break;
			default:
				printf("\nNhap sai!!!");
			
			
		}
		
		
	} while(choose != 5);
	
	
	
	return 0;
}

void showMenu() {
	printf("\n1. Nhap danh sach cac buc tranh");
	printf("\n2. Sap xep, thong ke, hien thi tranh");
	printf("\n3. Tim buc tranh theo gia ban");
	printf("\n4. Luu file");
	printf("\n5. Thoat");
	printf("\nChon choose: ");
}

void input(tranhtheu *p) {
	int i;
	for(i=0;i<3;i++) {
		printf("\nNhap thong tin buc tranh thu %d", i+1);
		printf("\nNhap ten: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].ten);
		
		printf("\nNhap tac gia: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].tacgia);
		
		printf("\nNhap chieu dai: ");
		fflush(stdin); fflush(stdout);
		scanf("%d", &p[i].chieudai);
		
		printf("\nNhap chieu rong: ");
		fflush(stdin); fflush(stdout);
		scanf("%d", &p[i].chieurong);
		
		printf("Nhap gia ban: ");
		fflush(stdin); fflush(stdout);
		scanf("%d", &p[i].giaban);
		

		
	}
}

void sapxep(tranhtheu *p) {
	int i, j, d1, d2;
	tranhtheu temp;
	for(i=0;i<2;i++) {
		d1 = p[i].chieudai * p[i].chieurong;
		for(j=1;j<3;j++) {
			d2 = p[j].chieudai * p[j].chieurong;
			if(d1 < d2) {
				temp = p[i];
				p[i] = p[j];
				p[j] = temp;				
				
			}
		}
	}
}

char *laykhotranh(tranhtheu t) {
	int dientichtranh = t.chieudai * t.chieurong;
	if (dientichtranh >= 10000) {
		return "Lon";
	} else if (dientichtranh >=2000) {
		return "TB";
	}
	else {
		return "Nho";
	}
}

void hienthi(tranhtheu *p) {
	int i, limit;
	limit = 70;
	for(i=0;i<limit;i++) {
		printf("=");
	}
	printf("\nDanh sach tranh");
	printf("\n%3s||%10s||%10s||%10s||%10s||%10s||%10s", 
	"STT", "Ten", "Tac gia", "Dai", "Rong", "Gia ban", "Kho");
	
	
	for(i=0;i<3;i++) {
		char *kho = laykhotranh(p[i]);
		printf("\n%3d||%10s||%10s||%10d||%10d||%10d||%10s", 
			i+1, p[i].ten, p[i].tacgia, p[i].chieudai, p[i].chieurong, p[i].giaban, kho);
	}
		for(i=0;i<limit;i++) {
		printf("=");
	}
	
}


void thongke(tranhtheu *p) {
	int i,j;
	char *k1, *k2;
	int count[3];
	for(i=0;i<3;i++) {
		count[i] = 1;
	}
	for(i=0;i<2;i++) {
		k1 = laykhotranh(p[i]);
		if(count[i] == 0) continue;
		
		for(j=1;j<3;j++) {
			k2 = laykhotranh(p[j]);
			if(count[j] == 0) continue;
			
			int cmp = strcmp(k1, k2);
			if (cmp == 0) {
				count[i]++;
				count[j]--;
			}
		}
			
	}
	
	for(i=0;i<3;i++) {
		if(count[i] == 0) continue;
		k1 = laykhotranh(p[i]);
		
		printf("\nKho %s co %d buc tranh", k1, count[i]);
	}
}

void timtranh(tranhtheu *p) {
	int i, maxgia, limit;
	int c = 0;
	printf("\nNhap gia ban max: ");
	scanf("%d", &maxgia);
	printf("\nDanh sach tranh co gia ban nho hon hoac bang gia nhap vao: ");
	printf("\n%3s||%10s||%10s||%10s||%10s||%10s||%10s", 
				"STT", "Ten", "Tac gia", "Dai", "Rong", "Gia ban", "Kho");
	for(i=0;i<3;i++) {
		if (p[i].giaban <= maxgia) {
			c++;
			char *kho = laykhotranh(p[i]);
			printf("\n%3d||%10s||%10s||%10d||%10d||%10d||%10s", 
				i+1, p[i].ten, p[i].tacgia, p[i].chieudai, p[i].chieurong, p[i].giaban, kho);
		
		}
		
	}
	if(c == 0) {
			printf("\nKhong co buc tranh nào thoa man!!!");
		}

}

void ghidulieu(tranhtheu *p) {
	FILE *fp;
	fp = fopen("D:\\Danhsachtranh.dat","w");
	fwrite(p,sizeof(tranhtheu),3,fp);
	fclose(fp);
}