#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLINE 220// The max char number of one line void putNumToBuffer(int num, char* buffer, int maxsize); void putSpaceToBuffer(char* buffer, int count); void main(int arg, char**args){ int n; char buffer[MAXLINE][MAXLINE+2]={0}; int value[MAXLINE][MAXLINE] ={0}; int index = 0,i,j,k,maxValue=0,maxSize=0,temp; if(arg == 1){ goto USAGE_ERROR; } n = atoi(args[1]); if( n < 0 ){ goto USAGE_ERROR; }else if( n >= MAXLINE ){ goto MAX_ERROR; }else if( n == 0 ){ printf("The step is zero./n"); return; }else if( n == 1 ){ printf("1/n"); return; } // caculate all values value[0][0] = 1; for(i = 1; i < n; i++){ value[i][0] = 1; for(j = 1; j < i+1; j++){ value[i][j] = value[i-1][j-1]+value[i-1][j]; } } i--; j--; // check max size of one number temp = maxValue = value[i][j/2]; while(temp){ maxSize++; temp /= 10; } maxSize++; if( maxSize*n > MAXLINE ){ goto MAX_ERROR; } // put numbers to char buffer for(i = 0; i < n; i++){ // put heading spaces to buffer index = (maxSize+1)/2*(n-1-i); putSpaceToBuffer((char*)&buffer[i], index); // put following numbers to buffer for(j = 0; j <= i; j++){ putNumToBuffer( value[i][j], &buffer[i][index] , maxSize ); index += maxSize; } // deal the ending buffer[i][index] = '/n'; buffer[i][index+1] = '/0'; } printf("The YangHui is : /n"); for(i = 0; i<n ; i++){ printf("%s", buffer[i]); fflush(stdout); } return; USAGE_ERROR: printf("Usage : yangHui <number>/n"); printf("eg : yangHui 5/n"); return; MAX_ERROR: printf("Error : current version can't display so many words!/n"); printf(" Please try again with smaller number./n"); return; } void putNumToBuffer(int num, char* buffer, int maxsize){ char temp[12]; int i; itoa(num ,temp , 10); for( i=0; i < strlen(temp); i++ ){ buffer[i] = temp[i]; } for( ; i < maxsize; i++ ){ buffer[i] = ' '; } } void putSpaceToBuffer(char* buffer, int count){ int i; for( i=0; i < count; i++ ){ buffer[i] = ' '; } }