能给我c++的爱心代码吗
时间: 2023-10-16 18:07:36 浏览: 72
基于C/C++的爱心源码
当然可以!以下是一段简单的 C++ 爱心代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const double PI = 3.14159265358979323846;
const int width = 60, height = 30;
char canvas[height][width+1];
// 初始化画布
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
canvas[i][j] = ' ';
}
canvas[i][width] = '\0';
}
// 绘制爱心形状
for (double theta = 0.0; theta <= 2*PI; theta += PI/180) {
double x = 16 * pow(sin(theta), 3);
double y = -13*cos(theta) + 5*cos(2*theta) + 2*cos(3*theta) + cos(4*theta);
int i = static_cast<int>((y + 15) * height / 30);
int j = static_cast<int>((x + width/2) * width / (2*width));
canvas[i][j] = '*';
}
// 输出画布
for (int i = 0; i < height; ++i) {
cout << canvas[i] << endl;
}
return 0;
}
```
希望对你有所帮助!
阅读全文