不用库函数不用int呢
时间: 2023-10-25 17:09:55 浏览: 40
如果不使用 math 库中的函数,同时也不使用 int 类型的强制类型转换,则可以采用以下方式将 double 类型的数转换为整数:
```c++
double num = 3.14159;
double intNum = (num > 0) ? floor(num) : ceil(num);
```
这个方法的原理是,对于正数,使用 floor 函数向下取整,对于负数,使用 ceil 函数向上取整,这样就可以得到一个 double 类型的整数。需要注意的是,由于 double 类型的数值范围较大,在极端情况下可能会出现精度损失的问题。如果需要更高的精度,建议使用其他数据类型,或者调用 math 库中的函数。
相关问题
不用库函数实现代码
好的,以下是使用C++代码实现二次和三次贝塞尔曲线的示例:
```c++
#include <iostream>
#include <vector>
using namespace std;
struct Point {
float x;
float y;
};
// 计算组合数
int comb(int n, int i) {
if (i == 0) return 1;
int res = 1;
for (int j = 1; j <= i; j++) {
res *= n - j + 1;
res /= j;
}
return res;
}
// 计算二次贝塞尔曲线上的点
Point quadraticBezier(float t, Point p0, Point p1, Point p2) {
float x = (1 - t) * (1 - t) * p0.x + 2 * t * (1 - t) * p1.x + t * t * p2.x;
float y = (1 - t) * (1 - t) * p0.y + 2 * t * (1 - t) * p1.y + t * t * p2.y;
return {x, y};
}
// 计算三次贝塞尔曲线上的点
Point cubicBezier(float t, Point p0, Point p1, Point p2, Point p3) {
float x = (1 - t) * (1 - t) * (1 - t) * p0.x + 3 * t * (1 - t) * (1 - t) * p1.x + 3 * t * t * (1 - t) * p2.x + t * t * t * p3.x;
float y = (1 - t) * (1 - t) * (1 - t) * p0.y + 3 * t * (1 - t) * (1 - t) * p1.y + 3 * t * t * (1 - t) * p2.y + t * t * t * p3.y;
return {x, y};
}
int main() {
vector<Point> controlPoints{{100, 100}, {200, 300}, {400, 200}, {500, 400}}; // 控制点
int n = controlPoints.size() - 1; // 曲线阶数
int steps = 20; // 绘制的线段数
// 绘制二次贝塞尔曲线
for (int i = 0; i < steps; i++) {
float t1 = (float)i / steps;
float t2 = (float)(i + 1) / steps;
Point p1 = quadraticBezier(t1, controlPoints[0], controlPoints[1], controlPoints[2]);
Point p2 = quadraticBezier(t2, controlPoints[0], controlPoints[1], controlPoints[2]);
cout << "Line segment " << i + 1 << ": (" << p1.x << ", " << p1.y << ") - (" << p2.x << ", " << p2.y << ")" << endl;
}
// 绘制三次贝塞尔曲线
for (int i = 0; i < steps; i++) {
float t1 = (float)i / steps;
float t2 = (float)(i + 1) / steps;
Point p1 = cubicBezier(t1, controlPoints[0], controlPoints[1], controlPoints[2], controlPoints[3]);
Point p2 = cubicBezier(t2, controlPoints[0], controlPoints[1], controlPoints[2], controlPoints[3]);
cout << "Line segment " << i + 1 << ": (" << p1.x << ", " << p1.y << ") - (" << p2.x << ", " << p2.y << ")" << endl;
}
return 0;
}
```
在这个示例代码中,我们定义了一个Point结构体表示二维坐标点,使用vector存储了控制点的坐标。在计算二次和三次贝塞尔曲线上的点时,我们使用了贝塞尔曲线的公式。在绘制时,我们将曲线分成多个线段,对每个线段进行绘制。
注:在实际应用中,需要将计算出的曲线上的点进行连接,形成一条光滑的曲线。
C语言 不用库函数实现正弦算法
正弦函数是一个周期函数,可以使用泰勒级数展开来近似计算其值。泰勒级数展开式如下:
sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
根据泰勒级数展开式,可以写出一个计算正弦值的函数,如下所示:
```c
#include <stdio.h>
double sin(double x) {
double result = 0.0;
double term = x;
int sign = 1;
int i;
for (i = 1; i <= 10; i += 2) {
result += sign * term;
term *= -(x * x) / (i * (i + 1));
sign = -sign;
}
return result;
}
int main() {
double angle = 30.0;
double radians = angle * 3.14159265358979323846 / 180.0;
double sin_value = sin(radians);
printf("sin(%.2f) = %.2f\n", angle, sin_value);
return 0;
}
```
这个函数使用了泰勒级数展开式的前10项来计算正弦值。需要注意的是,函数的参数为弧度制,因此需要将角度转换为弧度。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)