编写程序,每次按下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;
}
老师编写的代码,还未复盘😅