realloc() 예제
C & C++ 관련 :
2007. 4. 9. 18:15
반응형
가상 계좌 프로그램
001: #include <stdio.h> 002: #include <conio.h> 003: #include <stdlib.h> 004: 005: const int NAME_LEN=20; 006: int customer_count=0; 007: struct Account 008: { 009: int id; //계좌번호 010: int balance; //잔액 011: char name[NAME_LEN]; //고객이름 012: }; 013: void PrintMenu(); //메뉴출력 014: void MakeAccount(); //계좌개설 015: void Deposit(); //입금 016: void WithDraw(); //출금 017: void Remainder(); //잔액조회 018: 019: Account *acc=(Account *)malloc(1*sizeof(Account)); //초기 메모리 할당 020: void main (){ 021: int selectedmenu=0; 022: 023: PrintMenu(); 024: scanf("%d",&selectedmenu); 025: 026: switch(selectedmenu){ 027: case 1: 028: MakeAccount(); 029: break; 030: case 2: 031: Deposit(); 032: break; 033: case 3: 034: WithDraw(); 035: break; 036: case 4: 037: Remainder(); 038: break; 039: case 5: 040: exit(0); 041: } 042: } 043: void PrintMenu(){ //메뉴출력 044: system("cls"); 045: printf("----------------------------\n"); 046: printf("1. 계좌 개설\n"); 047: printf("2. 입금\n"); 048: printf("3. 출금\n"); 049: printf("4. 전체 고객 잔액조회\n"); 050: printf("5. 종료\n"); 051: printf("----------------------------\n"); 052: printf("Select Menu : "); 053: } 054: void MakeAccount(){ //계좌개설 055: system("cls"); 056: acc=(Account *)realloc(acc, (customer_count+1)*sizeof(Account)); 057: printf("\n이름을 입력하세요 : "); 058: scanf("%s",&acc[customer_count].name); 059: 060: printf("\n계좌 번호를 입력하세요 : "); 061: scanf("%d",&acc[customer_count].id); 062: 063: printf("\n입금 금액을 입력하세요 : "); 064: scanf("%d",&acc[customer_count].balance); 065: 066: customer_count++; 067: main(); 068: } 069: void Remainder(){ //계좌개설 070: system("cls"); 071: printf("------------------------------------------\n"); 072: printf(" 전체 고객 잔액조회 \n"); 073: printf("------------------------------------------\n"); 074: printf(" 이름 | ID | 잔액 \n"); 075: printf("------------------------------------------\n"); 076: for(int i=0;i<customer_count;i++) 077: printf("%12s %12d %14d \n",acc[i].name,acc[i].id,acc[i].balance); 078: getch(); 079: main(); 080: } 081: void Deposit(){ //입금 082: int temp, k, money; 083: system("cls"); 084: printf("고객님의 ID를 입력하여 주셔요 : "); 085: scanf("%d",&temp); 086: for(k=0;k<customer_count;k++){ 087: if(temp==acc[k].id){ 088: printf("------------------------------------------\n"); 089: printf(" 고객님의 정보 입니다. \n"); 090: printf("------------------------------------------\n"); 091: printf(" 이름 | ID | 잔액 \n"); 092: printf("------------------------------------------\n"); 093: printf("%12s %12d %14d \n",acc[k].name,acc[k].id,acc[k].balance); 094: break; 095: } 096: } 097: printf("입금액을 입력해 주셔요 : "); 098: scanf("%d",&money); 099: acc[k].balance+=money; 100: 101: printf("\n------------------------------------------\n"); 102: printf(" 입금 후 정보 입니다. \n"); 103: printf("------------------------------------------\n"); 104: printf(" 이름 | ID | 잔액 \n"); 105: printf("------------------------------------------\n"); 106: printf("%12s %12d %14d \n",acc[k].name,acc[k].id,acc[k].balance); 107: getch(); 108: main(); 109: } 110: void WithDraw(){ //출금 111: int temp, k, money; 112: system("cls"); 113: printf("고객님의 ID를 입력하여 주셔요 : "); 114: scanf("%d",&temp); 115: for(k=0;k<customer_count;k++){ 116: if(temp==acc[k].id){ 117: printf("------------------------------------------\n"); 118: printf(" 고객님의 정보 입니다. \n"); 119: printf("------------------------------------------\n"); 120: printf(" 이름 | ID | 잔액 \n"); 121: printf("------------------------------------------\n"); 122: printf("%12s %12d %14d \n",acc[k].name,acc[k].id,acc[k].balance); 123: break; 124: } 125: } 126: printf("출금액을 입력해 주셔요 : "); 127: scanf("%d",&money); 128: acc[k].balance-=money; 129: 130: printf("\n------------------------------------------\n"); 131: printf(" 처리 후 정보 입니다. \n"); 132: printf("------------------------------------------\n"); 133: printf(" 이름 | ID | 잔액 \n"); 134: printf("------------------------------------------\n"); 135: printf("%12s %12d %14d \n",acc[k].name,acc[k].id,acc[k].balance); 136: getch(); 137: main(); 138: }
반응형
'C & C++ 관련' 카테고리의 다른 글
malloc은 Heap에 변수를 할당하는 함수입니다. (0) | 2007.04.21 |
---|---|
File Copy LowLevel (0) | 2007.03.28 |
C++ file pointer 연습 (0) | 2007.03.28 |