使用函数填充数组中的数字.如果数组已满,则该数字将在第二个数组中填充

use function to filled an array in number. if the array is full, then the number will be filled on the second array

本文关键字:数组 数字 填充 第二个 如果 函数      更新时间:2023-10-16

我在一个名为registered的数组中存储已注册的学生id。如果数组已满,它会自动将学生id存储在另一个名为waitlist的数组中。这个操作将通过一个函数来完成。我不能使我的计算机存储到候补名单阵列的学生id当我注册的数组是满的。我只能使用特定的函数来存储学生id到两个数组。我还需要通过使用相同的功能打印已注册的学生id列表和候补名单。我不知道该怎么做,我找不到一个好的教程和书甚至没有帮助。谢谢你。

这是我的代码。

#include <iostream>
using namespace std;
const int maxCap = 10;
void showOption();
void addToRear(int arr[], int& howMany, int value);
void printArray(int arr[], int howMany);
bool isFull(int capacity, int howMany); 
bool isEmpty(int howMany);
int main()
{
    bool repeat = false;
    bool repeatOption = false;
    int reg[maxCap];
    int waitlist[maxCap];
    int option, value, capacity;
    int howMany = 0;
    int registered = 0;
    int waitlisted = 0;
    do
    {
        showOption();
        cout << "please enter your option: 1 ~ 5 " << endl;
        cin >> option;

        switch (option)
        {
        case 1:
            if(isEmpty(capacity, howMany) == true)
            addToRear(reg, howMany, maxCap);
            if(isFull(capacity, howMany) == true)
            addToRear(waitlist, howMany, maxCap);
            break;
        case 3:
            printArray(reg, howMany);
            break;
        case 4:
            printArray(waitlist, howMany);
            break;
        case 5:
            cout << "thank you for using this program. " << endl;
            return 0;
            break;
        default:
            cout << "thank you for using this program. " << endl;
            break;
        }
        //cout << "there are " << howMany << " registered student in this class. " << endl;
        cout << "back to main menu? enter 1 for yes:      " << endl;
        cin >> repeatOption;
    }while(repeatOption);
}
void showOption()
{
    cout << "Main Menu: " << endl;
    cout << "1) Register Student. " << endl;
    cout << "2) Unregister Student. " << endl;
    cout << "3) Print list of registered student. " << endl;
    cout << "4) Print list of waitlisted student. " << endl;
    cout << "5) Exit. " << endl;
    cout << " " << endl;
}
//void getID(int& value)
//enter student id and store it in array register and waitlist
void addToRear(int arr[], int& howMany, int value)
{
    cout << "there are only " << maxCap << " spot for student to be registered." << endl;
    cout << "if there are " << maxCap << " students registered in this class, " << endl;
    cout << "then the rest of the student will be put on waitlist. " << endl;
    cout << " " << endl;
    cout << "enter student ID. " << endl;
    cout << "enter negative value to stop. " << endl;
    int id;
    cin >> id;
    while((id >= 0) && (howMany < maxCap))
    {
        arr[howMany] = id;
        howMany++; 
        cin >> id;
    }

}
//print list of student that is registered or waitlisted
void printArray(int arr[], int howMany)
{
    cout << "there are " << howMany << " number of student in registered list. " << endl;
    cout << "the student ID number is " << endl;
    for (int index = 0; index < howMany; index++)
    {
        cout << arr[index];
        cout << " " << endl;
    }
}
bool isFull(int capacity, int howMany)
{
/if((capacity < 1) || (howMany > 9))
        return true;
    else
        return false;
}
bool isEmpty(int capacity, int howMany)
{
    if((capacity > 9) || (howMany < 1))
        return true;
    else
        return false;
}
  1. 拥有一个同时包含已注册和等待列表的候选人的数组会更容易。
  2. 添加时,您只需要有一个结束索引。因此,只要指数小于你的截止数字,你的候选人就注册了。
  3. 删除时,必须在删除的一个候选条目后将列表向上移动一个空格。这样,注册名单将自动更新,以减少候补名单候选人1。
  4. 打印你必须检查索引,如果它大于截止,你已经注册和等待名单。

您的代码无法识别注册列表和等待列表的满和空条件。我在下面的代码中做了一些修改您可以尝试下面的代码片段:

case 1:
            int len = sizeof(reg)/sizeof(reg[0]); // Get the length of reg list
            int waitlistLen = sizeof(waitlist)/sizeof(waitlist[0]); //get length of wait list
            if(maxCap > len)               // Check if reg list not full. Add to reg list
                addToRear(reg, howMany, maxCap);
            else if(maxCap > waitlistLen) // if reg list is full. Check if wait list is full or not
                addToRear(waitlist, howMany, maxCap);
            else                         // if both the list are full show user option menu and break.
                showOption();
            break;