我们应该创建一个“结构”,允许用户输入两个向量(x1,y1)和(x2,y2)并创建添加和减去向量的函数。我以为我的代码结构是正确的,但我在加法和减法函数中遇到错误,我不知道为什么。总决赛正在摧毁我。请指出我正确的方向。
/*
v1=(a,b) v2=(c,d)
v1+v2 = (a,b)+(c,d) = (a+c, b+d)
v1-v2 = (a,b)-(c,d) = (a-c, b-d)
define an array of two mathematical vector structures
each structure consists of two double-precision components, a and b
permit user to enter two vectors, call two functions to return the sum and
difference
display results calculated
*/
#include <iostream>
using namespace std;
struct Vector
{
double X;
double Y;
} v1, v2;
//prototypes for add and subtract functions
double addVector(Vector vectadd);
double subtVector(Vector vectsub);
int main()
{
cout << "This program will add and subtract two vectors." << endl;
cout << "Enter X1: " << endl;
cin >> v1.X;
cout << "Enter Y1: " << endl;
cin >> v1.Y;
cout << "Enter X2: " << endl;
cin >> v2.X;
cout << "Enter Y2: " << endl;
cin >> v2.Y;
addVector(v1);
subtVector(v2);
return 0;
}
double addVector(Vector vectadd)
{
cout << "When added together, (x1, y1) + (x2, y2) becomes: " << endl;
cout << "(" << (v1.X + v2.X) << "," << (v1.Y + v2.Y) << ")" << endl;
}
double subtVector(Vector vectsub)
{
cout << "When subtracted from one another, (x1, y1) - (x2, y2) becomes: " <<
endl;
cout << "(" << (v1.X - v2.X) << "," << (v1.Y - v2.Y) << ")" << endl;
}
编辑: 我将函数更改为VOID函数,现在代码按预期工作。这可能不是完成这项任务的最优雅方式,对吗?
#include <iostream>
using namespace std;
struct Vector
{
double X;
double Y;
}v1, v2;
//prototypes for add and subtract functions
void addVector (Vector vectadd);
void subtVector (Vector vectsub);
int main()
{
cout << "This program will add and subtract two vectors." << endl;
cout << "Enter X1: ";
cin >> v1.X;
cout << "\nEnter Y1: ";
cin >> v1.Y;
cout << "\nEnter X2: ";
cin >> v2.X;
cout << "\nEnter Y2: ";
cin >> v2.Y;
cout << endl;
addVector (v1);
subtVector (v2);
return 0;
}
void addVector (Vector vectadd)
{
cout << "When added together, (x1, y1) + (x2, y2) becomes: " << endl;
cout << "(" << (v1.X + v2.X) << "," << (v1.Y + v2.Y) << ")" << endl;
}
void subtVector (Vector vectsub)
{
cout << "When subtracted from one another, (x1, y1) - (x2, y2) becomes: " << endl;
cout << "(" << (v1.X - v2.X) << "," << (v1.Y - v2.Y) << ")" << endl;
答案 0 :(得分:0)
您的代码未编译,原因在于消息:
'subtVector':必须返回一个值
提示,该函数也应该返回一个向量, addVector函数可能如下所示:
Vector addVector(Vector& vA, Vector& vB)
{
Vector r;
r.X = vA.X + vB.X;
r.Y = vA.Y + vB.Y;
cout << "When added together, (x1, y1) + (x2, y2) becomes: " << endl;
cout << "(" << r.X << "," << r.Y << ")" << endl;
return r;
}
同样适用于 subVector
答案 1 :(得分:0)
函数void
或具有其他数据类型。
请注意:
void f()
{
// this function does something, but it does NOT have to return anything because
//it is a void function
}
但
int fun()
{
// does something
return expression;
}
此函数必须返回一个值,因为它不是void
函数。在这种情况下,它必须返回int
值。
void
类型的函数可以直接调用
//code
f();
// code
但non-void
函数可以分配给其他变量,如:
// code
v = fun();
或者可以是
这样的表达式的一部分//code
cout << 5 + fun();
因此...
让你的功能像这样无效
void addVector(Vector vectadd);
void subtVector(Vector vectsub);
void addVector(Vector vectadd)
{
// your code
}
void subtVector(Vector vectsub)
{
// your code
}
或者,您可以从函数返回向量而不是double
。
Vector addVector (Vector , Vector);
Vector subtVector (Vector , Vector);
int main()
{
// your code
Vector V;
cout << "When added together, (x1, y1) + (x2, y2) becomes: " << endl;
V = addVector (v1, v2);
cout << "(" << V.X << "," << V.Y << ")" << endl;
V = subtVector (v1, v2);
cout << "When added together, (x1, y1) + (x2, y2) becomes: " << endl;
cout << "(" << V.X << "," << V.Y << ")" << endl;
return 0;
}
Vector addVector (Vector v1, Vector V2)
{
Vector Result;
Result.X = v1.X + v2.X;
Result.Y = v1.Y + v2.Y;
return Result;
}
Vector subtVector (Vector v1, Vector V2)
{
Vector Result;
Result.X = v1.X - v2.X;
Result.Y = v1.Y - v2.Y;
return Result;
}