给定一个创建的带有货物的链表,我需要使用函数检查实际序列或"train"是否有效
Given a created linked list with cargos, I need to check if the actual sequence or "train" is valid using a function
您好,我正在做一个项目,我需要比较并检查我制作的火车是否有效,条件如下:
-如果满足以下任何条件,则列车被视为无效:
-氧化剂货物不能与可燃货物相邻
-3 可燃车不能排成一排
-5辆生物车不能排成一排
-生物汽车只能在一侧具有放射性(不能具有放射性->生物->放射性(
我的问题是,我应该如何实现它以正确匹配条件?,我应该如何修复它?。 这段代码没有错误,只是它与我想要它做的事情不匹配,我得到了我需要做的事情,尽管无法正确实现它。谢谢。
这是我到目前为止所拥有的:
enum CARGO_TYPE { //All of the possible cargo types (or car types)
BIOLOGICAL,
POISONOUS,
COMBUSTIBLE,
OXIDIZER,
RADIOACTIVE,
LOCOMOTIVE,
CABOOSE
};
// Name: isValid
// PreCondition: none
// PostCondition: returns a boolean indicating whether the current
// arrangement of the train is valid
bool Train :: isValid() const{
Car *current = m_head;
Car *traverse = current->getnext();
//iterates over the linked list, here's my logic
while(current->getNext() != NULL){
Car *temp = new Car(cargo)
//checks the cargo sequence for OXIDIZER adjacent to COMBUSTIBLE
if(current->getNext()->getType() == OXIDIZER && traverse->getNext->getType() == COMBUSTIBLE){
return false;
}
//checks the 3 COMBUSTIBLE in a row, just sub in COMBUSTIBLE inside
if(traverse == traverse->getNext()== traverse->getNext()->getNext()){
return false;
}
//checsk the 5 BIOLOGICAL cars in a row
if((traverse == traverse->getNext())== traverse->getNext()->getNext()){
return false;
}
// checks if BIOLOGICAL only have one RADIOACTIVE on one side.
if(traverse == traverse->getNext()== traverse->getNext()->getNext()){
}
return true;
}
}
按照
你之前的做法,你可以试试这个......虽然最初没有编译以检查它。
bool Train :: isValid() const
{
Car *current = m_head;
Car *traverse;
//iterates over the linked list
while(current->getNext() != NULL)
{
traverse = current->getNext();
//checks the cargo sequence for OXIDIZER adjacent to COMBUSTIBLE
// Need to check both ways, -O-C- and -C-O-
if((current->getType() == OXIDIZER && traverse->getType() == COMBUSTIBLE) ||
(current->getType() == COMBUSTIBLE && traverse->getType() == OXIDIZER)){
return false;
}
//checks the 3 COMBUSTIBLE in a row, just sub in COMBUSTIBLE inside
if(current->getType() == COMBUSTIBLE && traverse->getType() == COMBUSTIBLE){
//Need the inner conditional to make sure traverse isnt the
//end of the train
if(traverse->getNext() != NULL && traverse->getNext()->getType() == COMBUSTIBLE){
return false;
}
}
//check the 5 BIOLOGICAL cars in a row
if(current->getType() == BIOLOGICAL && traverse->getType() == BIOLOGICAL)
{
int count = 2; //already counted the first two in conditional above
while(traverse->getNext() != NULL && count < 5)
{
traverse = traverse->getNext();
if(traverse->getType() == BIOLOGICAL)
count++;
else
break;
}
if(count == 5)//if 5 biologicals were counted in a row, return false
return false;
}
// checks if BIOLOGICAL only have one RADIOACTIVE on one side.
if(current->getType() == RADIOACTIVE && traverse->getType() == BIOLOGICAL)
if(traverse->getNext() != NULL && traverse->getNext()->getType() == RADIOACTIVE)
return false
current = traverse; //continue iterating through train
}
return true;//passes everything, train should be good
}
使用风险自负,有更好的方法来做你想做的事......只是给出了与您的想法相似的东西。
相关文章:
- 检查函数返回类型是否与STL容器类型值相同
- 如何检查函数是否在LLVM Instrumentation pass的ModulePass的系统头文件中定义?
- 何时在类中检查函数的定义?
- 素数检查 C++ 函数输出非素数的数字
- 如何检查函数是否真的获得了定义为 const 的变量?
- 检查函数是否可调用
- 如何检查函数是在C++单元测试中的析构函数中调用的
- 使用 SFINAE 检查函数 std::to_string 是否存在类型
- 如何检查函数模板是否专用?
- 如何检查函数中的模板参数是否与给定类型别名的专用化匹配
- 如何检查函数中不同变量的大小
- C++如何检查函数是否接收到无符号int
- 使用模板递归检查函数方法是否存在
- 如何在Visual studio C++中检查函数运行时间
- 检查函数是否单调
- 如何检查函数是否在编译时在全局范围内声明
- 检查函数是否被调用
- Boost.Hana:如何检查函数是否具有特定类型的专用性
- 如何检查函数参数是否恒定
- C++ 编译时检查函数参数