This Blog is dedicated to Sir Johnson Lin
Learn flow chart from C language , master C with the chart!

巴斯卡三角形自行輸出excel格式 (PascalTri.csv) Pascal triangle auto output excel format ( PascalTri.csv)



D:\CPPcode\Pascal2excel\Pascal2excel.c++
  1 //++++++++++++++++++++++++++
  2  //      Pascal2excel.cpp
  3  //++++++++++++++++++++++++++
  4  
  5  ///#include header
  6  #include <iostream>
  7  #include <fstream>
  8  #include <iomanip> // setw(8)
  9  
 10  ///Using & universal
 11  using namespace std;
 12  
 13  ///main(i)
 14  int main()
 15  {
 16      ///declare
 17      int n;
 18      int i, j;
 19      unsigned tri[51][51];
 20  
 21      ///Prompt /Keyin
 22      cout <<   " Please input size of Pascal triangle:   ";
 23      cin >> n;
 24  
 25      ///within range
 26      if (n<0 || n>=50) { cout <<"Only  0 ~ 50 !\n" << endl;  return 1;}
 27  
 28      ///tri[0][0] initial=1
 29      tri[0][0] = 1;
 30  
 31      ///from 1st layer to Nth layer
 32      for (i = 1; i <= n; i++)
 33      {
 34          ///fill "1" in row_edge ,  col_edge
 35          tri[i][0] = tri[0][i] = 1;
 36  
 37          ///from 2nd layer to Nth layer
 38          for (j = 1; j <= i-1; j++)//i=2 do 1st
 39          {
 40              ///layer sum
 41              tri[i-j][j] = tri[i-j][j-1]+tri[i-j-1][j];
 42              //i=2 j=1 1 1=      1    0 +        0  1
 43              //i=3 j=1 2 1=      2    0 +        1  1
 44              //i=3 j=2 1 2=      1    1 +        0  2
 45  
 46              ///+
 47          }//end for
 48  
 49          ///+
 50      }//end for
 51  
 52      ///Open csv_file
 53      ofstream csv_file;
 54      csv_file.open( "PascalTri.csv" );
 55  
 56      ///from zero layer to Nth layer
 57      for (i = 0; i <= n; i++)
 58      {
 59          ///left_blank decrease
 60          for (j = 0; j < (n-i) ; j++)
 61          {
 62              ///put 1s blank
 63              csv_file << "," ;//one comma = one column blank in csv file
 64  
 65              ///+
 66          }//end for
 67  
 68          ///increase to layer end
 69          for (j = 0; j <= i; j++)
 70          {
 71              ///store sum
 72              csv_file << tri[i-j][j] ;
 73  
 74              ///layer end
 75              if (j!=i)
 76              {
 77                  ///store 2 blank
 78                  csv_file << ",," ;//2 blank prior to next sum
 79  
 80                  ///+
 81              }//end if
 82  
 83              ///+
 84          }//end for
 85  
 86          ///store CR/LF
 87          cout << "ending !";
 88          csv_file << endl;
 89  
 90          ///+
 91      }//end for
 92  
 93      ///Close csv_file
 94      csv_file.close();
 95  
 96      ///main(o)
 97      return 0 ;
 98  } //main_end
 99  

@ 下載程式碼 Download source code

沒有留言:

張貼留言