【问题标题】:Print heart shape with words inside打印带字的心形
【发布时间】:2013-11-19 15:40:20
【问题描述】:

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

画心的代码

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

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

【问题讨论】:

  • 提示:将使用字母代替您输出的某些空格。
  • 为什么不将值静态存储在 char 数组中,或者您希望心脏的消息/大小是动态的?
  • 使用文本编辑器而不是程序可能会容易得多。

标签: c++ draw cout ascii-art


【解决方案1】:

与其他答案几乎相同,但我已经开始了,所以我想我不妨完成。作为奖励,您可以指定打印在“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;
    }
}

【讨论】:

    【解决方案2】:

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

        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) 是使字符串居中的位置。

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

    【讨论】:

      【解决方案3】:

      试试这个代码:

      #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<<"\n\n\nPlease 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

      【讨论】:

      • 虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
      • 欢迎来到 Stack Overflow,感谢您的回答。请查看以改进您的答案:stackoverflow.com/help/how-to-answer
      猜你喜欢
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 2022-07-07
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多