C++我的数学有什么问题,为什么我的代码不能正确循环

C++ what is worng with my math and why wont my code loop correctly

本文关键字:我的 不能 代码 循环 为什么 问题 什么 C++      更新时间:2023-10-16

[我的程序不会循环,我不知道REC->POL][1]的数学有什么问题

-如果字符是p/p,则两个浮点数应解释为一组Polar坐标和程序应首先计算等效矩形坐标,然后显示矩形坐标和极坐标值。

-程序应连续读取坐标值集直到输入Q/Q(退出(。-一组坐标值由一个字符后跟两个浮点组成数字。单个字符可以是R/R或P/P。如果字符是R/R,则两个浮点数字应被解释为一组直角坐标,程序应首先计算等效极坐标,然后显示两个直角坐标和极坐标值。

如果输入R/R、p/p或Q/Q以外的任何其他字符,程序应显示错误消息。注意程序需要进行两次"伪"读取以忽略非法字符后面的两个坐标值。在下面的示例中,在检测到非法的"d"字符后,程序必须读取并忽略"d"后面的99.9和11.1。同样,对于非法的"L"输入

int main() {
double x;
double y;
double M;
double th;
char input;
cin >> input;
cin >> x >> y;
while ((input != 'q') && (input != 'Q')) {
if ((input == 'r') || (input == 'R')) {
M = sqrt((x*x) + (y*y));
th = atan2(y, x);
x = M * cos(th);
y = M * sin(th);
th = th * (180 / M_PI);
cout << "POL -> REC: REC: X = " << x << " Y = " << y << " POL: M = " << M << " A = " << th << endl;
cin >> input >> x >> y;
}
if ((input == 'p') || (input == 'P')) {
th = atan2(y, x);
M = sqrt((x*x) + (y*y));
M = M * (180 / M_PI);
th = th * (180 / M_PI);
x = M * cos(th);
y = M * sin(th);

x = x * (M_PI / 180);
y = x * (M_PI / 180);
cout << "REC -> POL: REC: X = " << x << " Y = " << y << " POL: M = " << M << " A = " << th << endl;
cin >> input >> x >> y;
}
if ((input != 'r') && (input != 'R') && (input != 'p') && (input != 'P')) {
cout << "Format Error!" << endl;
cin >> input >> x >> y;
}
cin >> input >> x >> y;
return 0;
}
}

循环末尾的return 0;是罪魁祸首。你可以完全清理它,在主要功能中不需要它

return 0;在您的if循环中。通过将return 0;移动到}之下并且在最后一个}之间,它应该运行。

您还应该考虑不使用using namespace std;。点击此处了解更多信息。

相关文章: