打印心形,内有文字
Print heart shape with words inside
我想画一个里面有单词的心形,作为明天给朋友的惊喜,但我不知道如何把单词放在心里。我只能画心形
绘制心脏的代码
#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;
}
}
您可以等到到达心脏中预先指定的位置,然后打印出一条消息,而不是"V"s,如下所示:
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)
是将字符串居中的位置。
如果你想尽可能地混淆代码(所以在代码运行之前你不知道消息是什么),你可以使用哈希表将位置映射到字母或类似的东西。
试试这个代码:
#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<<"nnnPlease 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
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- constexpr 函数中的非文字(通过 std::is_constant_evaluated)
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 将数字打印成文字
- 垂直打印文字
- 有没有办法将字符串打印为文字?
- 为什么通过添加字符串文字和char初始化时,为什么不打印字符串
- fprintf:打印文字十六进制字符串
- 打印心形,内有文字
- 如何使用qDebug打印字符串文字和QString