这是我写的一个简单的C程序,对于初学者都会写到的.但我想达到的不是写出这个程序的目的. 学习程序设计不是写源代码,虽然说程序设计是要写源代码,但是写源代码不一定就是程序设计. 虽然这个程序很简单,但它是一种思想和风格的结合,程序的算法多种多样,风格各异.但是在一个 程序中风格是必须保持一致的,这才是我真正想说的. 代码如下: /* * File: Triangle.c * ---------------- * This program is used to print the first eight rows of * Pascal's Triangle like: * 1 * 1 1 * 1 2 1 * 1 3 3 1 * 1 4 6 4 1 * 1 5 10 10 5 1 * 1 6 15 20 15 6 1 * 1 7 21 35 35 21 7 1 */ #include<stdio.h> /* * Constants: * ---------- * Rows of the Pascal's Triangle are represented by the * integer 8. */ #define ROW 8 /* Function prototypes */ void GiveInstructions(); void PrintTriangle(); void PrintFirstLine(); void PrintLine(int row,int BackRow[]); /* Main program */ main() { GiveInstructions(); PrintTriangle(); printf("\n"); } /* Function: GiveInstruction * Usage: void GiveInstruction(); * ----------------------------- * This procedure prints out instructions to the Reader. */ void GiveInstructions() { printf("This program display the first eight rows of Pascal's Triangle.\n\n"); } /* Function: PrintTriangle * Usage: void PrintTriangle(); * -------------------------- * This procedure prints out the Triangle. */ void PrintTriangle() { int ForeRow[ROW]=; int BackRow[ROW]=; int *pt,*Fore,*Back; int row,n; Fore=ForeRow; Back=BackRow; for(row=1;row<=ROW;row++) { if(row==1) PrintFirstLine(); else { for(n=0;n Back[n+1]=Fore[n]+Fore[n+1]; PrintLine(row,Back); } pt=Fore; Fore=Back; Back=pt; } } /* Function: PrintFirstLine * Usage: void PrintFirstLine(); * ----------------------------- * This procedure is printing the first line of the Triangle. */ void PrintFirstLine() { int row; for(row=1;row<=2*ROW-2;row++) { printf(" "); } row=1; printf("%2d\n",row); } /* Function: PrintLine * Usage:void PrintLine(int row,int *,int *); * ------------------------------------------ * This procedure is printing a line of the Triangle except first line. */ void PrintLine(int row,int BackRow[]) { int row_blank; for(row_blank=1;row_blank<=2*(ROW-row);row_blank++) { printf(" "); } for(row_blank=0;row_blank { printf("%2d ",BackRow[row_blank]); } printf("\n"); } /* That's the Triangle.c program end. */
|