상세 컨텐츠

본문 제목

출입명부 프로그램(C언어)

프로그래밍/C언어

by RosetteNebula 2021. 1. 12. 20:16

본문

 

-조건-

총 5명까지 작성 가능

입력항목 : 이름, 나이

1명씩 입력

메뉴 2, 3의 경우 명부가 없으면 없다고 출력

종료할 경우 한번더 확인

관리자만 출력 가능

 

-메뉴-

1. 출입명부 입력

2. 출입명부 출력

3. 출입명부 수정(이름으로 찾기)

4. 프로그램 종료

 

if문, for문 학습이 목적

 

 

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

typedef struct List{
	char name[10];  //이름
	int age;        //나이
}List;

typedef struct Login{
	char id[10];
	char pw[10];
}Login;

Login login[2] = {{"super", "1234"}, {"general", "0000"}};  //id, pw 초기세팅
char id[10], pw[10];
int authority = -1; //권한

List list[5];
int i, j, count = 0, menu, dump;
char search[10], end, del;

void LogIn()
{
    for(;;)
    {
        system("cls");  //해당 줄 위로 화면에 표시된 내용 지우기
        printf("ID와 PW 입력 : ");
        scanf("%s %s", &id, &pw);

        //로그인 정보 찾기
        for(i=0; i<2; i++)
        {
            //로그인 정보 비교
            if(strcmp(id, login[i].id) == 0 && strcmp(pw, login[i].pw) == 0)
            {
                //권한 부여
                if(strcmp(id, "super") == 0)
                {
                    authority = 1;
                }
                else if(strcmp(id, "general") == 0)
                {
                    authority = 0;
                }

            }//if
        }//for

        //일치하는 계정이 없을때
        if(authority != 1 && authority != 0)
        {
            printf("일치하는 계정이 없습니다\n\n");
            system("pause");
            continue;
        }//if
        else if(authority == 1 || authority == 0){
            break;
        }
    }
}

void Menu()
{
    for(;;)
    {
        system("cls");
        printf("\n---출입명부---\n");
        printf("1. 출입명부입력\n");
        printf("2. 출입명부출력\n");
        printf("3. 출입명부수정\n");
        printf("4. 출입명부삭제\n");
        printf("5. 프로그램종료\n");

        printf("\n메뉴 선택 : ");
        scanf("%d", &menu);

        //명부 입력
        if(menu == 1)
        {
            Input();
        }//if

        //명부 출력
        else if(menu == 2)
        {
            Output();
        }//else if

        //명부수정
        else if(menu == 3)
        {
            Change();
        }//else if

        //명부 삭제
        else if(menu == 4)
        {
            Delete();
        }//else if

        //종료
        else if(menu == 5)
        {
            End();
        }//else if
    }
}

void Input()
{
    system("cls");
    printf("이름과 나이를 입력 하세요 : ");
    scanf("%s %d", &list[count].name, &list[count].age);
    //printf("\n");
    count++;    //작성자 수
}

void Output()
{
    system("cls");
    //권한확인
    if(authority != 1)
    {
        printf("권한이 없습니다\n");
        system("pause");    //계속 하려면 아무키 입력
    }
    //명부 유무 판단
    if(count > 0)
    {
        for(i=0; i<count; i++)
        {
            printf("이름:%s, 나이:%d\n", list[i].name, list[i].age);
        }//for
    }//if
    else
    {
        printf("명부가 없습니다\n");
    }//else
    system("pause");
}

void Change()
{
    system("cls");
    //명부 유무 확인
    if(count > 0)
    {
        printf("수정 할 이름을 입력 : ");
        scanf("%s", &search);

        //이름검색
        for(i=0; i<count; i++)
        {
            //이름비교
            if(strcmp(search, list[i].name) == 0)
            {
                printf("수정 할 이름과 나이 입력 : ");
                scanf("%s %d", &list[i].name, &list[i].age);
                continue;
            }//if
            
            //찾는 명부가 없음
            else if(i == count)
            {
                printf("찾는 명부가 없습니다.\n");
                system("pause");
            }//else if
        }//for
    }//if
    else
    {
        printf("명부가 없습니다.\n");
        system("pause");
    }//else
}

void Delete()
{
    system("cls");
    //명부 유무 확인
    if(count > 0)
    {
        printf("삭제 할 이름 입력 : ");
        scanf("%s", &search);

        //이름검색
        for(i=0; i<count; i++)
        {
            //이름 비교
            if(strcmp(search, list[i].name) == 0)
            {
                if(authority == 0 && list[i].age < 20)  //권한과 미성년자 확인
                {
                    printf("권한이 없습니다\n");
                    system("pause");
                    continue;
                }
                printf("삭제 하겠습니까?(Y/N)");
                scanf(" %c", &del);

                //삭제 확인
                if(del == 'Y' || del == 'y')
                {
                    for(j=i; j<count-1; j++)    //삭제된 명부 뒤에 있는 명부를 앞으로 이동
                    {
                        dump = j+1;
                        strcpy(list[j].name, list[dump].name);
                        list[j].age = list[dump].age;
                    }

                    count--;
                    continue;
                }//if
                else if(del == 'N' || del == 'n')
                {
                    printf("취소\n");
                    system("pause");
                    continue;
                }//else if
            }//if
            //찾는 명부가 없음
            else if(i == count)
            {
                printf("찾는 명부가 없습니다.\n");
                system("pause");
            }//else if
        }//for

    }//if
    else
    {
        printf("명부가 없습니다.\n");
    }
}

void End()
{
    system("cls");
    printf("진짜 종료를 하겠습니까?(Y/N)");
    scanf(" %c", &end);

    if(end == 'Y' || end == 'y')
    {
        exit(1);    //프로그램 종료
    }//if
    else if(end == 'N' || end == 'n')
    {
        printf("취소\n");
        system("pause");
    }//else
}

int main(void){
    LogIn();
    Menu();

	return 0;
}

 

조건 동작 확인만 하고 멈춘 코드입니다

 

'프로그래밍 > C언어' 카테고리의 다른 글

전화번호부 프로그램(C언어)  (0) 2021.01.12
직원 관리 프로그램(C언어)  (0) 2021.01.12
영화 예매 프로그래밍(C언어)  (2) 2021.01.12

관련글 더보기