(一)
请设计一个描述形状的类Shape,Shape类中有两个成员函数:
(1)getArea()函数:用于计算形状的面积。
(2)getLen()函数:用于计算形状的周长。
Shape类中有两个派生类:
(1)Rectangle类:表示矩形。
(2)Circle类:表示圆形。
请编写程序实现以下功能:
(1)计算不同边长的矩形的面积和周长。
(2)计算不同半径的圆的面积和周长。
(二)
已知一个有若干元素的数组arr,使用函数模版求该数组的最大值
(三)
编写一个类模版对数组元素进行排序、求和
(四)
定义vector容器,对数字0-9进行插入、删除和遍历操作
第一个
#include<iostream>
using namespace std;
#define PI 3.14
class Shape{
public:
Shape(){}
virtual double getArea()=0;
virtual double getLen()=0;
};
class Rectangle:public Shape{
public:
double width;
double height;
Rectangle(double the_width, double the_hetght){
this->width = the_width;
this->height = the_hetght;
}
double getArea(){
return this->width*this->height;
}
double getLen(){
return (this->width+this->height)*2;
}
};
class Circle:public Shape{
public:
double radius;
Circle(double the_radius){
this->radius = the_radius;
}
double getArea(){
return PI*this->radius*this->radius;
}
double getLen(){
return 2*PI*this->radius;
}
};
void text(){
Shape *shape = NULL;
shape = new Rectangle(10, 20);
cout<<"长方形面积:"<<shape->getArea()<<endl;
cout<<"长方形周长:"<<shape->getLen()<<endl;
delete shape;
shape = new Circle(18.0);
cout<<"圆形面积:"<<shape->getArea()<<endl;
cout<<"圆形周长:"<<shape->getLen()<<endl;
}
int main(int argc, char const *argv[])
{
text();
return 0;
}
第二个:
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
T getMax1(T *arr, int length)
{
T the_max = arr[0];
for (int i = 0; i < length; i++)
{
if (the_max < arr[i])
{
the_max = arr[i];
}
}
return the_max;
}
template <typename T>
T getMax2(vector<T> arr)
{
int length = arr.size();
T the_max = arr[0];
for (int i = 0; i < length; i++)
{
if (the_max < arr[i])
{
the_max = arr[i];
}
}
return the_max;
}
void text()
{
int arr[10] = {1, 2, 3, 4, 5, 63, 7, 8, 99, 11};
vector<int> p = {1, 2, 3, 4, 5, 63, 7, 8, 99, 11};
int the_max1 = getMax1<int>(arr, 10);
int the_max2 = getMax2<int>(p);
cout<<"使用原生数组:"<<the_max1<<endl;
cout<<"使用vector容器:"<<the_max2<<endl;
}
int main(int argc, char const *argv[])
{
text();
return 0;
}
第三个:
#include<iostream>
#include<vector>
using namespace std;
template<class T>
class MySort{
public:
//冒泡排序
void Sort1(T *arr, int length, bool reverse=true){
if(reverse){
for(int i=0;i<length;i++){
bool symbol = false;
for(int j=0;j<length-i-1;j++){
if(arr[j]<arr[j+1]){
T temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
symbol=true;
}
}
if(symbol == false){
return ;
}
}
}else{
for(int i=0;i<length;i++){
bool symbol = false;
for(int j=0;j<length-i-1;j++){
if(arr[j]>arr[j+1]){
T temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
symbol=true;
}
}
if(symbol == false){
return ;
}
}
}
}
void Sort2(vector<T> &arr, bool reverse=true){
int length = arr.size();
if(reverse==true){
for(int i=0;i<length;i++){
bool symbol = false;
for(int j=0;j<length-i-1;j++){
if(arr[j]<arr[j+1]){
T temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
symbol=true;
}
}
if(symbol == false){
return ;
}
}
}else{
for(int i=0;i<length;i++){
bool symbol = false;
for(int j=0;j<length-i-1;j++){
if(arr[j]>arr[j+1]){
T temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
symbol=true;
}
}
if(symbol == false){
return ;
}
}
}
}
};
template<typename T>
void show1(T arr, int length){
for(int i=0;i<length;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
template<typename T>
void show2(const vector<T> &arr){
for(int i=0;i<arr.size();i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
void text(){
int arr[12] = {1,8,9,6,5,4,3,2,8,9,5,7};
vector<int> p={1,8,9,6,5,4,3,2,8,9,5,7};
MySort<int> sort;
cout<<"排序前"<<endl;
show1(arr, 12);
show2(p);
sort.Sort1(arr, 12);
sort.Sort2(p);
cout<<"排序后"<<endl;
show1(arr, 12);
show2(p);
}
int main(int argc, char const *argv[])
{
text();
return 0;
}
第四个:
#include<iostream>
#include<vector>
using namespace std;
void text(){
cout<<"-1:退出输入\n-2:插入\n-3:删除\n-4:遍历"<<endl;
vector<int> v;
while(true){
int choice;
cout<<"输入选择:";
cin>>choice;
switch(choice){
case -1:
return ;
case -2:
int cs;
cout<<"输入数字(尾插):";
cin>>cs;
v.push_back(cs);
break;
case -3:
cout<<"输入需要删除的数量(尾删):"<<endl;
if(v.size()==0){
cout<<"为空无法删除"<<endl;
break;
}
int num;
cin>>num;
for(int i=0;i<num;i++){
v.pop_back();
}
break;
case -4:
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}
cout<<endl;
break;
}
}
}
int main(int argc, char const *argv[])
{
text();
return 0;
}
创建TextShape类:
代码:
interface shape
{
public abstract void getPerimeter();
public abstract void getArea();
}
class Rectangle implements shape{
int a;
int b;
public Rectangle(int a,int b)
{
this.a = a;
this.b = b;
}
public void getArea()
{
System.out.println("矩形面积为:"+a*b);
}
public void getPerimeter()
{
System.out.println("矩形周长为:"+2*(a+b));
}
}
class Triangle implements shape
{
int x;
int y;
int z;
public Triangle (int x,int y,int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public void getArea()
{
System.out.println("三角形的周长是:"+(x+y+z));
}
public void getPerimeter()
{
double p=(x+y+z)/2;
System.out.println("三角形的面积是:"+Math.sqrt(p*(p-x)*(p-y)*(p-z)));
}
}
class Circle implements shape
{
int r;
public Circle(int r)
{
this.r = r;
}
public void getArea()
{
System.out.println("圆面积为:"+Math.PI*r*r);
}
public void getPerimeter()
{
System.out.println("圆周长为:"+2*Math.PI*r);
}
}
public class TextShape {
public static void main(String[] args)
{
shape r = new Rectangle(6,8);
r.getArea();
r.getPerimeter();
shape t = new Triangle(3,4,5);
t.getArea();
t.getPerimeter();
shape c = new Circle(6);
c.getArea();
c.getPerimeter();
}
}
(一)
class Shape {
public:
virtual ~Shape(){}
virtual int getArea() = 0;
virtual int getLen() = 0;
};
class Rectange
: public Shape
{
public:
Rectange(int w, int h) : m_width(w), m_height(h) {}
~Rectange() {}
virtual int getArea() { return m_width * m_height; }
virtual int getLen() { return (m_width + m_height) << 1; }
private:
int m_width = 0;
int m_height = 0;
};
class Circle
: public Shape
{
public:
Circle(int r): m_r(r) {}
~Circle() {}
virtual int getArea() { return m_r * m_r * 3.1415f; }
virtual int getLen() { return m_r * 3.1415f * 2; }
private:
int m_r = 0;
};
int main()
{
Rectangle A(10,20);
cout << A.getArea() << endl;
cout << A.getLen() << endl;
Rectangle A2(110,22weixin_33713707 0);
cout << A2.getArea() << endl;
cout << A2.getLen() << endl;
Circle B(15);
cout << B.getArea() << endl;
cout << B.getLen() << endl;
Circle B2(115);
cout << B2.getArea() << endl;
cout << B2.getLen() << endl;
return 0;
}
(二)
#include <iostream>
using namespace std;
template <typename T>
int maxInArray(T* arr, int num)
{
T max = arr[0];
for (int i = 1; i < num; i++)
{
arr[i] > max ? max = arr[i] : 0;
}
return max;
}
int main()
{
int arr1[4] = { 5,32,78,123 };
double arr2[4] = { 56.1,45.3,78.32,49.0 };
cout << "max = " << maxInArray(arr1, 4) << endl;
cout << "max = " << maxInArray(arr2, 4) << endl;
system("pause");
return 0;
}
(三)
#include <iostream>
using namespace std;
template <typename T>
class Arr
{
private:
T* p;
int n;
public:
Arr(T a[], int s);
void sort();
void sum();
void show();
};
template <typename T>
Arr<T>::Arr(T a[], int s)
{
p = new T[s];
n = s;
for (int i = 0; i < s; i++)
{
p[i] = a[i];
}
}
template <typename T>
void Arr<T>::sort()
{
for (int i = 0; i < n - 1; i++)
{
int k = i;
for (int j = i + 1; j < n; j++)
{
if (p[k] > p[j]) {
k = j;
if (k != i)
{
int tem;
tem = p[i];
p[i] = p[k];
p[k] = tem;
}
}
}
}
}
template <typename T>
void Arr<T>::sum()
{
T sum1 = 0;
for (int i = 0; i < n; i++)
{
sum1 += p[i];
}
cout << sum1;
}
template <typename T>
void Arr<T>::show(){
for(int i = 0; i < n; i++)
cout << *(p + i);
}
int main()
{
int a[8] = { 2,3,4,6,11,8,9,5 };
Arr<int> a1(a, 8);
cout << "排序:";
a1.sort();
a1.show();
cout << "sum:";
a1.sum();
return 0;
}
(四)
#include <stdio.h>
#include <vector>
using namespace std;
int main(){
vector<int> v;
//vector插入操作
for (int i = 0; i < 10; i++){
v.push_back(i);
}
//vector遍历操作
for (vector<int>::size_type ix = 0; ix != v.size(); ix ++){
printf("%d\t", v[ix]);
}
printf("\n");
//vector删除末尾元素
v.pop_back();
for (vector<int>::size_type ix = 0; ix != v.size(); ix ++){
printf("%d\t", v[ix]);
}
printf("\n");
return 0;
}
如果对回答满意,请点下采纳,谢谢!
(1)
#include <iostream>
using namespace std;
#define PI 3.1415926
class Shape
{
public:
int getArea()
{
return 0;
}
int getLen()
{
return 0;
}
};
class Rectangle :public Shape
{
public:
Rectangle(double a, double b)
{
h = a;
w = b;
}
double getArea()
{
return h*w;
}
double getLen()
{
return (h+w)*2;
}
protected:
double h, w;
};
class Circle:public Shape
{
public:
Circle(double a)
{
r = a;
}
double getArea()
{
return r*r*PI;
}
double getLen()
{
return r*2*PI;
}
protected:
double r;
};
int main()
{
Rectangle A(5,7);
Circle B(5);
cout << A.getArea() << endl;
cout << A.getLen() << endl;
cout << B.getArea() << endl;
cout << B.getLen() << endl;
return 0;
}