|
发表于 2019-3-29 13:11:22
|
显示全部楼层
这写法好暴力,main是程序入口,保留字递归。
难点 printf("%*s",w,"*******"+n);
函数中*是宽度通配符,w是宽度,n是输出串的起点位置
测试:
printf("%*s \n", 7, "*******" + 1);
printf("%*s \n", 7, "*******" + 2);
printf("%*s \n", 7, "*******" + 3);
printf("%*s \n", 7, "*******" + 4);
printf("%*s \n", 7, "*******" + 5);
printf("%*s \n", 7, "*******" + 6);
printf("%*s \n", 7, "*******" + 7);
运行结果:
******
*****
****
***
**
*
请按任意键继续. . .
这是一个非标准C用法,标准C中串哪有加数字地。但是它能运行。查资料,没有讲+n的,
测试发现不但可以加n,还可以-n,||n,比较流氓,都能编译通过,但运行结果是未知地。
递归控制结束是全局变量iLine。每次子递归只执行了一遍,直到iLine==8。
递归到iLine==8时,就都退出了。
强调一下,代码那样写,霸占main,即使是小测试程序,都会把无辜的人被卷入一场递归的是非漩涡。
要不得。垄断main要收租金。
该代码的等效代码:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int iLine = 1;
int pattern()
{
//printf("%*s \n", 7 - (iLine>4 ? iLine - 4 : 4 - iLine), "*******" + 2 * (iLine>4 ? iLine - 4 : 4 - iLine));
char *s ="*******";
if (iLine > 4) cout << setw(7 - (iLine - 4)) << (char*)&s[2 * (iLine - 4)] << endl;
else cout << setw(7 - (4 - iLine)) << (char*)&s[2 * (4 - iLine)] << endl;
if (++iLine != 8) pattern();
return 0;
}
int main(void) { return pattern();}
运行结果:
*
***
*****
*******
*****
***
*
请按任意键继续. . .

|
|