关于c++矩形相加程序问题,高手进
答案:3 悬赏:10
解决时间 2021-02-28 16:23
- 提问者网友:月葬花瑰
- 2021-02-27 18:54
#include
int l[2][4];
class Figure
{public:
Figure(int r,int b,int c,int d,int e,int f,int g,int h)
{a[0][0]=r;a[0][1]=b;a[0][2]=c;a[0][3]=d;a[1][0]=e;a[1][1]=f;a[1][2]=g;a[1][3]=h;};
friend int operator +(Figure &c1,Figure &c2);
private:
int a[1][3];
int m;
int n;
};
int operator +(Figure &c1,Figure &c2)
{
c1.m=0;
c1.n=0;
while(c1.n<=1)
{
while(c1.m<=3)
{
l[c1.n][c1.m]=c1.a[c1.n][c1.m]+c2.a[c1.n][c1.m];
cout< cout< cout< ++c1.m;
}
++c1.n;
c1.m=0;
}
return 0;
};
int main()
{
Figure s1(8,7,6,5,4,3,2,1);
Figure s2(8,7,6,5,4,3,2,1);
s1+s2;
return 0;
}
以上是我写的使两个矩形数组相加的程序,调试无问题,但是输出结果不对,在第4,5,6个数出现问题,相加结果不对,请哪位高手帮我看看哪里出错了,谢谢了
最佳答案
- 二级知识专家网友:荒唐后生
- 2021-02-27 19:44
8 8 16
7 7 14
6 6 12
5 5 10
4 4 8
3 3 6
2 2 4
1 1 2
Press any key to continue . . .
这个是不是你想要的结果,这是按你的计算矩阵方法得出来的结果
你的程序其他都没问题,唯一的问题就是private中的 int a[1][3];应该是int a[2][4]; 所以你在执行程序中,内存越界了,才产生值的错误,而且错误正好是 4 5 6行。
你二维数组int l[2][4]定义是正确的 怎么下面会定义错呢 对吧
全部回答
- 1楼网友:单身小柠`猫♡
- 2021-02-27 20:58
子程序如下。
主程序根据具体情况,要求用户输入参数,然后调用。
// 已知dx,dy 是长,宽
double area1(double dx, double dy)
{
return dx * dy;
}
// 已知[x0,y0] [x1,y1] 平面上矩形对角点
double area2(double x0, double y0,double x1, double y1)
{
return (x1-x0) * (y1-y0);
}
// 空间矩形,已知4角点座标x0,y0,z0,x1,y1,z1,x2,y2,z2,x3,y3,z3
#include
double area3(double x0,double y0,......)
{
double dx,dy;
dx = sqrt( (x1-x0)*(x1-x0) + (y1-y0)*(y1-y0) + (z1-z0) * (z1-z0) );
dy = sqrt( (x2-x0)*(x2-x0) + (y2-y0)*(y2-y0) + (z2-z0) * (z2-z0) );
return dx*dy;
}
- 2楼网友:爱情是怎么炼成的
- 2021-02-27 19:59
你的数据给的有形式错误
我要举报
大家都在看
推荐资讯