Task: Create an HTML table


C++: #include <fstream>
#include <boost/array.hpp>
#include <string>
#include <cstdlib>
#include <ctime>
#include <sstream>

void makeGap( int gap , std::string & text ) {
   for ( int i = 0 ; i < gap ; i++ ) 
      text.append( " " ) ;
}

int main( ) {
   boost::array<char , 3> chars = { 'X' , 'Y' , 'Z' } ;
   int headgap = 3 ;
   int bodygap = 3 ;
   int tablegap = 6 ;
   int rowgap = 9 ;
   std::string tabletext( "<html>\n" ) ;
   makeGap( headgap , tabletext ) ;
   tabletext += "<head></head>\n" ;
   makeGap( bodygap , tabletext ) ;
   tabletext += "<body>\n" ;
   makeGap( tablegap , tabletext ) ;
   tabletext += "<table>\n" ;
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "<thead align=\"right\">\n" ;
   makeGap( tablegap, tabletext ) ;
   tabletext += "<tr><th></th>" ;
   for ( int i = 0 ; i < 3 ; i++ ) {
      tabletext += "<td>" ;
      tabletext += *(chars.begin( ) + i ) ;
      tabletext += "</td>" ;
   }
   tabletext += "</tr>\n" ;
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "</thead>" ;
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "<tbody align=\"right\">\n" ;
   srand( time( 0 ) ) ;
   for ( int row = 0 ; row < 5 ; row++ ) {
      makeGap( rowgap , tabletext ) ;
      std::ostringstream oss ;
      tabletext += "<tr><td>" ;
      oss << row ;
      tabletext += oss.str( ) ;
      for ( int col = 0 ; col < 3 ; col++ ) {
	 oss.str( "" ) ;
	 int randnumber = rand( ) % 10000 ;
	 oss << randnumber ;
	 tabletext += "<td>" ;
	 tabletext.append( oss.str( ) ) ;
	 tabletext += "</td>" ;
      }
      tabletext += "</tr>\n" ;
   }
   makeGap( tablegap + 1 , tabletext ) ;
   tabletext += "</tbody>\n" ;
   makeGap( tablegap , tabletext ) ;
   tabletext += "</table>\n" ;
   makeGap( bodygap , tabletext ) ;
   tabletext += "</body>\n" ;
   tabletext += "</html>\n" ;
   std::ofstream htmltable( "testtable.html" , std::ios::out | std::ios::trunc ) ;
   htmltable << tabletext ;
   htmltable.close( ) ;
   return 0 ;
}








COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Table.
       AUTHOR.  Bill Gunshannon.
       INSTALLATION.  Home.
       DATE-WRITTEN.  1 January 2022.
      ************************************************************
      ** Program Abstract:
      **   Data values are hardcoded in this example but they
      **     could come from anywhere.  Computed, read from a
      **     file, input from the keyboard.
      ************************************************************
       
       ENVIRONMENT DIVISION.
       
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
            SELECT Table-File ASSIGN TO "index.html"
                 ORGANIZATION IS LINE SEQUENTIAL.
       
       DATA DIVISION.
       
       FILE SECTION.
       
       FD  Table-File
           DATA RECORD IS Table-Record.
       01  Table-Record.
           05 Field1                  PIC X(80).
       
       WORKING-STORAGE SECTION.
       
       01  Table-Data.
           05  Line3.
               10  Line3-Value1  PIC S9(4)  VALUE  1234.
               10  Line3-Value2  PIC S9(4)  VALUE  23.
               10  Line3-Value3  PIC S9(4)  VALUE  -123.
           05  Line4.
               10  Line4-Value1  PIC S9(4)  VALUE  123.
               10  Line4-Value2  PIC S9(4)  VALUE  12.
               10  Line4-Value3  PIC S9(4)  VALUE  -1234.
           05  Line5.
               10  Line5-Value1  PIC S9(4)  VALUE  567.
               10  Line5-Value2  PIC S9(4)  VALUE  6789.
               10  Line5-Value3  PIC S9(4)  VALUE  3.

       
       01  Table-HTML.
           05  Line1  PIC X(16)  VALUE
               "<table border=1>".
           05  Line2  PIC X(40)  VALUE
               "<th></th><th>X</th><th>Y</th><th>Z</th>".
           05  Line3.
               10  Line3-Field1  PIC X(31)  VALUE
                   "<tr align=center><th>1</th><td>".
               10  Line3-Value1   PIC -ZZZ9.
               10  Line3-Field3  PIC X(9)  VALUE
                   "</td><td>".
               10  Line3-Value2   PIC -ZZZ9.
               10  Line3-Field5  PIC X(9)  VALUE
                   "</td><td>".
               10  Line3-Value3   PIC -ZZZ9.
               10  Line3-Field5  PIC X(10)  VALUE
                   "</td></tr>".
           05  Line4.
               10  Line4-Field1  PIC X(31)  VALUE
                   "<tr align=center><th>2</th><td>".
               10  Line4-Value1   PIC -ZZZ9.
               10  Line4-Field3  PIC X(9)  VALUE
                   "</td><td>".
               10  Line4-Value2   PIC -ZZZ9.
               10  Line4-Field5  PIC X(9)  VALUE
                   "</td><td>".
               10  Line4-Value3   PIC -ZZZ9.
               10  Line4-Field5  PIC X(10)  VALUE
                   "</td></tr>".
           05  Line5.
               10  Line5-Field1  PIC X(31)  VALUE
                   "<tr align=center><th>3</th><td>".
               10  Line5-Value1   PIC -ZZZ9.
               10  Line5-Field3  PIC X(9)  VALUE
                   "</td><td>".
               10  Line5-Value2   PIC -ZZZ9.
               10  Line5-Field5  PIC X(9)  VALUE
                   "</td><td>".
               10  Line5-Value3   PIC -ZZZ9.
               10  Line5-Field5  PIC X(10)  VALUE
                   "</td></tr>".
           05  Line6  PIC X(8)  VALUE
               "</table>".
       
       PROCEDURE DIVISION.
       
       Main-Program.
           OPEN OUTPUT Table-File.
           MOVE CORRESPONDING Table-Data TO Table-HTML.
           PERFORM Write-Table.
           CLOSE Table-File.
           STOP RUN.
       
       
       Write-Table.
           WRITE Table-Record FROM Line1 OF Table-HTML
                              AFTER  ADVANCING 1 LINE.
           WRITE Table-Record FROM Line2 OF Table-HTML
                              AFTER  ADVANCING 1 LINE.
           WRITE Table-Record FROM Line3 OF Table-HTML
                              AFTER  ADVANCING 1 LINE.
           WRITE Table-Record FROM Line4 OF Table-HTML
                              AFTER  ADVANCING 1 LINE.
           WRITE Table-Record FROM Line5 OF Table-HTML
                              AFTER  ADVANCING 1 LINE.
           WRITE Table-Record FROM Line6 OF Table-HTML
                              AFTER  ADVANCING 1 LINE.
          
       
       END-PROGRAM.