Computer >> 컴퓨터 >  >> 프로그래밍 >> C 프로그래밍

C언어 구조체를 활용한 인벤토리(재고) 관리 시스템 프로그램

구조체(Structure)는 서로 다른 자료형을 가진 변수들을 하나의 이름 아래 묶어 놓은 사용자 정의 자료형입니다. C언어에서 복잡한 데이터를 체계적으로 관리할 때 매우 유용하게 활용됩니다.

구조체의 주요 특징

C 언어에서 구조체가 가지는 대표적인 특징은 다음과 같습니다.

  • 대입 연산자(=)를 사용하면 한 구조체 변수의 모든 멤버 값을 같은 타입의 다른 구조체 변수에 한 번에 복사할 수 있습니다.
  • 복잡한 데이터를 다룰 때는 구조체 안에 또 다른 구조체를 포함시키는 중첩 구조체(nested structure) 방식이 효과적입니다.
  • 함수 호출 시 구조체 전체, 개별 멤버, 또는 구조체의 주소를 모두 전달할 수 있습니다.
  • 구조체 포인터를 선언하여 메모리를 효율적으로 다룰 수 있습니다.

프로그램 코드

다음은 구조체를 이용해 인벤토리(재고) 정보를 저장하고 출력하는 C 프로그램 예제입니다. 상품명, 상품 코드, 수량, 가격, 제조일자를 입력받아 목록 형태로 출력합니다.

#include<stdio.h>
#include<conio.h>
void main(){
    struct date{
        int day;
        int month;
        int year;
    };
    struct details{
        char name[20];
        int price;
        int code;
        int qty;
        struct date mfg;   // 중첩 구조체: 제조일자
    };
    struct details item[50];
    int n,i;
    printf("Enter number of items:");
    scanf("%d",&n);
    fflush(stdin);
    for(i=0;i<n;i++){
        fflush(stdin);
        printf("Item name:");
        scanf("%s",item[i].name);
        fflush(stdin);
        printf("Item code:");
        scanf("%d",&item[i].code);
        fflush(stdin);
        printf("Quantity:");
        scanf("%d",&item[i].qty);
        fflush(stdin);
        printf("price:");
        scanf("%d",&item[i].price);
        fflush(stdin);
        printf("Manufacturing date(dd-mm-yyyy):");
        scanf("%d-%d-%d",&item[i].mfg.day,&item[i].mfg.month,&item[i].mfg.year);
    }
    printf(" ***** INVENTORY *****\n");
    printf("------------------------------------------------------------------\n");
    printf("S.N.| NAME | CODE | QUANTITY | PRICE |MFG.DATE\n");
    printf("------------------------------------------------------------------\n");
    for(i=0;i<n;i++)
        printf("%d %-15s %-d %-5d %-5d%d/%d/%d\n",i+1,item[i].name,item[i].code,item[i].qty,item[i].price,item[i].mfg.day,item[i].mfg.month,item[i].mfg.year);
    printf("------------------------------------------------------------------\n");
    getch();
}

코드 설명

  • struct date: 일(day), 월(month), 연도(year)를 담는 날짜용 구조체입니다.
  • struct details: 상품명(name), 가격(price), 코드(code), 수량(qty)과 함께 위에서 정의한 date 구조체를 멤버로 포함하는 중첩 구조체입니다.
  • item[50]: 최대 50개의 상품 정보를 배열로 저장할 수 있도록 선언되었습니다.
  • 사용자로부터 상품 개수를 입력받은 뒤, 반복문(for)을 통해 각 상품의 세부 정보를 차례대로 입력받습니다.
  • 마지막으로 표 형태의 인벤토리 목록을 화면에 출력합니다.

실행 결과

위 프로그램을 실행하면 다음과 같은 결과가 출력됩니다.

Enter number of items:5
Item name:pen
Item code:12
Quantity:50
price:25
Manufacturing date(dd-mm-yyyy):12-02-2020
Item name:pencil
Item code:15
Quantity:100
price:30
Manufacturing date(dd-mm-yyyy):11-03-2020
Item name:book
Item code:34
Quantity:30
price:60
Manufacturing date(dd-mm-yyyy):15-04-2020
Item name:bag
Item code:39
Quantity:20
price:70
Manufacturing date(dd-mm-yyyy):12-03-2021
Item name:sharpner
Item code:33
Quantity:20
price:40
Manufacturing date(dd-mm-yyyy):12-04-2021
***** INVENTORY *****
------------------------------------------------------------------
S.N.| NAME | CODE | QUANTITY | PRICE |MFG.DATE
------------------------------------------------------------------
1     pen     12       50         25    12/2/2020
2   pencil   15       100        30    11/3/2020
3    book    34       30         60    15/4/2020
4    bag     39       20         70    12/3/2021
5  sharpner  33       20         40    12/4/2021

이처럼 구조체와 중첩 구조체를 활용하면 여러 종류의 데이터를 하나의 단위로 묶어 관리할 수 있으며, 실무에서 재고 관리 프로그램이나 데이터베이스 처리 로직을 구현할 때 널리 응용됩니다.