打印心形,内有文字

Print heart shape with words inside

本文关键字:文字 打印      更新时间:2023-10-16

我想画一个里面有单词的心形,作为明天给朋友的惊喜,但我不知道如何把单词放在心里。我只能画心形

绘制心脏的代码

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double x,y;
    double size=10;

    for (x=0;x<size;x++)
    {
        for (y=0;y<=4*size;y++)
        {
            double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
            double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );
            if (dist1 < size + 0.5 || dist2 < size + 0.5 )
            cout<<"V";
            else
            cout<<" ";

        }
        cout<<endl;
    }
    for ( x=1;x<2*size;x++)
    {
        for(y=0;y<x;y++)
        cout<<" ";
        for (y=0; y<4*size + 1 - 2*x; y++)
        cout<<"V";
        cout<<endl;
    }
    system("PAUSE");
}

我需要帮助将单词放入心形

与其他答案大致相同,但我已经开始了,所以我想我还不如结束。作为奖励,你可以指定它打印在"V"形的哪一行

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double x, y, size=10;
    string message(" hello there ");
    int print_line = 4;
    if (message.length() % 2 != 0) message += " ";
    for (x=0;x<size;x++) 
    {
        for (y=0;y<=4*size;y++)   
        {
            double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
            double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );
            if (dist1 < size + 0.5 || dist2 < size + 0.5 ) {
                cout << "V";
            }
            else cout << " ";
        }
        cout<<"n";
    }
    for (x=1;x<2*size;x++)
    {
        for(y=0;y<x;y++) cout << " ";
        for (y=0; y<4*size + 1 - 2*x; y++) 
        {            
            if (x >= print_line - 1 && x <= print_line + 1) {
                int idx = y - (4*size - 2*x - message.length()) / 2;
                if (idx < message.length() && idx >= 0) {
                    if (x == print_line) cout<<message[idx];
                    else cout << " ";
                }
                else cout << "V";
            }
            else cout << "V";
        }
        cout<<endl;
    }
}

您可以等到到达心脏中预先指定的位置,然后打印出一条消息,而不是"V"s,如下所示:

    char message[] = " MY MESSAGE ";
    for ( x=1;x<2*size;x++)
    {
        for(y=0;y<x;y++)
        cout<<" ";
        for (y=0; y<4*size + 1 - 2*x; y++)
        {
            if (x == 1 && y == (2*size - strlen(message)/2))
            {
                cout << message;
                y += strlen(message)-1;
            }
            else
                cout<<"V";
        }
        cout<<endl;
    }

y += strlen(message)-1;是根据打印的字符数来推进列索引。(2*size - strlen(message)/2)是将字符串居中的位置。

如果你想尽可能地混淆代码(所以在代码运行之前你不知道消息是什么),你可以使用哈希表将位置映射到字母或类似的东西。

试试这个代码:

#include <iostream>
#include <cmath>    
using namespace std;
int main() {
    cout<<"Print Heart....C++n";
        int n=7; //size of heart
        for(int i=-3*n/2;i<=n;i++){
            for(int j=-3*n/2;j<=3*n/2;j++){
    /* inside either diamond or two circles */
            if((abs(i)+abs(j)<n)||((-n/2-i)*(-n/2-i)+(n/2-j)*(n/2-j)<=n*n/2)||((-n/2-i)*(-n/2-i)+(-n/2-j)*(-n/2-j)<=n*n/2)){
                   cout<<"v ";
               }
               else{
                   cout<<"  ";
               }
            }
               cout<<"n";
        }
        cout<<"nnnPlease don't forget to like.... :) :) :) n";
        cout<<"Credit: Heart Of Java : https://code.sololearn.com/caiu85u6tr30/#java";
    return 0;
}

此处演示:https://code.sololearn.com/cuXe0axsK8R2/#cpp