fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. const int MAXPHONEBOOKENTRY = 20;//저장 갯수제한
  6.  
  7. string nameData[] = {"Park SH", "Kang JH", "Kim KS", "Lee YH", "Kang SH",
  8. "Bae JM", "Lee DI", "Lee BH", "Chang WH", "Chun WY"};
  9. int numberData[] = {5023, 5002, 5008, 5067, 5038, 5381, 5125, 5165, 5684, 5752};
  10.  
  11. class Entry {
  12. public:
  13. Entry(){//디폴트 생성자
  14. phoneName = "";
  15. phoneNumber = -1;
  16. }
  17. Entry(string s, int n) {//생성자
  18. phoneName = s;
  19. phoneNumber = n;
  20. }
  21. string phoneName;//이름 변수
  22. int phoneNumber;//번호 변수
  23. };
  24.  
  25. Entry arrayPhoneBook[MAXPHONEBOOKENTRY];//배열선언
  26. int lastp = 0;//현재저장된 갯수
  27.  
  28. void insert(string name, int number);
  29. void remove(string name);
  30. void update(string name, int number);
  31. int find(string name);//함수 오버로딩사용
  32. void find(int num);
  33.  
  34. int findLoc(string name){//이름 검색 함수
  35. for (int i=0; i<lastp; ++i){
  36. if (arrayPhoneBook[i].phoneName == name)
  37. return i;
  38. }
  39. return -1; // not found 비어있을때 반환
  40. }
  41.  
  42. void find(int num){//번호 검색함수
  43. for(int i=0;i<lastp;++i){
  44. if(arrayPhoneBook[i].phoneNumber==num){
  45. cout<<"이름 : " <<arrayPhoneBook[i].phoneName<<"번호 : "<<arrayPhoneBook[i].phoneNumber<<endl;
  46. return;
  47. }
  48. }
  49. cout<<"**Error -- No data"<<endl;
  50. }
  51.  
  52. int find(string name){//이름 검색 함수
  53. int loc = findLoc(name);
  54. if (loc == -1)
  55. return -1; // not found
  56. return arrayPhoneBook[loc].phoneNumber;//전화번호 출력
  57. }
  58.  
  59. void insert(string name, int number){//추가함수
  60. int loc = findLoc(name);
  61. if (loc == -1){ // insert 에러 메세지 출력
  62. if (lastp > MAXPHONEBOOKENTRY){//최대 저장 갯수보다 더 저장하려 했을때
  63. cout << "***Error -- PhoneBook Overflow" << endl;
  64. }
  65. else {//lastp가 MAXPHONEBOOKENTRY보다 적으면 저장
  66. arrayPhoneBook[lastp].phoneName = name;
  67. arrayPhoneBook[lastp].phoneNumber = number;
  68. lastp++;//카운트 변수 +1
  69. }
  70. }
  71. else {//그자리에 이름이 저장됐을시
  72. cout << "***Error -- Duplicated Name" << endl;
  73. }
  74. }
  75. void remove(string name){//지우는 함수
  76. int loc = findLoc(name);
  77. if (loc != -1){ // there exist name
  78. // remove array entry at loc
  79. for (int i=loc+1; i<lastp; ++i){//지우는 배열 번째수부터 뒤에 번호들을 앞으로 당김
  80. arrayPhoneBook[i-1].phoneName = arrayPhoneBook[i].phoneName;
  81. arrayPhoneBook[i-1].phoneNumber = arrayPhoneBook[i].phoneNumber;
  82. }
  83. lastp--;//카운트 변수 -1
  84. }
  85. else {//존재 하지 않을때 출력
  86. cout << "***Error -- Name not found" << endl;
  87. }
  88. }
  89. void update(string name, int number){//입력 or 제거 함수
  90. remove(name);
  91. insert(name, number);
  92. }
  93. void listAll(){//모든 번호 출력
  94. cout << "Name" << " " << "Number" << endl;
  95. for (int i=0; i<lastp; ++i){
  96. cout << arrayPhoneBook[i].phoneName << " "
  97. << arrayPhoneBook[i].phoneNumber << endl;
  98. }
  99. }
  100. int main ()
  101. {
  102. int incount = 9;
  103.  
  104. for (int i=0; i < incount; ++i){//array 초기화
  105. insert(nameData[i], numberData[i]);
  106. }
  107.  
  108. insert("Kim SH", 1883);//5개 입력
  109. insert("Lee EJ", 0562);
  110. insert("Choi KJ", 9523);
  111. insert("Jung SW", 2341);
  112. insert("Jeon JH", 7818);
  113.  
  114. remove("Kim KS");//삭제
  115. update("Chang WH",5555);//업데이트
  116.  
  117.  
  118. cout << "List All Inserted Entry" << endl;
  119. listAll(); //모든 리스트 출력
  120. find(5002);//검색
  121.  
  122.  
  123. return 0;
  124. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
List All Inserted Entry
Name        Number
Park SH        5023
Kang JH        5002
Lee YH        5067
Kang SH        5038
Bae JM        5381
Lee DI        5125
Lee BH        5165
Kim SH        1883
Lee EJ        370
Choi KJ        9523
Jung SW        2341
Jeon JH        7818
Chang WH        5555
이름 : Kang JH번호 : 5002