程序填空题:三维向量- C/C++ 结构
对于三维向量V1(x1,y1,z1)和V2(x2,y2,z2),其加法运算定义为:
![image.png](~/9f2b9ac9-a2d4-4372-bae7-fae17f15f62e.png)
减法运算定义为:
![image.png](~/4720bc8f-c6e0-4a46-95a8-b382855c146c.png)
模长定义为:
![image.png](~/6dbd82a8-decd-48d2-bf0d-7ec88186cbfb.png)
请定义结构类型Vector3D,其三个分量为整数;请设计函数实现三维向量的加、减及求模长功能;编写合适的代码加以验证。
<br>
请将下述代码补充完整,以完成上述任务:
c
#include <stdio.h>
#include <math.h>
struct {
int x;
int y;
} Vector3D;
Vector3D add(const Vector3D a, const Vector3D b){
Vector3D c = {0,0,0};
c.x = a.x + b.x;
c.y = a.y + b.y;
return c;
}
Vector3D sub(const Vector3D a, const Vector3D b){
Vector3D c = {0,0,0};
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
}
double length(const Vector3D v){
return
}
int main()
{
Vector3D v1 = {10,10,10};
Vector3D v2 = {1,2,3};
Vector3D v3 = add(v1,v2);
Vector3D v4 = sub(v1,v2);
double dLength = ;
printf("(10,10,10)+(1,2,3)={%d,%d,%d}\n",);
printf("(10,10,10)-(1,2,3)={%d,%d,%d}\n",v4.x,v4.y,v4.z);
printf("|(10,10,10)|=%.2f",dLength);
return 0;
}
程序的正确执行结果为:
(10,10,10)+(1,2,3)={11,12,13}
(10,10,10)-(1,2,3)={9,8,7}
|(10,10,10)|=17.32
### 感觉不会? 那试着听听**免费的B站网课**
[简洁的C和C++ - 重庆大学在线课程](https://www.bilibili.com/video/BV1it411d7zx/)
[Python编程基础及应用 - 重庆大学在线课程](https://www.bilibili.com/video/BV1kt411R7uW/)
![image.png](~/6e79c9e3-cb7f-486d-ab78-36b5a8f655c0.png)
答案:
第1空:typedef
第2空: int z;
第3空: c.z = a.z + b.z;
第4空:return c;
第5空:sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
第6空:length(v1)
第7空:v3.x,v3.y,v3.z
![image.png](~/9f2b9ac9-a2d4-4372-bae7-fae17f15f62e.png)
减法运算定义为:
![image.png](~/4720bc8f-c6e0-4a46-95a8-b382855c146c.png)
模长定义为:
![image.png](~/6dbd82a8-decd-48d2-bf0d-7ec88186cbfb.png)
请定义结构类型Vector3D,其三个分量为整数;请设计函数实现三维向量的加、减及求模长功能;编写合适的代码加以验证。
<br>
请将下述代码补充完整,以完成上述任务:
c
#include <stdio.h>
#include <math.h>
struct {
int x;
int y;
} Vector3D;
Vector3D add(const Vector3D a, const Vector3D b){
Vector3D c = {0,0,0};
c.x = a.x + b.x;
c.y = a.y + b.y;
return c;
}
Vector3D sub(const Vector3D a, const Vector3D b){
Vector3D c = {0,0,0};
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
}
double length(const Vector3D v){
return
}
int main()
{
Vector3D v1 = {10,10,10};
Vector3D v2 = {1,2,3};
Vector3D v3 = add(v1,v2);
Vector3D v4 = sub(v1,v2);
double dLength = ;
printf("(10,10,10)+(1,2,3)={%d,%d,%d}\n",);
printf("(10,10,10)-(1,2,3)={%d,%d,%d}\n",v4.x,v4.y,v4.z);
printf("|(10,10,10)|=%.2f",dLength);
return 0;
}
程序的正确执行结果为:
(10,10,10)+(1,2,3)={11,12,13}
(10,10,10)-(1,2,3)={9,8,7}
|(10,10,10)|=17.32
### 感觉不会? 那试着听听**免费的B站网课**
[简洁的C和C++ - 重庆大学在线课程](https://www.bilibili.com/video/BV1it411d7zx/)
[Python编程基础及应用 - 重庆大学在线课程](https://www.bilibili.com/video/BV1kt411R7uW/)
![image.png](~/6e79c9e3-cb7f-486d-ab78-36b5a8f655c0.png)
答案:
第1空:typedef
第2空: int z;
第3空: c.z = a.z + b.z;
第4空:return c;
第5空:sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
第6空:length(v1)
第7空:v3.x,v3.y,v3.z