根据main函数中对printchs函数的调用,以及printchs的功能要求,编写printchs函数,使下面的程序能输出星号图:

来源:互联网 发布:java编码哪个是中文的 编辑:程序博客网 时间:2024/06/07 07:07

任务和代码:根据main函数中对printchs函数的调用,以及printchs的功能要求,编写printchs函数,使下面的程序能输出星号图。

/*文件名:main.c作者:小风景完成日期:2016.6.19问题描述:根据main函数中对printchs函数的调用,以及printchs的功能要求,编写printchs函数,使下面的程序能输出星号图:程序输出:星号图*/#include <stdio.h>void printchs(int m,char n) //定义能输出一行m个空格和n个星号的函数{    int j;    for (j=1; j<=m; ++j)         printf("%c",n);}int main( ){    int n=6; //n代表要输出的行数    int i;    //通过在下面的循环里调用printchs函数,输出右面的图    for(i=1; i<=n; ++i)    {        printchs(n-i,' ');        printchs(2*i-1,'*') ;        printf("\n");    }    return 0;}


程序运行截图:



在上面程序中,只修改一处,输出下面图形:



/*文件名:main.c作者:小风景完成日期:2016.6.19问题描述:根据main函数中对printchs函数的调用,以及printchs的功能要求,编写printchs函数,使下面的程序能输出星号图:程序输出:星号图*/#include <stdio.h>void printchs(int m,char n) //定义能输出一行m个空格和n个星号的函数{    int j;    for (j=1; j<=m; ++j)         printf("%c",n);}int main( ){    int n=6; //n代表要输出的行数    int i;    //通过在下面的循环里调用printchs函数,输出右面的图    for(i=1; i<=n; ++i)    {        printchs(n-i,' ');        printchs(2*i-1,'A' + i - 1) ;        printf("\n");    }    return 0;}

总结:通过观察输出的变化,在for语句中,空格输出不变,只变化第二次调用printfchs()传入的参数,ABCDEF之间的关系是渐次加1,因此改变第二个函数调用的第二个参数为'A'+i-1


0 0
原创粉丝点击