Programming

Just another WordPress.com weblog

ประวัติเอกสารจากหมวดหมู่ ‘Programming’

ข้อสอบ CT215 คูณเลขแบบมีเครื่องหมาย

แสดงความเห็นโดย จั่น บน ธันวาคม 10, 2008

โจทย์ ให้ตัวตั้งอย่างมากใส่ได้เป็นตัวเลข 3 หลัก และตัวนำมาคูณก็ 3 หลักเช่นกัน เขียน program คูณกันธรรมดาแบบมีเครื่องหมาย

Program Multiply Two Integers
MULTIPLICAND : 5
MULTIPLIER : -5
PRODUCT : -25
RUN AGAIN (y/n):

———–

TITLE (Multipli.asm)

Include Irvine32.inc
Include Macros.inc

.data

.code
main PROC

Start:

call Clrscr

mGotoxy 20,10
mWrite <”Program Multiply Two Integers”,0dh,0ah>

mGotoxy 20,11
mWrite “MULTIPLICAND : “

call ReadInt

mov ebx, eax

mGotoxy 20,12
mWrite “MULTIPLIER : “

call ReadInt

IMUL ebx

mGotoxy 20,13
mWrite “PRODUCT : “

call WriteInt

mGotoxy 20,14
mWrite “RUN AGAIN (y/n): “

call ReadChar
cmp al, ‘y’
je Start

exit
main ENDP
END main

เขียนแล้วใน CT215, Programming | 4 Comments »

ข้อสอบ CT215 แยก input ที่ใส่เข้าไป ตัวใหญ่,ตัวเล็ก,ตัวเลข

แสดงความเห็นโดย จั่น บน ธันวาคม 10, 2008

โจทย์ ป้อนตัวอักขระตัวเล็กตัวใหญ่ตัวเลขปนๆกันแล้วให้แยกออกมาว่ามีตัวเล็กอะไรบ้าง

และตัวใหญ่อะไรบ้างและตัวเลขอะไรบ้าง

Program Identify Lower,Upper,Digits
Input Lower or Upper or Digits: Junabcd1234s
LowerCase Characters: unabcds
UpperCase Characters: J
Digits Characters: 1234
Run Again (y/n):

————–

TITLE (.asm)

; This program
; Last update:

Include Irvine32.inc
Include Macros.inc
.data

allchars BYTE 128 DUP(0)

.code
main PROC

Start:

call Clrscr

mov esi, OFFSET allchars

mGotoxy 20,10
mWrite <”Program Identify Lower,Upper,Digits”,0dh,0ah>

mGotoxy 20,11
mWrite “Input Lower or Upper or Digits: “

mov ebx,0
mov ecx,0

L1:

call ReadChar
cmp al, 0DH
;if ENTER
je L2

;if less than ‘a’
cmp al,61H
jb LBIG

;if more than ‘z’
cmp al,7AH
ja L1

;ok a-z
call WriteChar

mov [esi], al

INC esi
INC ebx

jmp L1

LBIG:

;if less than ‘A’
cmp al,41H
jb LDIGITS

;if more than ‘Z’
cmp al,5AH
ja L1

;ok A-Z
call WriteChar

mov [esi], al

INC esi
INC ebx

jmp L1

LDIGITS:

;if less than ‘0′
cmp al,30H
jb L1

;if more than ‘9′
cmp al,39H
ja L1

;ok 0-9
call WriteChar

mov [esi], al

INC esi
INC ebx

jmp L1

L2:

mGotoxy 20,12
mWrite “LowerCase Characters: “

mov esi, OFFSET allchars
mov ecx, ebx

LPrintLower:

mov al, [esi]
;if less than ‘a’
cmp al,61H
jb L2X

;if more than ‘z’
cmp al,7AH
ja L2X

;ok a-z
call WriteChar

L2X: INC esi

LOOP LPrintLower

L3:

mGotoxy 20,13
mWrite “UpperCase Characters: “

mov esi, OFFSET allchars
mov ecx, ebx

LPrintUpper:

mov al, [esi]
;if less than ‘A’
cmp al,41H
jb L3X

;if more than ‘Z’
cmp al,5AH
ja L3X

;ok A-Z
call WriteChar

L3X: INC esi

LOOP LPrintUpper

L4:

mGotoxy 20,14
mWrite “Digits Characters: “

mov esi, OFFSET allchars
mov ecx, ebx

LPrintDigits:

mov al, [esi]
;if less than ‘0′
cmp al,30H
jb L4X

;if more than ‘9′
cmp al,39H
ja L4X

;ok 0-9
call WriteChar

L4X: INC esi

LOOP LPrintDigits

mGotoxy 20,15
mWrite “Run Again (y/n): “

L5:

call ReadChar
call WriteChar

;if equals ‘y’
cmp al,79H
je Start

exit
main ENDP
END main

เขียนแล้วใน CT215, Programming | Leave a Comment »

ข้อสอบ CT215 ปี 2551 เขียน code assembly ยาว

แสดงความเห็นโดย จั่น บน ธันวาคม 8, 2008

โจทย์ ป้อนข้อมูลเป็นฐาน 16 แล้วให้พิมพ์ออกมาในรูป Binary และฐาน 10 (ฺBinary ขอแสดงเพียง 8 หลักต่ำเท่านั้น ห้ามแสดง 32 หลักจาก eax)

Hex to Bin and Decimal
Hex: 56
Binary: 01010110
Decimal: +86
Continue (y/n):

————

TITLE (HexToBinAndDecimal.asm)

; This program
; Last update:

Include Irvine32.inc
Include Macros.inc
.data
.code
main PROC

Start:

call Clrscr

mWrite <”Hex to Bin and Decimal”,0dh,0ah>

mWrite “Hex: “

call ReadHex

mWrite “Binary: “

mov ebx, eax

;BLOCK PRINT 8 BIT BINARY
SHL eax,24
mov ecx,8

L1:

SHL eax,1
jnc WRITEZERO

mWrite “1″
jmp L2

WRITEZERO:

mWrite “0″

L2: LOOP L1

mov eax, ebx

call Crlf

mWrite “Decimal: “

call WriteInt

call Crlf

mWrite “Continue (y/n): “
call ReadChar
call WriteChar

cmp al,79H
je Start

exit
main ENDP
END main

เขียนแล้วใน CT215, Programming | Leave a Comment »

ข้อสอบ CT215 ปี 2551 เขียน code assembly ยาว

แสดงความเห็นโดย จั่น บน ธันวาคม 8, 2008

เป็นส่วนของการเขียน program ยาว ซึ่งเป็นตัวอย่างหนึ่งเท่านั้น อาจารย์สามารถออกโจทย์ได้เยอะมากครับ

โจทย์ หาค่า Factorial

Program Calculate Factorial
Insert N Factorial: 6
Result: +720
Run Again (y/n):

————–

TITLE (.asm)

; This program
; Last update:

Include Irvine32.inc
Include Macros.inc
.data

Total DWORD ?

.code
main PROC

Start:

call Clrscr

mGotoxy 20,10
mWrite <”Program Calculate Factorial”,0dh,0ah>

mGotoxy 20,11
mWrite “Insert N Factorial: “

call ReadInt

cmp eax,0
; if less than 0
;go to start again
;-1!, -2! cannot find value
jl Start
;else if eax=0
je L0
;else eax>0
jmp Entry

L0:

mov eax, 1

Entry:

mov ecx, eax
mov eax, 1

L1:

MUL ecx

LOOP L1

mGotoxy 20,12
mWrite “Result: “

call WriteInt

mGotoxy 20,13
mWrite “Run Again (y/n): “

L2:

call ReadChar
call WriteChar

cmp al,’y’
je Start

exit
main ENDP
END main

เขียนแล้วใน CT215, Programming | Leave a Comment »

ข้อสอบ CT215 ปี 2551 เขียน code assembly ยาว

แสดงความเห็นโดย จั่น บน ธันวาคม 8, 2008

เป็นส่วนของการเขียน program ยาว ซึ่งเป็นตัวอย่างหนึ่งเท่านั้น อาจารย์สามารถออกโจทย์ได้เยอะมากครับ สำหรับโจทย์ข้อนี้ผมเอา code ของอาจารย์ที่ให้ไว้มาปรับปรุงครับ

โจทย์ ให้พิมพ์ตัวอักขระใดๆ แล้วเมื่อกด Enter ให้เขียน String นั้นเริ่มจากขวาไปซ้าย (Reverse String)

Program Reverse String
Insert Sample String: abcdef
Reverse String: fedcba
Run Again (y/n)

————
TITLE Program Template (MyRevStr.asm)

; This program reverses a string.
; Last update: 1/28/02

INCLUDE Irvine32.inc
Include Macros.inc

.data

buffer BYTE 128 DUP(0)
byteCount DWORD ?

.code
main PROC

Start:

call Clrscr

mGotoxy 20,10
mWrite <”Program Reverse String”,0dh,0ah>

mGotoxy 20,11
mWrite “Insert Sample String: “

mov edx, OFFSET buffer
mov ecx, SIZEOF buffer

call ReadString

mov byteCount, eax

cmp eax, 0
je DISPLAY

; Push the name on the stack.
mov ecx,byteCount
mov esi,0

L1:

movzx eax,buffer[esi] ; get character
push eax ; push on stack
inc esi
Loop L1

; Pop the name from the stack, in reverse,
; and store in the buffer array.
mov ecx,byteCount
mov esi,0

L2:

pop eax ; get character
mov buffer[esi],al ; store in string
inc esi
Loop L2

DISPLAY:

; Display the name.
mGotoxy 20,12
mWrite “Reverse String: “

mov edx,OFFSET buffer
call Writestring
call Crlf

mGotoxy 20,13
mWrite “Run Again (y/n) “
call ReadChar

cmp al,’y’
je Start

exit
main ENDP
END main

เขียนแล้วใน CT215, Programming | Leave a Comment »

ข้อสอบ CT215 ปี 2551 เขียน code assembly ยาว

แสดงความเห็นโดย จั่น บน ธันวาคม 8, 2008

เป็นส่วนของการเขียน program ยาว ซึ่งเป็นตัวอย่างหนึ่งเท่านั้น อาจารย์สามารถออกโจทย์ได้เยอะมากครับ

โจทย์ ป้อนตัวอักษร a-z พิมพ์เล็ก เมื่อกด Enter จะให้พิมพ์ตัวอักษรพิมพ์ใหญ่ออกมาและพิมพ์จำนวนตัวอักษรออกมา จากนั้น program จะถาม y/n ว่าจะ run ต่อหรือไม่

LowerCase To UpperCase
LowerCase Characters: abcdefg
UpperCase Characters: ABCDEFG
Num of Uppercase Characters: +7
Run Again (y/n):

————–

TITLE (.asm)

; This program
; Last update:

Include Irvine32.inc
Include Macros.inc
.data

lowerchars BYTE 128 DUP(0)

.code
main PROC

Start:

call Clrscr

mov esi, OFFSET lowerchars

mGotoxy 20,10
mWrite <”LowerCase To UpperCase”,0dh,0ah>

mGotoxy 20,11
mWrite “LowerCase Characters: “

mov ebx,0
mov ecx,0

L1:

call ReadChar
cmp al, 0DH
;if ENTER
je L2

;if less than ‘a’
cmp al,61H
jb L1

;if less than ‘z’
cmp al,7aH
ja L1

;ok a-z
call WriteChar

mov [esi], al

INC esi
INC ebx

jmp L1

L2:

mGotoxy 20,12
mWrite “UpperCase Characters: “

mov esi, OFFSET lowerchars
mov ecx, ebx

cmp ecx,0
je LX

L3:

mov al, [esi]
sub al, 20H

call WriteChar

INC esi

LOOP L3

LX:

mGotoxy 20,13
mWrite “Num of Uppercase Characters: “
mov eax, ebx
call WriteInt

mGotoxy 20,14
mWrite “Run Again (y/n): “

L4:

call ReadChar
call WriteChar

;if equals ‘y’
cmp al,79H
je Start

exit
main ENDP
END main

เขียนแล้วใน CT215, Programming | Leave a Comment »

ข้อสอบ CT215 ปี 2551 เขียน code assembly ยาว

แสดงความเห็นโดย จั่น บน ธันวาคม 8, 2008

เป็นส่วนของการเขียน program ยาว ซึ่งเป็นตัวอย่างหนึ่งเท่านั้น อาจารย์สามารถออกโจทย์ได้เยอะมากครับ โจทย์ข้อนี้ถ้าอาจารย์เปลี่ยนตัวตั้งเป็น 3 digits และตัวนำมาบวกเป็น 3 digits และผลบวกเป็น 4 digits ก็จะัต้องเปลี่ยนตัวเลขใน program ด้วยครับ

โจทย์ บวกตัวเลขโดยใช้หลักการของการบวกรหัส ASCII

Summation of ASCII
Please Enter First ASCII Integer XXX: 4568
Please Enter Second ASCII Integer XXX: 1112
Summation XXXX: 05680

—————–

TITLE (.asm)

; This program
; Last update:

Include Irvine32.inc
Include Macros.inc
.data

ascii1 BYTE 4 DUP(0)
ascii2 BYTE 4 DUP(0)
ascsum BYTE 5 DUP(0),0

.code
main PROC

Start:

call Clrscr

mGotoxy 20,10
mWrite <”Summation of ASCII”,0dh,0ah>

mGotoxy 20,11
mWrite “Please Enter First ASCII Integer XXX: “

mov ecx,4
mov esi, OFFSET ascii1

L1:

call readChar
cmp al, 30H
jb L1
cmp al, 39H
ja L1

call WriteChar
mov [esi],al

inc esi

loop L1

mGotoxy 20,12
mWrite “Please Enter Second ASCII Integer XXX: “

mov ecx,4
mov esi, OFFSET ascii2

L2:

call readchar
cmp al, 30H
jb L2
cmp al, 39H
ja L2

call WriteChar
mov [esi], al

inc esi

loop L2

CLC

mov esi, OFFSET ascii1 + 3
mov edi, OFFSET ascii2 + 3
mov ebx, OFFSET ascsum + 4

mov ecx, 4

A20:

mov AH,00
mov AL,[esi]

ADC AL,[edi]

AAA

mov [ebx],AL

dec ESI
dec EDI
dec EBX

loop A20

;last carry
mov [ebx],AH

;;change to ascii
mov ebx, OFFSET ascsum+4
mov ecx, 5

A30:

OR BYTE PTR [ebx], 30H
dec ebx

loop A30

;print edx
mGotoxy 20,13
mWrite “Summation XXXX: “
mov edx, OFFSET ascsum
call WriteString

mGotoxy 20,14
mWrite “Continue (y/n): “
call ReadChar
call WriteChar

cmp al,79H
je Start

exit
main ENDP
END main

เขียนแล้วใน CT215, Programming | 2 Comments »

ข้อสอบ CT212 final เรื่องไฟล์

แสดงความเห็นโดย จั่น บน ธันวาคม 6, 2008

โจทย์ ให้เขียน program หยิบข้อมูล Input จากแฟ้มข้อมูล

(StudentScore.txt) ซึ่งประกอบไปด้วยข้อมูล

รหัสนักศึกษา, ชื่อนักศึกษา, คะแนนเก็บ, คะแนน midterm ,คะแนน final

ดังนี้

5102026     Sakon        15    40    40
5102027     Damrong      10    25    26
5102028     Lanna        5     10    18
5102029     Kwee         10    10    15
2102030     Udon         5     30    29
2102031     Tee          20    35    40

ให้แสดงผล output ออกทางหน้าจอ ตามนี้

นักศึกษาที่ได้เกรด G ได้แก่
5102026 Sakon 95
2102031 Tee 95

นักศึกษาที่ได้เกรด P ได้แก่
5102027 Damrong 61
2102030 Udon 64

นักศึกษาที่ได้เกรด F ได้แก่
5102028 Lanna 33
5102029 Kwee 35

code ด้านล่างจะรันได้ก็ต่อเมื่อมี file StudentScore.txt อยู่แล้ว โดยสร้างตามโจทย์

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

#define sourceFile “StudentScore.txt”

using namespace std;

int ProcessScore(int);
void PrintOutput(const string, const string, const int);

void main(){

cout << “Student Grade G ” << endl;
ProcessScore(1);

cout << endl;

cout << “Student Grade P ” << endl;
ProcessScore(2);

cout << endl;

cout << “Student Grade F ” << endl;
ProcessScore(3);

}

int ProcessScore(int type){

ifstream ifs;
ifs.open(sourceFile);

if(ifs.fail()){

cerr << “Error Cannot Open ” << sourceFile << endl;
return EXIT_FAILURE;

}

string studentcode, studentname;
int homework, midterm, finale;
int totalscore;

ifs >> studentcode >> studentname >> homework >> midterm >> finale;

while(!ifs.eof()){

totalscore = homework + midterm + finale;

if(type == 1){

if(totalscore >= 80){

PrintOutput(studentcode, studentname, totalscore);

}

}else if(type == 2){

if(totalscore >= 60 && totalscore <= 79 ){

PrintOutput(studentcode, studentname, totalscore);

}

}else if(type == 3){

if(totalscore < 60){

PrintOutput(studentcode, studentname, totalscore);

}

}

ifs >> studentcode >> studentname >> homework >> midterm >> finale;

}

ifs.close();
return 0;

}

void PrintOutput(const string studentcode, const string studentname, const int totalscore){

cout << studentcode << ” ” << studentname << ” ” << totalscore << endl;

}

เขียนแล้วใน CT212, Programming | 1 ความคิดเห็น »

ข้อสอบ CT212 final เรื่อง Recursive ปี 2551

แสดงความเห็นโดย จั่น บน ธันวาคม 5, 2008

พิจารณา Arkaman ’s function นี้

A(m,n) ={ n+1 เมื่ือ m=0;

A(m-1,1) เมื่อ n =0;

A(m-1, A(m, n-1)) เมื่อกรณีอื่นๆ }

จงเขียนเป็น Recursive function และให้หาด้วยว่า A(3, 1) มีค่าเท่าไหร่

int A(int m, int n){

if(m == 0){

return n+1;

}else if( n == 0){

return A(m-1,1);

}else{

return A(m-1, A(m, n-1));

}

}

สำหรับคำตอบของ A(3,1) จะเท่ากับ 13 (ไล่หาค่าใช้เวลานานมาก แนะนำให้เอาเวลาที่เหลือมาไล่หาค่าจะดีกว่า)

เขียนแล้วใน CT212, Programming | Leave a Comment »

ข้อสอบ CT212 finale ข้อย่อย Recursive ปี 2551

แสดงความเห็นโดย จั่น บน ธันวาคม 5, 2008

จงแปลง function นี้ให้เป็น Recursive

double test(int a, int b){

double sum = 1.0;

for(int i=0; i<b; i++){

sum = sum * (1/a);

}

}

แปลงเป็น Recursive function ได้เป็น

double test(int a, int b){

if(b == 0){

return 1.0;

} else{

return (1.0/a) * test(a, b-1);

}

}

เขียนแล้วใน CT212, Programming | Leave a Comment »