C+中的沙漏+;添加参数 在过去的几个小时里,我一直在处理C++中控制台上的沙漏,我已经创建了一个代码,它应该逻辑上工作,但实际上它并没有。p>
当我尝试运行代码时,它会询问沙漏的高度。但是,在我输入它之后,结果是一个无限的“0”列表,这肯定不是我所期望的 我还需要在这段代码中添加参数,但我真的不知道如何添加,所以如果您能帮助我,请帮我一个忙 这是我做的代码(工作不正常):C+中的沙漏+;添加参数 在过去的几个小时里,我一直在处理C++中控制台上的沙漏,我已经创建了一个代码,它应该逻辑上工作,但实际上它并没有。p>,c++,loops,iostream,C++,Loops,Iostream,当我尝试运行代码时,它会询问沙漏的高度。但是,在我输入它之后,结果是一个无限的“0”列表,这肯定不是我所期望的 我还需要在这段代码中添加参数,但我真的不知道如何添加,所以如果您能帮助我,请帮我一个忙 这是我做的代码(工作不正常): 您应该在此处得到警告: int height, base, distance, jeg, backup1, backup2, backup3; base = 2 * height - 1; distance = 1; jeg = base - 2; 用于在未初始化
您应该在此处得到警告:
int height, base, distance, jeg, backup1, backup2, backup3;
base = 2 * height - 1;
distance = 1;
jeg = base - 2;
用于在未初始化时使用高度。您需要首先获取用户输入,然后使用值,而不是相反的方式。在那之后,任何事情都可能发生。在代码中使用base
调用未定义的行为。同样backup1=backup1--
绝对不是做你认为它做的事情
要打印某个字符,例如“#”
,n
-您可以使用
std::cout << std::string(n,'#');
从这里开始,填充沙漏只需一小步:
#include <iostream>
#include <iomanip>
int main() {
int height;
std::cin >> height;
for (int i=0;i< height*2 + 1; ++i) {
auto empty = height - std::abs( height - i);
auto fill = 2*(height-empty)+1;
std::cout << std::string(empty,' ') << std::string(fill,'x') << "\n";
}
}
#包括
#包括
int main(){
内部高度;
标准:cin>>高度;
对于(int i=0;i<高度*2+1;++i){
自动清空=高度-std::abs(高度-i);
自动填充=2*(高度为空)+1;
std::cout首先,在height
初始化之前执行以下块,您需要在cin>>height
之后移动它:
base = 2 * height - 1;
distance = 1;
jeg = base - 2;
下一个重要问题是,您在未初始化的备份1上循环。更糟的是:即使您要初始化它,以下语句也是一个真正的问题:
backup1 = backup1--; // OUCH !!!
为什么?因为在同一语句中,同一变量的备份有两个副作用:第一个是减量,第二个是设置减量之前的值。您需要选择:
backup1--; // alternative 1
backup1 = backup1 -1; // alternative 2
backup1 -= 1; // alternative 3
您需要删除backup2
和backup3
、height
和distance
的所有类似构造(也使用distance++
)
最后,你有一个无止境的循环:
while(backup2>0) { // you loop on backup2
cout << "#";
backup3--; // but it doesn't change since you decrement the wrong variable
}
while(backup2>0){//您在backup2上循环
cout您的代码有很多错误。首先:
int ..., backup1, ...;
while(backup1 > 0) {
cout << "0";
backup1 = backup1--;
}
这将输出num_spaces
number of spaces,后跟num_sharps
number of sharps,然后是换行符。Viola
现在,您需要打印的下一行的锐度将比该行少两个,空间将多一个,因此请相应地更新这些值,然后再次运行输出行
有了这个,你应该能够非常容易和干净地产生你的沙漏(虽然你可能需要第二个循环的另一半!)
作为一种奖励,只是为了好玩,在几行中,只是为了表明,有了足够的数学,任何事情都是可能的:D总是很难进入别人代码的机制,尤其是在没有注释的情况下。
我写了一个全新的代码,希望它能有所帮助。下面是:
#include <iostream>
using namespace std;
int main()
{
int height, spaces = 0, nextLayer;
do
{
cout << "Insert the height of the hourglass: ";
cin >> height;
}while(height > 0 && height%2 == 0); //Loop into asking a valid height
nextLayer = height; //First layer has the same number of # as there are number of layers, aka height
//First loop for the top part of the hourglass
for(int c = 0; c < height/2 + 1; c++)
{
for(int k = 0; k < spaces; k++)
{
cout << " ";
}
for(int k = 0; k < nextLayer; k++)
{
cout << "#";
}
cout << endl; //end the line
nextLayer = nextLayer - 2; //Decrease the number of #
spaces++; //Augment the indentation
}
/*
** Since we are now going to make the bottom specular part
** let's "come back" by two loops
*/
nextLayer = nextLayer + 4;
spaces = spaces - 2;
//Second loop for the bottom part of the hourglass
for(int c = 0; c < height/2; c++)
{
for(int k = 0; k < spaces; k++)
{
cout << " ";
}
for(int k = 0; k < nextLayer; k++)
{
cout << "#";
}
cout << endl;
nextLayer = nextLayer + 2;
spaces--;
}
}
#包括
使用名称空间std;
int main()
{
整数高度,空格=0,下一层;
做
{
cout>高度;
}while(height>0&&height%2==0);//循环询问有效的高度
nextLayer=height;//第一层的#数与层数相同,即高度
//沙漏顶部的第一个循环
对于(int c=0;c 不知道为什么备份=备份--;
而不是普通的备份--
;@MichaelChourdakis这比你想象的还要糟糕,因为它会导致未定义的行为。欢迎使用堆栈溢出!听起来你可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,你可以逐行执行你的程序看看它偏离了你的期望。如果你要做任何编程,这是一个必不可少的工具。进一步阅读:和@一月,是的,一个合适的IDE和调试器。一个好的调试器可能比一个好的编译器更需要。backup1
没有初始化…好的开始!那么众多的xxx=xxx--;
?并且将xxx赋值为xxx递减之前的值?@Christophe in progress+1,感谢他们使用算法将OP的原始状态转换为工作状态的奉献精神。我会告诉他们放弃它,重新开始,这是值得称赞的。OP使用backup1=backup1-;
也很重要,它只是定义良好的since C++17。如果他们是为C++14编译的,那么它仍然是未定义的行为(因为你的第二句话在C++17之前不存在)。我也不确定所有编译器目前是否都完全实现了C++17顺序的求值规则更改。@胡桃木您完全正确。我承认我在写答案时犹豫了一下,几乎写了UB,但我更喜欢交叉检查。C++11和C++14都显示了示例I=I++1
,在第节中未定义1.9.但我们必须适应我们的时代,C++17现在用定义的行为显示了相同的示例,并将i=i+++i
显示为未定义,因为+运算符的两侧都是不确定的序列。
backup1--; // alternative 1
backup1 = backup1 -1; // alternative 2
backup1 -= 1; // alternative 3
while(backup2>0) { // you loop on backup2
cout << "#";
backup3--; // but it doesn't change since you decrement the wrong variable
}
int ..., backup1, ...;
while(backup1 > 0) {
cout << "0";
backup1 = backup1--;
}
int num_sharps = 2*height - 1;
int num_spaces = 0;
std::cout << std::string(num_spaces, ' ')
<< std::string(num_sharps, '#')
<< std::endl
#include <iostream>
using namespace std;
int main()
{
int height, spaces = 0, nextLayer;
do
{
cout << "Insert the height of the hourglass: ";
cin >> height;
}while(height > 0 && height%2 == 0); //Loop into asking a valid height
nextLayer = height; //First layer has the same number of # as there are number of layers, aka height
//First loop for the top part of the hourglass
for(int c = 0; c < height/2 + 1; c++)
{
for(int k = 0; k < spaces; k++)
{
cout << " ";
}
for(int k = 0; k < nextLayer; k++)
{
cout << "#";
}
cout << endl; //end the line
nextLayer = nextLayer - 2; //Decrease the number of #
spaces++; //Augment the indentation
}
/*
** Since we are now going to make the bottom specular part
** let's "come back" by two loops
*/
nextLayer = nextLayer + 4;
spaces = spaces - 2;
//Second loop for the bottom part of the hourglass
for(int c = 0; c < height/2; c++)
{
for(int k = 0; k < spaces; k++)
{
cout << " ";
}
for(int k = 0; k < nextLayer; k++)
{
cout << "#";
}
cout << endl;
nextLayer = nextLayer + 2;
spaces--;
}
}