![]() ![]() | ![]() |
|
ปรับปรุง : 2564-07-13 (ปรับแฟ้ม) |
| datatype | TurboC | Programming |
ผู้สอน : อ.อติชาต หาญชาญชัย ผู้สอน : อ.บุรินทร์ รุจจนพันธุ์ MSDN of C++ : Output File Stream function Download Site : Borland C++ 5.5 ! . To Install BCC55 #1, #2, #3 # Old Compiler : Turbo C++ 3.0 . . # แนวคิดการสอนเขียนโปรแกรม
|
ความหมายของภาษาซี
ภาษาซี คือ ภาษาคอมพิวเตอร์ใช้สำหรับพัฒนาโปรแกรมทั่วไป ถูกพัฒนาโดยเดนนิส ริสชี่ (Dennis Ritchie) เมื่อประมาณต้นปีค.ศ. 1970 เพื่อใช้งานบนระบบปฏิบัติการยูนิกส์ ต่อมาถูกนำไปใช้ในระบบปฏิบัติการต่าง ๆ จนถูกใช้เป็นภาษาพื้นฐานสำหรับภาษาอื่น เช่น ภาษาจาวา (Java) ภาษาพีเอชพี (PHP) ภาษาซีชาร์ป (C#) ภาษาซีพลัสพลัส (C++) ภาษาเพิร์ล (Perl) ภาษาไพทอล (Python) หรือภาษารูบี้ (Ruby) Dev-C++ 5.11 + https://sourceforge.net/projects/orwelldevcpp/ + http://orwelldevcpp.blogspot.com/
ตัวอย่างรหัสต้นฉบับสำหรับ Turbo C++ 3.0 #include <stdio.h> void main() { printf("hello"); getchar(); } |
|
Turbo C++ 3.0
DOS> tcc x.cpp
DOS> x
// AntiVir : DOS/Candy DOS virus
// if this code have code: name:
#include <stdio.h> // printf, gets
#include <conio.h> // getch
void main(){
char *code,*name,*address;
printf("code : "); gets(code);
printf("name : "); gets(name);
printf("address : "); gets(address);
printf("%s %s %s \n",code,name,address);
getch();
}
Visual Studio 8
http://msdn.microsoft.com/en-us/library/59zy697d(VS.80).aspx
start : Visual Studio 2005 Command Prompt
DOS> cl cl0101.cpp
DOS> cl /clr cl0101.cpp
- ถ้า #include <iostream> ต้องใช้ /clr
- /clr ทำให้มีการเรียกใช้ Incremental Linker
- ให้ใช้ getchar() แทน getch()
- ใช้ getchar() จะต้อง #include <stdio.h>
- ใช้ system("PAUSE") จะต้อง #include <stdlib.h>
- ถ้าใช้ cout ต้อง
#include <iostream> และ using namespace std; // for cout
จึงจะใช้ cout << "a" << endl;
แต่ถ้า #include <iostream> อย่างเดียว
สามารถใช้ std::cout << "a";
- จะใช้ system() ต้อง #include <iostream>
- ใช้ scanf หรือ gets อาจพบ warning
ให้ใช้ DOS>cl /clr x.cpp /D_CRT_SECURE_NO_DEPRECATE
|
| ตัวแปลภาษา (Compiler) BCC 5.5.1
+ http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/ Download Site : Borland C++ 5.5 9 MB . . Configuration สำหรับ Compile ใน Editplus เมื่อใช้ BCC 5.5.1 - ให้กำหนด Command : C:\Borland\BCC55\Bin\bcc32.exe - ให้กำหนด Argument : -IC:\Borland\BCC55\Include -LC:\Borland\BCC55\Lib -n$(FileDir) $(FilePath) - ให้กำหนด Initial directory : C:\Borland\BCC55\Bin - ให้ click ที่ Capture Output Configuration สำหรับ Run ใน Editplus เมื่อใช้ BCC 5.5.1 (กรณีไม่รับค่าจากแป้นพิมพ์) - ให้กำหนด Command : $(FileNameNoExt) - ให้กำหนด Initial directory : $(FileDir) - ให้ click ที่ Capture Output มีปัญหาเรื่องการ compile หรือ run - เพื่อนท่านหนึ่งโทรมาถามว่า run ใน Command line ไม่มีปัญหา แต่พบปัญหาการหยุดรับค่าจากแป้นพิมพ์ เมื่อใช้ EditPlus - ให้กำหนด Command เป็น "c:\windows\system32\cmd.exe" "/k" - ให้กำหนด Argument เป็น $(FileNameNoExt) หรือ E:\KEEPSRC\$(FileNameNoExt) - ไม่ click ที่ Capture Output เพื่อไม่ให้ run ใน Output window - ถ้าต้องการ Compile ผ่าน Command Line ควรสร้างแฟ้ม bcc32.cfg ในห้อง bin แล้วเพิ่ม 2 บรรทัดนี้ลงไป
|
แปลโปรแกรมที่มี main จะได้แฟ้ม .obj และ .exe และ .tds
#include <stdio.h>
#include <conio.h>
main(void) {
clrscr(); // use conio.h
printf("hello"); // use stdio.h
}
|
|
โปรแกรม adtqueue.cpp ไม่ต้องแปล ถูก include ได้เลย ถ้าพยายามแปลจะพบ error
Error: Unresolved external '_main' referenced from C:\BORLAND\BCC55\LIB\C0X32.OBJ typedef struct node {
void* dataPtr;
struct node* next;
} QUEUE_NODE;
typedef struct {
QUEUE_NODE* front;
QUEUE_NODE* rear;
int count;
} QUEUE;
QUEUE* createQueue (void);
QUEUE* destroyQueue (QUEUE* queue);
|
|
โปรแกรมจัดการ Queue (มีเพียง createQueue กับ destroyQueue)
+ http://www.dreamincode.net/forums/showtopic49439.htm + http://www.thaiall.com/datastructure/queue.htm |
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include "adtqueue.cpp"
void printQueue(QUEUE* queue);
main(void) {
clrscr(); // use conio.h
printf("hello"); // use stdio.h
QUEUE* queue1;
queue1 = createQueue();
QUEUE* queue2 = createQueue();
printQueue(queue1);
destroyQueue (queue1);
destroyQueue (queue2);
}
//
QUEUE* createQueue (void) {
QUEUE* queue;
queue = (QUEUE*) malloc (sizeof (QUEUE)); // malloc use stdlib.h
if (queue) {
queue->front = NULL;
queue->rear = NULL;
queue->count = 0;
}
return queue;
}
//
QUEUE* destroyQueue (QUEUE* queue) {
QUEUE_NODE* deletePtr;
if (queue) {
while (queue->front != NULL) {
free (queue->front->dataPtr);
deletePtr = queue->front;
queue->front = queue->front->next;
free (deletePtr);
}
free (queue);
}
return NULL;
}
//
void printQueue(QUEUE* queue){
QUEUE_NODE* node = queue->front;
printf ("Front=>");
while (node) {
printf ("%3d", *(int*)node->dataPtr);
node = node->next;
}
printf(" <=Rear\n");
return;
}
|
ตัวแปลภาษา (Compiler) TurboC 3.0|
Download Site : TurboC 3.0 [3 MB] | วิธีแก้ปัญหา ถ้า Compile ไม่ผ่าน เพราะหาห้อง include ไม่พบ เมื่อใช้ TCC ผ่าน command line หรือ Editplus แต่ไม่พบปัญหาใน TC - ถ้าโปรแกรมทั้งหมดกองรวมกันอยู่ในห้อง c:\tc - แก้แฟ้ม Turboc.cfg จาก -IC:\TC\INCLUDE เป็น -IC:\TC |
บทเรียนพิเศษ (Extra Lesson)
โปรแกรมค้นหาตัวอักษรใน string
หัวหน้าผมใช้โปรแกรมลักษณะนี้ออกข้อสอบ จึงต้องทดสอบโปรแกรม เตรียมสอนนักศึกษา
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
main(void) {
clrscr();
char string[6];
char *ptr, z=z;
strcpy(string, "abcdef");
ptr = strchr(string, z);
if (strlen(ptr) == 0) cout << "not found z";
ptr = strchr(string, c);
if (strlen(ptr) > 0) cout << "found c";
getch();
}
|
|
ศึกษาเรื่อง date เพราะมีนักศึกษาคนหนึ่งโทรมาถาม
พบตัวอย่างที่ http://www.cs.duke.edu/~ola/ap/dates/ นำตัวแปร myMonth myDate และ myYear ไปใช้ต่อได้
#include <time.h>
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
static struct tm timeHolder;
static struct tm *date = &timeHolder;
time_t tloc;
time(&tloc);
date = localtime(&tloc);
int myMonth = date->tm_mon + 1;
int myDay = date->tm_mday;
int myYear = date->tm_year + 1900;
printf("%d%d%d",myMonth,myDay,myYear);
getch(); // 9122007 (9 Dec 2007)
}
|
| อีเมลจากเพื่อนชาวไทย ที่ให้คำแนะนำทีมงาน | ||
|
1. คุณศร [sorn_kkc@hotmail.com] : ส่งโปรแกรมนี้มาให้ผมตอบคำถาม คำถามของคุณศร : เขียนโปรแกรมส่งข้อมูลผ่าน port printer โดยควบคุมหลอด LED ปรากฎว่าไม่ได้ผลตามโปรแกรมที่เขียน จากการค้นคว้าเพิ่มเติม ได้พบหนังสือภาษาซี ของ ดอนสัน ปงผาบ อ.คณะเทคโนโลยีอุตสาหกรรม ม.ราชภัฏลำปาง จึงเข้าใจว่า LED คืออะไร .. ผลการทดสอบต่อวงจรครั้งแรก ก็ได้มา 7 ภาพข้างล่างนี้ พร้อมแผลพุพองที่นิ้วกลางซ้ายอีกแผล และนี่คือจุดเริ่มต้นที่จะใช้ภาษาอื่น ๆ คุมอุปกรณ์ผ่าน printer port โปรแกรมนี้มีปัญหาใน WindowsXP แต่ใน WinMe กับ Win98 OK .. 2. คุณโอม [ohm10513@hotmail.com] : ส่ง e-mail มาแนะนำโปรแกรม userport.exe ใน etteam.com ที่หา download ได้จาก etteam.com สามารถแก้ปัญหาเรื่อง Printer Port ใน Windows XP ได้ [ UserPort.zip 33 KB ] กว่าจะทดสอบผ่าน ต้องหาวิธี compile ผ่าน command line ที่ทำงานควบคู่กับ tasm32 และ userport และแก้ IOPort.c มีรายละเอียดการทดสอบสร้าง IOPort.exe ด้วย BCC5.5 ที่ทำงานร่วมกับ UserPort.exe ใน http://www.thaiall.com/printer |
| ตัวอย่างการเขียนโปรแกรมภาษา C ที่แทรกด้วยโค้ดของภาษา Assembly | |
ตัวอย่างโปรแกรมที่แปลด้วย Turbo C 3.0
ทำหน้าที่พิมพ์ A ทางจอภาพ โดยใช้ Interrupt 21h
c:\tc>tcc x.cpp
void main() {
asm {
mov ah,02h;
mov dl,41h;
int 21h;
}
}
| ตัวอย่างโปรแกรมที่แปลด้วย Borland C 5.5
ทำหน้าที่พิมพ์เลข 11 เพราะเป็นผลบวกที่ได้จากภาษา assembly เช่น
|
https://dotnetfiddle.net/ http://rextester.com/ http://rextester.com/GNGHF8383 (pointer in clang) https://csharppad.com/ https://repl.it/repls/AttentiveClientsideSystemresource https://www.jdoodle.com/compile-c-sharp-online https://www.tutorialspoint.com/compile_csharp_online.php https://www.learncs.org/ https://www.codeguru.com/csharp/using-unsafe-code-and-pointers-in-c.htm https://ideone.com/ |
+ quora.com/What-is-a-pointer-variable + /datastructure/queue.htm |
#include <stdio.h>
void main() {
int a = 5;
int *b;
b = &a;
printf("%d \n",a); // 5
printf("%d \n",b); // 1703752
printf("%d \n",&a); // 1703752
printf("%d \n",&b); // 1703748
}
|
#include <stdio.h>
void main() {
int a = 5;
int *b;
b = &a; // reference by pointer
int c = *b; // reference by value
int **d; // this is pointer of pointer
// ===
printf("%d \n",a); // 5
printf("%d \n",b); // 1703752
printf("%d \n",&a); // 1703752
printf("%d \n",&b); // 1703748
printf("%d \n",*b); // 5
// ===
// compile error for printf("%d \n",*a);
// compile error for printf("%d \n",**a);
// compile error for printf("%d \n",*c);
// compile error for printf("%d \n",**c);
// compile error for printf("%d \n",**b);
printf("%d \n",c); // 5
printf("%d \n",&c); // 1703744
// ===
a = 6;
printf("%d \n",*b); // 6
printf("%d \n",c); // 5
printf("%d \n",&b); // 1703748
// ===
// compile error for d = &a;
d = &b;
printf("%d \n",d); // 1703748
printf("%d \n",*d); // 1703752
printf("%d \n",**d); // 6
}
|
|
แนะนำเว็บ
+ http://www.etteam.com/downloadf.html (มี compiler manual และ code แก้ปัญหา อาทิ userport.zip) + http://www.beyondlogic.org/spp/parallel.htm (Have detail of pin number) + http://www.xmlblaster.org/...TestQueue.html + http://www.dreamincode.net/forums/showtopic49439.htm (Data Structure : ADT Source Code of Queue ) ! http://my.tele2.ee/chipmcu/ing/prog705j.htm (อ่าน e-prom จาก printer port) ! http://www.nelnickrobotics.com/printer_port.html (ภาพวงจรการเชื่อมต่ออุปกรณ์) ! http://www.machinegrid.com/content/view/30/84/ (แสดงการประยุกต์กับมอเตอร์)
ตัวอย่างภาษาซี กับส่งข้อมูลออกทาง Printer Port ที่ http://www.thaiall.com/printer |
|
| "ไม่เริ่มต้นในวันนี้ จะไม่มีทางสำเร็จในวันพรุ่ง" โดย โยฮัน ว็อล์ฟกัง ฟ็อน เกอเทอ |