Showing posts with label cweb. Show all posts
Showing posts with label cweb. Show all posts

Thursday, June 3, 2010

โปรแกรม เรื่องสั้น (ต่อ(ต่อ))

เป็นโปรแกรมการเคลื่อนที่ทางเดียว กากฯก็ได้ แต่เคลื่อนที่ทางเดียว ดังนั้น กระสุนก็ใช้ได้ เพราะมันก็สูตรเดียวกัน ที่นำมาให้ชม ก็เป็นต้นฉบับ ที่ สับมิด ไปเมื่อวานนี้แล้ว ฉบับสุดท้าย

จากต้นฉบับนี้, filename.w,หากใช้ FreeBSD จริง จะมีคำสั่ง cweave
นี้อยู่ ก็เรียกมาใช้กับแฟ้มนี้ แล้วจะมีผลลัพธ์ออกมาด้วยแฟ้มชื่อเดียวกัน แต่จะลงท้ายด้วย .tex
% cweave filename.w

ก็ให้สั่งต่อไปด้วย
% pdftex filename.tex

ก็จะได้แฟ้ม filename.pdf ออกมาเป็น คู่มือการใช้งาน แต่อย่างที่บอกไว้ มันเป็นภาษาอังกฤษ ซึ่งเป็นข้อจำกัดของ TeX เขา และในคำสั่งแรก ถ้าเปลี่ยนจาก cweave เป็น ctangle ก็จะได้แฟ้ม filename.c ซึ่งนำไป compile ด้วย C compiler แล้วให้ executable program ออกมา ทำงานได้ ซึ่งก็ได้บอกไว้ชัดเจนแล้วในแฟ้มคู่มือ ที่ได้มา

ถ้าคิดว่าจะนำไปใช้บ้าง ก็คัดลอกไป ตามที่เห็นน่ะ แล้วไปดัดแปลงเอาเอง

แปลกตรงที่หน้าสารบัญ เขากลับสร้างมาหลังสุด ใครตอบถูกว่า ทำไม จะให้รางวัล

@* SGM programming for one dimensional movement.
This is SGM-6M-PROG-1d final
release.
This release adds command line arguments to ease, or to speed up,
users and also add some features to the programme with hope to meet specifications
and to enhance previous release such as crafting a system dependent in order
to get a relatively system independent softwares


@ Beginning with this release, assumptions have to be clearly stated here
that speed is a constant value.
Additionally, plotting graphs will
be done by other utilities, called gnuplot, available around the net.

Plotting graph by gnuplot within process is quite problematics one.
Data file
used in script for plotting can not be deferred, it must be specified directly.
Since
gnuplot command {\bf plot '{\it datafile}\/'} requires that {\it datafile}\/ is
{\it datafile}\/, the content is merely data for plotting and nothing more.


@ Now that let us imagine this programme should have the command, which is
an executable computer programme compiled from c source file, of the form,
within this release as either

{\bf sgm-6m-prog-1d -s filename}

\noindent or

{\bf sgm-6m-prog-1d -d filename}

\noindent whereas filename contains data for either speed or distant needed in
calculation for the main programme.
Moreover, {\bf filename} can be any
name but {\it jotawski\/}, {\it jotawski\/} is used for datafile mentioned
above, yes {\bf plot '{\it datafile}\/'} is actually
{\bf plot '{\it jotawski\/}'}.


@ Let us step forward a little bit into detail about |filename|
mention in the previous section.
Firstly, the format of data
in |filename| is a number of x,y pair which is an integer number,
and followed by x,y pair themselves, in floating point number.
The number of pairs of (x,y) is equal to the first number given, for example

{\bf 3} $\leftarrow$ {\it number of x,y pair\/,integer, followed by 3 lines of (x,y) pair}

{\bf 0.5,12.0} $\leftarrow$ {\it x,y pair itself separated by comma\/, floating point}

{\bf 1.8,20.0} $\leftarrow$ {\it x,y pair itself separated by comma\/, floating point}

{\bf 2.2,25.0} $\leftarrow$ {\it x,y pair itself separated by comma\/, floating point}


@ Secondly, for the x,y pairs themselves, the first one, x--value, is always time.
The second one, y--value, can be either distant or speed depending on what is needed.
If this programme is called with the first form, {\bf sgm-6m-prog-1d -s filename},
it is clearly that y--value is a distant.
Option s, --s, means speed is needed
so that distant has to be given.
There is no secret in the forest.


@ The output is a dot png file.
It is a portable network graphics format file which
is avaiable for further modification and the name is, as expected, |jotawski.png|.
For interested users, on how to get this file, please have a look at command file |jotawski.cmd|.
You can edit and play around with this command file safely.
Have fun.


@* Our problem layout, in KISS, is as follow.

@^sgm-6m-prog-1d-v1@>
@c

/*- Copyright (c) 2010 pirat sriyotha, aka jotawski
* All rights reserved.
* TBA meat of license.
*/
@<Global Variables@>@/
@<Header files@>@/
@<GNUPlot Script create@>@/
@<main@>
@ Header files and global variables are defined here.
At this moment,
only anticipated one are listed.


@<Header files@>=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


@ Global variables in this programme.

@<Global Variables@>=
char *programme; /* this file */
char *filename; /* filename from command line */
@* Entry point to the programme, |main| function.
|C| is nothing but function of
operating system which has main function as an entry point.

@<main@>=
main (argc,argv)
int argc; /* the number of arguments on the \UNIX/ command line */
char **argv; /* the arguments themselves, an array of strings */
{@/
@<Local variables for main@>@/
programme=argv[0];@/
@<Startup@>@/
if(status != OK)@/
exit(status);@/
@<Do calculation and plot graphs via |fork()|@>@/
@<CleanUp@>@/
exit(status);@/
}@/


@ The rest will explain the whole story from |main| down to
the last curly brace of this function.


@ Some local variables needed as in the first version are |speed|,
|distant|, |time| and |interval|.
They are all float data type
except the last one which is integer.
|x| and |y| are pointers to
float which will be used for real calculation and passing to gnuplot().

@<Local variables for main@>=
float *x, *y, *px, *py;
float speed, distant, time;
int interval;


@ More to come is a kind of file pointers type, |FILE *fp|, which
will hold informations relevant to data in input file.
This is really
the good side of writing softwares in more than one release since I have just
realized that |fp| is needed here at the moment of writing
and so inserted immediately.

@<Local ...@>=
FILE *fp;
@ Some preparations such as allocation of memories for filename,
define status for global use and so on are defined here, in global variables section.


@d OK 1
@d OPEN_FILE 2
@d USAGE 3
@d NO_ROOM 4


@ |status| is a local variable for main and initially set to |OK|.
The other integers are for command line parsing using system function |getopt(3)|.
|sflag| is used to indicate
that speed has been selected.
|interactive| is to indicate that we enter a child process
and doing something manually overthere and must be used with option s or option d.

@<Local ...@>=
int status=OK;
int sflag, interactive, ch;


@* Startup code.
This startup codes are just checked for calling syntax, memories allocation.
@<Start...@>=
if (argc == 1) {
fprintf(stderr,"usage: %s -[[i]s|[i]d] data-filename\n", programme);
status= USAGE;
} else {
if ( (filename = (char *)malloc(80)) == NULL) {
status = NO_ROOM;
} else {
@<Getoption@>
}
}


@ |errno| is needed at this point.
Get options is nothing
but services from system funtion.
@<Header files@>=
#include <errno.h>


@ |getopt(3)|, one of many functions from system services
@<Getoption@>=
sflag = 0;
interactive=0;
while ((ch = getopt(argc, argv, "is:d:")) != -1) {
switch (ch) {
case 'i':@/
interactive = 1;@/
break;@/
case 's':
if ((fp = fopen(optarg, "r")) == NULL) {
fprintf(stderr,"%s: %s: %s\n",programme, optarg, strerror(errno));
status = OPEN_FILE;
}
sflag = 1;
strcpy(filename,optarg); /* for later used */
break;
case 'd':
if ((fp = fopen(optarg, "r")) == NULL) {
fprintf(stderr,"%s: %s: %s\n",programme, optarg, strerror(errno));
status = OPEN_FILE;
}
strcpy(filename,optarg); /* for later used */
break;
case '?':@/
default:@/
fprintf(stderr,"usage: %s -[[i]s|[i]d] data-filename\n%s\n", programme,
strerror(errno));
status = USAGE;
break;
}
}
@ This part of task is to read in x,y data from filename,
doing gnuplot via forking the process to show the graph.
System funtion
wait() is needed in order to bring back the task to main() and
end with status from main.

@<Do calculation ...@>=
@<Reads in input@>@/
@<Acquire rooms for x and y@>@/
@<Scan input file for x and y@>@/
@<Real calculation for variable in question@>@/
@<Fork and wait@>


@ Reads in input consisting of two parts, number of interval
has to be read in first.
Later on, continue reading in data
into |double *x, *y| after we prepare rooms for them.
@<Reads in input@>=
fscanf(fp,"%d\n", &interval);


@ Acquire rooms for |double *x, *y| need to check for status and so
a place for saved status is needed here.
We use ch for this purpose.
@<Acquire rooms for x and y@>=
/* ch = status; saved status, i havefound that it is unnecessary to save */
if((x = (float *)malloc(interval*sizeof(float))) == NULL) {
free((char *)filename);@/
fclose(fp);@/
status = NO_ROOM;@/
}@/
if((y = (float *)malloc(interval*sizeof(float))) == NULL) {
free((char *)filename);@/
free((float *)x);@/
fclose(fp);@/
status = NO_ROOM;@/
}@/
if(status != OK)@/ /* if error occur in any case we stop here */
exit(status);@/
/* status = ch; see above for reason to remove.
restore saved status */


@ Scan input data into array of x and y.
@<Scan input file for x and y@>=
px = x;@/
py = y;@/
for(ch = 0; ch < interval; ch++)@/
fscanf(fp,"%f,%f\n", px++,py++);


@* Real calculation.
@<Real calculation for variable in question@>=
fclose(fp);@/
if ((fp = fopen("jotawski","w")) == NULL) {@/
status = OPEN_FILE;@/
fprintf(stderr,"open file probelm\n");@/
exit(status);@/
}@/
if(sflag) {@/ /* do calcuate speed from time and distant */
printf("speed");@/
for (ch=0, *px=0.0; ch<interval-1;ch++){
time = (*(x+ch+1)) - (*(x+ch));
distant = (*(y+ch+1)) - (*(y+ch));
speed = distant / time;
*px += speed;
fprintf(fp,"%f %f\n",*(x+ch), speed);
}
printf(" average : %f\n", *px/(interval-1));
/* number of interval used are interval - 1
* number of time, distant pairs are also interval -1
* speed(i) i=0,1,2,...,interval-1
* time(i) are to be calculated and print into 'jotawski' for plotting
* average speed is shown as label.
*/
fclose(fp);@/
CreateGnuPlot("Speed", fp, interactive);@/
} else {@/
printf("distant: opening file for output.\n");@/
for (ch=0; ch<interval; ch++){@/
time = *(x+ch);@/
speed = *(y+ch);@/
distant = speed * time;@/
fprintf(fp,"%f %f\n",time,distant);@/
}@/
fclose(fp);@/
CreateGnuPlot("Distant", fp, interactive);@/
}@/


@* Process control.
This program utilizes process control, via |fork()| and |wait()|,
instead of a single once through and die program normally written by others.


@ For wait() to be called properly sys/types.h and sys/wait.h must
be included.
@<Header ...@>=
#include <sys/types.h>
#include <sys/wait.h>


@ |fork()| and |wait()| are also now available.
|jotawski.cmd| is a set of gnuplot
commands.
An example is

set terminal png nocrop medium

set xrange [*x:*(x+interval-1)]

plot y

@<Fork and wait@>=
if((fork()) == 0) {@/ /* child process */
if (interactive)@/ /* load command inside gnuplot */
execlp("/usr/local/bin/gnuplot","/usr/local/bin/gnuplot",(char *)0);@/
else@/
execlp("/usr/local/bin/gnuplot","/usr/local/bin/gnuplot","jotawski.cmd",(char *)0);@/
} else {@/ /* parent, this programme */
wait(&sflag);@/
}


@* |gnuplot()| function.
Subroutine or funtion to create gnuplot script.
@<GNUPlot Script create@>=
void CreateGnuPlot(char *yasuko, FILE *fp, int interactive)@/
{
fp = fopen("jotawski.cmd","w");@/
fprintf(fp,"set terminal png nocrop medium\n");@/
if(!interactive)@/
fprintf(fp,"set output 'jotawski.png'\n");@/
fprintf(fp,"set key top left\nset key box\n");@/
fprintf(fp,"set ylabel \"%s\" center\n", yasuko);@/
fprintf(fp,"set xlabel \"time\"\n");@/
fprintf(fp,"set style data linespoints\n");@/
fprintf(fp,"set title \"SGM 6m plot 1 dimension\"\n");@/
fprintf(fp,"plot 'jotawski' title \"%s\"\n", yasuko);@/
fclose(fp);@/
}@/
@* Cleanup task.
A must for every programmes.
@<Clean...@>=
free((char *)filename);@/
free((double *)x);@/
free((double *)y);
/* fclose(fp); this has been closed already*/


@ Now that |main()| gain control back from child and die here afterward.


@ The final remaining loose end is to extract and compile programme written
from this file and get executable computer code,\,{\it{sgm-6m-prog-1d-v1\/}},
which is quite simple from four commands below.

The first two lines for executable commputer code and the last two
lines for producing a pretty print output of this manual,\,{\it sgm-6m-prog-1d-v1r0.pdf}.

\% ctangle sgm-6m-prog-1d-v1r0.w

\% cc -o sgm-6m-prog-1d-v1r0 sgm-6m-prog-1d-v1r0.c -lc

\% cweave sgm-6m-prog-1d-v1r0.w

\% pdftex sgm-6m-prog-1d-v1r0.tex


@* System requirement.
This \.{CWEB} programme, \.{sgm-6m-prog-1d-v1r0.w},
is developed on FreeBSD and has copyrighted in BSD style.
As in the first
submitted report, if one of us were interested in FreeBSD, here is some descriptions over
this operating system.



FreeBSD wmc.tint.or.th 8.0-RELEASE FreeBSD 8.0-RELEASE
\#2: Mon Apr 12 15:08:05 ICT 2010

root@@wmc.tint.or.th:/usr/obj/usr/src/sys/HARIPOONCHAI i386


@ This program require x--window system as graphic displaying tools
and gnuplot, a general purposes plotting utilities from GNU project,
for helping in displaying cute graphs and c compiler to compile
source code.
Project management and editing tools like leo is
heavily used in this task.
The unix--like operating system
such as FreeBSD is used to host the whole story finally.

All softwares mentioned above are free and are bundled with FreeBSD,
one need to even manually install x--window and gnuplot from ports tree though.
And
once they are in place, they are availble forever for every one.


@ Installation of operating system and applications are beyond the scope of this report.
But if anyone are interested in such a task, please feel free to contact to the author or
directly go to http://www.FreeBSD.ORG
@* Notes from the author.
This task is an inspiration of
the deputy secretary general of TINT about the computer programming
for the institute, he has foreseen that there is no development
in this field when the institute has been born since BE 2549.
As mention in
the first report that this kind of task can be used in managment of
radioactive liquid wastes treatment as well.
On the author look, the most
application is to calculate the amount of air flow from any stack in the
direction of wind, which in turn, the exposure rate associated with
polluted air can be assess, even very roughly though.

It is likely that a few computer interest groups for such a particular task as
safety assessments, in house development and so on will be formed
in the near future.
This will helps TINT comes up with some innovations
and is capable of competetion with other countries.
@* Index.

โดยเนื้อแท้แล้ว มึน กับงานนี้มากเลย มาสั่งให้เขียนโปรแกรม v = s/t แค่นี้ ยังนึกไม่ออก จะเอาไปทำอะไร เลยบ่นออกมาใน
@* Notes from the author นั่นแหละ

แต่เอาเถอะ หวังว่า ที่คาดหวังไว้ จะเป็นจริงได้ ก็พอแล้ว
i wishes that my dreams become true.

Tuesday, April 20, 2010

โปรแกรม เรื่องสั้น (ต่อ)


ยกมาที่นี่เลยก็แล้วกันน่ะ ก็จาก โน่น แหละ

งงงมั้ย เรื่องมันสั้น แล้วดันมา ต่อ เนี่ย

ก็งี้แหละ ชอบทำอะไรให้มันสับสนวกวนซะงั้น แต่อืมม จะว่าไปแล้ว ก็ไม่ถูกต้องนัก เพราะในชีวิตจริง มันก็ยาก ที่จะหาอะไรที่ ตรงไปตรงมา กันได้ง่ายๆ มักจะวกวน เลี้ยวลดไปแทบทั้งนั้น

ต้นฉบับ มอบให้น้องแขก เธอไปเมื่อครู่นี้เอง

ก่อนหน้านี้ คุยกับ สุสักไปแล้วเหมือนกัน ถึงเรื่อง km ที่ พี่สมพร ได้เคยพาดพิงถึงงานเขียนโปรแกรมเชิง literate programming ไว้ว่า เอาไปลง stkc อะไรทำนองนี้ แต่ต่อเรื่องราวกันไม่ติดซะแล้ว เพราะพูดกันไว้นานพอควร ราวๆ (กพ. นี่เอง )มีค ต่อ เมย นี่แหละ

ไหนไหนก็ไหนไหน wherewhere is a wherewhere เอามาเผยแผ่กันตรงนี้ซะเลย ประเดี๋ยวแฟนๆจะว่ามีแต่ราคาคุย ไม่เห็นทำให้ดู
% sgm-6m-prog-1d: An example of Literate programming to solve one dimensional flow by
% pirat sriyotha
\nocon % omit table of contents
\datethis % print date on listing
\def\TINT{TINT\-\kern.1em bang khen}

@* A showdown of {\tt Literate programming}. This programme is to
demonstrate problem solving with literate programming in \CEE/.

@ The purpose of \.{sgm-6m-prog-1d} is to calculate the velocity when other two components,
distance and time, are given and vise versa. All variables are for one dimensional water flow.
The results are stored in plain text files and may be used for other calculations and sure for
producing beautiful graphic too.

@ The assignment task is very broad by itself. For one dimensional flow, any thing that flows
will fit into this case. And since one dimension is part of many dimensions, any
solution can be applicable to this sort of problem too, by wisely choose an appropriate
situations. Further more, any flow (or moving) can be considered as one dimension
flow (or moving) as well. This includes from neutrons diffusion in nuclear reactor up to
flying kite, and rocket, in the sky and spaceship at outer spaces too.

This kind of problem seems to be useful in management of radioactive liquid waste. By measuring
liquid wastes flow out of the pipe at a discharge point, one can assess the amount of
radionuclides released into the environment. Additionally, the value of flow rate can be used in
controlling of treatment of liquid radioactive waste in particular. But there is no mention about
this assumption when this task has been assigned.

This resulted in commotion of literature searching or survey and manually filtering of unwanted
documents are unavoidable and more fun is that the most useful references are from oaep
library, Thai--AEC 31 and Thai--AEC 45. On the other hand, a number of solutions are also
found around the internet. Even the reinventing the wheel is to be avoided though, the manager
insists in writing codes by ourselves. Alas.

@ To complicate the story further, displaying in graphic mode has to be done
and a number of unknown constants are still in the mists. Fortunately, with some tools
from X-windows, {\tt xdpyinfo}, one of the desired constant, dot per inch of my machine,
is revealed. And since this is really a rather specific problem, it is no risk
to do a more specific problem solving with hardware depending constants
in a sense of independent from specific hardware.

@ To make the long story short, the deputy director of the \TINT\, gives the final word
by the end of one evening, simply do a bare bone programming for one dimensional flow,
$$v = {s \over t\/},$$ given velocity one check for distant travel during time interval
and that's all, no more investigation, and start coding.

@* Let's come down to an earth. By my design, the problem has two version. For the first quick
smart and sweet version, only calculation is performed but postpone displaying for later time.
The reasons behind this is very simple, there are a number of tools in plotting beautiful graph
for presentation in the net. And the second version is all-in-one which is self explanation.
So for this particular moment, only the first version is presented.

This programme has been develop on FreeBSD which is a blue--blood \UNIX/ operating system
by \.{CWEB} programme. Some informations about FreeBSD is as follow:-

FreeBSD maifa.homeunix.org 8.0-RELEASE FreeBSD 8.0-RELEASE \#1: Sun Apr 4 18:58:42 ICT 2010
root@@:/usr/obj/usr/src/sys/HARIPOONCHAI i386

@ Most \.{Literate programmings} share a common structure. It's probably a
good idea to state the overall structure explicitly at the outset, even though
the various parts could all be introduced in unnamed sections of the code
if we wanted to add them piecemeal, quoted from wc.w--a well known example of
literate programming.

Here, then, is an overview of the file \.{sgm-6m-prog-1d.c} that is defined
by this \.{Literate programming} \.{sgm-6m-prog-1d.w}:

@c
/*-
* Copyright (c) 2010 Pirat Sriyotha aka jotawski
* All rights reserved.
*
* to be added meat of license
*/
@<Global variables@>@/
@<Header files to include@>@/
@<The main program@>

@ We must include the standard I/O definitions, since we want to send
formatted output to |stdout| and |stderr|.

@<Header files...@>=
#include <stdio.h>
#include <stdlib.h>

@ The |status| variable will tell the operating system if the run was
successful or not, and |prog_name| is used in case there's an error message to
be printed.

@d OK 0 /* |status| code for successful run */
@d cannot_open_file 1 /* |status| code for file access error */
@d no_room 2 /* |status| code for memory in sufficient */

@<Global variables@>=
int status=OK; /* exit status of command, initially |OK| */
char *prog_name; /* who we are */
char *outputfile; /* contain data for later plotting */

@ Now we come to the general layout of the |main| function.

@<The main...@>=
main (argc,argv)
int argc; /* the number of arguments on the \UNIX/ command line */
char **argv; /* the arguments themselves, an array of strings */
{
@<Variables local to |main|@>@;
prog_name=argv[0];
@<Set up for taking input@>;
@<Open file for output@>;
@<Process the calculation@>;
exit(status);
}

@ Variable local to main programmes are quite simple one,
for example velocity, distance, time, file pointer and so on
@<Var...@>=
double velocity; /* velocity, input data */
double distance; /* distant travel */
double time; /* time interval */
FILE *fd; /* file pointer */

@ Now that one must give |velocity|, as a floating point number, to the program.
Without any number given, this programme is of no value. This is the {\bf{ONLY
ONE INPUT}} to the program.
@<Set up...@>=
printf("\ngive me the value of velocity in units of meter per second please\n\n");
scanf("%lf", &velocity); /* {\bf{NEED THIS}} */

@ Here's the code to open the file. {\bf{You can change the name of file to whatever
you want}}, but for this moment, ``|jotawski_1d|'' is used. No particular reason
for this, but I like the name.

@<Open file for...@>=
if ((outputfile = (char *)malloc(20)) == NULL) {
fprintf(stderr, "sorry no room left for a single byte\n");
status = no_room;
} else
sprintf(outputfile,"%s","jotawski_1d"); /* {\bf{change |jotawski_1d| to suit your need}} */
if ((fd=fopen(outputfile,"w")) == NULL) {
fprintf (stderr, "%s: cannot open file %s\n", prog_name, outputfile);
status=cannot_open_file;
}

@ Now we process the calculation by assuming that a 100 intervals of 10 seconds
will be calculated and printed out into output file. My instinct tell me that
1000 seconds is the most suitable duration for testing.

@<Process...@>=
@<Initialize output file header for ease of reading@>;
@<Print table@>;
@<Close file@>;

@ The most important one is to close all files opened, |FILE *fd|, and freed
all memories, |char *outfile|, back to operating system
or you will face a few weird problems otherwise. Just to clean up.

@<Close file@>=
fclose(fd);
free((char *)outputfile);

@ Header of output file for ease of reading and back checking.

@<Init...@>=
fprintf(fd,"plotting of distance and time at velocity %lf meters per second", velocity);
fprintf(fd,"\ntime distant in meters\n\n");

@ We need this counter for looping around. Actually, 1000 seconds are needed
but since it starts with number 0, zero, so a hundred and one intervals
is used instead. Once again, this is just a number, any integer number.

@<Var...@>=
int counter; /* time interval as stated above, just only 1010 seconds */

@ And finally and the meat of this project: output your calculation.

@<Print table@>=
for ( counter = 0; counter < 1010 ; counter += 10) {
distance = velocity * counter;
fprintf(fd,"%4d %lf\n", counter, distance);
}
printf("\nThanks for your time and have a look at %s for output\n", outputfile);

@ The final remaining loose end is to extract and compile programme written from this
file and get executable computer code,\,{\it{sgm-6m-1d\/}}, which is quite simple from
four commands below, the first two lines for this purpose and the last two lines for
producing a pretty print output of this manual,\,{\it{~sgm-6m-1d.pdf\/}}.

\% ctangle sgm-6m-1d.w

\% cc -o sgm-6m-1d sgm-6m-1d.c -lc

\% cweave sgm-6m-1d.w

\% pdftex sgm-6m-1d.tex


@* Index.
Here is a list of the identifiers used, and where they appear. Underlined
entries indicate the place of definition. Error messages are also shown.


เตรียมด้วย vi น่ะ
ไม่รู้ cut & paste แล้วจะรักษาสภาพเดิมไว้ได้รึเปล่า มันมีผลครับ มันมีผล เพราะ TeX เขาจะบ่นเอาง่ายๆ

โห ลำบากเลย แต่ไม่เป็นไร เพื่อแฟนๆ มะไฟ ทนได้ ทำให้ได้ ก็พวกเครื่องหมาย น้อยกว่า มากกว่า นั่นแหละ ที่ editor เขาตัดออกไปเลย เลยต้องใช้ วิชาแมว มาอีดิดเอา

ยังไม่ได้ตรวจสอบน่ะครับว่า ที่อยู่ในกรอบเทาๆน่ะ ผิดพลาดคลาดเคลื่อนอะไรไปอีกมั้ย ยังไม่ตรวจครับ ยังไม่ตรวจทานกับต้นฉบับ ยังไงจะพยายามใช้ html tag <pre> ดู ลองดูแล้ว ไม่ได้ครับ ดูไม่สวยเลย สำหรับ tag ที่ว่า

จากต้นฉบับนี้ ท่านสั่ง

# cweave sgm-6m-prog-1d.w
# pdftex sgm-6m-prog-1d.tex

เท่านี้ ท่านก็ได้เอกสาร .pdf สวยๆ มาอ่านแล้วหละ น่ะ ส่วนตัวโปรแกรมทีเป้นแฟ้ม sgm-6m-prog-1d.c นั้น ก็สั่งง่ายๆ

# ctangle sgm-6m-prog-1d.w

ก็ได้มาแล้ว จากนั้น ก็ compile รวดเดียวเลย

# cc -o sgm-6m-prog-1d sgm-6m-prog-1d.c -lc

ก็จบงานกัน ได้โปรแกรมมาใช้งานแล้ว งานจริง เริ่มต่อจากตรงนี้ครับ และใช้ leo สิงห์โต ซดต้มยำ อะไรอย่างว่านั้นแหละ เตรียมเอา

อย่าคิดว่า มะไฟ อวดโง่ เขียนด้วยภาษาอังกฤษน่ะ TeX มันยังไม่รู้จักภาษาไทยน่ะ ไม่มีเหตุผลอะไรมากหรอก ที่อวดๆน่ะ เพราะมันจำเป็น

Monday, December 21, 2009

FreeBSD: Leo ---> programmer's editor

เด็กๆ ที่ไม่ใช่ เด็ก หรือ DEK ในภาคภาษาต่างประเทศ

แต่ในต่างประเทศ DEK ที่ไม่ใช่เด็กๆในบ้านเรานั้น มีจริง Donald E. Knuth ครับ เจ้าพ่อทางคอมพิวเตอร์

Donald E. Knuth ผู้ที่เขียนตำราคอมพิวเตอร์ แล้วไม่พอใจผลงานงานพิมพ์ จนกระทั่ง มาเขียนโปรแกรม จัดพิมพ์เอกสาร ด้วยคอมพิวเตอร์ ซะเอง แล้วจึงหันไปเขียนหนังสือต่อนั่นแหละครับ

ผลงาน การจัดพิมพ์เอกสาร ด้วยคอมพิวเตอร์ ของเขา ก็นี่แหละครับ TeX ซึ่งผู้ที่คลุกกับคอมฯมาพอควร หรือทำงานเกี่ยวกับ computer typesetting น่าจะรูจักดี และ เขาเขียนขึ้นในลักษณะของ web หรือข่ายใยแมงมุม

ส่วนตำราที่เขาเขียนขึ้นนั้น ก็ The Art of Computer Programming นั่นแหละ

งานพิมพ์ด้วยคอมพิวเตอร์ทุกชนิดนั้น TeX จัดการได้หมด ลองหาหนังสือที่เขาเขียนมาอ่านดูซี ไม่ได้ค่าคอมมิดฉันอะไรหรอก เป็นแต่อยากแนะนำของดี ให้เพื่อนๆได้รู้ไว้ ก็ในตำราเล่มนี้แล ที่ ทุกบท เขาจะมี ตัวการ์ตูน มาแสดงสาระสั้นๆของเรื่องเอาไว้ ตัวการ์ตูน ตัวนี้ก็ สิงห์โต หรือ leo นั่นแหละ

น่ารักมาก

ในตำราที่เขียนขึ้นนั้น Literate Programming ก็เป็นหนึ่งในหลายๆเรื่องเด่นของเขา

ส่วน สิงห์โต หรือ leo ในนี้นั้น เป็น editor ที่เขียนขึ้นในแบบ หรือ สไตล์ ของ Literate Programming ซึ่งทำให้การเขียนโปรแกรมง่ายขึ้นมาก นอกจากนี้ ยังสามารถใช้งานเก็บข้อมูลงานส่วนตัวได้ด้วย ลองไปหามาเล่นดูได้น่ะ ใน ports นั่นแหละ อ้อ เขามี ติว ให้ด้วย ไม่ต้องห่วงเรื่องที่ว่าจะใช้ไม่เป็น


จะว่าไปแล้วก็ไม่มีอะไรใหม่ ในเรื่องของ TeX น่ะ นอกจากแค่ เพิ่ม c mode ขึ้นมาเท่านั้นเอง

Wednesday, November 11, 2009

literate programming

จากความจำเป็นที่ให้เขียนโปรแกรม เลยต้องใช้บริการของ literate programming บ้าง
คนเขียนโปรแกรม มักขี้เกียจเขียน users manual หรือ programme descriptions ต่างๆ ซึ่งความยุ่งยากอันนี้เอง ที่ DEK กับคณะ ได้พบมาแล้ว ทราบดีถึงความยุ่งยากใจของ programmers จึงได้เกิดงาน literate programming ขึ้นมา

ก็ เขียนไป ทั้ง source code และคู่มือพร้อมๆกัน ที่ซึ่งเมื่อเขียนจบ ก็ได้ทั้ง ตัวโปรแกรม และ คู่มือ มาใช้งาน ได้เลย


ใน ports ครับ ในหมวด devel ชื่อ cweb มีคู่มือเป็น pdf สวยสวยให้อ่านด้วย
ข้อจำกัดคือ มีให้จำเพาะภาษา C เท่านั้นเอง

บางส่วน จาก Introduction ของเอกสารคู่มือที่ให้มา

\section Introduction.
The philosophy behind \.{CWEB} is
that programmers who want to provide the best
possible documentation for their programs need two things
simultaneously: a language like \TEX/ for formatting, and a language like
\CEE/ for programming. Neither type of language can provide the
best documentation by itself. But when both are appropriately combined, we
obtain a system that is much more useful than either language separately.

ที่เมื่อทำเป็น .pdf แล้ว ก็ ประมาณนี้ ในรูปที่เห็นนี้แหละ



ลองดูน่ะพี่น้อง

View My Stats