Monday, October 28, 2013

TeX และ ตาราง

มูลเหตุ ที่ต้องทำเอกสารขึ้นมา เพราะ ขี้เกียจทำกับมือ ซึ่ง งานแบบนี้แหละ คอมพิวเตอร์ถนัดนัก ก็แค่ตารางบวกเลข 0-9 ที่จับมาสุ่มบวกกันเองเข้า เท่านั้นแหละ  รวมแล้วก็ ๑๐๐ คู่ที่จับมาบวกกัน แล้วก็ สุ่มจับมาด้วย ซึ่งอันหลังนี้เองที่วุ่นวายหากทำกับมือ

แก้ปัญหาสุ่มจับมาด้วยโปรแกรมภาษา C ที่เขียนมา, วิ่งผ่าน cygwin บน windows XP, แล้วพิมพ์ออกมา ซึ่งแรกๆนั้นกลับ copy & paste มาลงใน template ของ MikTeX

ทำเองสองสามครั้งเข้า ชักขี้เกียจและฉลาดขึ้น เลยแก้ตัวโปรแกรมภาษา C นั้น ให้พิมพ์ส่วนที่ MikTeX ต้องการใช้มาด้วยซะเลยทีเดียว งวดนี้ พอ cut แล้วนำมา paste ให้ถูกช่อง ก็ได้ตารางออกมา ตารางที่ generate ออกมายังไงก็ไม่มีเหมือนกัน รับประกันว่า เด็กๆขืนลอกกันเองเข้าก็ทำผิดเท่านั้นเอง

ข้างล่างนี้คือ template สำหรับใช้งานด้วย TeX ผ่าน MikTeX และตรงที่ต้องนำมา paste นั้นก็ high light เอาไว้ให้แล้วด้วย คงพอดูออก

% !TEX TS-program = pdflatex
% !TEX encoding = TIS-620 Unicode


\documentclass{article} % use larger type & default would be 10pt

\usepackage[thai]{babel}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}


%%% PAGE DIMENSIONS
\usepackage{geometry} % to change the page dimensions
\geometry{a4paper} % or letterpaper (US) or a5paper or....

\usepackage{graphicx} % support the \includegraphics command and options

%\usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent

%%% PACKAGES
\usepackage{booktabs} % for much better looking tables
\usepackage{array} % for better arrays (eg matrices) in maths
\usepackage{paralist} % very flexible &customisable lists (eg. enumerate/itemize, etc.)
\usepackage{verbatim} % adds environment for commenting out blocks of text &for better verbatim
\usepackage{subfig} % make it possible to include more than one captioned figure/table in a single float
% These packages are all incorporated in the memoir class to one degree or another...

%%% HEADERS & FOOTERS
\usepackage{fancyhdr} % This should be set AFTER setting up the page geometry
\pagestyle{fancy} % options: empty , plain , fancy
\renewcommand{\headrulewidth}{0pt} % customise the layout...
\lhead{}\chead{}\rhead{}
\lfoot{}\cfoot{\thepage}\rfoot{}

%%% SECTION TITLE APPEARANCE
\usepackage{sectsty}
\allsectionsfont{\sffamily\mdseries\upshape} % (See the fntguide.pdf for font help)
% (This matches ConTeXt defaults)

%%% ToC (table of contents) APPEARANCE
\usepackage[nottoc,notlof,notlot]{tocbibind} % Put the bibliography in the ToC
\usepackage[titles,subfigure]{tocloft} % Alter the style of the Table of Contents
\renewcommand{\cftsecfont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftsecpagefont}{\rmfamily\mdseries\upshape} % No bold!

%%% END Article customizations

%%% The "real" document content comes below...
\title{ตารางบวกเลข}
\author{ลุงตึ๋ง}
%\date{} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed 
\date{16 ตุลาคม พ.ศ. 2556}
\begin{document}
\maketitle

\section{ตารางรวมเลข} ตัวเลข 0 -- 9 สุ่มมาผลิตเป็นตารางสำหรับทดสอบความถูกต้อง และ ความเร็วในการบวกเลข
%% ตรงนี้ สำหรับนำมา paste
\tableofcontents
\end{document}

จบเท่านี้

ใครที่คิดว่าบวกเลขหลักเดียว 0-9 นั้นง่าย ไม่เถียงดอก แต่ขอได้ไหมว่า 
  1. อย่าผิดแม้แต่ข้อเดียว
  2. ทำให้เร็วที่สุด น้อยกว่า ๓๐๐ วินาฑีได้ ยิ่งดี เพราะเด็กที่ฉลาดและเก่งนั้น พอนับ หนึ่ง สอง พวกก็ตอบออกมาแล้ว ก็ราวๆ ๓๐๐ วินาฑีเมื่อรวมทุกข้อ
ขอแค่ ๒ อย่างนี้เท่านั้นแหละ
งวดหน้า จะสั่งให้โปรแกรม เขียนเอกสารนี้ออกมาเองทั้งหมดเลย โดยไม่ต้อง cut & paste เอา

ข้างล่างนี้คือเอกสารที่พิมพ์ออกทางกระดาษ A4 หลังจากที่ MikTeX process แล้วได้แฟ้ม .pdf ออกมา แล้วมอบให้เด็ก ทดสอบความสามารถของเขาเอง
แบบทดสอบการบวกเลข

ตั้งใจจำทำให้เพื่อน freshy วิดยา 14 ที่จากไปเมื่อต้นเดือนนี้ แต่ก็ไม่ทัน ถึงอย่างนั้นก็ขออุทิศความดีอันนี้ให้กับเพื่อนที่จากไป ส่วนข้อผิดพลาดขาดตกบกพร่องอันใด มะไฟ ขอน้อมรับไว้แต่ผู้เดียว

สำหรับตัวโปรแกรมภาษา C นั้นต้องแก้ entities ให้ html เขาพิมพ์ออกมาให้ถูกต้องไวยากรณ์ของภาษา C ที่ออกจะวุ่นวายอยู่ ยังไงหากสงสัย หรือ เห็นว่าคลาดเคลื่อนประการใด ถามมาได้ครับ ยินดีตอบเสมอ ข้างล่างนี้ครับ ตัวโปรแกรม

เริ่ม

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static unsigned long kseed=1;
time_t *mtp;

main()
{
int *left, k, r,j, tmp;
int *right,l,m,n;

left = (int *)malloc(10*sizeof(int));
right = (int *)malloc(10*sizeof(int));
for(k=0;k<10;k++) {
*(left+k)=k;
*(right+k)=k;
}

/*
 * I got this code from forums.freebsd.org http://forums.freebsd.org/showthread.php?p=236648#post236648
 */

srand((unsigned int)time(0));
for(k=0;k<10;k++){
r=rand()%10;
tmp = left[k];
left[k] = left[r];
left[r] = tmp;

/*
 * srand() and rand() here make somewhat good result
 */

srand((unsigned int)time(0));
r=rand() % 10;
}
srand((unsigned int)time(mtp));
for(k=0;k<10;k++){
l=rand()%10;
tmp = right[k];
right[k] = right[l];
right[l] = tmp;
}
printf("\n\\par");
for(n=0;n<10;n++)
printf(" %d ",*(left+n));
printf("\n\\par");
for(n=0;n<10;n++)
printf(" %d ",*(right+n));
printf("\n\n\\par Start at\\space\\space\\space:\\space\\space\\space:\\space\\space\\space\\par \n");
printf("\\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|}\n\\hline\n");
for(l=0;l<10;l++) {
for(m=0;m<10;m++)
printf("%d+%d=\\space\\space\\space%c",*(right+l),*(left+m),(m<9)? '&' : ' ');
printf("\\");
printf("\\\n");
}
printf("\n\\hline\n\\end{tabular}");
printf("\n\n\\par Stop at\\space\\space\\space:\\space\\space\\space:\\space\\space\\space\\par \n");
free((int *)left);
free((int *)right);
}

จบเท่านี้ครับสำหรับ code ส่วนที่ว่ามันดูเปรอะไปนั้น ขออภัยจริงๆครับ

ขอบคุณ ที่สละเวลามาอ่าน

2 comments:

dekdar said...

ขอบคุณมากครับลุง กับบทความ ได้ทดลองเจ้านัจมีย์บางแล่ะ อิอิ

มะขาม said...

ด้วยความยินดีครับ
แต่เจ้า "นัจมีย์" นี่คืออะไรหรือครับ


View My Stats