当前位置:   article > 正文

教你用c++写爱心。_c++爱心代码

c++爱心代码

大家好,我是kekedeLa,这是我的第一个作品,废话不多说,进入正题。

说到用c++写爱心,是每个单身程序猿的必修课。

一、普普通通的爱心;

  1. #include<bits/stdc++.h>
  2. #include <unistd.h>
  3. #include <iostream>
  4. #include <stdlib.h>
  5. using namespace std;
  6. int main(){
  7. //倒数 3 2 1 时间到:
  8. for (int i=3;i>0;i--){
  9. cout<<i<<endl;
  10. sleep(1);
  11. }
  12. cout<<"时间到!"<<endl;
  13. sleep(1);
  14. cout<<"准备好开始了吗?";
  15. sleep(1);
  16. system("cls");
  17. //爱心:
  18. int i,j,k,l,m;
  19. char c='/*';
  20. cout<<endl;
  21. for (i=1;i<=3;i++){
  22. for (j=1;j<=32-2*i;j++)
  23. cout<<" ";
  24. for (k=1;k<=4*i+1;k++)
  25. cout<<c;
  26. for (l=1;l<=13-4*i;l++)
  27. cout<<" ";
  28. for (m=1;m<=4*i+1;m++)
  29. cout<<c;
  30. cout<<endl;
  31. }
  32. for (i=1;i<=3;i++){
  33. for (j=1;j<=24+1;j++)
  34. cout<<" ";
  35. for (k=1;k<=29;k++)
  36. cout<<c;
  37. cout<<endl;
  38. }
  39. for (i=7;i>=1;i--){
  40. for (j=1;j<=40-2*i;j++)
  41. cout<<" ";
  42. for (k=1;k<=4*i-1;k++)
  43. cout<<c;
  44. cout<<endl;
  45. }
  46. for (i=1;i<=39;i++)
  47. cout<<" ";
  48. cout<<c<<endl;
  49. return 0;
  50. }

以上只是简简单单的爱心,成品图:


二、红色的大爱心;

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

成品图:

三、会动的爱心;

  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. {
  7. float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
  8. return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
  9. }
  10. float h(float x, float z)
  11. {
  12. for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
  13. if (f(x, y, z) <= 0.0f)
  14. return y;
  15. return 0.0f;
  16. }
  17. int main()
  18. {
  19. HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);
  20. _TCHAR buffer[25][80] = { _T(' ') };
  21. _TCHAR ramp[] = _T(".:-=+*#%@");
  22. for (float t = 0.0f;; t += 0.1f)
  23. {
  24. int sy = 0;
  25. float s = sinf(t);
  26. float a = s * s * s * s * 0.2f;
  27. for (float z = 1.3f; z > -1.2f; z -= 0.1f)
  28. {
  29. _TCHAR* p = &buffer[sy++][0];
  30. float tz = z * (1.2f - a);
  31. for (float x = -1.5f; x < 1.5f; x += 0.05f)
  32. {
  33. float tx = x * (1.2f + a);
  34. float v = f(tx, 0.0f, tz);
  35. if (v <= 0.0f)
  36. {
  37. float y0 = h(tx, tz);
  38. float ny = 0.01f;
  39. float nx = h(tx + ny, tz) - y0;
  40. float nz = h(tx, tz + ny) - y0;
  41. float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
  42. float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
  43. *p++ = ramp[(int)(d * 5.0f)];
  44. }
  45. else
  46. *p++ = ' ';
  47. }
  48. }
  49. for (sy = 0; sy < 25; sy++)
  50. {
  51. COORD coord = { 0, sy };
  52. SetConsoleCursorPosition(o, coord);
  53. WriteConsole(o, buffer[sy], 79, NULL, 0);
  54. }
  55. Sleep(33);
  56. }
  57. }
四、会动的红色爱心
  1. #include<iostream>
  2. #include<cmath>
  3. #include<Windows.h>
  4. using namespace std;
  5. void big_heart(){
  6. double x_1,y_1,m_1,n_1;
  7. for(y_1=1.5;y_1>-1.5;y_1-=0.1){
  8. for(x_1=-1.5;x_1<=1.5;x_1+=0.05){
  9. m_1=x_1*x_1+y_1*y_1-1;
  10. n_1=pow(m_1,3)-pow(x_1,2)*pow(y_1,3);
  11. n_1<0? cout<<'x': cout<<' ';
  12. }
  13. Sleep(100);
  14. cout<<endl;
  15. }
  16. }
  17. void big_heart_2(){
  18. double x_1,y_1,m_1,n_1;
  19. for(y_1=1.5;y_1>-1.5;y_1-=0.1){
  20. for(x_1=-1.5;x_1<=1.5;x_1+=0.05){
  21. m_1=x_1*x_1+y_1*y_1-1;
  22. n_1=pow(m_1,3)-pow(x_1,2)*pow(y_1,3);
  23. n_1<0? cout<<'x': cout<<' ';
  24. }
  25. cout<<endl;
  26. }
  27. }
  28. void small_heart(){
  29. double x,y,m,n;
  30. for(y=3.0;y>-3.0;y-=0.2){
  31. for(x=-3.0;x<=3.0;x+=0.1){
  32. m=x*x+y*y-1;
  33. n=pow(m,3)-pow(x,2)*pow(y,3);
  34. n<0? cout<<'x': cout<<' ';
  35. }
  36. cout<<endl;
  37. }
  38. }
  39. HANDLE han=GetStdHandle(-11);
  40. void hide(){
  41. CONSOLE_CURSOR_INFO cursor;
  42. cursor.bVisible = 0;
  43. cursor.dwSize = 1;
  44. SetConsoleCursorInfo(han,&cursor);
  45. }
  46. int main(){
  47. hide();
  48. system("color C");
  49. big_heart();
  50. int a=1;
  51. while(1){
  52. system("cls");
  53. if(a%2==0){
  54. small_heart();
  55. Sleep(400);
  56. }
  57. else{
  58. big_heart_2();
  59. Sleep(100);
  60. }
  61. a++;
  62. }
  63. return 0;
  64. }

好了,今天的内容就到这里,还是感谢你认真看完我的作品

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

闽ICP备14008679号