编写一个程序,用同一函数名对圆、矩形、梯形求面积,参数自行设置,要求所有输入输出在主函数内进行。
点击编译以后显示
你之前运行的程序要先关闭,否则没法编译写入新的exe
#include <iostream>
using namespace std;
// 函数名:Area(radius)
// 参数:radius 圆的半径
// 返回值:圆的面积
double Area(double radius) {
return 3.14 * radius * radius;
}
// 函数名:Area(width, height)
// 参数:width 矩形的宽,height 矩形的高
// 返回值:矩形的面积
double Area(double width, double height) {
return width * height;
}
// 函数名Area(width, height, depth)
// 参数:width 梯形的上底,height 梯形的高,depth 梯形的下底
// 返回值:梯形的面积
double Area(double width, double height, double depth) {
return (width * depth) * height / 2;
}
int main() {
double radius, width, height;
cout << "请输入圆的半径 (单位:米):";
cin >> radius;
double area = Area(radius);
cout << "圆的面积是:" << area << endl;
cout << "请输入矩形的宽和高 (单位:米):";
cin >> width >> height;
area = Area(width, height);
cout << "矩形的面积是:" << area << endl;
double depth;
cout << "请输入梯形的上底、高和下底:";
cin >> depth >> height >> width;
area = Area(width, height, depth);
cout << "梯形的面积是:" << area << endl;
return 0;
}
这次肯定没问题了
基本思想:
直线与直线,直线与圆:利用公式直接求解
直线与矩形:分解成直线与直线的交点:
#include <iostream>
#include <math.h>
class VecPosition//////坐标
{
public:
double mx;
double my;
public:
void setpoint(double x,double y)
{
mx = x;
my = y;
}
};
class Line////直线
{
public:
double a;
double b;//ax+by+c=0
double c;
public:
void setLine(double ma,double mb,double mc)
{
a=ma;
b=mb;
c=mc;
}
};
class Circle////圆
{
public:
VecPosition
center;
double R;
public :
void setcircle(VecPosition my_center,
double my_R)
{
center = my_center;
R = my_R;
}
};
class Rectangel////矩形
{
public:
VecPosition top;
VecPosition bottem;
public:
void setRectangel(VecPosition
mytop,VecPosition mybottom)
{
top=mytop;
bottem=mybottom;
}
};
using namespace std;
////////////////////////直线与直线的交点//////////////////////////
VecPosition
LinecrossLine(Line l1,Line l2)
{
VecPosition cross;
double k1=(-1)*l1.a/l1.b;
double k2=(-1)*l2.a/l2.b;
double d;
if(k1==k2)
{
cout<<"没有交点"<<endl;
return cross;
}
else
{
d=l1.a*l2.b - l2.a*l1.b;
cross.mx = (l1.b*l2.c-l2.b*l1.c)/d;
cross.my = (l1.c*l2.a-l2.c*l1.a)/d;
return cross;
}
}
////////////////////////直线与圆的交点//////////////////////////////
void LinecrossCircle(Line
l,Circle c)
{
double k;
double b;
double dc;//直线与与圆心的距离
double x1,y1,x2,y2;
double cx=c.center.mx;//cx,cy为圆心坐标;
double cy=c.center.my;
double r=c.R;//圆的半径;
if(l.b!=0)
{
k=(-1)*l.a/l.b;//转换为y=kx+b;
b=(-1)*l.c/l.b;
}
else
{
k=0;
b=(-1)*l.c/l.a;
}
dc =
(fabs(l.a*c.center.mx+l.b*c.center.my+l.c))/sqrt(l.a*l.a+l.b*l.b);//求直线与圆心的距离
double
f=sqrt((k*k+1)*r*r-cx*cx*k*k+2*(cx*cy+b*cx)*k-cy*cy-2*b*cy-b*b);//辅助运算
if(l.b!=0)
{
if(dc>c.R)
{
cout<<"圆与直线没有交点"<<endl;
}
else if(dc==c.R)
{
x1=((-1)*f+(cy+b)*k+cx)/(k*k+1);
y1=(-1)*(k*(f+cx)+cy*k*k-b)/(k*k+1);
cout<<"圆与直线只有一个交点,交点x="<<x1<<",y="<<y1<<endl;
}
else
{
x1=((-1)*f+(cy+b)*k+cx)/(k*k+1);
y1=k*x1+b;
x2=(f+(-1)*(cy+b)*k-cx)/(k*k+1);
y2=k*x2+b;
cout<<"第一个交点,("<<x1<<","<<y1<<")"<<endl;
cout<<"第二个交点,("<<x2<<","<<y2<<")"<<endl;
}
}
else// x=-c/a的情况;
{
if(dc>c.R)
{
cout<<"圆与直线没有交点"<<endl;
}
else if(dc==c.R)
{
x1=((-1)*f+(cy+b)*k+cx)/(k*k+1);
y1=(-1)*(k*(f+cx)+cy*k*k-b)/(k*k+1);
cout<<"圆与直线只有一个交点,交点x="<<y1<<",y="<<x1<<endl;
}
else
{
x1=((-1)*f+(cy+b)*k+cx)/(k*k+1);
y1=(-1)*(k*(f+cx)+cy*k*k-b)/(k*k+1);
cout<<"第一个交点,("<<y1<<","<<x1<<")"<<endl;
cout<<"第二个交点,("<<y1<<","<<(-1)*x1<<")"<<endl;
}
}
}
////////////////////////直线与矩形的交点//////////////////////////
Line getLine(VecPosition
top,VecPosition bottom)//由两个点获得直线
{
Line lTemp;
lTemp.a = top.my - bottom.my;
lTemp.b = bottom.mx- top.mx;
lTemp.c = top.mx*bottom.my -
bottom.mx*top.my;
return lTemp;
}
void LinecrossRectangel(Rectangel
rect,Line l0)
{
VecPosition x1,x2,x3,x4;
VecPosition cross1,cross2,cross3,cross4;
Line l1,l2,l3,l4;
x1.setpoint(rect.top.mx,rect.bottem.my);
x2.setpoint(rect.bottem.mx,rect.top.my);
x3=rect.top;
x4=rect.bottem;
l1=getLine(x4,x2);
l2=getLine(x4,x1);
l3=getLine(x2,x3);
l4=getLine(x1,x3);
cross1 = LinecrossLine(l1,l0);
cross2 = LinecrossLine(l2,l0);
cross3 = LinecrossLine(l3,l0);
cross4 = LinecrossLine(l4,l0);
int flag=0;
if(cross1.mx>=rect.bottem.mx&&cross1.mx<=rect.top.mx&&cross1.my>=rect.bottem.my&&cross1.my<=rect.top.my)
{
flag=1;
cout<<"交点,("<<cross1.mx<<","<<cross1.my<<")"<<endl;
}
if(cross2.mx>=rect.bottem.mx&&cross2.mx<=rect.top.mx&&cross2.my>=rect.bottem.my&&cross2.my<=rect.top.my)
{
flag=1;
cout<<"交点,("<<cross2.mx<<","<<cross2.my<<")"<<endl;
}
if(cross3.mx>=rect.bottem.mx&&cross3.mx<=rect.top.mx&&cross3.my>=rect.bottem.my&&cross3.my<=rect.top.my)
{
flag=1;
cout<<"交点,("<<cross3.mx<<","<<cross3.my<<")"<<endl;
}
if(cross4.mx>=rect.bottem.mx&&cross4.mx<=rect.top.mx&&cross4.my>=rect.bottem.my&&cross4.my<=rect.top.my)
{
flag=1;
cout<<"交点,("<<cross4.mx<<","<<cross4.my<<")"<<endl;
}
if(flag==0)
{
cout<<"直线与矩形没有交点"<<endl;
}
}
int main()
{
int position;
double a,b,c;
Line l1;
Line l2;
double R;
double cx,cy;
Circle c1;
VecPosition center;
VecPosition top,bottom;
Rectangel rect;
cout<<"直线与直线的交点"<<endl;
cout<<"直线与圆的交点"<<endl;
cout<<"直线与矩形的交点"<<endl;
cout<<"请输入请求:"<<endl;
cin>>position;
switch(position)
{
case 1:
VecPosition cross;
cout<<"请输入第一条线的a,b,c"<<endl;
cin>>a>>b>>c;
l1.setLine(a,b,c);
cout<<"请输入第二条线的a,b,c"<<endl;
cin>>a>>b>>c;
l2.setLine(a,b,c);
cross=LinecrossLine(l1,l2);
cout<<"交点:x="<<cross.mx<<",y="<<cross.my;
break;
case 2:
cout<<"请输入直线的a,b,c"<<endl;
cin>>a>>b>>c;
l1.setLine(a,b,c);
cout<<"请分别输入圆心的坐标,圆的半径想x,y,R"<<endl;
cin>>cx>>cy>>R;
center.mx=cx;
center.my=cy;
c1.setcircle(center,R);
LinecrossCircle(l1,c1);
break;
case
3:
cout<<"请输入直线的a,b,c"<<endl;
cin>>a>>b>>c;
l1.setLine(a,b,c);
cout<<"请输入矩形右上角顶点的坐标"<<endl;
cin>>top.mx>>top.my;
cout<<"请输入矩形左下角顶点的坐标"<<endl;
cin>>bottom.mx>>bottom.my;
rect.setRectangel(top,bottom);
LinecrossRectangel(rect,l1);
break;
}
}
我可以给出一个基本实现方案,但需要根据具体代码实现情况进行调整。可能会涉及到函数重载、结构体定义、指针、数学计算等多个知识点。
首先需要定义一个结构体,用于存储圆形、矩形、梯形的参数。结构体中可以包含不同类型的变量,例如圆形的半径、矩形的长和宽、梯形的上下底和高等等。
定义一个函数,函数名可以任意,但是需要使用函数重载,让同一个函数名可以接受不同类型的参数。例如,可以定义以下三个函数:
float calcArea(float radius); // 计算圆形面积
float calcArea(float length, float width); // 计算矩形面积
float calcArea(float top, float bottom, float height); // 计算梯形面积
int main() {
// 假设有一个圆形,半径为2,一个矩形,长为3,宽为4,一个梯形,上底为5,下底为7,高为6
float radius = 2;
float length = 3;
float width = 4;
float top = 5;
float bottom = 7;
float height = 6;
// 分别调用三个函数计算面积
float area1 = calcArea(radius);
float area2 = calcArea(length, width);
float area3 = calcArea(top, bottom, height);
// 打印结果
cout << "圆形面积为:" << area1 << endl;
cout << "矩形面积为:" << area2 << endl;
cout << "梯形面积为:" << area3 << endl;
return 0;
}
需要注意的是,在计算圆形面积时,可以使用数学库中的pi常量,或者自己定义一个常量。在计算梯形面积时,需要将上下底的和乘以高再除以2。
根据实际代码情况,可能还需要使用指针传递参数,或者在定义结构体时使用指针类型的成员变量来保存参数。不同情况需要采取不同的解决方法,具体详见代码实现。