2

我正在研究哈佛在线 CS50 课程的一些初始问题集。我让问题正常工作,但我想知道是否有更清洁或更好的方法让程序正常工作。

该程序的目标是打印由井号标签和空格字符组成的右对齐金字塔。非常欢迎任何有关风格或技巧的指导。

/* Creating the mario program, whose goal is to create a 
*  pyramid by accepting input from the user to get the 
*  height then aligning the pyrimid to the right.
*
*/

#include <stdio.h>
#include <cs50.h>

int main(void)
{

    // get user input and set to variable
    printf("Height: ");
    int height = GetInt();

    int i, j, k;
    for(i = 1 ; i < height; i++)
    {

        // create n-1 spaces
        for(k = (height - 2); k > (i-1); k--)
        {
            printf("%c", ' ');      
        }

        // create n+1 hash tags
        for(j = 0; j < (i+1); j++)
        {
            printf("#");
        }

        printf("\n");
    }
    return 0;
}
4

4 回答 4

2

I'm assuming by cleaner you mean "spiffy and fancyer".

This looks spiffy to me:

#include <stdio.h>
#include <cs50.h>

int main(void) {
    // get user input and set to variable
    printf("Height: ");
    int height = GetInt();
    int hm2 = height - 2;

    int j, k;
    for(int i = 1 ; i < height; i++) {
        // create n-1 spaces
        for(k = hm2; k > (i-1); k--)
            printf("%c", ' ');      

        // create n+1 hash tags
        for(j = 0; j < (i+1); j++)
            printf("#");

        printf("\n");
    }
    return 0;
}

However, don't get too caught up in making your code fancy. Although it's nice if you're working with others, or yourself really. Your example looked fine.

Now, optimization-wise, that's something to worry about. Just remember that too much optimization can potentially break your program.

于 2013-11-24T22:47:36.837 回答
2

For everyone's consideration: this is what "all style and no readability" looks like :)

i = 0;
while (i++ < height*height)
    printf ("%c%s", (i-1)/height < height-(i-1)%height-1 ? ' ' : '#',
    i % height ? "" : "\n");

It is nigh on impossible to see what the code does without running it. If there is to be a follow-up exercise, this is hard to re-write to form, say, an even-sided pyramid. I'd probably throw this away and start again with the basics, before concatenating it again into a little monster such as this.


(later) Ever so slightly more neat to put the i++ at the end, so two times (i-1) gets traded for a slightly more complicated end-of-line test:

i = 0;
do
    printf ("%c%s", i/height < height-i%height-1 ? ' ' : '#',
    i % height==height-1 ? "\n" : "");
while (++i < height*height);
于 2013-11-25T00:30:56.177 回答
1

I think by cleaner and better way you mean to be a perfect shaped right angled triangle pyramid.
For this you should do as
Change

printf("Height: ");  

to

printf("Height: \n\n");  

and

for(i = 1 ; i < height; i++)  

to

for(i = 0 ; i < height; i++)   

And see the sample output.

于 2013-11-24T22:49:04.897 回答
0

这里有一个建议:

#include <stdio.h>
#include <cs50.h>

int main(void) {
    //initialize variables
    int height, hm2, j, k, i;

    printf("Height: \n");
    // Get user input
    height = GetInt();
    hm2 = height - 1;
    for(i = 0; i < height; i++) {
        // create n spaces
        for(k = hm2; k > i; k--)
            printf("%c", ' ');      

        // create n+1 hash tags
        for(j = 0; j < i+1; j++)
            printf("#");

        printf("\n");
    }
    return 0;
}

如果用户输入 5 作为高度的结果:

Height: 
    #
   ##
  ###
 ####
#####

我在这个版本的代码中考虑了几件事:

- 在 C 中,最好单独声明所有变量,而不是给它们赋值,然后再赋值。如果您在 for 循环中声明和赋值,某些编译器可能会出现此错误:“错误:'for' 循环初始声明仅在 C99 模式下允许”。这些更改是根据我提供的内容考虑的。

//initialize variables                                                            
int height, hm2, j, k, i;

-我在这里添加了一个换行符

printf("Height: \n");

- 而不是 hm2 = height - 2 我将其更改为:

hm2 = height - 1;

-第一个循环,现在我们给 ia 值并将其设置为 0 以满足所做的其他更改:

for(i = 0; i < height; i++) {

- 对于创建 n 个空格的循环,我将其更改为:

for(k = hm2; k > i; k--)

-最后在最后一个 for 循环中删除了括号(在这种情况下不需要):

for(j = 0; j < i+1; j++)

干杯

于 2017-08-29T19:40:16.473 回答