c语言—圈猫

编写程序,每次按下Enter键时,就将一只猫放在围栏里。已知一个围栏可以放7只猫。每只猫具有皮色(黑、灰、棕)、眼睛颜(绿、蓝、棕)和毛长(长、短)三种属性。要求放进围栏的猫应被随机的附于属性值。对这些属性应提供对应的get和set方法。
将猫放进围栏里,某些组合会发生争斗。例如,灰色猫的数量如果超过棕色猫的数量,灰色猫会和棕色猫打架。如果围栏中有一只黑色棕眼猫,同时至少一只黑毛绿眼猫以及至少一只黑毛蓝眼猫,也会发生争斗,编写一个全局函数check(),确定猫之间是否会打架。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct cat
{	
    int eyeColor;	
    int skinColor;	
    int further;
};
typedef struct cat cat;
const char eyeColors[3][5] = {"绿眼","蓝眼","棕眼"};
const char skinColors[3][5] = {"黑","灰","棕色"};
const char furthers[2][5] = {"长毛","短毛"};
void checkGB(cat ** p,int size)
{	
    int countGray = 0, countBrown = 0, i;	
    for(i = 0;i < size && p[i] != NULL;i++)	
    {		
    if(p[i]->skinColor == 1)
    countGray++;
    else if(p[i]->skinColor == 2)
    countBrown++;	
    }	
    if(countGray > countBrown)
    printf("灰猫会和棕色猫打架.\n");
    }
int main()
{	
    cat * p[7] = {0};//checkGB(cat ** p,int size);	
    int a[10];//sort(a,10); sort(int * p, int size)	
    char ch;	
    srand(time(NULL));	
    int i = 0;
    while((ch = getchar()) == '\n' && i <= 7)	
    {		
    p[i] = (cat *)malloc(sizeof(cat));
    p[i]->eyeColor = rand() % 3;
    p[i]->skinColor = rand() % 3;
    p[i]->further = rand() % 2;
    printf("一只%s%s%s猫被放进围栏里.\n",eyeColors[p[i]->eyeColor],
                                      furthers[p[i]->further],
                                      skinColors[p[i]->skinColor]);	
    i++;	
    }
    checkGB(p, 7);	//checkBlack(p, 7)      
    for(i = 0;i < 7;i++)	
    {		
    free(p[i]);	
    }	
    return 0;
}

老师编写的代码,还未复盘😅

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值