开始C程序 [英] Beginning C Program

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

问题描述

我的工作虽然有些问题,开始与哈佛大学的在线CS50类设置。我得到了这个问题才能正常工作,但我不知道是否有可能会成为一个更清洁的或更好的方式来获得该计划的工作。

该计划的目标是将打印由哈希标签和空格字符的右对齐金字塔。在问候风格和技巧的指导意见将是非常欢迎的。

  / *创建马里奥计划,其目标是建立一个
*金字塔通过接受来自用户的输入,以获得
*高度然后对准嘧啶右侧。
*
* /#包括LT&;&stdio.h中GT;
#包括LT&;&cs50.h GT;INT主要(无效)
{    //获取用户输入和设置为可变
    的printf(高度:);
    INT高度=调用getInt();    INT I,J,K;
    对于(i = 1; I<高度;我++)
    {        //创建n-1个空格
        为(K =(高度 - 2); K&GT(I-1); K--)
        {
            的printf(%C,'');
        }        //创建N + 1哈希标签
        为(J = 0; J&下;第(i + 1); J ++)
        {
            的printf(#);
        }        的printf(\\ n);
    }
    返回0;
}


解决方案

有关每个人的考虑:这是什么的所有风格和可读性无的样子:)

  I = 0;
而(我++ LT;高度*高)
    的printf(%C%的中,(i-1)/身高及下;高度 - (I-1)%的高度-1'?':'#',
    我%的高度? :\\ n);

这是不可能的近了看code做什么,但不运行它。如果是一个后续的运动,这是很难重新编写成,比方说,一个更片面的金字塔。我会再串联成一个小怪物,如在此之前可能抛出这个路程,从基础重新开始。


(后下)非常轻微更加利落地把我++ 结尾,所以两次(I-1)被交易了一个稍微复杂的尾线测试:

  I = 0;

    的printf(%C%S,I /高度:LT;高度-I%的高度-1'':'#'?
    我%的高度==身高1? \\ n:);
而(++ I<高度*高);

I was working though some beginning problem sets with Harvard's online CS50 class. I got the problem to work correctly but I was wondering if there would possibly be a cleaner or better way to get the program to work.

The goal of the program is to print a right-aligned pyramid comprised of hash-tags and space characters. Any guidance in regards to style or tricks would be very welcome.

/* 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;
}

解决方案

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);

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

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