By GokiSoft.com|
19:16 04/10/2023|
Lập Trình C
[Share Code] Quản lý đồ điện tử - Lập trình C - C2307L
Quản lý đồ điện tử - Lập trình C BT1920
#main.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 spdientu_st {
char ten[20], ma[25], nhasx[20];
long int gia;
} spdientu;
void showMenu();
void input(spdientu danhsachsp[3]);
void sort(spdientu danhsachsp[3]);
void display(spdientu danhsachsp[3]);
void statistic(spdientu danhsachsp[3]);
void find(spdientu danhsachsp[3]);
void saveFile(spdientu danhsachsp[3]);
int main(int argc, char *argv[]) {
spdientu danhsachsp[3];
char c;
do {
showMenu();
fflush(stdin); fflush(stdout);
scanf("%c", &c);
switch(c) {
case '1': {
input(danhsachsp);
break;
}
case '2': {
sort(danhsachsp);
display(danhsachsp);
statistic(danhsachsp);
break;
}
case '3': {
find(danhsachsp);
break;
}
case '4': {
saveFile(danhsachsp);
break;
}
case '5': {
printf("\nThoat");
break;
}
default: {
printf("\nNhap sai!!!");
break;
}
}
} while(c != '5');
return 0;
}
void showMenu() {
printf("\n1. Nhap");
printf("\n2. Sap xep & hien thi");
printf("\n3. Tim kiem");
printf("\n4. Luu file");
printf("\n5. Thoat");
printf("\nChon: ");
}
void input(spdientu danhsachsp[3]) {
int i;
for(i=0;i<3;i++) {
printf("\nNhap san pham %d:", i + 1);
printf("\nTen: ");
fflush(stdin); fflush(stdout);
gets(danhsachsp[i].ten);
printf("\nMa: ");
fflush(stdin); fflush(stdout);
gets(danhsachsp[i].ma);
printf("\nNha san xuat: ");
fflush(stdin); fflush(stdout);
gets(danhsachsp[i].nhasx);
printf("\nGia: ");
fflush(stdin); fflush(stdout);
scanf("%d", &danhsachsp[i].gia);
}
}
void sort(spdientu danhsachsp[3]) {
int i, j, cmp;
spdientu tmp;
for(i=0;i<2;i++) {
for(j=i+1;j<3;j++) {
cmp = strcmp(danhsachsp[i].nhasx, danhsachsp[j].nhasx);
if(cmp < 0) {
tmp = danhsachsp[i];
danhsachsp[i] = danhsachsp[j];
danhsachsp[j] = tmp;
}
}
}
//Ends
}
void display(spdientu danhsachsp[3]) {
int i;
printf("\nSTT | Ten | Ma | Nha San Xuat | Gia");
for(i=0;i<3;i++) {
printf("\n%d | %s | %s | %s | %d", i + 1, danhsachsp[i].ten, danhsachsp[i].ma, danhsachsp[i].nhasx, danhsachsp[i].gia);
}
}
void statistic(spdientu danhsachsp[3]) {
int i, j, cmp;
//B1. Khai bao mang countList[3] -> luu so phan tu trong mang
int countList[3];
for(i=0;i<3;i++) {
countList[i] = 1;
}
//B2. Thuc hien qua trinh don phan tu
for(i=0;i<2;i++) {
if(countList[i] == 0) continue;
for(j=i+1;j<3;j++) {
if(countList[j] == 0) continue;
cmp = strcmp(danhsachsp[i].nhasx, danhsachsp[j].nhasx);
if(cmp == 0) {
countList[i]++;
countList[j]--;
}
}
}
//B3. Hien thi ket qua
for(i=0;i<3;i++) {
if(countList[i] == 0) continue;
printf("\nNha san xuat %s co %d san pham", danhsachsp[i].nhasx, countList[i]);
}
}
void find(spdientu danhsachsp[3]) {
char s[25];
printf("\nNhap nha san xuat can tim: ");
fflush(stdin); fflush(stdout);
gets(s);
int isFind = 0, i, cmp;
for(i=0;i<3;i++) {
cmp = strcmp(danhsachsp[i].nhasx, s);
if(cmp == 0) {
if(isFind == 0) {
printf("\nSTT | Ten | Ma | Nha San Xuat | Gia");
}
printf("\n%d | %s | %s | %s | %d", i + 1, danhsachsp[i].ten, danhsachsp[i].ma, danhsachsp[i].nhasx, danhsachsp[i].gia);
isFind = 1;
}
}
if(isFind == 0) {
printf("\nKhong tim thay san pham cua nha san xuat nay");
}
}
void saveFile(spdientu danhsachsp[3]) {
FILE *fp;
fp = fopen("spdientu.dat", "wb");
if(ferror(fp)) {
printf("\nFile is error");
} else {
fwrite(danhsachsp, sizeof(spdientu), 3, fp);
}
fclose(fp);
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)