写一个c语言动态爱心代码
时间: 2023-03-24 11:02:49 浏览: 171
很高兴回答你的问题。以下是一个简单的 C 语言动态爱心代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define PI 3.14159265358979323846
int main()
{
int i, j, k;
double x, y, r;
int width = 80, height = 40;
char heart[height][width];
srand(time(NULL));
for (i = ; i < height; i++)
{
for (j = ; j < width; j++)
{
heart[i][j] = ' ';
}
}
for (k = ; k < 100; k++)
{
r = (double)rand() / RAND_MAX * (1 - sin(PI / 4));
x = r * cos(2 * PI * rand() / RAND_MAX) + .5;
y = r * sin(2 * PI * rand() / RAND_MAX) + .5;
i = (int)(y * (height - 1));
j = (int)(x * (width - 1));
heart[i][j] = '*';
}
for (i = ; i < height; i++)
{
for (j = ; j < width; j++)
{
putchar(heart[i][j]);
}
putchar('\n');
}
return ;
}
阅读全文