首页 > 其他分享 >打卡第二十九天

打卡第二十九天

时间:2023-05-20 21:22:53浏览次数:41  
标签:polygon int len side ++ 第二十九 100 打卡

多边形周长计算

一、

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;
}

标签:polygon,int,len,side,++,第二十九,100,打卡
From: https://www.cnblogs.com/xscya/p/17417810.html

相关文章

  • 每天打卡一小时 第三十一天 PTA520钻石 争霸赛
    第一题  源代码#include<iostream>usingnamespacestd;intmain(){intn;cin>>n;cout<<"520"<<n<<"Times!";}第一题,简简单单打印输出第二题 源代码#include<iostream>usingnamespacestd;intmain(......
  • 每日打卡,超时
    集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里。比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸。本题给定一张不相容物品的清单,需要你检查每一张集装箱货品清单,判断它们是否能装在同一只箱子里。输入格式:输入第一行给出两个正整数:N (≤104)是成......
  • 每天打卡一小时 第三十天
     template<typenameT>voidsort(T*a,intsize){for(inti=0;i<size;i++){cin>>a[i];}Tb;for(inti=0;i<size;i++){for(intj=0;j<size-1-i;j++){......
  • 每日打卡一小时(第二十八天)
    一.问题描述定义一个空的list,将用户输入的数组a[10]的10个数插入到list中,在list头部插入数b,用迭代器遍历list并输出其中的元素值。然后将list从大到小排序,删除list尾部的元素,用迭代器遍历list并输出其中的元素值。最后将list清空。二.设计思路注意列表容器的使用方法,注意迭代器......
  • c++打卡练习(35)
    求分母为40的所有真分数流程图:伪代码:源代码:#include<iostream>usingnamespacestd;intmain(){inti,j,num1,num2,t,n=0;for(i=1;i<40;i++){ num1=40; num2=i; while(num2!=0){ t=num1%num2; num1=num2; num2=t; } if(num1==1){ n++; ......
  • 编程打卡:面向对象程序设计测试
    ``gopackagemainimport"bufio"import"flag"import"fmt"import"io"import"os"import"strconv"varinfile*string=flag.String("i","unsorted.dat","Filecontains......
  • 打卡18
    #include<iostream>#include<string>#include<iomanip>usingnamespacestd;intgcd(inta,intb){if(a%b==0)returnb;elsereturngcd(b,a%b);}//շת���������classFS{ private: intfz,fm; public: friendintgcd(i......
  • 每日打卡
    真分数序列问题问题描述:按递增序列依次排出分子小于40,分母为40的最简分数问题分析:采用穷举法,h与1中不能有40的公因数,用if判断输出代码:#include<stdio.h>intmain(){inti,h,k,j,n;printf("40之内的真分数有,\n");for(i=1;i<40;i++){           h=40;   ......
  • 打卡第二十八天
    计算点到直线的距离一、1.设计一个点类Point,它有2个私有数据成员x和y,表示点的坐标2.直线类Line,它有3个私有数据成员a,b和c,表示直线方程ax+by+c=03.构建友元函数dist,用于计算一个点到直线的距离二、三、#include<iostream>#include<cmath>#include<iomanip>usingnam......
  • 5.19打卡
    #include<iostream>usingnamespacestd;intgetpower(intx,inty){if(y<0)return0;elseif(y==0)return1;elseif(y==1)returnx;elsereturnx*getpower(x,y-1);}doublegetpower(d......