教你用c++写爱心。

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

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

一、普普通通的爱心;

#include<bits/stdc++.h>
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
//倒数 3 2 1 时间到:
    for (int i=3;i>0;i--){
        cout<<i<<endl;
        sleep(1);
    }
    cout<<"时间到!"<<endl;
    sleep(1);
    cout<<"准备好开始了吗?";
    sleep(1);
    system("cls"); 
//爱心:
    int i,j,k,l,m;
    char c='/*'; 
    cout<<endl;
    for (i=1;i<=3;i++){ 
    for (j=1;j<=32-2*i;j++)
      cout<<" ";
    for (k=1;k<=4*i+1;k++)
      cout<<c;
    for (l=1;l<=13-4*i;l++)
      cout<<" ";
    for (m=1;m<=4*i+1;m++)
      cout<<c;
    cout<<endl;
}
    for (i=1;i<=3;i++){ 
         for (j=1;j<=24+1;j++)
              cout<<" ";
        for (k=1;k<=29;k++)
              cout<<c;
        cout<<endl; 
    }
    for (i=7;i>=1;i--){ 
        for (j=1;j<=40-2*i;j++)
              cout<<" ";
        for (k=1;k<=4*i-1;k++)
              cout<<c;
        cout<<endl; 
  }
    for (i=1;i<=39;i++)
        cout<<" "; 
    cout<<c<<endl;  
    return 0;
}

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


二、红色的大爱心;

#include<bits/stdc++.h>
using namespace std; 
int main() { 
    float x,y,a;
    for (y=1.5;y>-1.5;y-=0.1){
        for (x=-1.5;x<1.5;x+=0.05){
            a=x*x+y*y-1;
            putchar(a * a * a - x * x * y * y * y <= 0.0 ? '*' : ' ');
    }
        system("color 0c");
        putchar('\n');
        }
    return 0;
}

成品图:

三、会动的爱心;

#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <tchar.h>

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(".:-=+*#%@");

    for (float t = 0.0f;; t += 0.1f)
    {
        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);
        }
        Sleep(33);
    }
}
四、会动的红色爱心
#include<iostream>
#include<cmath>
#include<Windows.h>
using namespace std;
void big_heart(){
    double x_1,y_1,m_1,n_1;
    for(y_1=1.5;y_1>-1.5;y_1-=0.1){
        for(x_1=-1.5;x_1<=1.5;x_1+=0.05){
            m_1=x_1*x_1+y_1*y_1-1;
            n_1=pow(m_1,3)-pow(x_1,2)*pow(y_1,3);
            n_1<0? cout<<'x': cout<<' ';
        }
        Sleep(100);
        cout<<endl;
    }
}
void big_heart_2(){ 
    double x_1,y_1,m_1,n_1;
    for(y_1=1.5;y_1>-1.5;y_1-=0.1){ 
        for(x_1=-1.5;x_1<=1.5;x_1+=0.05){
            m_1=x_1*x_1+y_1*y_1-1;
            n_1=pow(m_1,3)-pow(x_1,2)*pow(y_1,3);
            n_1<0? cout<<'x': cout<<' ';
        }
        cout<<endl;
    }
}
void small_heart(){ 
    double x,y,m,n;
    for(y=3.0;y>-3.0;y-=0.2){
        for(x=-3.0;x<=3.0;x+=0.1){
            m=x*x+y*y-1;
            n=pow(m,3)-pow(x,2)*pow(y,3);
            n<0? cout<<'x': cout<<' ';
        }
        cout<<endl;
    }
}
HANDLE han=GetStdHandle(-11);
void hide(){
    CONSOLE_CURSOR_INFO cursor;
    cursor.bVisible = 0;
    cursor.dwSize = 1;
    SetConsoleCursorInfo(han,&cursor);
}
int main(){ 
    hide();
    system("color C"); 
    big_heart();
    int a=1;
    while(1){
        system("cls");
        if(a%2==0){
            small_heart();
            Sleep(400); 
        }
        else{
            big_heart_2();
            Sleep(100); 
        }
        a++;
    }
    return 0;
}

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

  • 35
    点赞
  • 138
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值