/* 프로그램 기능 1. 계좌 개설 2. 입금 3. 출급 4. 전체 고객 잔액 조회 */ struct account { int id; int balance; char name[25]; }typedef account; account act[10]; int act_i = 0; enum{MAKE = 1, DEPOSIT, WITHDRAW, INQUIRE, EXIT}; void print_menu() { cout << "------Menu------" << endl; cout << "1. 계좌 개설" << endl; cout << "2. 입글 " << endl; cout << "3. 출급 " << endl; cout << "4. 잔액 조회 " << endl; cout << "5. 프로그램 종료 " << endl; } void make_act() { int id; char name[15]; int balance; cout << "---계좌 개설---" << endl; cout << "계좌 ID : "; cin >> id; cout << "이름 : "; cin >> name; cout << "입금액 : "; cin >> balance; act[act_i].id = id; strcpy(act[act_i].name, name); act[act_i].balance = balance; act_i++; } void deposit() { int money; int id; cout << "계좌 ID : "; cin >> id; cout << "입금액 : "; cin >> money; for(int i = 0; i < act_i; i++) { if(act[i].id == id) { act[i].balance += money; cout << "입금 완료" << endl; return; } } cout << "등록되어 있지 않은 계좌 ID입니다." << endl; } void withdraw() { int money; int id; cout << "계좌 ID : "; cin >> id; cout << "출금액 : "; cin >> money; for(int i = 0; i < act_i; i++) { if(act[i].id == id) { if(act[i].balance < money) { cout << "잔액이 부족합니다." << endl; return; } act[i].balance -= money; cout << "출금되었습니다." << endl; return; } } cout << "등록되어 있지 않은 계좌 ID입니다." << endl; } void inquire() { cout << act_i << endl; int i; for(i = 0; i < act_i; i++) { cout << "계좌 ID : " << act[i].id << endl; cout << "이 름 : " << act[i].name << endl; cout << "잔 액 : " << act[i].balance << endl; } } void main() { int menu; while(1) { print_menu(); cout << "선택 : "; cin >> menu; switch(menu) { case MAKE : make_act(); break; case DEPOSIT : deposit(); break; case WITHDRAW : withdraw(); break; case INQUIRE : inquire(); break; case EXIT : return; default : cout << "잘못된 선택입니다." << endl; } } }
프로그램 실행화면
은행계좌 관리 프로그램이다. 동작은 간단하다. 기능도 1 ~ 5까지 있다. 프로그램 제일 위에 있는 주석문이 구현한 기능들이다.
계좌 개설, 입금, 출금, 잔액 조회, 종료이다. 계좌를 저장하는 구조체를 하나 선언했고 이 구조체의 배열을 전역변수로 선언했다.
만약 main함수의 지역변수로 선언했다면 함수를 호출할 때마다 인자로 넘겨주어야 하기 때문에 전역변수로 선언하여 두었다.
나머지 부분은 내 설명보다는 소스코드를 보는게 더 이해가 빠를 것이라 생각된다. enum은 #define과 비슷하 동작을 한다.
MAKE에 1을 넣어주면 그 다음부터 2, 3, 4, 5로 대입된다. #define MAKE 1 이랑 같은 것이다. 난 enum보다는 #define을 좋아한다.