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 มันยังไม่รู้จักภาษาไทยน่ะ ไม่มีเหตุผลอะไรมากหรอก ที่อวดๆน่ะ เพราะมันจำเป็น

No comments:


View My Stats