cthien

 avatar
anhhackta
c_cpp
a month ago
2.5 kB
8
Indexable
Never
// Online C compiler to run C program online
#include <stdio.h>
#include <string.h>
#define max 100
struct date {
    int day, month, year;
};
typedef struct date DATE;
struct item {
    char name[100];
    char id[100];
    float price;
    DATE ngayxuat;
};
typedef struct item ITEM;
struct queue {
    ITEM A[max];
    int front, rear;
};
typedef struct queue QUEUE;
void Init(QUEUE &q) {
    q.front = 0;
    q.rear = -1;
}
int IsEmpty(QUEUE q) {
    if(q.front > q.rear) {
        return 1;
    }
    return 0;
}
int IsFull(QUEUE q) {
    if(q.rear==max-1) {
        return 1;
    }
    return 0;
}
void Input(QUEUE &q, int n) {
    for(int i = 0; i < n; i++) {
        printf("Moi ban nhap info hang hoa thu [%d]: \n", i);
        ITEM x;
            printf("nhap ten hang: ");
            fflush(stdin);
            gets(x.name);
            printf("nhap id: ");
            fflush(stdin);
            gets(x.id);
            printf("nhap gia: ");
            fflush(stdin);
            scanf("%f", &x.price);
    }
}
void search(QUEUE &q,  char *namesearch) {
    for(int i = q.front; i <= q.rear; i++) {
        if(strcmp(q.A[i].name, namesearch) == 0) {
            printf("tim thay mat hang");
            return;
        }
    }
    printf("khong tim thay");
}
void add(QUEUE &q, ITEM x) {
    if(IsFull(q) == 0) {
        q.rear++;
        q.A[q.rear] = x;
    }
}
void OUTPUT(QUEUE q) {
    ITEM x;
    for(int i = q.front; i <= q.rear; i++) {
        printf("san phan thu [%d]: ", i);
        printf("ten san pham: %s \n", x.name);
        printf("ID san phan: %s \n", x.id);
        printf("gia san pham: %s \n", x.price);
        printf("--------------------\n");
    }
}
void change(QUEUE q, ITEM x) {
    char changename[100];
    printf("nhap ten san pham muon sua: ");
    gets(changename);
    for(int i = q.front; i <= q.rear; i++) {
        if(strcmp(changename, q.A[i].name) == 0) {
            printf("nhap ten san pham muon thay doi lai: ");
            gets(x.name);
        }
            printf("khong tim thay san pham de thay doi");
    }
}
int main() {
    QUEUE q;
    int n;
    char namesearch[100];
    printf("Nhap so luong hang hoa: ");
    scanf("%d", &n);
    Init(q);
    Input(q, n);
    printf("nhap ten hang hoa muon tim: ");
    gets(namesearch);
    search(q, namesearch);
    OUTPUT(q);
    return 0;
}
Leave a Comment