c++ - 打印心形,里面有文字

标签 c++ draw cout ascii-art

我想画一个心形,里面有文字,作为明天给 friend 的一个惊喜,但我不知道如何把文字放在心里。我只能画出心形

绘制心形的代码

#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;
    }
}

关于c++ - 打印心形,里面有文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20075775/

相关文章:

c++ - 与模板类 : Compile error 成为 friend

python - 为什么绘制一个具有很厚边框的 PyGame 矩形会绘制一个加号形状?

python-3.x - 如何在Python中慢慢画一条线

c++ - 为什么这个volatile变量的地址一直是1?

c++ - 为什么我可以在 C++ 中的函数中定义结构和类?

Python C 包装器内存泄漏

c++ - 覆盖子类中的公共(public)静态常量成员

android - 单击 RecyclerView 更改项目布局

c++ - boost 库的哪一部分使 cout 能够打印 wstring 以及如何打印?

c++ - C++ 中的 "Ch++"或 "ch+1"?