在c ++中对类中的c字符串动态数组进行排序的最佳方法是什么?

问题描述 投票:0回答:2

在我当前的编程课程中,我曾被创建一个程序来接受用户输入的课程(由课程名称,课程等级,课程单位变量组成),并将它们存储在最大为10的动态生成的数组中。 >

由于我不熟悉面向对象的编程和类,但是我发现除了纯粹创建类之外,其他任何事情都非常困难。我想出了一种创建条目的方法,以及在使用程序头文件中存储的朋友功能(我认为这是名称?)创建这些条目后如何对其进行编辑的方法。

然而,现在我需要对数据进行排序(通过每个条目的名称变量),然后对其进行搜索,我发现很难继续进行。我知道如何制作可以完成这两项功能的函数(就像我在上一堂课中所做的那样),但是在涉及到类的情况下做到这一点非常困难。

我的头文件:

#ifndef COURSE_H
#define COURSE_H

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include <vector>


class Course
{
private:
    char name[10] = ""; //name of course
    char grade; //grade in course
    int units; //units in course

public:
    Course()
    {
        name;
        grade;
        units;
    }

    void read() //Initializes course and collects information from user
    {
        std::cout << "\nEnter course name: ";
        std::cin.getline(name, 10, '\n');
        std::cout << "\nEnter number of units: ";
        std::cin >> units;
        std::cout << "\nEnter grade received: ";
        std::cin >> grade;
        std::cin.ignore();
    }

    void display() const //Displays course to user
    {
        std::cout << name << ' ' << units << ' ' << grade << std::endl;
    }

    ~Course() //Destructor frees allocated dynamic memory
    {
        std::cout << "\nDeleting any dynamically created object";
    }
};

#endif // COURSE_H

我的主要源文件:

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include <vector>
#include "courses.h"

int menu();
void add(Course* co_ptr[], int& size);
void edit(Course* co_ptr[], int size);
void swap_ptrs(Course*& pt1, Course*& pt2);

int main()
{
    Course* courses[10] = {};
    int selection;

    int size = 0;
    do
    {
        selection = menu();

        if (selection == 1)
        {
            if (size < 10)
                add(courses, size);
            else
                std::cout << "\nUnable to add more classes.";
        }
        else if (selection == 2)
        {
            edit(courses, size);
        }
        else if (selection == 3)
        {

        }
        else if (selection == 4)
        {

        }
        else if (selection == 5)
        {

        }
        else if (selection == 6)
        {

        }
        else if (selection == 7)
        {
            break;
        }
        else
        {
            std::cout << "\nInvalid selection.";
        }
    } while (selection != 7);

    std::cout << "\nPress any key to exit.";
    (void)_getch();
    return 0;
}

我的函数源文件:

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include <vector>
#include "courses.h"

int menu()
{
    int selection;

    std::cout << "\nSelect one of the following actions: " << std::endl
        << "1. Add new course" << std::endl
        << "2. Edit an existing course" << std::endl
        << "3. Display a course" << std::endl
        << "4. List all courses" << std::endl
        << "5. Display GPA" << std::endl
        << "6. Delete all courses" << std::endl
        << "7. Quit";
    std::cout << "\nEnter selection number: ";
    std::cin >> selection;
    std::cin.ignore();

    return selection;
}

void add(Course* co_ptr[], int& size)
{
    co_ptr[size] = new Course;
    co_ptr[size]->read();
    size++;
}

void edit(Course* co_ptr[], int size)
{
    int selection;
    for (int i = 0; i < size; i++)
    {
        std::cout << std::endl << i << ". ";
        co_ptr[i]->display();
    }

    std::cout << "Enter your selection: ";
    std::cin >> selection;
    std::cin.ignore();

    co_ptr[selection]->read();
}

我最后一次创建排序函数的尝试(我试图在标头中创建它,因为当我将旧的排序代码作为普通函数移植时,由于这些变量是“私有”的,它无法访问所需的数据。 )

void Course::sort_name(Course* co_ptr[], int size) //has to be apart of the class (Course::) to have access to the name data
{
    bool swap;

    do
    {
        swap = false;
        for (int i = 0; i < size - 1; i++)
        {
            if (strcmp(co_ptr[i]->name, co_ptr[i + 1]->name) > 0) //We're now comparing and swapping pointers
            {
                swap_ptrs(co_ptr[i], co_ptr[i + 1]);
                swap = true;
            }
        }
    } while (swap);
}

最后我的swap_ptrs函数也位于函数源文件中:

void swap_ptrs(Course*& pt1, Course*& pt2) //Passes the pointers by reference
{
    Course* tmp = pt1;
    pt1 = pt2;
    pt2 = tmp;
}

对于这么长的帖子,我很抱歉,但是这个项目确实很艰难,我感觉自己没有取得任何进展。

在我当前的编程课程中,我曾受命创建一个程序来接受用户输入的课程(由课程名称,课程等级,课程单位变量组成,并将其存储在...中)]

c++ class sorting computer-science
2个回答
0
投票

不要使用char数组。使用字符串代替

char name[10]; //bad form

0
投票

您可以使用诸如std :: list或std :: vector之类的容器来存储课程对象而不是数组,并使用std::sort和您选择的比较方法。那可能是最简单的。如果您必须坚持将数组作为容器,则还可以使用如下排序:

std::sort(array, array + array_size,[](Course* a, Course* b) {
        return strcmp(a->name, b->name) < 0;   
    });
© www.soinside.com 2019 - 2024. All rights reserved.