214专属,C语言实现跳动的心!
掐指一算
正式进入214
在此祝大家开心
那么大家是怎么向心目中的女/男神表白的呢?
玫瑰?巧克力? 情书?
实现过程其实很简单
首先使用for循环绘制心形
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 = z * z * z - x * x * y * y * y;putchar(f <= 0.0f ? "*********"[(int)(f * -8.0f)] : ' ');}putchar('\n');}
然后使用for循环进行切换cmd窗口颜色就可以做到
for (time = 0; time < 99999999; time++);system("color b");
完整代码如下:
#include <stdio.h>#include <math.h>#include <stdlib.h>#define I 20#define R 340#include <string.h>int main(){char answer[10];printf(" 自从遇见了你,就不停地想你\n");printf(" 编一个死循环\n 让我们一直走,一直走。");printf(" 如果你也愿意\n 输入yes,你可以看到我的真心\n");scanf("%s", answer);if (!(answer[0] == 'y' && answer[1] == 'e' && answer[2] == 's')){printf("555 \n");return 0;}//首先使用for循环打印出心形float y, x, z, f;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 = z * z * z - x * x * y * y * y;putchar(f <= 0.0f ? "*********"[(int)(f * -8.0f)] : ' ');}putchar('\n');}//循环变色long time;for (;;){system("color a");for (time = 0; time < 99999999; time++);system("color b");for (time = 0; time < 99999999; time++);system("color c");for (time = 0; time < 99999999; time++);system("color d");for (time = 0; time < 99999999; time++);system("color e");for (time = 0; time < 99999999; time++);system("color f");for (time = 0; time < 99999999; time++);system("color 0");for (time = 0; time < 99999999; time++);system("color 1");for (time = 0; time < 99999999; time++);system("color 2");for (time = 0; time < 99999999; time++);system("color 3");for (time = 0; time < 99999999; time++);system("color 4");for (time = 0; time < 99999999; time++);system("color 5");for (time = 0; time < 99999999; time++);system("color 6");for (time = 0; time < 99999999; time++);system("color 7");for (time = 0; time < 99999999; time++);system("color 8");for (time = 0; time < 99999999; time++);system("color 9");}getchar();return 0;}
效果如下:
如何让心跳动起来
一颗不会动的心怎么能显得足够真(zhuang)诚(B)呢?
让我们使用数学函数巧妙地让这颗心动起来。
完整代码如下:
float f(float x, float y, float z) {float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;return a * a* a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;}float h(float x, float z) {for (float y = 1.0f; y >= 0.0f; y -= 0.001f)if (f(x, y, z) <= 0.0f)return y;return 0.0f;}int main() {HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);_TCHAR buffer[25][80] = { _T(' ') };_TCHAR ramp[] = _T(".:-=++#%@"); //心 每层使用的字符int is = 0;for (float t = 0.0f;; t += 0.1f){//两种颜色循环切换is++;if(is%2==0)system("color d");else system("color c");int sy = 0;float s = sinf(t);float a = s * s * s * s * 0.2f;//生成当前心的形态for (float z = 1.3f; z > -1.2f; z -= 0.1f){_TCHAR* p = &buffer[sy++][0];float tz = z * (1.2f - a);for (float x = -1.5f; x < 1.5f; x += 0.05f) {float tx = x * (1.2f + a);float v = f(tx, 0.0f, tz);//如果是在心的范围中间 就计算对应的层if (v <= 0.0f) {float y0 = h(tx, tz);float ny = 0.01f;float nx = h(tx + ny, tz) - y0;float nz = h(tx, tz + ny) - y0;float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);float d = (nx + ny - nz) * nd * 0.5f + 0.5f;*p++ = ramp[(int)(d * 5.0f)];}//如果不在直接添加空格else*p++ = ' ';}}//将当前的心形打印出来 按行打印for (sy = 0; sy < 25; sy++) {COORD coord = { 0, sy };SetConsoleCursorPosition(o, coord);WriteConsole(o, buffer[sy], 79, NULL, 0);//if(sy ==12 )printf("520");}Sleep(33);}}
效果如下:
欢迎大家评论区留言!
来一道 C++面试题试试手?
你可能对 sizeof(i++) 有点误解。。。
