C++ 打印心形,里面有文字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20075775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print heart shape with words inside
提问by Computernerd
I am trying to draw a heart shape with words inside as a surprise for a friend tomorrow but i cant figure out how to put the words inside the heart . I am only able to draw the heart shape
我想画一个心形,里面有文字,明天给朋友一个惊喜,但我不知道如何把文字放在心里面。我只能画心形
Code to draw Heart
绘制心形的代码
#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");
}
I need help putting words inside the heart shape
我需要帮助把词放在心形里面
回答by Tom Fenech
Pretty much the same as the other answer but I had already started so I thought I may as well finish. As a bonus you can specify what line of the "V" shape it prints on.
与其他答案几乎相同,但我已经开始了,所以我想我也可以完成。作为奖励,您可以指定它打印在“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;
}
}
回答by Taylor Brandstetter
You could just wait until you get to a pre-specified position in the heart and print out a message instead of the "V"s, like this:
您可以等到到达心脏中预先指定的位置并打印出一条消息而不是“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;
}
The y += strlen(message)-1;
is to advance the column index according to the number of characters printed. (2*size - strlen(message)/2)
is a position which will center the string.
的y += strlen(message)-1;
是根据打印的字符的数目来推进列索引。(2*size - strlen(message)/2)
是将字符串居中的位置。
If you want to obfuscate the code as much as possible (so you don't know what the message is until the code runs), you could use a hash table to map positions to letters or something like that.
如果您想尽可能地混淆代码(因此在代码运行之前您不知道消息是什么),您可以使用哈希表将位置映射到字母或类似的东西。
回答by Xar Hang
Try this code:
试试这个代码:
#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;
}
Demo here: https://code.sololearn.com/cuXe0axsK8R2/#cpp