多边形周长计算
一、
1.先定义一个多变型类,定义边长,输入函数,输出函数
2.定义派生类矩形类,等边类
二、
三、
#include<iostream>
using namespace std;
class polygon
{
protected:
int number;
private:
int side_length[100];
public:
polygon() = default;
polygon(int n)
{
number = n;
for (int i = 0; i < 100; i++)
{
side_length[i] = 0;
}
}
void setlen(int *l)
{
for (int i = 0; i < 100; i++)
{
side_length[i] = *(l + i);
}
}
int perimeter()
{
int sum = 0;
for (int i = 0; i < 100; i++)
{
sum += side_length[i];
}
return sum;
}
void display()
{
cout << number <<" "<< perimeter()+1 << endl;
}
};
class rectangle :public polygon
{
protected:
int height;
int width;
public:
rectangle() = default;
rectangle(int h, int w)
{
height = h;
width = w;
}
int perimeter()
{
return 2 * (height + width);
}
void display()
{
cout << "4" << " " << perimeter() << endl;
}
};
class equal_polygon :public polygon
{
protected:
int side_len;
public:
equal_polygon() = default;
equal_polygon(int n, int l)
{
number = n;
side_len = l;
}
int perimeter()
{
return number * side_len;
}
void display()
{
cout << number << " " << perimeter() << endl;
}
};
int main()
{
int n;
cin >> n;
int judge;
for (int i = 0; i < n; i++)
{
cin >> judge;
if (judge == 0)
{
int len[100];
for (int i = 0; i < 100; i++)
{
len[i] = 0;
}
int count = 0;
for (int i = 0; i < 100; i++)
{
cin >> len[i];
if (len[i] == -1)
{
break;
}
count++;
}
polygon p(count);
p.setlen(len);
p.display();
}
if (judge == 1)
{
int h, w;
cin >> h >> w;
rectangle r(h, w);
r.display();
}
if (judge == 2)
{
int n;
int l;
cin >> n >> l;
equal_polygon e(n, l);
e.display();
}
}
return 0;
}
四、
#include<iostream>
using namespace std;
class polygon
{
protected:
int number;
private:
int side_length[100];
public:
polygon() = default;
polygon(int n)
{
number = n;
for (int i = 0; i < 100; i++)
{
side_length[i] = 0;
}
}
void setlen(int *l)
{
for (int i = 0; i < 100; i++)
{
side_length[i] = *(l + i);
}
}
int perimeter()
{
int sum = 0;
for (int i = 0; i < 100; i++)
{
sum += side_length[i];
}
return sum;
}
void display()
{
cout << number <<" "<< perimeter()+1 << endl;
}
};
class rectangle :public polygon
{
protected:
int height;
int width;
public:
rectangle() = default;
rectangle(int h, int w)
{
height = h;
width = w;
}
int perimeter()
{
return 2 * (height + width);
}
void display()
{
cout << "4" << " " << perimeter() << endl;
}
};
class equal_polygon :public polygon
{
protected:
int side_len;
public:
equal_polygon() = default;
equal_polygon(int n, int l)
{
number = n;
side_len = l;
}
int perimeter()
{
return number * side_len;
}
void display()
{
cout << number << " " << perimeter() << endl;
}
};
int main()
{
int n;
cin >> n;
int judge;
for (int i = 0; i < n; i++)
{
cin >> judge;
if (judge == 0)
{
int len[100];
for (int i = 0; i < 100; i++)
{
len[i] = 0;
}
int count = 0;
for (int i = 0; i < 100; i++)
{
cin >> len[i];
if (len[i] == -1)
{
break;
}
count++;
}
polygon p(count);
p.setlen(len);
p.display();
}
if (judge == 1)
{
int h, w;
cin >> h >> w;
rectangle r(h, w);
r.display();
}
if (judge == 2)
{
int n;
int l;
cin >> n >> l;
equal_polygon e(n, l);
e.display();
}
}
return 0;
}