中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > C/C++
一个简单的C程序
作者:未知 时间:2005-09-13 23:27 出处:Blog.ChinaUnix.net 责编:chinaitpower
              摘要:一个简单的C程序

    这是我写的一个简单的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. */





关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有