By GokiSoft.com| 19:45 11/01/2022|
Lập Trình C

[Video]Quản lý motobike - Quản lý xe cộ - Lập trình C - C2110L

Quản lý motobike - Quản lý xe cộ - Lập trình C



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct MotorBike_ST {
	char name[20];
	char manufacturer[10];
	char madein[12];
	long int price;
} MotorBike;

void showMenu();
void input(MotorBike p[3]);
void display(MotorBike p[3]);
void find(MotorBike p[3]);
void saveFile(MotorBike *p);
void drawLine();

int main(int argc, char *argv[]) {
	MotorBike *motorbikeList;
	
	//Malloc -> cap phat 3 phan tu cho pointer tren
	motorbikeList = (MotorBike *) malloc (3 * sizeof(MotorBike));
	
	int choose;
	
	do {
		showMenu();
		scanf("%d", &choose);
		
		switch(choose) {
			case 1: {
				input(motorbikeList);
				break;
			}
			case 2: {
				display(motorbikeList);
				break;
			}
			case 3: {
				find(motorbikeList);
				break;
			}
			case 4: {
				saveFile(motorbikeList);
				break;
			}
			case 5: {
				printf("\nThoat chuong trinh");
				break;
			}
			default: {
				printf("\nNhap sai!!!");
				break;
			}
		}
	} while(choose != 5);
	
	return 0;
}

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

void input(MotorBike p[3]) {
	int i;
	for(i=0;i<3;i++) {
		printf("\nEnter data of motorbike %d", i + 1);
		printf("\nName: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].name);
		printf("\nManufacturer: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].manufacturer);
		printf("\nMade in: ");
		fflush(stdin); fflush(stdout);
		gets(p[i].madein);
		printf("\nPrice: ");
		scanf("%ld", &p[i].price);
	}
}

void drawLine() {
	int i;
	printf("\n");
	for(i=0;i<47;i++) {
		printf("-");
	}
}

void display(MotorBike p[3]) {
	int i, j;
	
	//Sap xep
	for(i=0;i<2;i++) {
		for(j=i+1;j<3;j++) {
			int cmp = strcmp(p[i].manufacturer, p[j].manufacturer);
			
			//Sap xep theo thu tu Z - A
			if(cmp < 0) {
				MotorBike tmp = p[i];
				p[i] = p[j];
				p[j] = tmp;
			}
		}
	}
	
	//Hien thi
//	printf("\n%3s|%5s|%15s|%10s|%8s", "No", "Name", "Manufacturer", "Made In", "Price");
//	for(i=0;i<3;i++) {
//		printf("\n%3d|%5s|%15s|%10s|%8ld", i+1,p[i].name, p[i].manufacturer, p[i].madein, p[i].price);
//	}
	drawLine();
	printf("\n|%3s|%5s|%15s|%10s|%8s|", "No", "Name", "Manufacturer", "Made In", "Price");
	drawLine();
	for(i=0;i<3;i++) {
		printf("\n|%3d|%5s|%15s|%10s|%8ld|", i+1,p[i].name, p[i].manufacturer, p[i].madein, p[i].price);
		drawLine();
	}

	//Thong ke theo nha san xuat: manufacturer
	int count[3];
	
	//p -> {A, B, A, C, A}
	//count -> {1, 1, 1, 1, 1} -> don mang -> {3, 1, 0, 1, 0} -> A: 3, B: 1, C: 1
	for(i=0;i<3;i++) {
		count[i] = 1;
	}
	
	for(i=0;i<2;i++) {
		if(count[i] == 0) {
			continue;
		}
		for(j=i+1;j<3;j++) {
			if(count[j] == 0) {
				continue;
			}
			
			int cmp = strcmp(p[i].manufacturer, p[j].manufacturer);
			if(cmp == 0) {
				count[i]++;
				count[j]--;
			}
		}
	}
	
	for(i=0;i<3;i++) {
		if(count[i] > 0) {
			printf("\n%s co %d xe may", p[i].manufacturer, count[i]);
		}
	}
}

void find(MotorBike p[3]) {
	char s[30];
	printf("\nVao nsx can tim kiem: ");
	fflush(stdin); fflush(stdout);
	gets(s);
	
	int i, count = 0, cmp;
	for(i=0;i<3;i++) {
		cmp = strcmp(p[i].manufacturer, s);
		if(cmp != 0) {
			continue;
		}
		if(count == 0) {
			printf("\n%3s|%5s|%15s|%10s|%8s", "No", "Name", "Manufacturer", "Made In", "Price");
		}
		count++;
		printf("\n%3d|%5s|%15s|%10s|%8ld", i+1,p[i].name, p[i].manufacturer, p[i].madein, p[i].price);
	}
	if(count == 0) {
		printf("\nKhong tim thay bat ky xe nao theo nsx tren");
	}
}

void saveFile(MotorBike *p) {
	int i;
	FILE *fp;
	
	//B1. Mo file
	fp = fopen("motorbike.txt", "w");
	
	if(fp == NULL) {
		printf("\nFile error");
	} else {
		for(i=0;i<3;i++) {
			fprintf(fp, "%s, %s\n", p[i].name, p[i].manufacturer);
		}
	}
	
	fclose(fp);
}




Tags:



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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó