vlambda博客
学习文章列表

C语言程序员表白-心形图案

C语言程序员表白-心形图案

最近后台收到很多心形图案的留意,有几位甚至是没有编程基础的男同学,为了博红颜一笑努力学习。

为此我准备了一遍完整的编写过程。

在准备开发需要先安装编译器,可以安装VS、dev-c等,推荐使用dev-c,简洁方便。VS更适合长远深入学习windows编程。dev-c安装过程简单,下载后按提示下一步安装即可。安装完毕后桌面上有相应图标,双击运行即可。

打开DEV-C程序,然后新建代码文件,将下列代码复制至文件内。每次只复制一段,一次复制到文件内会报多个main重复。

可参考下面这个视频,实现彩色心形告白代码。

大家可以参考着使用,下面是几种不同的实现代码。

如果喜欢的话请分享给有需要的朋友,谢谢。

彩色告白

 
   
   
 
  1. #include<stdio.h>

  2. #include<math.h>

  3. #include<windows.h>

  4. #include<time.h>

  5. #define U 0.1

  6. #define V 0.053


  7. void SetColor(unsigned short ForeColor,unsigned short BackGroundColor)

  8. {

  9. HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);

  10. SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));

  11. }

  12. int main()

  13. {

  14. int i,s=0,t,a=10,b=11,c=12,d=13,e=14;

  15. int z[] = {32,32,206,210,207,178,187,182,196,227,33,32,32};

  16. float x,y;

  17. srand(time(NULL));

  18. for(y=1.3;y>=-1.1;y-=U)

  19. {

  20. for(x=-2;x<1.4;x+=V)

  21. {

  22. if((((x*x+y*y-1)*(x*x+y*y-1)*(x*x+y*y-1)-x*x*y*y*y)<=0))

  23. {

  24. if(y>=1.3-10*U||y<=1.3-11*U)

  25. {

  26. s++;

  27. if(s%4==1){SetColor(a,0);printf("l");}

  28. if(s%4==2){SetColor(e,0);printf("o");}

  29. if(s%4==3){SetColor(c,0);printf("v");}

  30. if(s%4==0){SetColor(d,0);printf("e");}

  31. }

  32. else

  33. {

  34. for(i = 0;i < 42;i++)

  35. {

  36. if(i<=14||i>=28)

  37. {

  38. s++;

  39. if(s%4==1){SetColor(a,0);printf("l");}

  40. if(s%4==2){SetColor(e,0);printf("o");}

  41. if(s%4==3){SetColor(c,0);printf("v");}

  42. if(s%4==0){SetColor(d,0);printf("e");}

  43. }

  44. else

  45. {

  46. SetColor(b,0);

  47. printf("%c", z[i-15]);

  48. Sleep(50);

  49. }

  50. }

  51. break;

  52. }

  53. }

  54. else

  55. printf(" ");

  56. Sleep(1);

  57. }

  58. printf("\n");

  59. }

  60. printf("按任意键继续!");

  61. getchar();

  62. while(1)

  63. {

  64. system("cls");

  65. t=a;a=b;b=c;c=d;d=e;e=t;

  66. for(y=1.3;y>=-1.1;y-=U)

  67. {

  68. for(x=-2;x<1.4;x+=V)

  69. {

  70. if((((x*x+y*y-1)*(x*x+y*y-1)*(x*x+y*y-1)-x*x*y*y*y)<=0))

  71. {

  72. if(y>=1.3-10*U||y<=1.3-11*U)

  73. {

  74. s++;

  75. if(s%4==1){SetColor(a,0);printf("l");}

  76. if(s%4==2){SetColor(b,0);printf("o");}

  77. if(s%4==3){SetColor(c,0);printf("v");}

  78. if(s%4==0){SetColor(d,0);printf("e");}

  79. }

  80. else

  81. {

  82. for(i = 0;i < 42;i++)

  83. {

  84. if(i<=14||i>=28)

  85. {

  86. s++;

  87. if(s%4==1){SetColor(a,0);printf("l");}

  88. if(s%4==2){SetColor(b,0);printf("o");}

  89. if(s%4==3){SetColor(c,0);printf("v");}

  90. if(s%4==0){SetColor(d,0);printf("e");}

  91. }

  92. else

  93. {

  94. SetColor(e,0);

  95. printf("%c", z[i-15]);

  96. }

  97. }

  98. break;

  99. }

  100. }

  101. else

  102. printf(" ");

  103. }

  104. printf("\n");

  105. }

  106. Sleep(1000);

  107. system("cls");


  108. }

  109. }

简单心形

 
   
   
 
  1. #include <math.h>

  2. #include <stdio.h>

  3. int main()

  4. {

  5. float y, x, a;

  6. for (y = 1.5f;y > -1.5f;y -= 0.1f)

  7. {

  8. for (x = -1.5f;x < 1.5f;x += 0.05f)

  9. {

  10. a = x*x + y*y - 1;

  11. putchar(a*a*a - x*x*y*y*y <= 0.0f ? '*' : ' ');

  12. }

  13. putchar('\n');

  14. }

  15. return 0;

  16. }

花纹心形

 
   
   
 
  1. #include <math.h>

  2. #include <stdio.h>

  3. int main()

  4. {

  5. float y, x, z,f;

  6. for (y = 1.5f;y > -1.5f;y -= 0.1f)

  7. {

  8. for (x = -1.5f;x < 1.5f;x += 0.05f)

  9. {

  10. z = x*x + y*y - 1;

  11. f = z*z*z - x*x*y*y*y;

  12. putchar(f <= 0.0f ? ".:-=+*#%@"[(int)(f*-8.0f)] : ' ');

  13. }

  14. putchar('\n');

  15. }



  16. getchar();

  17. return 0;

  18. }

跳动心形

 
   
   
 
  1. #include <stdio.h>

  2. #include <math.h>

  3. #include <windows.h>

  4. #include <tchar.h>


  5. float f(float x, float y, float z) {

  6. float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;

  7. return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;

  8. }

  9. float h(float x, float z)

  10. {

  11. for (float y = 1.0f; y >= 0.0f; y -= 0.001f)

  12. if (f(x, y, z) <= 0.0f)

  13. return y;

  14. return 0.0f;

  15. }

  16. int main()

  17. {

  18. HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);

  19. _TCHAR buffer[25][80] = {_T(' ')};

  20. _TCHAR ramp[] = _T(".:-=+*#%@");

  21. for (float t = 0.0f;; t += 0.1f)

  22. {

  23. int sy = 0;

  24. float s = sinf(t);

  25. float a = s * s * s * s * 0.2f;

  26. for (float z = 1.3f; z > -1.2f; z -= 0.1f)

  27. {

  28. _TCHAR *p = &buffer[sy++][0];

  29. float tz = z * (1.2f - a);

  30. for (float x = -1.5f; x < 1.5f; x += 0.05f)

  31. {

  32. float tx = x * (1.2f + a);

  33. float v = f(tx, 0.0f, tz);

  34. if (v <= 0.0f)

  35. {

  36. float y0 = h(tx, tz);

  37. float ny = 0.01f;

  38. float nx = h(tx + ny, tz) - y0;

  39. float nz = h(tx, tz + ny) - y0;

  40. float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);

  41. float d = (nx + ny - nz) * nd * 0.5f + 0.5f;

  42. *p++ = ramp[(int)(d * 5.0f)];

  43. }

  44. else

  45. *p++ = ' ';

  46. }

  47. }

  48. for (sy = 0; sy < 25; sy++)

  49. {

  50. COORD coord = {0, sy};

  51. SetConsoleCursorPosition(o, coord);

  52. WriteConsole(o, buffer[sy], 79, NULL, 0);

  53. }

  54. Sleep(33);

  55. }

  56. }

心形小程序

 
   
   
 
  1. #include <stdio.h>

  2. #include <math.h>

  3. #include <windows.h>

  4. #include <time.h>

  5. #include <stdlib.h>

  6. void

  7. SetColor(unsigned short ForeColor, unsigned short BackGroundColor)

  8. {

  9. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);

  10. SetConsoleTextAttribute(hCon, (ForeColor % 16) | (BackGroundColor % 16 * 16));

  11. }

  12. int main()

  13. {


  14. float x, y;

  15. int s = 0, n, i, j;

  16. for (i = 1; i <= 50; i++)

  17. {

  18. SetColor(0, 14);

  19. printf(" ");

  20. printf("%d%%", 2 * i);

  21. Sleep(101 - 2 * i);

  22. printf("\b\b\b");

  23. }

  24. SetColor(15, 0);

  25. printf("\b\n加载完成!即将进入:");

  26. Sleep(2000);

  27. system("cls");

  28. do

  29. {

  30. system("color 0E");

  31. for (i = 0; i <= 56; i++)

  32. {

  33. if (i >= 20)

  34. {

  35. printf("*");

  36. Sleep(10);

  37. }

  38. else

  39. printf(" ");

  40. }

  41. printf("\n");

  42. for (i = 0; i <= 56; i++)

  43. {

  44. if (i >= 20)

  45. {

  46. printf("*");

  47. Sleep(10);

  48. }

  49. else

  50. printf(" ");

  51. }

  52. printf("\n\t\t\t你想要怎样的心形:\n");

  53. printf("\n\t\t\t1:由love组成的心形!\n");

  54. printf("\n\t\t\t2:由随机字符组成的心形!\n");

  55. printf("\n\t\t\t3:由随机数字组成的心形!\n");

  56. printf("\n\t\t\t4:由随机颜色组成的心形!\n");

  57. printf("\n\t\t\t5:退出!\n");

  58. for (i = 0; i <= 56; i++)

  59. {

  60. if (i >= 20)

  61. {

  62. printf("*");

  63. Sleep(10);

  64. }

  65. else

  66. printf(" ");

  67. }

  68. printf("\n");

  69. for (i = 0; i <= 56; i++)

  70. {

  71. if (i >= 20)

  72. {

  73. printf("*");

  74. Sleep(10);

  75. }

  76. else

  77. printf(" ");

  78. }

  79. printf("\n\t\t\t");


  80. srand(time(NULL));

  81. scanf("%d", &n);

  82. system("cls");


  83. switch (n)

  84. {

  85. case 1:

  86. for (x = 1.3; x >= -1.1; x -= 0.1)

  87. {

  88. for (y = -2; y <= 1.4; y += 0.053)

  89. {

  90. if (pow(x * x + y * y - 1, 3) - x * x * x * y * y <= 0)

  91. {

  92. s++;

  93. if (s % 4 == 1)

  94. printf("l");

  95. if (s % 4 == 2)

  96. printf("o");

  97. if (s % 4 == 3)

  98. printf("v");

  99. if (s % 4 == 0)

  100. printf("e");

  101. Sleep(2);

  102. }

  103. else

  104. printf(" ");

  105. }

  106. printf("\n");

  107. }

  108. printf("绘制完成,按任意键返回主页面!");

  109. break;

  110. case 2:

  111. for (x = 1.3; x >= -1.1; x -= 0.1)

  112. {

  113. for (y = -2; y <= 1.4; y += 0.053)

  114. {

  115. if (pow(x * x + y * y - 1, 3) - x * x * x * y * y <= 0)

  116. {

  117. printf("%c", rand() % 10 + rand() % 10 + 97);

  118. Sleep(2);

  119. }

  120. else

  121. printf(" ");

  122. }

  123. printf("\n");

  124. }

  125. printf("绘制完成,按任意键返回主页面!");

  126. break;

  127. case 3:

  128. for (x = 1.3; x >= -1.1; x -= 0.1)

  129. {

  130. for (y = -2; y <= 1.4; y += 0.053)

  131. {

  132. if (pow(x * x + y * y - 1, 3) - x * x * x * y * y <= 0)

  133. {

  134. printf("%d", rand() % 10);

  135. Sleep(2);

  136. }

  137. else

  138. printf(" ");

  139. }

  140. printf("\n");

  141. }

  142. printf("绘制完成,按任意键返回主页面!");

  143. break;

  144. case 4:

  145. for (x = 1.3; x >= -1.1; x -= 0.1)

  146. {

  147. for (y = -2; y <= 1.4; y += 0.053)

  148. {

  149. if (pow(x * x + y * y - 1, 3) - x * x * x * y * y <= 0)

  150. {

  151. SetColor(0, rand() % 6 + 10);

  152. printf(" ");

  153. Sleep(2);

  154. }

  155. else

  156. {

  157. SetColor(0, 0);

  158. printf(" ");

  159. }

  160. }

  161. printf("\n");

  162. }

  163. SetColor(15, 0);

  164. printf("绘制完成,按任意键返回主页面!");

  165. break;

  166. default:

  167. break;

  168. }

  169. system("cls");

  170. } while (n != 5);

  171. }