Create tables in an HTML file from an input sequence
To return to the main Polymath5 page, click here.
Here is a C++ code which you can compile with e.g. gcc under linux or cygwin.
It allows to create an HTML file displaying a sequence as well as its HAPs.
A first table contains the sequence, a second one contains it together with HAPs displayed as columns.
It takes as input a sequence in the stringent format of a file (say mysequence.txt) starting immediately with + or – and separating each element with a whitespace (put also a white space after the last element), without ever adding any carriage returns. This very format is nearly available on the wiki, just copy-paste to some file, add the spaces and suppress the non-wanted carriage returns.
Obviously you should call the output file mytable.html or something.
Feel free to add further functionalities. I'm not claiming it's fantastically written, but it works well.
/*----------------------------------------------------------
author: Thomas Sauvaget
licence: public domain.
files: just this one.
------------------------------------------------------------
modification record:
11-jan-2010: written from scratch. Really basic code...
----------------------------------------------------------*/
//---------------------------------------------------------
//-- libraries & preprocessor
//---------------------------------------------------------
#include <iostream> //for imput-output with terminal
#include <cmath> //small math library
#include <algorithm> //useful for permutations and the likes
#include <vector>
#include <fstream> //for imput-output with files
#include <iomanip> //for precision control when outputing
#include <stdlib.h>
#include <string>
#include <sstream>
//#include <assert> //for end of file command
//---------------------------------------------------------
//-- namespace (standard)
//---------------------------------------------------------
using namespace std;
//---------------------------------------------------------
//-- global variables
//---------------------------------------------------------
const int M=10000;
char filenameIN[50], filenameOUT[50];
ofstream myfileOUT;
ifstream myfileIN;
//---------------------------------------------------------
//-- useful handmade functions
//---------------------------------------------------------
//---------
//getintval
//converts string containing a single integer to int
//---------
int getintval(string strconv){
int intret;
intret=atoi(strconv.c_str());
return(intret);
}
//----------------------------------------------------------
//-- main:
//
//we ask the user the name of a file containing a sequence
//and output a full (X)HTML page for use with a browser.
//
//format MUST be: starts with either + or -, each sign separated
//by a whitespace including the last one, no carriage returns,
//in particular no extra blank lines at the end.
//This very format is nearly available on the wiki, just copy-paste to
//some file and suppress the non-wanted carriage returns.
//
//----------------------------------------------------------
int main(int argc, char *argv[]){
int i, k, d, u, v, j, c,imax=0;
int datab[M];
string line, buff;
string myplus ("+");
string myminus ("-");
//-- textlike user interface
cout<<endl;
cout<<" give input SEQUENCE filename:"<<endl;
cin>>filenameIN;
cout<<endl;
cout<<endl;
cout<<" give output TABLES filename:"<<endl;
cin>>filenameOUT;
cout<<endl;
//-- file creation
myfileOUT.open(filenameOUT,ios::out);
myfileOUT.close();
//fill table with zeros
for(i=0;i<M;i++){
datab[i]=0;
}
//-- read the sequence
myfileIN.open(filenameIN,ios::in);
if(myfileIN.is_open()){
i=0;
getline(myfileIN,line);
stringstream stsm(line);
while(stsm>>buff){
if(buff == myplus){
datab[i]=1;
//cout<<"took 1"<<endl;
}
if(buff == myminus){
datab[i]=-1;
//cout<<"took -1"<<endl;
}
if( datab[i] !=1 && datab[i] !=-1 ){
cout<<"problem: element number "<<i+1<<" is neither + nor -."<<endl;
abort();
}
//cout<<endl;
i+=1;
}
imax=i;
cout<<" this sequence contains "<<imax<<" elements."<<endl;
cout<<endl;
}
else{
cout<<"there is no file with this very name"<<endl;
cout<<" (don't forget extension like .txt or .dat in particular)"<<endl;
abort();
}
myfileIN.close();
//-- main processing:
//first a table containg the full sequence
//next a table showing the first 20 terms of the sequence and
//its HAPs as columns.
myfileOUT.open(filenameOUT,ios::app);
myfileOUT<<"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" ";
myfileOUT<<"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.tdt\"> \n\n";
myfileOUT<<"<html xmlns=\"http://www.w3.org/1999/xhtml\"> \n\n";
myfileOUT<<"<head>\n <title>a sequence and its HAPs</title>\n </head>\n";
myfileOUT<<"<body>\n";
//-- first table
myfileOUT<<" Full sequence in a squarish table:"<<endl;
myfileOUT<<"<table border=\"1\">"<<endl;
//set number of columns to sqrt(length) roughly
k=int( floor(sqrt(imax)) );
c=0;
d=imax/k;
//loop on rows
for(j=0;j<d;j++){
//construct one row, cell by cell
myfileOUT<<"<tr>"<<endl;
for(i=0;i<k;i++){
if(i==0 && j==0){
myfileOUT<<"<td bgcolor=\"black\">0</td>"<<endl;
}
else{
c+=1;
if(datab[j*k+i-1]==1){
myfileOUT<<"<td bgcolor=\"cyan\">"<<j*k+i<<"+</td>"<<endl;
}
else{
myfileOUT<<"<td bgcolor=\"white\">"<<j*k+i<<"-</td>"<<endl;
}
}
}
myfileOUT<<"</tr>"<<endl;
}
//add final cells if last row is incomplete
if((imax-1)%k != 0){
myfileOUT<<"<tr>"<<endl;
for(u=c+1;u<=imax;u++){
if(datab[u-1]==1){
myfileOUT<<"<td bgcolor=\"cyan\">"<<u<<"+</td>"<<endl;
}
else{
myfileOUT<<"<td bgcolor=\"white\">"<<u<<"-</td>"<<endl;
}
}
myfileOUT<<"</tr>"<<endl;
}
//finish table
myfileOUT<<"</table><br><br><br>"<<endl;
//-- now the table of HAPs as columns, first 30 elements each as rows
myfileOUT<<"Now the HAPs as columns:<br> "<<endl;
myfileOUT<<" first column is the sequence, second column is x_{2n},..."<<endl;
myfileOUT<<"<table border=\"1\">"<<endl;
//set number of columns to length/6 roughly
// (i.e. we want subsequences which have at least 6 elements)
k=int( floor(imax/6) );
//take only 30 first rows
d=30;
//loop on rows
for(j=0;j<=d;j++){
//construct one row, cell by cell
myfileOUT<<"<tr>"<<endl;
for(i=0;i<k;i++){
if(datab[(j+1)*(i+1)-1]==1){
myfileOUT<<"<td bgcolor=\"cyan\">"<<(j+1)*(i+1)<<"+</td>"<<endl;
}
if(datab[(j+1)*(i+1)-1]==-1){
myfileOUT<<"<td bgcolor=\"white\">"<<(j+1)*(i+1)<<"-</td>"<<endl;
}
if(i*j>=M || datab[j*(i+1)]==0){
myfileOUT<<"<td bgcolor=\"grey\">.</td>"<<endl;
}
}
myfileOUT<<"</tr>"<<endl;
}
//finish table
myfileOUT<<"</table>"<<endl;
//finish HTML
myfileOUT<<"</body>\n </html>"<<endl;
//close file!
myfileOUT.close();
//exit normally
return 0;
}