编写程序,每次按下Enter键时,就将一只猫放在围栏里。已知一个围栏可以放7只猫。每只猫具有皮色(黑、灰、棕)、眼睛颜色(绿、蓝、棕)和毛长(长、短)三种属性。要求放进围栏的猫应被随机的附于属性值。对这些属性应提供对应的get和set方法。
将猫放进围栏里,某些组合会发生争斗。例如,灰色猫的数量如果超过棕色猫的数量,灰色猫会和棕色猫打架。如果围栏中有一只黑色棕眼猫,同时至少一只黑毛绿眼猫以及至少一只黑毛蓝眼猫,也会发生争斗,编写一个全局函数check(),确定猫之间是否会打架。
1 #include <iostream> 2 #include<cstdlib> //rand、srand 3 #include<ctime> 4 using namespace std; 5 6 class Cat 7 { 8 private: 9 char skin[10];//肤色 10 char eye[10];//眼睛颜色 11 char len[10] ;//毛长 12 static int count; 13 public: 14 Cat();//缺省构造函数,因为值随机不能传入参数 15 16 //get 17 char *GetSkin(); 18 char *GetEye(); 19 char *GetLen(); 20 //set 21 void SetSkin();//毛长、肤色、眼睛颜色是随机值,因而无参数 22 void SetEye(); 23 void SetLen(); 24 25 //print 26 //void Print(Cat *cats); 27 }; 28 29 int Cat::count =0; 30 //没有初始化静态数据成员 31 //构造函数初始化; 32 Cat::Cat() 33 { 34 SetSkin(); 35 SetEye(); 36 SetLen(); 37 } 38 //set 39 void Cat::SetSkin() 40 { 41 int color = rand()%3; 42 43 if (0 ==color) 44 strcpy(skin,"黑"); 45 else if(1==color) 46 strcpy(skin,"灰"); 47 else 48 strcpy(skin,"棕"); 49 50 } 51 52 53 void Cat::SetEye() 54 { 55 int color = rand()%3; 56 57 if (0 ==color) 58 strcpy(eye,"绿"); 59 else if(1==color) 60 strcpy(eye,"蓝"); 61 else 62 strcpy(eye,"棕"); 63 } 64 65 void Cat::SetLen() 66 { 67 int color = rand()%2; 68 69 if (0 ==color) 70 strcpy(len,"长"); 71 else 72 strcpy(len,"短"); 73 } 74 //get 75 char *Cat::GetSkin() 76 { 77 return skin; 78 } 79 char *Cat::GetEye() 80 { 81 return eye; 82 } 83 char *Cat::GetLen() 84 { 85 return len; 86 } 87 //print 88 /*void Cat::Print(Cat *cats) 89 { 90 cout<<"一只"<<cats->GetEye()<<"眼"<<cats->GetLen()<<"毛"<<cats->GetSkin()<<"色猫被放进了围栏"<<endl; 91 }*/ 92 93 //check 94 int Check(Cat *cats[], int numberofCats) 95 { 96 int grayCats =0,brownCats =0; 97 int blueEyes =0, brownEyes =0,greenEyes = 0; 98 99 for(int i =0; i <numberofCats; i++) 100 { 101 if(strcmp(cats[i]->GetSkin(),"灰")) 102 grayCats =grayCats+1; 103 else if (strcmp(cats[i]->GetSkin(),"棕")) 104 brownCats++; 105 else 106 { 107 if (strcmp(cats[i]->GetEye(),"蓝")) 108 blueEyes++; 109 else if (strcmp(cats[i]->GetEye(),"棕")) 110 brownEyes++; 111 else 112 greenEyes++; 113 } 114 //判断灰色猫是否和棕色猫打架 115 if(grayCats>brownCats) 116 cout<<"灰色猫和棕色猫打架"<<endl; 117 else 118 cout <<"灰色猫和棕色猫不打架"<<endl; 119 if((greenEyes>=1||blueEyes>=1)&&brownEyes) 120 cout <<"会打架"<<endl; 121 } 122 return 0; 123 } 124 125 int main () 126 { 127 Cat *cat[7]={NULL}; 128 char key; 129 int i =0; 130 srand(time(NULL)); 131 while(i<7&&(key=cin.get())==' ') 132 { 133 cat[i++] = new Cat(); 134 cout<<"一只"<<cat[i++]->GetEye()<<"眼"<<cat[i++]->GetLen()<<"毛"<<cat[i++]->GetSkin()<<"色猫被放进了围栏"<<endl; 135 } 136 Check(cat,i); 137 delete *cat; 138 }
改正版1:改正是因为对指针数组不是很熟悉导致错误;
1 #include <iostream> 2 #include<cstdlib> //rand、srand 3 #include<ctime> 4 #include<string> 5 using namespace std; 6 7 class Cat 8 { 9 private: 10 string skin;//肤色 11 string eye;//眼睛颜色 12 string len;//毛长 13 static int count; 14 public: 15 Cat();//缺省构造函数,因为值随机不能传入参数 16 17 //get 18 string GetSkin(); 19 string GetEye(); 20 string GetLen(); 21 //set 22 void SetSkin();//毛长、肤色、眼睛颜色是随机值,因而无参数 23 void SetEye(); 24 void SetLen(); 25 26 //print 27 void Print()const; 28 }; 29 30 int Cat::count = 0; 31 //没有初始化静态数据成员 32 //构造函数初始化; 33 Cat::Cat() 34 { 35 SetSkin(); 36 SetEye(); 37 SetLen(); 38 } 39 //set 40 void Cat::SetSkin() 41 { 42 int color = rand() % 3; 43 44 if (0 == color) 45 skin ="黑"; 46 else if (1 == color) 47 skin ="灰"; 48 else 49 skin = "棕"; 50 51 } 52 53 54 void Cat::SetEye() 55 { 56 int color = rand() % 3; 57 58 if (0 == color) 59 eye = "绿"; 60 else if (1 == color) 61 eye ="蓝"; 62 else 63 eye ="棕"; 64 } 65 66 void Cat::SetLen() 67 { 68 int color = rand() % 2; 69 70 if (0 == color) 71 len = "长"; 72 else 73 len = "短"; 74 } 75 //get 76 string Cat::GetSkin() 77 { 78 return skin; 79 } 80 string Cat::GetEye() 81 { 82 return eye; 83 } 84 string Cat::GetLen() 85 { 86 return len; 87 } 88 //print 89 void Cat::Print() const 90 { 91 cout<<"一只"<<eye<<"眼"<<len<<"毛"<<skin<<"色猫被放进了围栏"<<endl; 92 } 93 94 //check 95 int Check( Cat *cats[], int numberofCats) 96 { 97 int grayCats = 0, brownCats = 0; 98 int blueEyes = 0, brownEyes = 0, greenEyes = 0; 99 100 for (int i = 0; i <numberofCats; i++) 101 { 102 if (cats[i]->GetSkin()=="灰") 103 grayCats = grayCats + 1; 104 else if (cats[i]->GetSkin() == "棕") 105 brownCats++; 106 else 107 { 108 if (cats[i]->GetEye()== "蓝") 109 blueEyes++; 110 else if (cats[i]->GetEye()== "棕") 111 brownEyes++; 112 else 113 greenEyes++; 114 } 115 } 116 //判断灰色猫是否和棕色猫打架 117 if (grayCats>brownCats) 118 cout << "灰色猫和棕色猫打架" << endl; 119 else 120 cout << "灰色猫和棕色猫不打架" << endl; 121 if ((greenEyes >= 1 || blueEyes >= 1) && brownEyes) 122 cout << "会打架" << endl; 123 return 0; 124 } 125 126 int main() 127 { 128 Cat *cat[7] ; 129 char key; 130 int i = 0; 131 srand(time(NULL)); 132 while (i<7 && (key = cin.get()) == ' ') 133 { 134 *(cat+i) = new Cat; 135 cout << "一只" << cat[i]->GetEye() << "眼" << cat[i]->GetLen() << "毛" << cat[i]->GetSkin() << "色猫被放进了围栏" << endl; 136 i++; 137 } 138 Check(cat, i); 139 delete *cat; 140 }
改正2:
1 #include <iostream> 2 #include<cstdlib> //rand、srand 3 #include<ctime> 4 #include<string> 5 using namespace std; 6 7 class Cat 8 { 9 private: 10 string skin;//肤色 11 string eye;//眼睛颜色 12 string len;//毛长 13 static int count; 14 public: 15 Cat();//缺省构造函数,因为值随机不能传入参数 16 17 //get 18 string GetSkin(); 19 string GetEye(); 20 string GetLen(); 21 //set 22 void SetSkin();//毛长、肤色、眼睛颜色是随机值,因而无参数 23 void SetEye(); 24 void SetLen(); 25 26 //print 27 void Print(); 28 }; 29 30 int Cat::count = 0; 31 //没有初始化静态数据成员 32 //构造函数初始化; 33 Cat::Cat() 34 { 35 SetSkin(); 36 SetEye(); 37 SetLen(); 38 } 39 //set 40 void Cat::SetSkin() 41 { 42 int color = rand() % 3; 43 44 if (0 == color) 45 skin ="黑"; 46 else if (1 == color) 47 skin ="灰"; 48 else 49 skin = "棕"; 50 51 } 52 53 54 void Cat::SetEye() 55 { 56 int color = rand() % 3; 57 58 if (0 == color) 59 eye = "绿"; 60 else if (1 == color) 61 eye ="蓝"; 62 else 63 eye ="棕"; 64 } 65 66 void Cat::SetLen() 67 { 68 int color = rand() % 2; 69 70 if (0 == color) 71 len = "长"; 72 else 73 len = "短"; 74 } 75 //get 76 string Cat::GetSkin() 77 { 78 return skin; 79 } 80 string Cat::GetEye() 81 { 82 return eye; 83 } 84 string Cat::GetLen() 85 { 86 return len; 87 } 88 //print 89 void Cat::Print() 90 { 91 cout<<"一只"<<this->GetEye()<<"眼"<<this->GetLen()<<"毛"<<this->GetSkin()<<"色猫被放进了围栏"<<endl; 92 } 93 94 //check 95 int Check( Cat *cats[], int numberofCats) 96 { 97 int grayCats = 0, brownCats = 0; 98 int blueEyes = 0, brownEyes = 0, greenEyes = 0; 99 100 for (int i = 0; i <numberofCats; i++) 101 { 102 if (cats[i]->GetSkin()=="灰") 103 grayCats = grayCats + 1; 104 else if (cats[i]->GetSkin() == "棕") 105 brownCats++; 106 else 107 { 108 if (cats[i]->GetEye()== "蓝") 109 blueEyes++; 110 else if (cats[i]->GetEye()== "棕") 111 brownEyes++; 112 else 113 greenEyes++; 114 } 115 } 116 //判断灰色猫是否和棕色猫打架 117 if (grayCats>brownCats) 118 cout << "灰色猫和棕色猫打架" << endl; 119 else 120 cout << "灰色猫和棕色猫不打架" << endl; 121 if ((greenEyes >= 1 || blueEyes >= 1) && brownEyes) 122 cout << "会打架" << endl; 123 return 0; 124 } 125 126 int main() 127 { 128 Cat *cat[7] ; 129 char key; 130 int i = 0; 131 srand(time(NULL)); 132 while (i<7 && (key = cin.get()) == ' ') 133 { 134 *(cat+i) = new Cat; 135 (*cat[i]).Print(); 136 i++; 137 } 138 Check(cat, i); 139 delete *cat; 140 }