c语言 如下 输出一个心形,然后想在心形内部加入汉字,如何修改? #includ

c语言 如下 输出一个心形,然后想在心形内部加入汉字,如何修改?

#include <stdio.h>

int main()
{
int i,j;

printf(" ****** ******\n"
" ********** **********\n"
" ************* *************\n");
//前三排的规律性不强 所以直接显示就好了

for(i=0;i<3;i++)//显示中间三排
{
for(j=0;j<29;j++)
printf("*");
printf("\n");
}

for(i=0;i<7;i++)//显示呈递减趋势规律的中间7排
{
for(j=0;j<2*(i+1)-1;j++)
printf(" ");
for(j=0;j<27-i*4;j++)
printf("*");
printf("\n");
}

for(i=0;i<14;i++)//最后一个星号*与上面的规律脱节了 所以独立显示
printf(" ");
printf("*\n");
return 0;
}

##C++
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double x, y,
    size=10;
    char ch='*';
    string message("fateland");
    int print_line = 4;
    if(message.length() % 2 != 0) message += " ";
    for(x = 0; x < size -4; x++)
    {
        for(y = 0; y < 4 * size; y++)
        {
            double dist1 = sqrt( pow(x - size,2) + pow(y - size,2) ) -0.5;
            double dist2 = sqrt( pow(x - size,2) + pow(y - 3 * size,2) ) -0.5;
            if (dist1 < size + 0.5 || dist2 < size + 0.5 ) {
                cout << ch;
            }
            else cout << " ";
        }
        cout << endl;
    }
    //cout << "ddddddd" << endl;
    for(x = 1; x < 2 * size; x++)
    {
        for(y = 0; y < x; y++)
        {
            cout << " ";
        }
        for(y = 0; y < 4 * size - 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 << ch;
                }
            }
            else cout << ch;
        }
        cout<<endl;
    }
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答