C/C++教程

C++基础Day14

本文主要是介绍C++基础Day14,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Day14.2021.12.5

案例:立方体相等

这段代码考虑了同一立方体正放侧放是同一种情况

 #include"methodState.h"
 ​
 class Cube {
 private:
     int len;
     int wid;
     int hig;
 public:
     int getLen() {
         return this->len;
     }
     void setLen(int len) {
         this->len = len;
     }
     int getWid() {
         return this->wid;
     }
     void setWid(int wid) {
         this->wid = wid;
     }
     int getHig() {
         return this->hig;
     }
     void setHig(int hig) {
         this->hig = hig;
     }
     int getArea() {
         return (len * wid + wid * hig + len * hig) * 2;
     }
     int getVolume() {
         return len * wid * hig;
     }
 };
 //判断两立方体是否相等
 bool equals(Cube& c1, Cube& c2) {//引用传递不必复制形参空间
 ​
     /*创建两个数组记录立方体长宽高,用嵌套for循环遍历两个数组,
     若数组1中有元素等于数组2中某元素,就将数组2中该元素删除,数组长度减1,count加1
     ,若count=3,证明两数组中三个元素分别相等(位置可能不同)*/
     int arrays1[] = { c1.getLen(),c1.getWid(),c1.getHig() };
     int arrays2[] = { c2.getLen(),c2.getWid(),c2.getHig() };
     int len1 = sizeof(arrays1) / sizeof(arrays1[0]);
     int len2 = sizeof(arrays2) / sizeof(arrays2[0]);
     int count = 0;//计数变量,若两个立方体中有两条边分别相等,则count++
 ​
     for (int i = 0; i < len1; i++) {
         for (int j = 0; j < len2; j++) {
             if (arrays1[i] == arrays2[j]) {
                 count++;
                 for (int k = j; k < len2; k++) {
                     //将该元素删除,下一次重新遍历,删除的好处在于防止出现长宽高为20,20,20的重复情况
                     arrays2[k] = arrays2[k + 1];
                 }
                 len2--;
             }
         }
     }
     if (count == 3) {
         return true;
     }
     else
         return false;
 }
 int main() {
 ​
     Cube c1;
     Cube c2;
     Cube c3;
     Cube c4;
 ​
     c1.setLen(10);
     c1.setWid(20);
     c1.setHig(30);
     cout << "c1的长宽高:" << c1.getLen() << "  " << c1.getWid() << "  "<< c1.getHig() << "  ";
     cout << "c1的表面积:" << c1.getArea() << endl;
     cout << "c1的体积:" << c1.getVolume() << endl;
 ​
     c2.setLen(40);
     c2.setWid(20);
     c2.setHig(30);
     cout << "c2的长宽高:" << c2.getLen() << "  " << c2.getWid() << "  " << c2.getHig() << "  ";
     cout << "c2的表面积:" << c2.getArea() << endl;
     cout << "c2的体积:" << c2.getVolume() << endl;
     c3.setLen(40);
     c3.setWid(20);
     c3.setHig(30);
     cout << "c3的长宽高:" << c3.getLen() << "  " << c3.getWid() << "  " << c3.getHig() << "  "; 
cout << "c3的表面积:" << c3.getArea() << endl; 
cout << "c3的体积:" << c3.getVolume() << endl; 
c4.setLen(30); 
c4.setWid(20); 
c4.setHig(10); 
cout << "c4的长宽高:" << c4.getLen() << "  " << c4.getWid() << "  " << c4.getHig() << "  "; 
cout << "c4的表面积:" << c4.getArea() << endl; 
cout << "c4的体积:" << c4.getVolume() << endl; 
​ 
bool r1 = equals(c1, c2); 
bool r2 = equals(c3, c2); 
bool r3 = equals(c1, c3); 
bool r4 = equals(c1, c4); 
if (r1 == true) {  
cout << "c1=c2" << endl;  
    } 
else 
cout << "c1!=c2" << endl; 
if (r2 == true) { 
cout << "c3=c2" << endl; 
    } 
else 
cout << "c3!=c2" << endl; 
if (r3 == true) { 
cout << "c1=c3" << endl; 
    } 
else 
cout << "c1!=c3" << endl; 
if (r4 == true) { 
cout << "c1=c4" << endl; 
    } 
else 
cout << "c1!=c4" << endl; 
system("pause"); 
return 0; 
}

 

image-20211205172820416

 来源:b站黑马

 

这篇关于C++基础Day14的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!