0

我试图在数组的前后插入元素以模拟优先级列表,其中以高优先级输入的任务被放在顶部,但低优先级的任务在底部。我的问题是代码适用于在顶部或底部插入,但不显示输入的中间值。我在某处覆盖值吗?

我仅限于我可以使用的头文件。否则我会用向量来完成这一切

感谢您提供的任何帮助。

下面是我的代码:

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

void storeinfo() ;
void showinfo() ;
void menu() ;
void deleteinfo() ;
void inserttask() ;
void displayallinfo() ;
void selectionSort() ;
void inserttop(string) ;
void insertbottom(string) ;
int linsearch(string val) ;


class user 
{
    string firstname, lastname, currentteam, position, status ;
    int age ;
public:
    user() {};
    user(string fname, string lname, string cteam, string pos, string stat, int age) 
    {
        setFirstName(fname);
        setLastName(lname);
        setCurrentTeam(cteam);
        setPosition(pos);
        setStatus(stat);
        setAge(age);
    } ;

    void setFirstName(string fname)
        {firstname = fname;}
    void setLastName(string lname)
        {lastname = lname;}
    void setCurrentTeam(string cteam)
        {currentteam = cteam;}
    void setPosition(string pos)
        {position = pos;}
    void setStatus(string stat)
        {status = stat;}
    void setAge(int _age)
        {age = _age;}

    string getFirstName()
        {return firstname ;}
    string getLastName()
        {return lastname ;}
    string getCurrentTeam()
        {return currentteam ;}
    string getPosition()
        {return position ;}
    string getStatus()
        {return status ;}
    int getAge()
        {return age ;}
};

user player[20] ;
int arrlength = 3 ;
string list[6] ;
int listlength = 6;

int main()
{
    menu() ;

    cin.get() ;
    return 0 ;
}

void storeinfo()
{
    string firstname ;
    string lastname ;
    string currentteam ;
    string position;
    string status ;
    int age ;

    for (int i=0; i < 3; i++)
    {
        cout << "\n\n Enter First Name : " ; 
        cin >> firstname ;
        player[i].setFirstName(firstname) ;
        cout << "Enter Last Name : " ; 
        cin >> lastname ;
        player[i].setLastName(lastname) ;
        cout << "Enter Player's Age : " ; 
        cin >> age;
        player[i].setAge(age) ;
        cout << "Enter Current Team : " ; 
        cin >> currentteam ;
        player[i].setCurrentTeam(currentteam) ;
        cout << "Enter Position : " ; 
        cin >> position ;
        player[i].setPosition(position) ;
        cout << "Enter Status : " ; 
        cin >> status ;
        player[i].setStatus(status) ;

        cout << "\n\n\n" ;
    }

    /*cout << string(50, '\n');*/

    menu() ;

}

void showinfo()
{
    string search;
    int found ;


    cout << "Please Enter The Player's Last Name : " ;
    cin >> search ;

    found=linsearch(search);

    if (found==-1)
    {
        cout << "\n There is no player called " << search ;
    }
    else
    {
        cout << "\n First Name : " << player[found].getFirstName() << "\n" << "Last Name : " << player[found].getLastName() <<
            "\n" << "Age : " << player[found].getAge() << "\n" << "Current Team : " << player[found].getCurrentTeam() << 
            "\n" << "Position : " << player[found].getPosition() << "\n" << "Status :  " << player[found].getStatus()  << "\n\n";
    }

    cin.get() ;

    menu() ;

}

void deleteinfo()
{
    int arrlength = 3 ;
    string search ;
    int found ;

    cout << "\n Delete A Player's Information \n\n" ;
    cout << "Please Enter The Player's Last Name : " ;
    cin >> search ;

        found=linsearch(search);

    if (found==-1)
    {
        cout << "\n There is no player called " << search ;
    }
    else
    {
        for (int i=found + 1; i < arrlength; ++i)
        {
            player[i-1].setFirstName(player[i].getFirstName()) ;
            player[i-1].setLastName(player[i].getLastName()) ;
            player[i-1].setAge(player[i].getAge()) ;
            player[i-1].setCurrentTeam(player[i].getCurrentTeam()) ;
            player[i-1].setPosition(player[i].getPosition()) ;
            player[i-1].setStatus(player[i].getStatus()) ;
        }

        --arrlength ;

        cout << "\n Player has been deleted." ;

        player[arrlength].setAge(0) ;
        player[arrlength].setCurrentTeam("") ;
        player[arrlength].setFirstName("") ;
        player[arrlength].setLastName("") ;
        player[arrlength].setPosition("") ;
        player[arrlength].setStatus("");
    }

    cin.get() ;

    menu() ;
}

void displayallinfo()
{

    for (int index = 0; index < arrlength -1; index++) // sorting algorithm,
        {
            if (player[index].getAge() > player[index+1].getAge())
            {
                user temp; // create temporary user
                temp = player[index]; // position is set to temp
                player[index] =  player[index+1]; // swap takes place
                player[index+1] =  temp; // new value is what was stored in temp
            }
        }


    for (int i=0; i < 3; i++) //display all
    {
        cout << "\nFirst Name : " << player[i].getFirstName() << "\n" << "Last Name : " << player[i].getLastName() <<
            "\n" << "Age : " << player[i].getAge() << "\n" << "Current Team : " << player[i].getCurrentTeam() << 
            "\n" << "Position : " << player[i].getPosition() << "\n" << "Status :  " << player[i].getStatus()  << "\n\n";
    }

    cin.get() ;

    menu() ;
}

void inserttask()
{
    string task ;
    string priority ;

    for (int i = 0; i < 6; i++)
    {
        cout << "\nEnter name of task to be performed : " ;
        cin >> task ;

        cout << "\nEnter priority rating : " ;
        cout << "\nHigh or Low" ;
        cin >> priority ;
            if (priority == "High" | priority == "high")
            {
                inserttop(task) ;
            }
            else if(priority =="Low" | priority =="low")
            {
                insertbottom(task) ;
            }
    }

    for (int j = 0; j < 6; j++)
    {
        cout << list[j] ;
    }

    cin.get() ;

    menu() ;
}

void menu()
{
    cout << "\n\n MENU" << "\n" ;
    cout << "\n A. Store Player Information" ;
    cout << "\n B. Show Player Informaton" ;
    cout << "\n C. Delete Player Information" ;
    cout << "\n D. Display All Players Sorted By Age";
    cout << "\n E. Insert tasks by priority" ;
    cout << "\n Z. Exit \n\n" ;

    string x =  "";
    cin >> x ;

    if (x=="a" | x=="A")
    { 
        storeinfo() ;
    }
    else if (x=="b" | x=="B")
    {
        showinfo() ;
    }
    else if (x=="c" | x=="C")
    {
        deleteinfo() ;
    }
    else if (x=="d" | x=="D")
    {
        displayallinfo() ;
    }
    else if (x=="e" | x=="E")
    {
        inserttask() ;
    }
    else if (x=="z" | x=="Z")
    {
        exit(0) ;
    }
    else
    {
        cout << "Invalid Choice" ;
        menu() ;
    }
}

int linsearch(string val)
{
    for (int j=0; j <= 3; j++)
    {
        if  (player[j].getLastName()==val)
         return j ;         
    }
        return -1 ;
}

void inserttop(string task)
{
    int head = 0 ;
    string temp ;

    for (int i = 0; i < listlength; i++)
    {
        list[head + 1] = list[head] ;
        temp = task ;
        list[head] = temp ;
        temp = "" ;
    }
}

void insertbottom(string task)
{
    int tail = listlength ;
    string temp ;

    for (int i = 0; i < listlength; i++)
    {
        list[tail - 1] = list[tail] ;
        temp = task ;
        list[tail] = temp ;
        temp = "" ;
    }
}
4

1 回答 1

0

该算法存在一些问题。我将以这个函数为起点:

void inserttop(string task)
{
    int head = 0 ;
    string temp ;

    for (int i = 0; i < listlength; i++)
    {
        list[head + 1] = list[head] ;
        temp = task ;
        list[head] = temp ;
        temp = "" ;
    }
}

首先要注意的是,您循环了 的不同值i,但您在循环中根本不使用i。其次,您似乎添加了元素,但从不修改列表的长度(虽然不确定这些变量是如何声明的,所以我可能会在这里做出错误的假设)。第三,这个变量temp在这里是没有用的(尤其是你不需要在每个循环结束时将它重置为空字符串)。

您的意思可能与此更相似:

void inserttop(string task)
{
    // Expand the list by one... [bounds check should be done, omitted here]
    listlength++;

    // Overwrite each element with the previous one, making room
    // for the new element in the first position of the array.
    // Take care of proceeding backwards (see the indexes used
    // inside the cycle) to avoid overwriting the first value
    // all over the list.
    for (int i = 1; i <= listlength; i++)
    {
        list[listlength - i] = list[listlength - i - 1] ;
    }

    // Now you can set the first element...
    list[0] = task;
}

而且我相信insertbottom会遇到同样的问题,因此您可能必须做一些对称的事情:

// Again, don't forget to do bounds checking... this piece of code is just a hint
void insertbottom(string task)
{
    for (int i = 0; i < listlength - 1; i++)
    {
        list[i] = list[i + 1] ;
    }

    list[listlength] = task;

    listlength++;
}
于 2013-01-24T16:26:11.730 回答