我是C++编程的新手.我想用while循环编写一个程序,它显示sin,cos和Tan的三角表.它以角度为单位,相差5,显示结果.这就是我试过的,
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int num;
cout<< "Angle Sin Cos Tan"<<endl;
cout<< "..........................."<<endl;
num=0;
while (num<=360)
{
cout <<setw(3)<<num<<" "
<<setw(3)<<setprecision(3)<<sin(num)<<" "
<<setw(3)<<setprecision(3)<<cos(num)<<" "
<<setw(5)<<setprecision(3)<<tan(num)<<endl;
num=num+5;
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法在while循环中将弧度更改为度数,即使对于弧度,显示也看起来不太有希望.我该如何解决?
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main() {
const long double longDoublePI = 3.141592653589793238;
cout << setw(16) << "longDoublePI = " << setprecision(numeric_limits<long double>::digits10 + 1) << longDoublePI << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
But the result output is:
longDoublePI = 3.141592653589793116
Why the answer is wrong?
在我遇到问题之前,我想指出一些事情:1)我知道在cmath库中已经有一个atan2函数,这纯粹是一个练习和我自己的练习,2)我知道代码不占0.
好吧,所以tan(theta)= y/x,其中y和x是平面上的坐标......这意味着:
四元组和第四组中的theta = atan(y/x),四元组和第三组中的theta = atan(y/x)+ 180
那么为什么当我使用以下代码时:
float atan(float y, float x)
{
float result = 0.0f;
if (x > 0) //quads I and IV if x is positive
{
result = atanf(y/x);
}
else if (x < 0)
{
result = atan(y/x) + 180; //quads II and III if x is negative
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
它吐了我垃圾吗?例如,对于坐标(-4,4),它给出了结果:179.215,它应该是135:
atan(4/-4)= -45度+ 180度= 135度
但正在发生的是计算
atan(4.0f/-4.0f)= -0.785398 + 180度= 179.215.
我在这里错过了一些步骤吗?
在这个基本的C++程序中,为什么不能打印出常量Pi?
#include <iostream>
using namespace std;
#define Pi 3.1415;
int main() {
cout << Pi << endl;
return 0;
} //main
Run Code Online (Sandbox Code Playgroud) 我在C++ Intro中做了一个功课.其中一个说将以下数学方程式转换为C++代码:
x = 10?/(a+b) sin3C+ 3(ln a)(tan C)
Run Code Online (Sandbox Code Playgroud)
其中a,b和C是用户输入的,C是度.
我自己尝试过并最终得到了这个:
float x,y,z,a,b,C;
cout<< "Input the a-value: ";
cin>> a;
cout<< "\nInput the b-value: ";
cin>> b;
cout<< "\nInput the C-value: ";
cin>> C;
C = C*3.1416/180;
x = (10*3.1416/a+b)*pow(sin(C),3)+3*log(a)*tan(C);
cout<< "\n The value of x is " << x;
Run Code Online (Sandbox Code Playgroud)
我试过a = 5,b = 10,C = 15,x的结果是1.57606.我在科学计算器中尝试过,x变为1.33005.我的代码中可能出现什么问题?谢谢!
如果我的帖子结构有任何问题,我很抱歉,因为这是我第一次在这里发帖,英语不是我的母语