当前位置:   article > 正文

爱心代码--C语言特供(可直接复制,亲测有效)_爱心代码可复制

爱心代码可复制

情人节到了,作为一名程序员,我们拥有属于我们的浪漫。

这里我总结了几种常见的爱心代码,简单易上手。

一.这是一种最为常见的爱心代码

  1. #include<stdio.h>
  2. #include<Windows.h>
  3. int main()
  4. {
  5. float x, y, a;
  6. for (y = 1.5; y > -1.5; y -= 0.1)
  7. {
  8. for (x = -1.5; x < 1.5; x += 0.05)
  9. {
  10. a = x * x + y * y - 1;
  11. putchar(a * a * a - x * x * y * y * y <= 0.0 ? '*' : ' ');
  12. }
  13. system("color 0c");
  14. putchar('\n');
  15. }
  16. return 0;
  17. }

也许我们可以再加一丁点东西让它变的更加浪漫一些

  1. #include<stdio.h>
  2. #include<Windows.h>
  3. int main()
  4. {
  5. float x, y, a;
  6. char color;
  7. while (1)
  8. {
  9. for (y = 1.5; y > -1.5; y -= 0.1)
  10. {
  11. for (x = -1.5; x < 1.5; x += 0.05)
  12. {
  13. a = x * x + y * y - 1;
  14. putchar(a * a * a - x * x * y * y * y <= 0.0 ? '*' : ' ');
  15. }
  16. system("color 0c");
  17. putchar('\n');
  18. }
  19. }
  20. return 0;
  21. }

二.一个会跳动的爱心又有谁不爱呢?

  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. for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
  11. if (f(x, y, z) <= 0.0f)
  12. return y;
  13. return 0.0f;
  14. }
  15. int main() {
  16. HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);
  17. _TCHAR buffer[25][80] = { _T(' ') };
  18. _TCHAR ramp[] = _T(".:-=+*#%@");
  19. for (float t = 0.0f;; t += 0.1f) {
  20. int sy = 0;
  21. float s = sinf(t);
  22. float a = s * s * s * s * 0.2f;
  23. for (float z = 1.3f; z > -1.2f; z -= 0.1f) {
  24. _TCHAR* p = &buffer[sy++][0];
  25. float tz = z * (1.2f - a);
  26. for (float x = -1.5f; x < 1.5f; x += 0.05f) {
  27. float tx = x * (1.2f + a);
  28. float v = f(tx, 0.0f, tz);
  29. if (v <= 0.0f) {
  30. float y0 = h(tx, tz);
  31. float ny = 0.01f;
  32. float nx = h(tx + ny, tz) - y0;
  33. float nz = h(tx, tz + ny) - y0;
  34. float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
  35. float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
  36. *p++ = ramp[(int)(d * 5.0f)];
  37. }
  38. else
  39. *p++ = ' ';
  40. }
  41. }
  42. for (sy = 0; sy < 25; sy++) {
  43. COORD coord = { 0, sy };
  44. SetConsoleCursorPosition(o, coord);
  45. WriteConsole(o, buffer[sy], 79, NULL, 0);
  46. }
  47. Sleep(33);
  48. }
  49. }

三.简单的love

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i, j, k, n = 0, x = 0, y = 50;
  5. //爱心的头部没有规律,所以直接打印
  6. printf("\n\n\n\n\n");
  7. printf(" lovelove lovelove\n");
  8. printf(" lovelovelove lovelovelove\n");
  9. printf(" lovelovelovelove lovelovelovelove\n");
  10. printf(" lovelovelovelovelove lovelovelovelovelove\n");
  11. printf(" lovelovelovelovelovelo lovelovelovelovelovelo\n");
  12. printf(" lovelovelovelovelovelove lovelovelovelovelovelov\n");
  13. for (i = 0; i < 2; i++)
  14. {
  15. printf("lovelovelovelovelovelovelovelovelovelovelovelovelove\n");
  16. }
  17. for(i=0;i<5;i++) //爱心的中间部分的上部分
  18. {
  19. y = 50;
  20. y = y - i*2;
  21. n++;
  22. for (k = 0; k < n; k++) //在每一行的起始位置先打印空格
  23. {
  24. printf(" ");
  25. }
  26. while(1) //空格后面打印love,但是要注意love即使没打印完,也要换行
  27. {
  28. if (x < y)
  29. {
  30. printf("l");
  31. y--;
  32. }
  33. else
  34. break;
  35. if (x < y)
  36. {
  37. printf("o");
  38. y--;
  39. }
  40. else
  41. break; if (x < y)
  42. {
  43. printf("v");
  44. y--;
  45. }
  46. else
  47. break; if (x < y)
  48. {
  49. printf("e");
  50. y--;
  51. }
  52. else
  53. break;
  54. }
  55. printf("\n");
  56. }
  57. //最下面的部分,具体内容同上,没和上一部分放一起是因为从这行开始多两个空格
  58. for (i = 0,n=3; i < 10; i++)
  59. {
  60. y = 37;
  61. y = y - i * 4;
  62. n++;
  63. for (k = 0; k < n; k++)
  64. {
  65. printf(" ");
  66. }
  67. while (1)
  68. {
  69. if (x < y)
  70. {
  71. printf("l");
  72. y--;
  73. }
  74. else
  75. break;
  76. if (x < y)
  77. {
  78. printf("o");
  79. y--;
  80. }
  81. else
  82. break; if (x < y)
  83. {
  84. printf("v");
  85. y--;
  86. }
  87. else
  88. break; if (x < y)
  89. {
  90. printf("e");
  91. y--;
  92. }
  93. else
  94. break;
  95. }
  96. printf("\n");
  97. }
  98. printf("\n\n\n\n\n\n\n\n\n\n\n\n");
  99. return 0;
  100. }

四.简单又明了

  1. #include<stdio.h>
  2. #define V 4//设置心形放大倍数
  3. #define list (4*V+3)//列数
  4. #define row (3*V+3)//行数
  5. void FindMainPoint(char line[row][list]);
  6. void Connect(int r1, int r2, int l1, int l2, char line[row][list]);//r行l列
  7. void Print(char line[row][list]);
  8. void Finger(char point[17][20]);
  9. int main(void)
  10. {
  11. char line[row][list];
  12. char point[17][20];
  13. FindMainPoint(line);
  14. Print(line);
  15. printf("\n\n");
  16. Finger(point);
  17. return 0;
  18. }
  19. //找到关键点
  20. void FindMainPoint(char line[row][list])
  21. {
  22. int list_mid = (list + 1) / 2 - 1;
  23. int list_left = (list + 1) / 4 - 1;
  24. int list_left_right = list_left + 1;
  25. int list_right = list_mid + list_left + 1;
  26. int list_right_left = list_right - 1;
  27. int row_above = 1 - 1;
  28. int row_mid = row / 3 - 1;
  29. int row_mid_below = row_mid + 1;
  30. int row_below = row - 1;
  31. line[row_above][list_left] = '*';//(4,1)
  32. line[row_above][list_left_right] = '*';//(5,1)
  33. line[row_above][list_right] = '*';//(12,1)
  34. line[row_above][list_right_left] = '*';//(11,1)
  35. line[row_mid][0] = '*';//(1,4)
  36. line[row_mid][list_mid] = '*';//(8,4)
  37. line[row_mid][list - 1] = '*';//(15,4)
  38. line[row_mid_below][0] = '*';//(1,5)
  39. line[row_mid_below][list - 1] = '*';//(15,5)
  40. line[row_below][list_mid] = '*';//(8,12)
  41. Connect(row_mid, row_above, 0, list_left, line);//(1,4)&&(4,1)
  42. Connect(row_above, row_mid, list_left_right, list_mid, line);//(5,1)&&(8,4)
  43. Connect(row_mid, row_above, list_mid, list_right_left, line);//(8,4)&&(11,1)
  44. Connect(row_above, row_mid, list_right, list - 1, line);//(12,1)&&(15,4)
  45. Connect(row_mid_below, row_below, 0, list_mid, line);//(1,5)&&(8,12)
  46. Connect(row_below, row_mid_below, list_mid, list - 1, line);//(8,12)&&(15,5)
  47. return;
  48. }
  49. //连接关键点
  50. void Connect(int r1, int r2, int l1, int l2, char line[row][list])
  51. {
  52. int i, j;
  53. if (r1 > r2)
  54. {
  55. for (i = r1, j = l1;i >= r2;i--, j++)
  56. {
  57. line[i][j] = '*';
  58. }
  59. }
  60. else
  61. {
  62. for (i = r1, j = l1;i <= r2;i++, j++)
  63. {
  64. line[i][j] = '*';
  65. }
  66. }
  67. return;
  68. }
  69. //打印爱心
  70. void Print(char line[row][list])
  71. {
  72. int i, j;
  73. for (i = 0;i < row;i++)
  74. {
  75. printf("\t\t\t\t\t\t");//将图形尽量居中,改变大小后,自行修改
  76. for (j = 0;j < list;j++)
  77. {
  78. if (line[i][j] != '*')
  79. {
  80. printf(" ");
  81. }
  82. else
  83. printf("*");
  84. }
  85. printf("\n");
  86. }
  87. }
  88. //打印手指
  89. void Finger(char point[17][20])
  90. {
  91. int i, j;
  92. for (i = 0, j = 5;j <= 6;j++)
  93. point[i][j] = '*';//
  94. for (j = 4, i = 1;i <= 8;i++)
  95. point[i][j] = '*';//
  96. for (j = 7, i = 1;i <= 6;i++)
  97. point[i][j] = '*';//
  98. for (i = 4, j = 8;j <= 10;j++)
  99. point[i][j] = '*';//
  100. for (j = 11, i = 5;i <= 6;i++)
  101. point[i][j] = '*';//
  102. for (i = 5, j = 12;j <= 14;j++)
  103. point[i][j] = '*';//
  104. for (j = 15, i = 6;i <= 7;i++)
  105. point[i][j] = '*';//
  106. for (i = 6, j = 15;j <= 18;j++)
  107. point[i][j] = '*';
  108. for (j = 19, i = 7;i <= 11;i++)
  109. point[i][j] = '*';//
  110. for (j = 18, i = 12;i <= 13;i++)
  111. point[i][j] = '*';//
  112. for (j = 17, i = 14;i <= 15;i++)
  113. point[i][j] = '*';//
  114. for (i = 16, j = 6;j <= 16;j++)
  115. point[i][j] = '*';//
  116. for (i = 14, j = 7;j <= 15;j++)
  117. point[i][j] = '*';
  118. for (j = 7;j <= 15;j += 4)//
  119. for (i = 10;i <= 12;i++)
  120. point[i][j] = '*';
  121. for (j = 5, i = 14;i <= 15;i++)
  122. point[i][j] = '*';//
  123. for (j = 2, i = 10;i <= 11;i++)
  124. point[i][j] = '*';//
  125. for (j = 0, i = 7;i <= 8;i++)
  126. point[i][j] = '*';//
  127. for (i = 6, j = 1;j <= 2;j++)
  128. point[i][j] = '*';
  129. point[7][3] = '*';
  130. point[9][1] = '*';
  131. point[12][3] = '*';
  132. point[13][4] = '*';
  133. for (i = 0;i < 17;i++)
  134. {
  135. printf("\t\t\t\t\t\t ");//将图形尽量居中,改变大小后,自行修改
  136. for (j = 0;j < 20;j++)
  137. {
  138. if (point[i][j] != '*')
  139. {
  140. printf(" ");
  141. }
  142. else
  143. printf("*");
  144. }
  145. printf("\n");
  146. }
  147. }

五.好看又直接

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #define I 20
  6. #define R 350
  7. int main()
  8. {
  9. system("mode con cols=80 lines=80");
  10. system("color F4");
  11. int i, j, e;
  12. int a;
  13. long time;
  14. for (i = 1, a = I; i < I / 2; i++, a--)
  15. {
  16. for (j = (int)(I - sqrt(I * I - (a - i) * (a - i))); j > 0; j--)
  17. printf(" ");
  18. for (e = 1; e <= 2 * sqrt(I * I - (a - i) * (a - i)); e++)
  19. printf("\3");
  20. for (j = (int)
  21. (2 * (I - sqrt(I * I - (a - i) * (a - i)))); j > 0; j--)
  22. printf(" ");
  23. for (e = 1; e <= 2 * sqrt(I * I - (a - i) * (a - i)); e++)
  24. printf("\3");
  25. printf("\n");
  26. }
  27. for (i = 1; i < 49;i++)
  28. {
  29. if (i == 20)
  30. {
  31. printf(" 我喜欢你 ");
  32. i += 15;
  33. }
  34. printf("\3");
  35. }
  36. printf("\n");
  37. for (i = 1; i <= R / 2; i++)
  38. {
  39. if (i % 2 || i % 3)
  40. continue;
  41. for (j = (int)(R - sqrt(R * R - i * i)); j > 0; j--)
  42. printf(" ");
  43. for (e = 1; e <= 2 * (sqrt(R * R - i * i) - (R - 2 * I)); e++)
  44. printf("\3");
  45. printf("\n");
  46. }
  47. system("pause");
  48. }

码字不易,求关注~~

(文中代码与部分内容来源与网络,如有侵权请联系删除)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/194434
推荐阅读
相关标签
  

闽ICP备14008679号