题目:
编写一个函数,按如下格式打印你的名字。
*************************
* *
* 你的名字 *
* *
**************************
代码:
#include <iostream>
#include <string>
using namespace std;
void print(string s);
int main()
{
string s;
cout << "请输入你的名字:";
getline(cin, s);
cout << endl << "打印名字如下:" << endl;
print(s);
system("pause");
return 0;
}
void print(string s)
{
int i, len = s.length();
for (i = 1; i <= 25; ++i)
{
cout << "*";
}
cout << endl << endl << "*";
for (i = 1; i <= 24; ++i)
{
cout << " ";
}
cout << "*" << endl << endl << "*";
for (i = 1; i <= (24 - len) / 2; ++i)
{
cout << " ";
}
cout << s;
for (i = 1; i <= 24 - (24 - len) / 2 - len; ++i)
{
cout << " ";
}
cout << "*" << endl << endl << "*";
for (i = 1; i <= 24; ++i)
{
cout << " ";
}
cout << "*" << endl << endl;
for (i = 1; i <= 25; ++i)
{
cout << "*";
}
cout << endl << endl;
}