我正在此列表中进行最后一个练习(称为毕业):http://www.cplusplus.com/forum/articles/12974/,但是有一个主要问题。我编写的代码可以运行,但是有时会崩溃(删除半个兔子之后),有时在程序第一次删除半个兔子之后,有时在10个这样的循环之后,请注意,我还没有实现很多,因为我想修复此错误,在您的帮助下。我也知道这不是代码审查,但是有关样式和改进的一些小技巧也很不错。所以这是我到目前为止编写的代码:
Main.cpp:
include bunnyList.h
include windows.h
using namespace std;
int main(){
srand(time(NULL));
bunnyList Colony;
int turns = 0;
Colony.setUp();
while(Colony.getColonySize() > 0){
//New turn
Colony.increaseAgeAndKill();
Colony.breedBunnies();
std::cout << "Turn: "<< turns << ". Colony size: " << Colony.getColonySize() << std::endl;
//Get rid of these food eaters
if(Colony.getColonySize() > 1000){
std::cout << "500 bunnies died!" << std::endl;
Colony.killHalfBunnies();
}
Sleep(100);
turns++;;
}
}
bunnyList.h:
#ifndef BUNNYLIST_H
#define BUNNYLIST_H
#include <stdlib.h>
#include "node.h"
#include <time.h>
#include <iostream>
#include <string>
const int numOfNames = 4;
const int numOfColors = 4;
const int bunniesIni = 5;
const std::string colors[numOfColors] = {"Black", "White", "Brown", "Spotted"};
const std::string maleNames[numOfNames] = {"Joe", "Rafael", "Buby", "Messi"};
const std::string femaleNames[numOfNames] = {"Reichel", "Agnesa", "Mr Flufy", "Flower"};
class bunnyList{
private:
node *head;
int noOfBunnies;
node *current, *prev;
public:
bunnyList();
void newBunny(std::string);
void killHalfBunnies();
void increaseAgeAndKill();
void deleteNode();
void breedBunnies();
void setUp();
int getRandomNumber(int) const;
std::string getRandomColor();
std::string getRandomName(bool);
bool isMaleRandom();
int getColonySize() const;
};
#endif
bunnyList.cpp:
#include "bunnyList.h"
bunnyList::bunnyList(){
noOfBunnies = 0;
}
void bunnyList::setUp(){
std::string temp = "";
head = NULL;
for(int i = 0; i <= bunniesIni; i++){
newBunny(temp);
}
}
void bunnyList::killHalfBunnies(){
prev = head;
current = head;
while(noOfBunnies > 500){
if(getRandomNumber(2) == 1){
deleteNode();
continue;
} else if(current == NULL){
current = head;
prev = head;
} else {
prev = current;
current = current->next;
continue;
}
}
}
void bunnyList::newBunny(std::string color){
node *bunny = new node();
node *temp = head;
if(color == ""){
bunny->color = getRandomColor();
} else {
bunny->color = color;
}
bunny->isMale = isMaleRandom();
bunny->name = getRandomName(bunny->isMale);
bunny->age = 0;
bunny->next = NULL;
bunny->isBreedable = 0;
if(head == NULL){
head = bunny;
return;
}
while(temp->next != NULL){
temp = temp->next;
}
temp->next = bunny;
noOfBunnies++;
}
void bunnyList::increaseAgeAndKill(){
current = head;
prev = head;
while(current != NULL){
current->age++;
//Check if bunny can breed
if(current->age > 2){
current->isBreedable = 1;
}
//Check if its time to die :/
if(current->age > 10){
deleteNode();
}
prev = current;
current = current->next;
}
current = head;
prev = head;
}
void bunnyList::breedBunnies(){
node *temp = head;
bool oneMale = 0;
int femaleCount = 0;
//Check if there is at least one breedable male
while(temp!=NULL){
if(temp->isMale && temp->isBreedable){
oneMale = 1;
break;
}
temp = temp->next;
}
//For every female bunny over 2 years old a new bunny is born
temp = head;
if(oneMale){
while(temp != NULL){
if(temp->isMale == 0 && temp->isBreedable){
newBunny(temp->color);
}
temp = temp->next;
}
}
}
void bunnyList::deleteNode(){
if(current==head){
head = current->next;
prev = head;
delete current;
current = head;
noOfBunnies--;
} else if(current->next==NULL){
delete current;
prev->next = NULL;
prev = head;
current = head;
noOfBunnies--;
} else {
prev->next = current->next;
current->next = NULL;
delete current;
current = prev->next;
noOfBunnies--;
}
}
std::string bunnyList::getRandomName(bool isMale){
int r = getRandomNumber(numOfNames - 1);
if(isMale)
return maleNames[r];
return femaleNames[r];
}
std::string bunnyList::getRandomColor(){
int r = getRandomNumber(numOfColors - 1);
return colors[r];
}
bool bunnyList::isMaleRandom(){
if(getRandomNumber(2) == 1) {return true;}
return false;
}
int bunnyList::getRandomNumber(int limit) const{
return rand() % limit + 1;
}
int bunnyList::getColonySize() const{
return noOfBunnies;
}
node.h:
#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED
#include <string>
class node {
friend class bunnyList;
private:
std::string name;
int age;
std::string color;
bool isMale;
node *next;
bool isBreedable;
public:
};
#endif // NODE_H_INCLUDED
谢谢您的帮助。
最佳答案
既然您要求审查...
切勿写using namespace std
。决不。就在今天早上,SO上出现了一个问题,该问题的起因是那条臭名昭著的路线。我不知道是谁和为什么暗示这是一种好方法-应该在这本书上写一本书。如果我愿意的话,那么作者将永远被迫从每个文件中手动删除这一行。
即使不从代码中读一行,仅凭解释,我也知道问题很可能与内存管理有关(如100%一样)。您正在释放未正确分配的内存,正在释放相同的内存两次,或者正在释放根本没有分配的内存,或者正在释放内存之后访问内存。查看您的delete
并检查它们。
在风格上。您的代码基本上是业务逻辑感知列表的实现。通常,这不是一个好习惯。实现通用列表,支持添加,删除和其他通用列表操作,比在该通用列表之上实现业务逻辑要好得多。
不要在列表中使用current
。而是在删除功能中传递要删除的节点。
最后,在调试器中运行程序,然后查看要删除的变量。
编辑
回答问题。
这就是我所说的业务逻辑分离。有一个通用的数据结构,称为列表。它可以是任何东西的列表,无论是兔子还是太空火箭,都没关系-但它仍然支持基本的列表操作。显然,最重要的两个是插入和删除,但这不是通用列表的唯一操作。您可以阅读列表(数据结构)上的Wikipedia以获得一般想法,并在实现时查看std :: list。现在,您有了列表的特定用例,即兔子列表。对于该特定用例,您将在通用列表的顶部添加功能。为了进一步说明,从列表中删除项目是通用列表支持的功能。但是,当穷人的年龄达到10岁时,“杀死兔子”是一种商业逻辑。它包含遍历兔子列表(由通用列表提供),检查年龄并做出消除该生物的决定(业务逻辑级别)以及删除元素(通用列表)。如果要使用std::list
编写此代码,则大约需要以下内容:
std::list<Bunny> bunnies;
for (auto bunny = bunnies.cbegin(), end = bunnies.cend(); bunny != end; ++bunny) {
if (bunny->age() > 10)
bunny = bunnies.erase(bunny);
}