計算如下公式,並輸出結果:

其中r、s的值由鍵盤輸入。sin x的近似值按如下公式計算,計算精度為10-10:

程序說明:
#include <math.h>和#include<cmath>的區別:
#include <math.h>為C語言中的庫函數,由於C++兼容C,所以仍可以使用此函數庫
在C++中推薦使用 #include <cmath>, 兩者中的函數以及使用方法幾乎完全相同
C++代碼如下:
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 const double TINY_VALUE = 1e-10; //計算精度 5 6 double tsin(double x) { //為了和標准庫中的sin()函數區別,所以取名為tsin()函數 7 double n = x,sum=0; 8 int i = 1; 9 do 10 { 11 sum += n; 12 i++; 13 n = -n * x*x / (2 * i - 1) / (2 * i - 2); 14 15 } while (fabs(n)>=TINY_VALUE); 16 return sum; 17 } 18 int main() { 19 int r, s; 20 double k; 21 cin >> r >> s; 22 if (r*r <= s * s) 23 k = sqrt(tsin(r)*tsin(r) + tsin(s)*tsin(s)); 24 else 25 k = tsin(r*s) /2 ; 26 cout << k; 27 return 0; 28 }
