给我一个使用cmath的c++demo
时间: 2024-02-01 14:16:58 浏览: 30
以下是一个使用cmath库的C++代码示例:
```c++
#include <iostream>
#include <cmath>
int main() {
double x = 2.0;
double y = 3.0;
// 计算 x 的平方根和 y 的绝对值
double sqrt_x = std::sqrt(x);
double abs_y = std::abs(y);
// 输出结果
std::cout << "The square root of " << x << " is " << sqrt_x << std::endl;
std::cout << "The absolute value of " << y << " is " << abs_y << std::endl;
return 0;
}
```
在这个例子中,我们使用了cmath库中的sqrt和abs函数分别计算x的平方根和y的绝对值。注意,cmath库中的函数都在std命名空间中,所以我们需要使用std::前缀来调用它们。
相关问题
给我一个C++生成花朵的代码
以下是一个简单的C++代码,可以生成一个基本的花朵图案:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const int size = 40; // 花朵大小
const int center_x = size / 2; // 中心点x坐标
const int center_y = size / 2; // 中心点y坐标
const int petal_num = 8; // 花瓣个数
const double petal_angle = 2 * M_PI / petal_num; // 花瓣角度
const int petal_size = size / 4; // 花瓣长度
const double petal_width = 0.3; // 花瓣宽度
const double petal_curve = 0.5; // 花瓣曲线
const double stem_width = 0.2; // 茎宽度
const int stem_height = size / 2; // 茎高度
// 生成花朵
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
double distance = sqrt(pow(x - center_x, 2) + pow(y - center_y, 2));
if (distance < petal_size) {
double angle = atan2(y - center_y, x - center_x);
double petal_distance = petal_size - distance;
double petal_width_distance = petal_distance * petal_width;
double petal_curve_distance = pow(petal_distance / petal_size, petal_curve) * petal_size;
for (int i = 0; i < petal_num; i++) {
double petal_angle_distance = angle - i * petal_angle;
double petal_x = center_x + petal_curve_distance * cos(petal_angle_distance) + petal_width_distance * sin(petal_angle_distance);
double petal_y = center_y + petal_curve_distance * sin(petal_angle_distance) - petal_width_distance * cos(petal_angle_distance);
if (petal_x >= 0 && petal_x < size && petal_y >= 0 && petal_y < size) {
cout << "*";
} else {
cout << " ";
}
}
} else if (y == center_y && x >= center_x - stem_width * size / 2 && x < center_x + stem_width * size / 2) {
cout << "|";
} else if (y >= center_y && y < center_y + stem_height && x >= center_x - stem_width * size / 2 && x < center_x + stem_width * size / 2) {
cout << "H";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
```
这个代码生成的花朵是一个简单的ASCII图案,可能需要一些调整来适应你的需求。
使用C++输出一个心形
以下是使用C++输出一个心形的代码,可以在命令行窗口中运行:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const int size = 30;
const double PI = 3.14159265358979323846;
const char heart = '*';
for (int y = size; y >= -size; --y)
{
for (int x = -size; x <= size; ++x)
{
double fx = x / 20.0;
double fy = y / 20.0;
double f = (pow(fx, 2.0) + pow(fy, 2.0) - 1.0) * pow(fx, 2.0) * pow(fy, 2.0);
if (f <= 0.0)
{
cout << heart;
}
else
{
cout << ' ';
}
}
cout << endl;
}
return 0;
}
```
运行效果如下:
```
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)