是鱼汁啊 2020-04-26 12:45 采纳率: 0%
浏览 124

请问能帮忙完善下面的圈猫程序吗?

#pragma once
#ifndef CATS_H
#define CATS_H

class Cat {
public:
Cat();
const char* getFurColor();
const char* getHairLength();
const char* getEyeColor();
private:
void setFurColor();
void setHairLength();
void setEyeColor();
char* furColor;
char* hairLength;
char* eyeColor;
};

#endif
// catsM.cpp
#include "cats.h"
#include
#include
#include

Cat::Cat()
{
    srand(time(0));
    setFurColor();
    setEyeColor();
    setHairLength();
}

const char* Cat::getFurColor()
{
    /* Write function implementation here */
    return furColor;
}

const char* Cat::getHairLength()
{
    /* Write function implementation here */
    return hairLength;
}

const char* Cat::getEyeColor()
{
    /* Write function implementation here */
    return eyeColor;
}

void Cat::setFurColor()
{
    int x = rand() % 3;

    if (x == 0)
         furColor = "black";
    else if (x == 1)
          furColor = "gray";
    else
          furColor = "brown";
}

void Cat::setEyeColor()
{
    int x = rand() % 3;

    if (x == 0)
         eyeColor = "blue";
    else if (x == 1)
         eyeColor = "brown";
    else
         eyeColor = "green";
}
// cats.cpp
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cstring>

#include "cats.h"

void check(Cat* pen[], int);



int main()
{
    const int PEN_SIZE = 7;
    Cat* pen[PEN_SIZE];
    for (int c = 0; c < PEN_SIZE; c++) {
        /* Construct a new cat object            */
        *(pen + 1) = new Cat;
        /* Print the new cat object抯 attributes */

        /* Write call to function check here     */

        cout << "Press a key to add the next cat." << endl;
        cin.get();
    }

    /* Write a statement to delete all cat objects */

    return 0;
}

void check(Cat* catPen[], int numberOfCats)
{
    int brownCats = 0, grayCats = 0, blueEyes = 0,
        greenEyes = 0, brownEyes = 0;

    for (int x = 0; x <= numberOfCats; x++) {
        /* Write a series of statements which sum the total
           number of each type of cat (i.e., brownCats,
           greyCats, blueEyes, etc.) currently in the pen */
           if (strcmp(catPen[x]->getFurColor(), "gray") == 0)
               grayCats = grayCats + 1;
           if (strcmp(catPen[x]->getFurColor(), "brown") == 0)
               ++brownCats;
           if (strcmp(catPen[x]->getEyeColor(), "blue") == 0)
               ++blueEyes;
           if (strcmp(catPen[x]->getEyeColor(), "brown") == 0)
               ++brownEyes;
           if (strcmp(catPen[x]->getEyeColor(), "green") == 0)
               ++greenEyes;

    }
    if (grayCats > brownCats/* Write a condition that tests if            */
        /*    gray cats are fighting with brown cats  */)
        cout << "The gray cats are fighting with the brown cat"
        << (brownCats > 1 ? "s." : ".") << endl;

    if ((greenEyes >= 1 || blueEyes >= 1) && brownEyes/* Write a condition that tests if black cats are fighting */)
        cout << "The black cat(s) with blue eyes and the black "
        << "cat(s) with green eyes are\nfighting with the black "
        << "cat that has brown eyes." << endl;
}

每次按下功能键(如Enter键)时就将一只猫放在一个围栏里,这个围栏可以容纳7只猫。每只猫都是一个类Cat的实例化对象,而该类是在cat.h中定义的。每个猫对象在动态地实例化时,都会随机地赋予一种皮毛颜色[如black(黑色)、gray(灰色)或者brown(棕色)等],还将被随机地赋予一种眼睛映射[如green(绿色)、blue(蓝色)或者brown(棕色)等]以及毛长[short(短)和long(长)]
在将猫放在围栏中时,某些组合会使其发生争斗。例如,如果灰猫的数量超过棕色猫的数量,灰猫就会与棕色猫打架。如果在围栏里有一只黑毛、棕色眼睛的猫,同时还有至少一只黑色的绿眼睛猫和至少一只黑色蓝眼睛猫,也会发生争斗。编写一段名为check的非成员函数确定猫之间是否会打架。
实例输出:
A long-haired black cat with green eyes has been added to the pan.
Press a key to add the next cat.
A short-haired black cat with blue eyes has been added to the pan.
Press a key to add the next cat.
A short-haired black cat with blue eyes has been added to the pan.
Press a key to add the next cat.
A short-haired black cat with brown eyes has been added to the pan.
The black cat(s) with blue eyes and the black cat(s) with green eyes are fighting with the black cat that has brown eyes.
Press a key to add the next cat.
A short-haired black cat with brown eyes has been added to the pan.
Press a key to add the next cat.
A short-haired gray cat with blue eyes has been added to the pan.
Press a key to add the next cat.
A short-haired gray cat with blue eyes has been added to the pan.
Press a key to add the next cat.
  • 写回答

1条回答 默认 最新

  • LUMINGFEo 2022-06-07 11:19
    关注

    a.cpp:1:9: warning: #pragma once in main file
    #pragma once
    ^~~~
    a.cpp:22:10: fatal error: cats.h: No such file or directory
    #include "cats.h"
    ^~~~~~~~
    compilation terminated.

    评论

报告相同问题?

问题事件

  • 请采纳用户回复 6月7日

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算