加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C类问题

发布时间:2020-12-16 09:44:55 所属栏目:百科 来源:网络整理
导读:我是一所独立寄宿学校的老师,我正在尝试用C语言编写一个程序,该程序会随机将学生安排在我们餐厅的餐桌旁,这样他们每周都会与不同的学生和不同的工作人员坐在一起.理想情况下,在一定时期内,他们不会两次坐在同一张桌子上,并且尽可能多的不同学生.我用 Python
我是一所独立寄宿学校的老师,我正在尝试用C语言编写一个程序,该程序会随机将学生安排在我们餐厅的餐桌旁,这样他们每周都会与不同的学生和不同的工作人员坐在一起.理想情况下,在一定时期内,他们不会两次坐在同一张桌子上,并且尽可能多的不同学生.我用 Python创建了这个程序,它运行得很好(非常好).出于各种原因,我试图把它移到C(我根本不知道),所以我可以把它交给寄宿人员.学生和教职员工(以及表格容量)都是从文本文件中读取的.我创建了两个自定义类,一个用于学生,一个用于表,用于处理数据.以下是两个类头文件:

Table.h

#pragma once
#include <iostream>
#include "Student.h"
#include <vector>

using namespace std;

class Table
{
    // Private class variables
    string staff;
    int numSeats;
    vector<Student> seating;
public:
    Table(); // Default constructor
    Table(string s,int n);
    Table(const Table& that) : staff(that.staff),numSeats(that.numSeats)
    {
    }
    // Copy Constructor
    Table& operator=(const Table& that)
    {
        staff = that.staff;
        numSeats = that.numSeats;
        return *this;
    }
    int getNumSeats();
    string getStaffName();
    void addStudent(Student student);
    void removeStudent(Student student);
    void clearStudents();
    vector<Student> getTableSeating();
    int getRemainingSeats();
    ~Table(void);
};

这是学生班级文件:

#pragma once
#include <iostream>
#include <vector>

using namespace std;

class Student
{
    string name;
    string country;
    vector<int> tablesSatAt;
public:
    Student(string n,string c);
    Student();
    Student(const Student& that) : name(that.name),country(that.country)
    {
    }
    Student& operator=(const Student& that)
    {
        name = that.name;
        country = that.country;
        return *this;
    }
    string getName();
    string getCountry();
    void addTable(int tableNumber);
    void removeTable(int tableNumber);
    bool satAtTable(int tableNumber);
    friend bool operator==(Student s1,Student s2);
    friend bool operator!=(Student s1,Student s2);
    ~Student(void);
};

bool operator==(Student s1,Student s2);
bool operator!=(Student s1,Student s2);

这是执行繁重工作的递归函数:

bool seatRecursive(vector<Student> &tempStudents,vector<Table> &tempTables)
{
    if (tempStudents.size() == 0) return true; //base case

    Student nextStudent = randomSelect(tempStudents);
    for (vector<int>::size_type i=0; i<tempTables.size(); i++)
    {
        if (tempTables[i].getRemainingSeats() > 0 && !nextStudent.satAtTable(i))
        {
            addStudentToTable(nextStudent,tempTables,i);
            if (seatRecursive(tempStudents,tempTables)) return true;
            else 
            {
                removeStudentFromTable(nextStudent,i);
                tempStudents.push_back(nextStudent);
            }
        }
    }
    return false;
}

大部分都有效.当我运行该程序时,我得到一个有10周座位的文本文件,但所有桌面座位都是相同的.即如果我是一名特定的工作人员,我有同样的孩子在我的餐桌上待了整整10个星期.我有一个int的向量,应该存储学生随时间坐着的表号.在调试时,我注意到那些表号不存储在该向量中,它总是空的.我的问题是,我无法弄清楚为什么会这样.是因为我通过引用传递向量吗?是否与指针有关,即使我没有明确声明指针?

非常感谢任何建议,如有必要,我可以粘贴其余的代码.

布赖恩

解决方法

我看到Student和Table类的复制构造函数(和赋值构造函数)忘记了复制包含的STL向量(向量< Student> seat和vector< int> tablesSatAt).由于重载这些构造函数,您应该通过向量复制构造函数复制它们,因为它不是隐式完成的.

在不复制它们的情况下,每当学生或表围绕向量移动(或分配给临时)时,内部向量将被丢弃以用于新对象.

正如Useless在这种情况下的评论所指出的,你不需要声明它们,因为rule of three不适用:你不需要析构函数,所以你可能不需要两个拷贝构造函数.

注意:您在注释中称为复制构造函数的是复制赋值构造函数.之前的一个是真正的拷贝构造函数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读