编写Triangle程序需要帮助 [英] Need Help with writing a Triangle program

查看:87
本文介绍了编写Triangle程序需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个创建四个三角形的程序。

程序首先提示用户输入三角形的大小,

从1到N的数字是四个三角形的大小例如,如果我们

输入4,则四个三角形的大小为4行。


另外,我还在写一个程序,我开始并且在

中失败,给出了正确尺寸的traingles。我会很感激,如果有人可以帮助我们提供
帮助。

*

* *

* * *

* * * *

* * * *

* * *

* *

*

* * * *

* * *

* *

*

*

* *

* * *

* * * *

我也试过创建这个程序但是我无法确认*的格式化。



#include< iostream>

使用命名空间std;

int main()

{

cout<< 输入一个字符:>" ;;

int n;

cin> n;

for(int row = 0; row< n; row ++)

{

for(int column = 0; column< = row; column ++)

cout< < '''';

for(int column = 0; column<(3-row); column ++)

cout<< " *" ;;

cout<< " "<< endl;

cout<< endl;

}

for(int row = 0; row< n; row ++)

{

for(int column = 0; column< =(3 - row); column ++)

cout<< " *" ;;

cout<< " " ;;

cout<< endl;

}

cout<< endl;

for(int row = 0; row< n; row ++)

{

for(int column = 0; column< = row; column ++)

cout<< " *" ;;

cout<< " " ;;

cout<< endl;

}

cout<< endl;

for(int row = 0; row< n; row ++)

{

for(int column = 0; column< row; column ++)

cout<< '''';

for(int column = 0; column< =(3-row);

column ++)

cout< ;< " *" ;;

cout<<结束;

}

返回0;

I am trying to write a program which creates four triangles. The
program begins with prompting a user " Enter the size of triangles",
number from 1 to N is the size of four triangles For Example if we
enter 4, the size of four triangles are 4 rows.

In addition, I am also writing a program which i started and failed in
giving the right size of traingles. I will appreciate if somebody can
help.
*
* *
* * *
* * * *
* * * *
* * *
* *
*
* * * *
* * *
* *
*
*
* *
* * *
* * * *
I have also tried to create this program but i couldn''t be able to
correct the formating of "*".


#include <iostream>
using namespace std;
int main()
{
cout << "Enter a character:>";
int n;
cin >n;
for ( int row = 0; row < n; row++ )
{
for ( int column = 0; column <= row; column++ )
cout << '' '';
for ( int column = 0; column < (3 - row); column++ )
cout << " * ";
cout << " "<< endl;
cout << endl;
}
for ( int row = 0; row < n; row++ )
{
for ( int column = 0; column <= (3 - row); column++ )
cout << " * ";
cout << " ";
cout << endl;
}
cout<<endl;
for ( int row = 0; row < n; row++ )
{
for ( int column = 0; column <= row; column++ )
cout << " * ";
cout << " ";
cout << endl;
}
cout<<endl;
for ( int row = 0; row < n; row++ )
{
for ( int column = 0; column < row; column++ )
cout << '' '';
for ( int column = 0; column <= (3 - row);
column++ )
cout << " * ";
cout << endl;
}
return 0;

推荐答案


as*****@hotmail.com 在留言中写道...

as*****@hotmail.com wrote in message ...

>我正在尝试编写一个创建四个三角形的程序。
程序首先提示用户输入三角形的大小,
从1到N的数字是四个三角形的大小。例如,如果我们输入4,则四个三角形的大小为4行。

另外,我也在编写一个程序,我开始并且没有给出适当大小的traingles。如果有人能帮助我,我将不胜感激。
>I am trying to write a program which creates four triangles. The
program begins with prompting a user " Enter the size of triangles",
number from 1 to N is the size of four triangles For Example if we
enter 4, the size of four triangles are 4 rows.

In addition, I am also writing a program which i started and failed in
giving the right size of traingles. I will appreciate if somebody can
help.



为什么你已经开始了另一个线程?

Why did you start another thread when you already had one going?


>
我也试图创建这个程序,但我无法纠正*的格式化。

#include< iostream>
using namespace std;

int main(){

cout<< 输入一个字符:>" ;;

int n;

cin> n;
>
I have also tried to create this program but i couldn''t be able to
correct the formating of "*".

#include <iostream>
using namespace std;

int main(){
cout << "Enter a character:>";
int n;
cin >n;



/ *

std :: istringstream icin(" R \ n");

cout << 输入一个字符:>" ;;

int n;

icin> n;

cout<< n<< std :: endl;

// out:输入一个字符:> 10673684


你问了一个''字符'' ! ''int''的''cin''将失败,你得到

未定义的行为!

* /


std :: istringstream icin(" 4 \ n");

cout<< 输入单个数字:>" ;;

int n(0); // INITIALIZE !!!

icin> n;

if(not n){return EXIT_FAILURE;} //总是检查你的输入。

//或:if(n< 1 || n 1000){return EXIT_FAILURE;}

cout<< n<< std :: endl;

// out:输入一个数字:> 4

/*
std::istringstream icin("R \n");
cout << "Enter a character:>";
int n;
icin >n;
cout << n <<std::endl;
// out: Enter a character:>10673684

You asked for a ''character''! The ''cin'' to an ''int'' will fail, you get
undefined behavior!
*/

std::istringstream icin("4 \n");
cout << "Enter a single number:>";
int n(0); // INITIALIZE!!!
icin >n;
if( not n ){ return EXIT_FAILURE;} // ALWAYS check your input.
// or: if( n < 1 || n 1000 ){ return EXIT_FAILURE;}
cout << n <<std::endl;
// out: Enter a single number:>4


>

for(int row = 0; row< n; ++ row){

for(int column = 0; column< = row; ++ column){

cout<< ''';
>
for( int row = 0; row < n; ++row ){
for( int column = 0; column <= row; ++column ){
cout << '' '';



}

}


for(int column = 0; column<(3-row); + +列){

cout<< " *;;
for( int column = 0; column < (3 - row); ++column ){
cout << " * ";



}

}


cout<< " "<< endl<< endl;

} // for(row)


for(int row = 0; row< n; row ++){

for(int column = 0; column< =(3-row); column ++){

cout<< " *;;
cout << " "<< endl << endl;
} // for(row)
for( int row = 0; row < n; row++ ){
for ( int column = 0; column <= (3 - row); column++ ){
cout << " * ";



}

}


cout<< " " << endl;

} // for(row)

cout<< endl;

for(int row = 0; row< n ;行++){

for(int column = 0; column< = row; column ++){

cout<< " *;;
cout << " " << endl;
} // for(row)
cout<<endl;
for( int row = 0; row < n; row++ ){
for ( int column = 0; column <= row; column++ ){
cout << " * ";



}

}


cout<< " " << endl;

} // for(row)

cout<< endl;

for(int row = 0; row< n ;行++){

for(int column = 0; column< row; column ++){

cout<< ''';
cout << " " << endl;
} // for(row)
cout<<endl;
for( int row = 0; row < n; row++ ){
for( int column = 0; column < row; column++ ){
cout << '' '';



}

}


for(int column = 0; column< =(3-row);专栏++){

cout<< " *;;
for( int column = 0; column <= (3 - row); column++ ){
cout << " * ";



}

}


cout<< endl;

} // for(row)

返回0;
cout << endl;
} // for(row)
return 0;



你需要在这里建立一个结束的支持!!!


} // main()结束

Alf P. Steinbach写道:

YOU NEED TO PUT AN CLOSING BRACE HERE!!!

} // main() end
Alf P. Steinbach wrote:


>尝试更正缩进以查看此代码的真实结构。
>Try correcting the indenting to see the real structure of this code.



我在上面为你做了这个。

I did that for you above.


>为了帮助你,/ always / put {}围绕一个循环体。
>To help with that, /always/ put {} braces around a loop body.



我在上面为你做了这件事。


现在你告诉我们应该做什么,以及它是什么这样做不是你想要的b $ b。

(....并确保你没有顶级!请。)


-

Bob R

POVrookie

I did that for you above.

Now you tell us what it is supposed to do, and what it is doing that is not
what you want.
(....and be sure you do not top-post! Please.)

--
Bob R
POVrookie




BobR写道:

BobR wrote:
as*****@hotmail.com 在消息中写道。 ..
as*****@hotmail.com wrote in message ...

我正在尝试编写一个创建四个三角形的程序。

程序首先提示用户输入三角形的大小,

从1到N的数字是四个三角形的大小例如,如果我们

输入4,则四个三角形的大小为4行。


另外,我还在写一个程序,我开始并且在

中失败,给出了正确尺寸的traingles。如果有人可以提供帮助,我将不胜感激。
I am trying to write a program which creates four triangles. The
program begins with prompting a user " Enter the size of triangles",
number from 1 to N is the size of four triangles For Example if we
enter 4, the size of four triangles are 4 rows.

In addition, I am also writing a program which i started and failed in
giving the right size of traingles. I will appreciate if somebody can
help.



为什么你已经开始了另一个线程?


Why did you start another thread when you already had one going?



我我也试图创建这个程序,但我无法确定*的格式化。


#include< iostream>

使用命名空间std;


int main(){

cout<< 输入一个字符:>" ;;

int n;

cin> n;

I have also tried to create this program but i couldn''t be able to
correct the formating of "*".

#include <iostream>
using namespace std;

int main(){
cout << "Enter a character:>";
int n;
cin >n;



/ *

std :: istringstream icin(" R \ n");

cout << 输入一个字符:>" ;;

int n;

icin> n;

cout<< n<< std :: endl;

// out:输入一个字符:> 10673684


你问了一个''字符'' ! ''int''的''cin''将失败,你得到

未定义的行为!

* /


std :: istringstream icin(" 4 \ n");

cout<< 输入单个数字:>" ;;

int n(0); // INITIALIZE !!!

icin> n;

if(not n){return EXIT_FAILURE;} //总是检查你的输入。

//或:if(n< 1 || n 1000){return EXIT_FAILURE;}

cout<< n<< std :: endl;

// out:输入一个数字:> 4


/*
std::istringstream icin("R \n");
cout << "Enter a character:>";
int n;
icin >n;
cout << n <<std::endl;
// out: Enter a character:>10673684

You asked for a ''character''! The ''cin'' to an ''int'' will fail, you get
undefined behavior!
*/

std::istringstream icin("4 \n");
cout << "Enter a single number:>";
int n(0); // INITIALIZE!!!
icin >n;
if( not n ){ return EXIT_FAILURE;} // ALWAYS check your input.
// or: if( n < 1 || n 1000 ){ return EXIT_FAILURE;}
cout << n <<std::endl;
// out: Enter a single number:>4



for(int row = 0; row< n; ++ row){

for(int column = 0; column< = row; ++ column){

cout<< ''';

for( int row = 0; row < n; ++row ){
for( int column = 0; column <= row; ++column ){
cout << '' '';



}

}


for(int column = 0; column<(3-row); + +列){

cout<< " *;;
for( int column = 0; column < (3 - row); ++column ){
cout << " * ";



}

}


cout<< " "<< endl<< endl;

} // for(row)

for(int row = 0; row< n; row ++){

for( int column = 0; column< =(3 - row); column ++){

cout<< " *;;
cout << " "<< endl << endl;
} // for(row)
for( int row = 0; row < n; row++ ){
for ( int column = 0; column <= (3 - row); column++ ){
cout << " * ";



}

}


cout<< " " << endl;

} // for(row)

cout<< endl;

for(int row = 0; row< n ;行++){

for(int column = 0; column< = row; column ++){

cout<< " *;;
cout << " " << endl;
} // for(row)
cout<<endl;
for( int row = 0; row < n; row++ ){
for ( int column = 0; column <= row; column++ ){
cout << " * ";



}

}


cout<< " " << endl;

} // for(row)

cout<< endl;

for(int row = 0; row< n ;行++){

for(int column = 0; column< row; column ++){

cout<< ''';
cout << " " << endl;
} // for(row)
cout<<endl;
for( int row = 0; row < n; row++ ){
for( int column = 0; column < row; column++ ){
cout << '' '';



}

}


for(int column = 0; column< =(3-row);专栏++){

cout<< " *;;
for( int column = 0; column <= (3 - row); column++ ){
cout << " * ";



}

}


cout<< endl;

} // for(row)

返回0;
cout << endl;
} // for(row)
return 0;



你需要在这里放一个结束的支持!!!


} // main()结束


Alf P. Steinbach写道:


YOU NEED TO PUT AN CLOSING BRACE HERE!!!

} // main() end
Alf P. Steinbach wrote:


尝试更正缩进以查看此代码的真实结构。
Try correcting the indenting to see the real structure of this code.



我在上面为你做了这个。


I did that for you above.


为了帮助你,/ always / put {}环体周围的支撑。
To help with that, /always/ put {} braces around a loop body.



我在上面为你做了这件事。


现在你告诉我们应该做什么,以及它是什么这样做不是你想要的b $ b。

(....并确保你没有顶级!请。)


-

Bob R

POVrookie


I did that for you above.

Now you tell us what it is supposed to do, and what it is doing that is not
what you want.
(....and be sure you do not top-post! Please.)

--
Bob R
POVrookie



你好!首先,我在

帖子的开头编写了它编译运行的程序。当我把这个程序粘贴在董事会上时,我错过了收盘的主要支架。 }" ;.我用

程序的问题是它没有改变三角形的大小,除了第一个

三角形,最后三个三角形的角度不正确或

变形了。


我修改过的程序我试过并编译并运行它。它给了我

同样的问题。现在第一个三角形看起来不再是一个

三角形。下面的示例显示了输出现在的样子:


输入整数的三角形大小:5

* * *

* *


*


* * * *

* * *

* *

*


* * * *

* * *

* *

*

* * * *

* * *

* *

*

Hi again! first of all the program i wrote it in the begining of the
post it compiles an run. while i pasted the program in the board i
missed the closing main bracket " }". The problem i had with the
program was its not changing the size of the triangles except the first
triangle, and the angles of the last three triangles are not right or
out of shape.

The program you modified i tried and i compiled and ran it. It gives me
the same problem. Now the first triangle is no more look like a
triangle. The example below shows how the output looks like now:

Enter a size of triangles in integer :5
* * *

* *

*


* * * *
* * *
* *
*

* * * *
* * *
* *
*
* * * *
* * *
* *
*


2006年11月18日19:14:16 -0800 comp.lang.c ++, as ***** @ hotmail.com

写道,
On 18 Nov 2006 19:14:16 -0800 in comp.lang.c++, as*****@hotmail.com
wrote,

for(int column = 0; column<(3-row); column ++)
for ( int column = 0; column < (3 - row); column++ )



魔法数字3来自哪里?

对于一个n值,对所有其他值都是错误的。

应该是(n - 行)或类似的东西(你选择有多相似。)

Where did that magic number 3 come from?
It will be right for one value of n, wrong for all others.
Should be (n - row) or something similar (you choose how similar.)


这篇关于编写Triangle程序需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆