Design and Implement the Class Phonebook Using Bst

C++ implements PhoneBook class

tags: C++ program  c++  class

Problem Description

Class diagram design

Source code

                          //class              #include                <vector>                            #include                <string>                            using              namespace              std;              typedef              struct              phBook{              string no;              string name;              string number;              }pb;              class              PhoneBook              {              vector<pb>              arr;              int              size_pb;              public              :              PhoneBook              (              )              ;              PhoneBook              (              int              size_pb)              ;              PhoneBook              (PhoneBook&              x)              ;              PhoneBook&              operator              =              (PhoneBook&              x)              ;              pb&              operator              [              ]              (string name)              ;              ~              PhoneBook              (              )              ;              vector<pb>              &              getArr              (              )              ;              void              add              (pb x)              ;              void              serch1              (string name)              ;              void              serch2              (string number)              ;              void              serch3              (string no)              ;              void              del              (string no)              ;              void              inCtrl              (              int              n)              ;              void              inFile              (string filename)              ;              void              outCtrl              (              )              ;              void              outFile              (string filename)              ;              }              ;              //class.cpp              #include                "class.h"                            #include                <iostream>                            #include                <cstring>                            #include                <fstream>                            PhoneBook::              PhoneBook              (              )              {              vector<pb>              temp;              arr              =              temp;              size_pb              =              0              ;              }              PhoneBook::              PhoneBook              (              int              size_pb)              {              vector<pb>              temp              (size_pb)              ;              arr              =              temp;              this              -              >size_pb              =              size_pb;              }              vector<pb>              &              PhoneBook::              getArr              (              )              {              return              arr;              }              PhoneBook::              PhoneBook              (PhoneBook&              x)              {              size_pb              =              x.size_pb;              arr              =              x.              getArr              (              )              ;              }              PhoneBook&              PhoneBook::              operator              =              (PhoneBook&              x)              {              if              (              this              ==              &x)              return              *              this              ;              size_pb              =              x.size_pb;              arr              =              x.              getArr              (              )              ;              return              *              this              ;              }              pb&              PhoneBook::              operator              [              ]              (string name)              {              serch2              (name)              ;              }              PhoneBook::              ~              PhoneBook              (              )              {              }              void              PhoneBook::              add              (pb x)              {              int              num              =              arr.              size              (              )              ;              if              (num              ==              size_pb)              {              cout              <<              "this array is full!"              <<              endl;              return              ;              }              arr.              push_back              (x)              ;              }              void              PhoneBook::              serch1              (string no)              {              int              num              =              arr.              size              (              )              ;              for              (              int              i=              0              ;i<num;i++              )              {              pb temp              =              arr[i]              ;              int              n              =              no.              compare              (temp.no)              ;              if              (n              ==              0              )              {              cout<<              "no: "              <<              temp.no              <<              endl;              cout<<              "name: "              <<              temp.name              <<              endl;              cout<<              "number: "              <<              temp.number              <<              endl;              break              ;              }              }              }              void              PhoneBook::              serch2              (string name)              {              int              num              =              arr.              size              (              )              ;              for              (              int              i=              0              ;i<num;i++              )              {              pb temp              =              arr[i]              ;              int              n              =              name.              compare              (temp.name)              ;              if              (n              ==              0              )              {              cout<<              "no: "              <<              temp.no              <<              endl;              cout<<              "name: "              <<              temp.name              <<              endl;              cout<<              "number: "              <<              temp.number              <<              endl;              }              }              }              void              PhoneBook::              serch3              (string number)              {              int              num              =              arr.              size              (              )              ;              for              (              int              i=              0              ;i<num;i++              )              {              pb temp              =              arr[i]              ;              int              n              =              number.              compare              (temp.number)              ;              if              (n              ==              0              )              {              cout<<              "no: "              <<              temp.no              <<              endl;              cout<<              "name: "              <<              temp.name              <<              endl;              cout<<              "number: "              <<              temp.number              <<              endl;              }              }              }              void              PhoneBook::              del              (string no)              {              int              num              =              arr.              size              (              )              ;              for              (              int              i=              0              ;i<num;i++              )              {              pb temp              =              arr[i]              ;              int              n              =              no.              compare              (temp.no)              ;              if              (n              ==              0              )              {              pb t              =              arr[num-              1              ]              ;              arr[num-              1              ]              =              arr[i]              ;              arr[i]              =              t;              arr.              pop_back              (              )              ;              break              ;              }              }              }              void              PhoneBook::              inCtrl              (              int              n)              {              cout              <<              "please input (no name number)"              <<              endl;              if              (size_pb<              n)              {              cout              <<              "this array is small!"              <<              endl;              return              ;              }              for              (              int              i=              0              ;i<n;i++              )              {              pb temp              =              arr[i]              ;              cin              >>              temp.no              >>              temp.name              >>              temp.number;              arr[i]              =              temp;              }              for              (              int              i              =              0              ;i<size_pb-n;i++              )              arr.              pop_back              (              )              ;              }              void              PhoneBook::              inFile              (string filename)              {              ifstream              in              (filename.              c_str              (              )              )              ;              string s;              int              m              =              1              ;              cout              <<              "no\tname\tnumber\n"              ;              while              (in              >>              s              )              {              if              (m%              3              )cout              <<              s              <<              "\t"              ;              else              cout              <<              s              <<endl;              m++              ;              }              in.              close              (              )              ;              }              void              PhoneBook::              outCtrl              (              )              {              int              num              =              arr.              size              (              )              ;              cout              <<              "no\tname\tnumber\n"              ;              for              (              int              i=              0              ;i<num;i++              )              {              pb temp              =              arr[i]              ;              cout              <<              temp.no<<              "\t"              <<temp.name              <<              "\t"              <<              temp.number              <<              endl;              }              }              void              PhoneBook::              outFile              (string filename)              {              ofstream              out              (filename.              c_str              (              )              )              ;              int              num              =              arr.              size              (              )              ;              for              (              int              i=              0              ;i<num;i++              )              {              pb temp              =              arr[i]              ;              out              <<              temp.no              <<              "\n"              ;              out              <<              temp.name              <<              "\n"              ;              out              <<              temp.number              <<              "\n"              ;              }              out.              close              (              )              ;              }              //main.cpp              #include                <iostream>                            #include                "class.h"                            using              namespace              std;              int              main              (              )              {              PhoneBook              b              (              5              )              ;              string s1              =              "out.txt"              ;              b.              inCtrl              (              2              )              ;              b.              outFile              (s1)              ;              return              0              ;              }                      

Partial test results


For more related content, see

my blog

Intelligent Recommendation

C++ implements a String class

Yesterday, when I was interviewing Xunyou, I was asked how to implement a String class, because I have not done this kind of topic before (mentioned in Jianzhi offer) So in the evening, I realized it ...

C ++ implements a complex class

The implementation of the complex number contains the basics of C ++, which has a great help to learn C ++. Among them, "preamp ++, - and back ++, -" in operator overload is slightly difficu...

C ++ implements an array class

An example of the teacher knocked on the class, modified a bug 1 This array is supported to delete and insert elements in the corresponding location. 2 Static objects can only call static member funct...

More Recommendation

Phonebook 2.0

Reproduced in: https: //my.oschina.net/jingzigege/blog/651799...

HTB Phonebook

0x01 prompt Look at the prompt means to enumerate or find the phone book? Open directly: 0x02 try View the source code and find out anything. Blasting is impossible to blast, can only try to bypass, f...

Design and Implement the Class Phonebook Using Bst

Source: https://www.programmersought.com/article/62646133509/

0 Response to "Design and Implement the Class Phonebook Using Bst"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel