C++打印一个BOX应用程序 我现在正在研究一个C++项目,它根据用户输入的宽度和高度输入,将一个框打印到屏幕上。到目前为止,我可以在屏幕上打印出顶部、底部和左侧的点。我唯一需要帮助的就是打印出最右边的点。我在代码下面提供了输出的屏幕截图
代码: 截图:C++打印一个BOX应用程序 我现在正在研究一个C++项目,它根据用户输入的宽度和高度输入,将一个框打印到屏幕上。到目前为止,我可以在屏幕上打印出顶部、底部和左侧的点。我唯一需要帮助的就是打印出最右边的点。我在代码下面提供了输出的屏幕截图,c++,C++,代码: 截图: 正如你所看到的,最右边的圆点没有打印出来,盒子也不完整,我需要修复这个,任何有帮助的,请 根据输入的宽度,插入左点时,先插入适当数量的空格,然后插入右点。将换行符放在右点之后。根据输入的宽度,插入左点时,先插入适当数量的空格,然后插入右点。将换行符放在正确的点后。我从中得到了一些乐趣: 看 我玩得很开心: 看 可以在垂直线之后输出空格。以下是完成此任务的代码的修改版本: //Prints the top dots (horizontal): for (int dots = 0;
正如你所看到的,最右边的圆点没有打印出来,盒子也不完整,我需要修复这个,任何有帮助的,请 根据输入的宽度,插入左点时,先插入适当数量的空格,然后插入右点。将换行符放在右点之后。根据输入的宽度,插入左点时,先插入适当数量的空格,然后插入右点。将换行符放在正确的点后。我从中得到了一些乐趣: 看
我玩得很开心: 看
可以在垂直线之后输出空格。以下是完成此任务的代码的修改版本:
//Prints the top dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout<<"*";
}
cout<<endl;
//Prints the left dots (vertical):
for (int dots = 0; dots < height-2; dots++)
{
cout<<"*";
for(int i = 0; i < width-2; ++i) cout<<" ";
cout<<"*"<<endl;
}
//Prints the bottom dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout<<"*";
}
可以在垂直线之后输出空格。以下是完成此任务的代码的修改版本:
//Prints the top dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout<<"*";
}
cout<<endl;
//Prints the left dots (vertical):
for (int dots = 0; dots < height-2; dots++)
{
cout<<"*";
for(int i = 0; i < width-2; ++i) cout<<" ";
cout<<"*"<<endl;
}
//Prints the bottom dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout<<"*";
}
也许是一个:
但对大多数C程序员来说可能有点困惑
也许是一个:
但对大多数C程序员来说可能有点困惑
此解决方案可能晚了5年,但确实纠正了原始程序的问题 它检查用户输入是否在标准端子80x24输出的可接受值范围内,以及尺寸是否为正值。将大值裁剪为最大80x24个字符,并使用输入的负数的绝对值。 注意:此程序不检查是否输入了非整数值 除了用户输入验证之外,此解决方案会更正原始代码中的错误,并使用c++的std::endl而不是c的\n作为新行 如果您只对打印框的代码感兴趣,请在注释后查找代码: //三,。打印框
此解决方案可能晚了5年,但确实纠正了原始程序的问题 它检查用户输入是否在标准端子80x24输出的可接受值范围内,以及尺寸是否为正值。将大值裁剪为最大80x24个字符,并使用输入的负数的绝对值。 注意:此程序不检查是否输入了非整数值 除了用户输入验证之外,此解决方案会更正原始代码中的错误,并使用c++的std::endl而不是c的\n作为新行 如果您只对打印框的代码感兴趣,请在注释后查找代码: //三,。打印框
你不应该指望整数点;你不应该指望整数点的点;点我会用一个lambda表示“line”,它捕捉“width”,并获取一个长度为3的字符串,将其拆分为3个字符。这样,方框图代码块将只是:line+-+等…@PamalMangat:这就是所谓的。当w减量大于0时,对whilew->0进行了一次有趣的重写。@PamalMangat由于您仍在学习编码,我不得不提醒您,这段代码虽然功能强大,但主要是一个笑话。了解它是如何工作的以及为什么工作是很好的,但是不要试图模仿它,除非你的意图是编写非常糟糕的代码。在这种情况下,请仿效并建立它。老实说,伙计们,我仍然在学习代码,对Python来说是新的C++。对于一个新的程序员来说,学习新语言有什么建议吗@KennetBelenkyI会用lambda表示“line”,它捕捉“width”,并获取长度为3的字符串,然后将其拆分为3个字符。这样,方框图代码块将只是:line+-+等…@PamalMangat:这就是所谓的。当w减量大于0时,对whilew->0进行了一次有趣的重写。@PamalMangat由于您仍在学习编码,我不得不提醒您,这段代码虽然功能强大,但主要是一个笑话。了解它是如何工作的以及为什么工作是很好的,但是不要试图模仿它,除非你的意图是编写非常糟糕的代码。在这种情况下,请仿效并建立它。老实说,伙计们,我仍然在学习代码,对Python来说是新的C++。对于一个新的程序员来说,学习新语言有什么建议吗@肯尼特贝伦基
//Prints the top dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout<<"*";
}
cout<<endl;
//Prints the left dots (vertical):
for (int dots = 0; dots < height-2; dots++)
{
cout<<"*";
for(int i = 0; i < width-2; ++i) cout<<" ";
cout<<"*"<<endl;
}
//Prints the bottom dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout<<"*";
}
#include <stdio.h>
void line(char s, int w, char b, char e)
{
{ putchar(s); }
while(w --> 0) { putchar(b); }
{ putchar(e); }
{ putchar('\n'); }
}
int main(void)
{
int width = 10;
int height = 6;
width -= 2;
height-= 2;
{ line('+', width, '-', '+'); }
while(height --> 0) { line('|', width, ' ', '|'); }
{ line('+', width, '-', '+'); }
return 0;
}
+--------+
| |
| |
| |
| |
+--------+
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
int width = 0;
int height = 0;
// 1. Get User Input
//Introduction:
cout << "Welcome to the [Draw A Rectangle] program!" << endl;
cout << "This program will draw a rectangle in the application" << endl;
cout << "You will have to enter the width and the height"
<< " and it will draw it" << endl;
//User enters box width and height:
cout << "Please enter the width: ";
cin >> width;
cout << "Please enter the height: ";
cin >> height;
// 2. Verify Validity of User Input
// Height Max 24 width max 80 not zero or negative values
// 2a. check that width is not zero or negative.
if (width < 1) // zero or negative
{
if (width < 0)
{
cout << "A rectange must have a positive width [" << width
<< "] set to " << abs(width) << "." << endl;
width = abs(width);
}
else // width == zero
{
cout << "A rectangle must had a width of 1 or more."
<< " Width [" << width << "] set to 1." << endl;
width = 1;
}
}
// 2b. check that height is not zero or negative.
if (height < 1)
{
if (height < 0)
{
cout << "A rectange must have a positive height [" << height
<< "] set to " << abs(height) << "." <<endl;
height = abs(height);
}
else // height == zero
{
cout << "A rectangle must had a height of 1 or more."
<< " Height [" << height << "] set to 1." << endl;
height = 1;
}
}
// 2c. Limit to 80x24 chars.
// The standard vt100 terminal was only 80x24 chars
// 2c i) check width 80 or less
if (width > 80)
{
cout << "Width must be 80 or less. Width [" << width
<< "] limited to 80." << endl;
width = 80;
}
// 2c ii) check height 24 or less
if (height > 24 )
{
cout << "Height must be 24 or less. Height [" << height
<< "] limited to 24." << endl;
height = 24;
}
// 3. Print the box
//Prints the top dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout << "*";
}
cout << endl;
//Prints the left dots (vertical):
// note first and last row are rows of dots
if (height > 1 )
{
for (int dots = 0; dots < height-2; dots++) // first row already printed
{
cout << setw(1) <<"*";
if (width > 1 )
{
cout << setw(width-1) << right << "*" << endl;
}
else
cout << endl;
}
//Prints the bottom dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout<<"*";
}
cout << endl;
}
//Keeps program running:
cin.get();
}