C++ 利用函数, 制作一个三角函数表
2016-06-01 22:26
351 查看
#include"stdlib.h" #include<iostream> #include<iomanip> using namespace std; double mysin(double); double myabs(double); void show_sin_table(int, int); const double pi = 3.1415926; int main() { show_sin_table(0, 90); system("pause"); return 0; } // 定义 show_sin_table () 函数 void show_sin_table(int iFromAngle, int iToAngle) { int x, count = 0; std::cout << " 到" << iFromAngle << iToAngle << "的三角函数表" << endl; std::cout << setiosflags(ios::fixed) << setiosflags(ios::right) << setprecision(3); for (x = 0; x < 3; x++) std::cout << setw(8) << "角度" << setw(8) << "sin(x)"; std::cout << endl; // 逐个地方转换并输出 for (x = iFromAngle; x <= iToAngle; x++) { cout << setw(8) << x << setw(8); cout << mysin(x*pi / 180); if (count % 3 == 2) cout << endl; ++count; } cout << endl; return ; } // 下面定义 mysin 函数 double mysin(double x) { double sum = x, x_pow = x, item; int n = 1, fact = 1, sign = 1; do { fact = fact*(n + 1)*(n + 2); // fact用于 表示阶乘,在公式作分母 x_pow *= x*x; // x_pow是分子中用于表示阶乘的 sign = -sign; // 确定 累加项的符号 item = x_pow / fact*sign; //计算要累加的项 sum += item; n += 2; } while (myabs(item) > 1e-5); return sum; } //下面定义myabs函数 double myabs(double x) { return ((x >= 0) ? x : -x); }
相关文章推荐
- 银行储蓄程序(C++,simple)
- C++ 指针与引用的偶遇
- c语言:int与char转换示例
- c++第6次实验 作业
- C++中的文件输入/输出
- C++实现堆排序
- C++走向远洋——59(项目三、图形面积、抽象类)
- 第7次c++实验
- andriod eclipse 如何设置ndk-build也即NDK自动编译C/C++、JNI程序
- MD5算法C语言实现16、32位版
- 四个C++函数模板实例
- C++构造函数析构函数
- C语言程序设计(二)
- C++实现堆
- C语言练习篇-4河内之塔
- C语言学习篇-6函数、递归、嵌套
- win10下c++通用开发杂记
- c++命名类型转换
- C++中头文件的使用
- c++中的成员访问级别和派生继承方式