c语言表白的代码合集,多种创意表白方式
1. 经典的心形表白
c
include
include
int main() {
float y, x, z, f;
int i, j;
for (y = 1.5f; y > -1.5f; y -= 0.1f) {
for (x = -1.5f; x < 1.5f; x += 0.05f) {
z = x x + y y - 1;
f = -1;
if (z z z - x x y y y <= 0.0f) {
f = sin(sqrt(abs(z z z - x x y y y)));
if (f > 0.4f) {
printf("");
} else if (f < -0.4f) {
printf(" ");
} else {
printf("o");
}
} else {
printf(" ");
}
}
printf("");
}
return 0;
}
这段代码会在控制台输出一个心形图案,你可以将你的表白信息放在心形中间,形成表白的效果。
2. 表白动画
c
include
include
include
include
define WIDTH 80
define HEIGHT 20
typedef struct {
int x, y;
char ch;
} Point;
Point points[25] = {{0}};
void init() {
int i;
for (i = 0; i < 25; i++) {
points[i].x = rand() % WIDTH;
points[i].y = rand() % HEIGHT;
points[i].ch = '';
}
}
void draw() {
int i;
for (i = 0; i < HEIGHT; i++) {
int j;
for (j = 0; j < WIDTH; j++) {
int k;
int found = 0;
for (k = 0; k < 25; k++) {
if (points[k].y == i && points[k].x == j) {
putchar(points[k].ch);
found = 1;
break;
}
}
if (!found) {
putchar(' ');
}
}
putchar('');
}
}
void move() {
int i;
for (i = 0; i < 25; i++) {
points[i].x = (points[i].x + 1) % WIDTH;
}
}
int main() {
init();
while (1) {
system("clear");
draw();
move();
usleep(500000);
}
return 0;
}
这段代码会随机生成25个点,然后不断向右移动这些点,形成动画效果。你可以将特定的点替换成表白的信息,形成动态表白的效果。
3. 表白音乐盒
c
include
include
include
include
define PI 3.14159265358979323846
int main() {
struct SoundIo sound_io;
struct SoundIoDevice device;
struct SoundIoStream stream;
struct SoundIoChannelArea area;
sound_io = soundio_create();
soundio_connect(sound_io);
device = soundio_get_default_output_device(sound_io);
stream = soundio_output_stream_create(sound_io, device);
soundio_stream_set_format(stream, SOUNDIO_FORMAT_FLOAT32LE);
soundio_stream_set_channels(stream, 2);
soundio_stream_set_sample_rate(stream, 44100);
while (1) {
float buffer[1024 2];
int i;
for (i = 0; i < 1024 2; i++) {
buffer[i] = sin(2 PI 440 i / 44100) 128;
}
soundio_stream_write_frames(stream, buffer, 1024);
usleep(50000);
}
soundio_stream_destroy(stream);
soundio_disconnect(sound_io);
soundio_destroy(sound_io);
return 0;
}
这段代码会输出440Hz的正弦波声音,这是音乐中的A音。你可以将频率改为其他值,播放不同的音乐,形成表白音乐盒的效果。
请注意,这些代码需要相应的库支持,例如`soundio`库用于播放声音。在编译时需要链接这些库。例如,使用gcc编译上述代码,需要添加`-lsoundio`参数。

