【发布时间】:2018-04-05 13:02:06
【问题描述】:
我正在尝试将从文件接收的矢量值转换为两种不同的格式。转换为第一种格式并打印出矢量后,我想使用原始的“读入”值将它们转换为第二种格式。
目前,似乎第二次转换发生在已转换的值上。但是,我不明白为什么它不转换回原始值呢?最终,我怎样才能使用向量的原始值进行第二次转换,所以
else {
GetType();
GetXArg();
GetYArg();
}
第二次工作?
这里是sn-p的代码:
void Force::convToP() //converts to polar
{
if (forceType == 'c')
{
SetType('p');
SetXArg(sqrt(xArg * xArg + yArg * yArg));
SetYArg(atan(yArg / xArg));
}
else {
GetType(); //just return type, xArg and yArg in their original form
GetXArg();
GetYArg();
}
}
void Force::convToC() //converts to cartesian
{
if (forceType == 'p') {
SetType('c');
SetXArg(xArg * cos(yArg));
SetYArg(xArg * sin(yArg));
}
else {
GetType();
GetXArg();
GetYArg();
}
}
及主要功能:
while (file >> type >> x >> y) {
Force f(type, x, y);
force.push_back(f);
}
for (int i = 0; i < force.size(); ++i) {
force[i].printforce();
}
cout << "Forces in Polar form: " << endl;
for (int i = 0; i < force.size(); ++i)
{
force[i].convToP();
force[i].printforce();
}
cout << "Forces in Cartesian form: " << endl;
for (int i = 0; i < force.size(); ++i) {
force[i].convToC();
force[i].printforce();
}
最后,此刻的输出是:
p 10 0.5
c 12 14
p 25 1
p 100 0.8
c 50 50
p 20 3.14
c -100 25
p 12 1.14
Forces in Polar form: <-first conversion. All works fine
p 10 0.5
p 18.4391 0.649399
p 25 1
p 100 0.8
p 70.7107 0.61548
p 20 3.14
p 103.078 0.237941
p 12 1.14
Forces in Cartesian form:
c 8.77583 4.20736 <-works fine
c 14.6858 8.8806 <-why doesn't convert back to c 12 14/ how to use the original values of vector
c 13.5076 11.3662
c 69.6707 49.9787
c 57.735 33.3333
c -20 -0.0318509
c 100.173 23.6111
c 5.01113 4.55328
Press any key to continue . . .
对此非常陌生,困惑了一段时间,因此非常感谢任何帮助和建议。
【问题讨论】:
-
请提供minimal reproducible example。例如,在您的代码中,不清楚所有
GetXArg();在做什么。顺便说一句,如果您执行a = foo(a);之类的操作,您将如何获得a的原始值? -
@user463035818 所有
Get函数只返回xArg、yArg和fType -
是的,我看到了评论,但这让我更加困惑,因为如果那些
Get函数返回一些东西,那么你就忽略返回的值...... -
MCVE 不是“这里有一些代码片段和对缺失位的通俗描述”。
-
通过手动检查
c 50 50,你得到了p 70.7107 0.61548,但atan(1)是0.785398...