-----Menu----- 1.계좌개설 2.입 금 3.출 금 4.잔액조회 5.모든계좌 정보 출력 6.프로그램 종료 |
위와 같은 메뉴에 대해 동작하는 프로그램입니다.
cpp 파일로 작성되었습니다.
그러나 C 스타일로 구현되었기 때문에 구조체가 사용되었고 입출력 정도만 C++이 사용되었습니다.
#include <iostream> #include <cstring> using namespace std; const int accSize = 10;//최대 계좌수 const int adminPassword = 0000;//관리자 암호 설정 void Menu(void);//메뉴 출력 void MakeAccount(void);//계좌개설 void Deposit(void);//입금 void Withdraw(void);//출금 void Balance(void);//잔액조회 void allBalance(void);//모든계좌 정보조회 typedef struct { int accID; char name[20]; int balance; } Account; Account accSave[accSize]; int accNum=0; int main(void) { int select; while(1) { Menu(); cout<<"선택 : "; cin>>select; cout<<endl; switch(select) { case 1: MakeAccount(); break; case 2: Deposit(); break; case 3: Withdraw(); break; case 4: Balance(); break; case 5: allBalance(); break; case 6: return 0; default: cout << "올바른 선택이 아닙니다." << endl; } } return 0; } void Menu(void) { cout<<"-----Menu-----"<<endl; cout<<"1.계좌개설"<<endl; cout<<"2.입 금"<<endl; cout<<"3.출 금"<<endl; cout<<"4.잔액조회"<<endl; cout<<"5.모든계좌 정보 출력"<<endl; cout<<"6.프로그램 종료"<<endl; } void MakeAccount(void) { int id; char name[20]; int money; cout<<"[계좌개설]"<<endl; cout<<"계좌ID : "; cin>>id; cout<<"이 름 : "; cin>>name; cout<<"입금액 : "; cin>>money; cout<<endl; accSave[accNum].accID = id; strcpy(accSave[accNum].name, name); accSave[accNum].balance = money; accNum++; } void Deposit(void) { int id; int money; cout<<"[입 금]"<<endl; cout<<"계좌ID : "; cin>>id; cout<<"입금액 : "; cin>>money; for(int i=0; i<accNum; i++) { if(accSave[i].accID == id) { accSave[i].balance += money; cout<<"입금완료"<<endl; return; } } cout<<"잘못된 ID 입니다!"<<endl<<endl; } void Withdraw(void) { int id; int money; cout<<"[출 금]"<<endl; cout<<"계좌ID : "; cin>>id; cout<<"출금액 : "; cin>>money; for(int i=0; i<accNum; i++) { if(accSave[i].accID == id) { if(accSave[i].balance < money) { cout<<"잔액이 부족합니다."<<endl; return; } accSave[i].balance -= money; cout<<"출금완료"<<endl; return; } } cout<<"잘못된 ID 입니다!"<<endl<<endl; } void Balance(void) { int id; cout<<"[잔액조회]"<<endl; cout<<"계좌ID : "; cin>>id; for(int i=0; i<accNum; i++) { if(accSave[i].accID == id) { cout<< "ID : " << accSave[i].accID<<endl; cout<< "이름 : "<<accSave[i].name<<endl; cout<< "잔액 : "<<accSave[i].balance<<endl<<endl; return; } } cout<<"잘못된 ID 입니다!"<<endl<<endl; } void allBalance(void) { int confirm_password; cout<<"[관리자 모드]"<<endl; cout<<"Input the admin password : "; cin>>confirm_password; if(accNum==0) { cout<<"Bank has no account..."<<endl<<endl; return; } if(confirm_password == adminPassword) { for(int i=0; i<accNum; i++) { cout<< "ID : " << accSave[i].accID<<endl; cout<< "이름 : "<<accSave[i].name<<endl; cout<< "잔액 : "<<accSave[i].balance<<endl<<endl; } } else { cout<<"You are not admin!!!"<<endl<<endl; return; } } |
'프로그래밍 > C++' 카테고리의 다른 글
new & delete 기본 사용법 (0) | 2013.07.23 |
---|---|
포인터와 참조자 (0) | 2013.07.23 |
참조자(Reference)를 이용한 Swap() 함수 (0) | 2013.07.23 |
범위지정 연산자 (Scope Resolution Operator) (0) | 2013.07.20 |
이름공간(namespace) 기본형 (0) | 2013.07.20 |
입력 받은 수의 구구단 출력 (0) | 2013.07.19 |
포인터 (0) | 2013.06.01 |