c++ - 三角表生成器的输入问题
问题描述
- 选择的三角函数(A. sine, B. cosine, C. tangent)。
- 角度单位(A. 度或 B. 弧度)
- 角度的起始值和结束值
- 从一个角度值到下一个角度值的增量
这些是我目前的任务。目前我正在尝试做,例如,如果我选择正弦-A,那么我想选择度数-A,如何防止这两个发生冲突?我还是 C++ 的新手,所以我仍然对如何做到这一点感到困惑。任何帮助,将不胜感激。这是我在这里的第一篇文章,如果我做错了什么,请原谅我。
#include <iostream>
#include <iomanip>
#include <cmath>
const double PI = 3.14159265;
using namespace std;
int main()
{
double v1, v2 , i;
cout << "Trigonometric Table Generator" << endl;
cout << " Choose the trignometric function(s): A-Sine, B-Cosine, C-Tangent" << endl;
cout << " (e.g Enter A for Sine or AC for both Sine and Tangent): ";
char input1;
cin >> input1;
cout << " Choose the unit of angle : A - degree or B - radian(e.g Enter A for degree) ";
char input2;
cin >> input2;
if (input2 = 'A') {
double degrees,i;
degrees = (PI / 180)* i;
}
else if (input2 == 'B') {
double radians, i;
radians = ( 180 / PI)* i;
}
cout << "Enter the starting value and ending values for the angle, separated with a space: ";
cin >> v1 >> v2;
cout << "Enter the increment from one angle value to the next: ";
cin >> i;
switch (input1) {
case 'A':
double sine;
for (i = 0; i <= v2; i = i + v1) {
//degrees
sine = (PI / 180)* i;
cout << i << setw(10) << setprecision(3) << sine << endl;
}
}
system("pause");
return 0;
}
解决方案
这里有一些想法可以让你走上正确的道路:
首先,在 C++ 中(与大多数其他编程语言一样),您必须先分配给变量,然后才能使用它的值。例如:
double radians, i;
radians = ( 180 / PI)* i;
由于您i
在为其分配值之前已在表达式中使用,因此这将导致未定义的行为。相反,请尝试将表达式移动到已分配radians = ( 180 / PI)* i;
的 for 循环内。i
其次,在 C++ 中,变量的范围通常由{}
它们在内部定义的括号确定。因此,当您编写以下内容时:
if (input2 = 'A') {
double degrees,i;
degrees = (PI / 180)* i;
}
那么变量degrees
只i
存在于if语句中。即使您定义了另一个具有相同名称的变量,如果它位于不同的范围内,它实际上也不会是同一个变量。
最后,您需要仔细考虑您在此处执行的数学运算。例如,看起来您实际上并没有在sin
任何地方调用过该函数。在 C++ 中,通过使用名称后跟括号中的参数来调用函数。例如,sin(3)
返回 3 的正弦值。
当您刚接触 C++ 时,这些东西可能会非常棘手,所以不要气馁!您可能想向老师或朋友寻求帮助,他们将能够提供更加个性化的帮助来指导您完成整个过程。
推荐阅读
- machine-learning - 可以将使用人工神经网络的模型视为多线性回归模型吗?
- .net - 为什么我的测试 winforms 应用程序似乎按顺序运行?
- r - 如何在 Shiny 中使用 Crosstalk 过滤反应数据表?
- sql - 运行查询的 MS Access 帮助
- javascript - Bootstrap输入按钮组插件,隐藏第二个按钮后显示第一个带圆角的按钮
- java - 无法在 Mac 中创建 org.apache.maven.archetypes:maven-archetype-quickstart
- php - 如何每次从mysql中的select查询中获取唯一记录
- javascript - 如何在 NestJS 中使用多个 sequelize DB 连接
- javascript - 在 List onclick 事件中添加数据
- c# - 不同View的ViewModel Initialize方法