我的任务是在一个框架内打印一个字符串,例如一个正方形
my task is to print a string inside a frame for example a square
#include<iostream>
using namespace std;
int main()
{
int i=1,len;
char ch[26][26],ch2;
cout<<"enter string: "<<endl;
for(i=0;;i++)
{
cin>>ch[i];
len++;
if(getchar()=='\n')
break;
}
int n,j;
cout<<"enter size: "<<endl;
cin>>n;
int k;
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(i==0||i==n||j==0||j==n)
{
cout<<"*";
}
else
cout<<" ";
if(i==((n/2)-2)&&j==((n/2)-2))
{
for(k=0;k<len;k++)
{
cout<<ch[k]<<endl;
cout<<"*";
}
}
}
cout<<"\n";
}
}
此程序在正方形内显示字符串,但正方形的星形图案变得混乱,尤其是最右边的列
我们将不胜感激任何帮助
由于您没有在代码中提供太多细节,我从头开始使用新代码,这就是我想出的:
#include <iostream>
#include <vector>
为您的字符串使用向量,并动态调整大小(如果在您的代码中输入超过 26 个单词怎么办?提示:分段错误!)
using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::endl;
最好避免使用 using namespace std;
。只需导入您真正需要的内容即可。
int main() {
vector<string> strings;
你肯定想在这里使用字符串,而不是 char
数组。
cout << "Enter string: ";
输入提示后不要换行! (作为 Linux 用户,我个人讨厌它)
for(;;) {
你不需要这里的变量 i
,只是 运行 一个无限循环(尝试后 运行ge 那个,如果你能避免无限循环,再一次 while(getchar() != '\n')
更不言自明。
string s;
cin >> s;
strings.push_back(s);
正如评论中 pstrjds 所建议的那样,如果可以,请使用 getline()
。
if(getchar() == '\n')
break;
就像我说的,尝试用 while
条件重新表述。
}
unsigned int n, i, j;
cout << "Enter size: ";
cin >> n;
// assuming strings.size() < n
unsigned int empty_lines_around_text((n - strings.size()) / 2);
因为你想在你的正方形内居中打印你的文字,你必须显示少于半个正方形的 * (...) *
行:实际上是半个正方形 减去 一半要打印的字符串数。
// first horizontal row of stars
for(j = 0; j < n; ++j)
cout << '*';
cout << endl;
正方形的上边。
for(i = 1; i < empty_lines_around_text; ++i) {
cout << '*';
for(j = 1; j < n - 1; ++j) {
cout << ' ';
}
cout << '*' << endl;
}
要打印的第一行,其中没有字符串的行。
//here we do the actual printing of the strings
for(i = 0; i < strings.size(); ++i) {
string s = strings[i];
// once again, assuming the size of each string is < n
unsigned int empty_chars_around_string((n - s.size()) / 2);
cout << '*';
for(j = 0; j < empty_chars_around_string; ++j)
cout << ' ';
cout << s;
for(j = empty_chars_around_string + s.size() + 1; j < n - 1; ++j)
cout << ' ';
cout << '*' << endl;
}
这是有问题的部分。与空行一样,我们需要一个变量来包含我们必须在字符串之前打印多少空格,以便它显示在中间(变量 empty_chars_around_string)。
我们打印了那么多的空格,字符串,我们在行尾 *
之前用空格完成了这一行,数组中的每个字符串都是如此。
for(i = empty_lines_around_text + strings.size() + 1; i < n; ++i) {
cout << '*';
for(j = 1; j < n - 1; ++j) {
cout << ' ';
}
cout << '*' << endl;
}
在打印字符串后,我们用空行完成正方形。
// last horizontal line of '*' (we close the square)
for(j = 0; j < n; ++j)
cout << '*';
cout << endl;
...我们关闭广场。
return 0;
}
现在,这段代码并不完美,需要进行大量重构和优化,但它最大限度地利用了 C++ 功能。
Here 是一个包含完整代码的 PasteBin。
当 运行 字符串 Hello friends
和大小 12
:
时的输出
************
* *
* *
* *
* *
* hello *
* friends *
* *
* *
* *
* *
************
主要问题出在:
for(k=0;k<len;k++)
{
cout<<ch[k]<<endl;
cout<<"*";
}
这里当你来放置输入字符串时,你也输入一个新行并以星号(*)开头。你不仅没有把最后一个星号放在输入字符串的行上,而且你也没有在那里更新 j ,它仍然大于 0 并且当代码继续 for(j=0;j<=n;j++)
j 已经有了换行符+星号的剩余值。
尝试:
for( k = 0; k<len; k++ )
{
cout << ch[k];
j += strlen( ch[k] );
}
这样 j 将更新到输入字符串的最后一个位置。
PS: 对于常见的编码实践,将开头的 len 也初始化为 0。
#include<iostream>
using namespace std;
int main()
{
int i=1,len;
char ch[26][26],ch2;
cout<<"enter string: "<<endl;
for(i=0;;i++)
{
cin>>ch[i];
len++;
if(getchar()=='\n')
break;
}
int n,j;
cout<<"enter size: "<<endl;
cin>>n;
int k;
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(i==0||i==n||j==0||j==n)
{
cout<<"*";
}
else
cout<<" ";
if(i==((n/2)-2)&&j==((n/2)-2))
{
for(k=0;k<len;k++)
{
cout<<ch[k]<<endl;
cout<<"*";
}
}
}
cout<<"\n";
}
}
此程序在正方形内显示字符串,但正方形的星形图案变得混乱,尤其是最右边的列 我们将不胜感激任何帮助
由于您没有在代码中提供太多细节,我从头开始使用新代码,这就是我想出的:
#include <iostream>
#include <vector>
为您的字符串使用向量,并动态调整大小(如果在您的代码中输入超过 26 个单词怎么办?提示:分段错误!)
using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::endl;
最好避免使用 using namespace std;
。只需导入您真正需要的内容即可。
int main() {
vector<string> strings;
你肯定想在这里使用字符串,而不是 char
数组。
cout << "Enter string: ";
输入提示后不要换行! (作为 Linux 用户,我个人讨厌它)
for(;;) {
你不需要这里的变量 i
,只是 运行 一个无限循环(尝试后 运行ge 那个,如果你能避免无限循环,再一次 while(getchar() != '\n')
更不言自明。
string s;
cin >> s;
strings.push_back(s);
正如评论中 pstrjds 所建议的那样,如果可以,请使用 getline()
。
if(getchar() == '\n')
break;
就像我说的,尝试用 while
条件重新表述。
}
unsigned int n, i, j;
cout << "Enter size: ";
cin >> n;
// assuming strings.size() < n
unsigned int empty_lines_around_text((n - strings.size()) / 2);
因为你想在你的正方形内居中打印你的文字,你必须显示少于半个正方形的 * (...) *
行:实际上是半个正方形 减去 一半要打印的字符串数。
// first horizontal row of stars
for(j = 0; j < n; ++j)
cout << '*';
cout << endl;
正方形的上边。
for(i = 1; i < empty_lines_around_text; ++i) {
cout << '*';
for(j = 1; j < n - 1; ++j) {
cout << ' ';
}
cout << '*' << endl;
}
要打印的第一行,其中没有字符串的行。
//here we do the actual printing of the strings
for(i = 0; i < strings.size(); ++i) {
string s = strings[i];
// once again, assuming the size of each string is < n
unsigned int empty_chars_around_string((n - s.size()) / 2);
cout << '*';
for(j = 0; j < empty_chars_around_string; ++j)
cout << ' ';
cout << s;
for(j = empty_chars_around_string + s.size() + 1; j < n - 1; ++j)
cout << ' ';
cout << '*' << endl;
}
这是有问题的部分。与空行一样,我们需要一个变量来包含我们必须在字符串之前打印多少空格,以便它显示在中间(变量 empty_chars_around_string)。
我们打印了那么多的空格,字符串,我们在行尾 *
之前用空格完成了这一行,数组中的每个字符串都是如此。
for(i = empty_lines_around_text + strings.size() + 1; i < n; ++i) {
cout << '*';
for(j = 1; j < n - 1; ++j) {
cout << ' ';
}
cout << '*' << endl;
}
在打印字符串后,我们用空行完成正方形。
// last horizontal line of '*' (we close the square)
for(j = 0; j < n; ++j)
cout << '*';
cout << endl;
...我们关闭广场。
return 0;
}
现在,这段代码并不完美,需要进行大量重构和优化,但它最大限度地利用了 C++ 功能。
Here 是一个包含完整代码的 PasteBin。
当 运行 字符串 Hello friends
和大小 12
:
************
* *
* *
* *
* *
* hello *
* friends *
* *
* *
* *
* *
************
主要问题出在:
for(k=0;k<len;k++)
{
cout<<ch[k]<<endl;
cout<<"*";
}
这里当你来放置输入字符串时,你也输入一个新行并以星号(*)开头。你不仅没有把最后一个星号放在输入字符串的行上,而且你也没有在那里更新 j ,它仍然大于 0 并且当代码继续 for(j=0;j<=n;j++)
j 已经有了换行符+星号的剩余值。
尝试:
for( k = 0; k<len; k++ )
{
cout << ch[k];
j += strlen( ch[k] );
}
这样 j 将更新到输入字符串的最后一个位置。
PS: 对于常见的编码实践,将开头的 len 也初始化为 0。