📜  C++中的菜单驱动程序对数组执行各种基本操作(1)

📅  最后修改于: 2023-12-03 15:14:03.855000             🧑  作者: Mango

C++中的菜单驱动程序对数组执行各种基本操作

在C++中,数组是一种非常有用的数据结构,它可以存储一系列相同类型的数据。在本文中,我们将介绍如何使用菜单驱动程序对数组执行各种基本操作。

准备工作

在开始编写程序之前,我们需要先定义一个数组和一些变量,如下所示:

const int SIZE = 10; //数组大小
int menuChoice; //菜单选项
int arr[SIZE]; //数组
int numElements = 0; //数组中已有元素个数
int searchKey; //需要查找的元素
int index; //元素的索引
int insertValue; //需要插入的元素
int deleteValue; //需要删除的元素
编写菜单驱动程序

接下来,我们将编写一个菜单驱动程序,使用switch语句实现不同操作的选项。初始菜单如下所示:

do {
    cout << "Menu:\n";
    cout << "1. Insert\n";
    cout << "2. Delete\n";
    cout << "3. Find\n";
    cout << "4. Print\n";
    cout << "5. Exit\n";
    cout << "Enter your choice: ";
    cin >> menuChoice;
    cout << endl;

    switch (menuChoice) {
        case 1:
            //插入操作
            break;
        case 2:
            //删除操作
            break;
        case 3:
            //查找操作
            break;
        case 4:
            //打印数组
            break;
        case 5:
            //退出程序
            break;
        default:
            cout << "Invalid choice. Try again.\n";
            break;
    }
} while (menuChoice != 5);

接下来,我们将介绍如何实现不同操作的选项。

插入操作

要向数组中插入元素,我们需要询问用户要插入的值,并将其存储在insertValue变量中。然后,我们将元素插入到数组的下一个空闲位置,如下所示:

case 1:
    //插入操作
    if (numElements == SIZE) {
        cout << "Array is full. Cannot insert element.\n";
    }
    else {
        cout << "Enter value to insert: ";
        cin >> insertValue;

        arr[numElements] = insertValue;
        numElements++;
    }
    break;
删除操作

要从数组中删除元素,我们需要询问用户要删除的值,并将其存储在deleteValue变量中。然后,我们遍历数组,找到要删除的元素,并将其从数组中删除,如下所示:

case 2:
    //删除操作
    if (numElements == 0) {
        cout << "Array is empty. Cannot delete element.\n";
    }
    else {
        cout << "Enter value to delete: ";
        cin >> deleteValue;

        index = -1;
        for (int i = 0; i < numElements; i++) {
            if (arr[i] == deleteValue) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            cout << "Element not found. Cannot delete.\n";
        }
        else {
            for (int i = index; i < numElements - 1; i++) {
                arr[i] = arr[i + 1];
            }
            numElements--;
            cout << "Element deleted.\n";
        }
    }
    break;
查找操作

要查找数组中的元素,我们需要询问用户要查找的值,并将其存储在searchKey变量中。然后,我们遍历数组,查找要查找的元素,并返回其索引,如下所示:

case 3:
    //查找操作
    if (numElements == 0) {
        cout << "Array is empty. Cannot find element.\n";
    }
    else {
        cout << "Enter value to find: ";
        cin >> searchKey;

        index = -1;
        for (int i = 0; i < numElements; i++) {
            if (arr[i] == searchKey) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            cout << "Element not found.\n";
        }
        else {
            cout << "Element found at index " << index << ".\n";
        }
    }
    break;
打印数组

要打印数组,我们只需要遍历数组,并将每个元素打印到屏幕上,如下所示:

case 4:
    //打印数组
    if (numElements == 0) {
        cout << "Array is empty.\n";
    }
    else {
        cout << "Array:";
        for (int i = 0; i < numElements; i++) {
            cout << " " << arr[i];
        }
        cout << endl;
    }
    break;
完整代码

最终,我们将所有代码组合成一个完整的程序,如下所示:

#include <iostream>

using namespace std;

const int SIZE = 10; //数组大小
int menuChoice; //菜单选项
int arr[SIZE]; //数组
int numElements = 0; //数组中已有元素个数
int searchKey; //需要查找的元素
int index; //元素的索引
int insertValue; //需要插入的元素
int deleteValue; //需要删除的元素

int main() {
    do {
        cout << "Menu:\n";
        cout << "1. Insert\n";
        cout << "2. Delete\n";
        cout << "3. Find\n";
        cout << "4. Print\n";
        cout << "5. Exit\n";
        cout << "Enter your choice: ";
        cin >> menuChoice;
        cout << endl;

        switch (menuChoice) {
            case 1:
                //插入操作
                if (numElements == SIZE) {
                    cout << "Array is full. Cannot insert element.\n";
                }
                else {
                    cout << "Enter value to insert: ";
                    cin >> insertValue;

                    arr[numElements] = insertValue;
                    numElements++;
                }
                break;
            case 2:
                //删除操作
                if (numElements == 0) {
                    cout << "Array is empty. Cannot delete element.\n";
                }
                else {
                    cout << "Enter value to delete: ";
                    cin >> deleteValue;

                    index = -1;
                    for (int i = 0; i < numElements; i++) {
                        if (arr[i] == deleteValue) {
                            index = i;
                            break;
                        }
                    }

                    if (index == -1) {
                        cout << "Element not found. Cannot delete.\n";
                    }
                    else {
                        for (int i = index; i < numElements - 1; i++) {
                            arr[i] = arr[i + 1];
                        }
                        numElements--;
                        cout << "Element deleted.\n";
                    }
                }
                break;
            case 3:
                //查找操作
                if (numElements == 0) {
                    cout << "Array is empty. Cannot find element.\n";
                }
                else {
                    cout << "Enter value to find: ";
                    cin >> searchKey;

                    index = -1;
                    for (int i = 0; i < numElements; i++) {
                        if (arr[i] == searchKey) {
                            index = i;
                            break;
                        }
                    }

                    if (index == -1) {
                        cout << "Element not found.\n";
                    }
                    else {
                        cout << "Element found at index " << index << ".\n";
                    }
                }
                break;
            case 4:
                //打印数组
                if (numElements == 0) {
                    cout << "Array is empty.\n";
                }
                else {
                    cout << "Array:";
                    for (int i = 0; i < numElements; i++) {
                        cout << " " << arr[i];
                    }
                    cout << endl;
                }
                break;
            case 5:
                //退出程序
                break;
            default:
                cout << "Invalid choice. Try again.\n";
                break;
        }
    } while (menuChoice != 5);

    return 0;
}

以上就是使用C++中的菜单驱动程序对数组执行各种基本操作的介绍。