2

明日、友人へのサプライズとしてハートの形に言葉を入れようとしていますが、ハートの中に言葉を入れる方法がわかりません。ハートの形しか描けない

ハートを描くコード

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

ハートの形の中に言葉を入れるのに助けが必要

4

3 に答える 3

1

次のように、心臓の事前に指定された位置に到達するまで待って、「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)弦の中心になる位置です。

コードを可能な限り難読化したい場合 (コードが実行されるまでメッセージが何であるかがわからないため)、ハッシュ テーブルを使用して位置を文字などにマップできます。

于 2013-11-19T15:55:56.317 に答える
0

このコードを試してください:

#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

于 2018-01-30T02:07:43.933 に答える