使用数组和向量

时间:2017-03-18 18:45:02

标签: c++ class

我需要帮助使用伪代码来删除和显示所有族函数。 它应该(1)添加家庭(有1或2个父母和0或更多孩子)2)按姓氏删除家庭(3)显示按姓氏搜索的家庭和(4)显示列表所有家庭。感谢您的任何意见或建议! 这就是我所拥有的:

HEADER:

    using namespace std;

struct Person{
     char* FN; char* LN; int age;
};

class Family {
    private:
    char* name;
    Person parent[2];
    Person children[10];
    public:
    void setName(char* n);
    char * getName();
    void setParent(char FN[], char LN[], int a, int i );
    void setChildren(char FN[], char LN[], int a, int i);

};

家庭班:

void Family::setName(char n[]){
    name = n;
}

char* Family::getName(){
    return name;
}
void Family::setParent(char FN[], char LN[], int a, int i ){
    parent[i].FN = FN;
    parent[i].LN = LN;
    parent[i].age = a;
}
void Family::setChildren(char FN[], char LN[], int a, int i ){
    children[i].FN = FN;
    children[i].LN = LN;
    children[i].age = a;
}

char printMenu();
void addFamily(std::list<Family*> list);
void printAll(std::list<Family*> list);
void prinFamily(std::list<Family*> list,char * n);
void deleteFamily(std::list<Family*> list,char * n);

MAIN
       using namespace std;

void printMenu();
int getInput();
int main()
{
    vector<Family*> mylist;
    bool fin = false;
    int selection;

    do
    {
        printMenu();
        selection = getInput();

        switch (selection)
        {
        case 1:
            addFamily(mylist);
            break;


        case 2:
            char* name;
            cout << "Enter the Name to Delete: ";
            cin >> name;
            deleteFamily(myList, name);
            break;

        case 3:
            char* name;
            cout << "Enter Family Last name: ";
            cin >> name;
            printFamily(mylist, name);
            break;

        case 4:
            printAll(mylist);
            break;

        case 5:
            if (!myList.empty())
            {
                cout << myList[0].getName() << " says bye!\n";
            }
            fin = true;
            break;
        }

        return 0;
    }

void printAll(vector<Family*> list) {
    for (size_t i = 0; i < myList.size(); i++)
    {
        cout << "\nFamily #: " << i;
        cout << "\nName: " << myList[i].getName();
        cout << "\nAge: " << myList[i].getAge() << endl << endl;
    }

}
void printFamily(vector <Family*> list, char * n) {


            cout << "Name: " << (*it)->getName() << endl;


    //if wrong name
    cout << "Not found" << endl;

void deleteFamily(vector <Family*> list, char * n) {


    myList.erase(myList.begin() + name); 


    cout << "Not found" << endl;
}
void addFamily(vector <Family*> list) {

    Person* p = new Person;
    Family* f = new Family;
    char* name;
    int numOfP;
    cout << "Enter Name :";
    cin >> name;
    f->setName(name);
    cout << "Enter Number of Parent :";
    cin >> numOfP;
    for (int i = 0;i<numOfP;i++) {
        char* fname;
        char* lname;
        int age;
        cout << "Enter Parent First Name :";
        cin >> fname;
        cout << "Enter Parent Last Name :";
        cin >> lname;
        cout << "Enter Parent Age :";
        cin >> age;
        f->setParent(fname, lname, age, i);
    }
    cout << "Enter Number of Child :";
    cin >> numOfP;
    for (int i = 0;i<numOfP;i++) {
        char* fname;
        char* lname;
        int age;
        cout << "Enter Child First Name :";
        cin >> fname;
        cout << "Enter Child Last Name :";
        cin >> lname;
        cout << "Enter Child Age :";
        cin >> age;
        f->setChildren(fname, lname, age, i);
    }
    myList.push_back(f); //not sure if this is right
}

void printMenu() {

    cout << "\n 1.Add Family " << endl;
    cout << " 2. Delete Family By Last Name " << endl;
    cout << " 3. Display Families " << endl;
    cout << " 4. Display All Famillies " << endl;
    cout << " 5. Quit" << endl; 

}

int getInput()
{
    int choice;
    cout << "\nEnter a number to select an option. \n";
    cin >> choice;
    return choice;
}

1 个答案:

答案 0 :(得分:0)

std::list实现了一个双向链表,是一种非常繁重且缓慢的数据存储方式。 请考虑使用std::vector。它是一个连续的动态阵列,可以提供更好的性能。