29 พฤศจิกายน 2554

[JAVA] โปรแกรมแปลงเลขฐานสิบเป็นฐานใดๆ


import java.util.Scanner;
public class Exchange {  
// กำหนดตัวแปร
    public static void main(String [] args){
        int number, base;
        String basedNumber = "";
        int decNumber = 0;
        int iRemainder, c = ' ';
        char cRemainder = 0;
        Scanner key = new Scanner(System.in);
        // รับค่าตัวเลข และ ฐาน
        System.out.print("Enter number :");
        number=key.nextInt();

        do {
          System.out.print("Enter base (2-16): ");
          base =key.nextInt();
          if (base < 2 || base > 16)
              System.out.println("Invalid Base! Try again!");
        } while (base < 2 || base > 16);
        // แปลงเลขฐานสิบเป็นฐานใดๆ
        do {
          iRemainder = number % base; // หารเอาเศษ
          if (iRemainder >= 10) { // เศษมากกว่า 10 ต้องเปลี่ยนเป็น A,B,C,D,E,F
            switch (iRemainder) {
              case 10 : cRemainder = 'A'; break;
              case 11 : cRemainder = 'B'; break;
              case 12 : cRemainder = 'C'; break;
              case 13 : cRemainder = 'D'; break;
              case 14 : cRemainder = 'E'; break;
              case 15 : cRemainder = 'F'; break;
            }
            basedNumber = cRemainder + basedNumber; // ต่อสตริงจากหลังไปหน้า
          } else { // เศษ 0-9 เอามาต่อกับสตริงได้เลย เพราะ int จะถูกแปลงเป็น String อัตโนมัติ
            basedNumber = iRemainder + basedNumber; // ต่อสตริงจากหลังไปหน้า
          }
          number = number / base; // หารเลขด้วยฐาน
        } while (number > 0); // ทำไปจนกว่า number จะเป็น 0
        // แปลงเลขฐานใดๆ มาเป็นฐานสิบ
        c = 0;
        while (c < basedNumber.length()) {
          cRemainder = basedNumber.charAt(c);
          // เชคว่าเป็น Character ตัวใด แล้วเปลี่ยนให้เป็นตัวเลข
          switch (cRemainder) {
            case '0' : iRemainder = 0; break;
            case '1' : iRemainder = 1; break;
            case '2' : iRemainder = 2; break;
            case '3' : iRemainder = 3; break;
            case '4' : iRemainder = 4; break;
            case '5' : iRemainder = 5; break;
            case '6' : iRemainder = 6; break;
            case '7' : iRemainder = 7; break;
            case '8' : iRemainder = 8; break;
            case '9' : iRemainder = 9; break;
            case 'A' : iRemainder = 10; break;
            case 'B' : iRemainder = 11; break;
            case 'C' : iRemainder = 12; break;
            case 'D' : iRemainder = 13; break;
            case 'E' : iRemainder = 14; break;
            case 'F' : iRemainder = 15; break;
          }
          decNumber += iRemainder * (int) Math.pow(base, (basedNumber.length() - 1 - c)); // เอาเลขที่ได้คูณกับค่าประจำหลัก
          c++;
        }
        // แสดงผล
        System.out.println("Convert to base " + base + ": " + basedNumber);
        System.out.println("Convert back to base 10: " + decNumber);
    }
}

25 พฤศจิกายน 2554

[C++] โปรแกรมหา Series cos x ด้วยวิธี Maciaurin Series


สามารถเปลี่ยนมุมได้ตรงบรรทัด x = M_PI / 3
ในที่นี้ M_PI / 3 ก็คือ ไพน์ หาร ด้วย 3 นั่นเอง



24 พฤศจิกายน 2554

[C++] คำสั่งรับค่า แบบ getch()

// x ในที่นี้ ทำขึ้นเพื่อใช้กับคำสั่ง gotoxy(x,y) เพื่อให้ตำแหน่งของส่วนแสดงผล 
//สามารถแสดงผลได้อย่างถูกต้อง (ลืมเอา ert ออก -.- เวลาไปใช้ไม่ต้องใส่ตัวนี้นะครับ)


[C++]โปรแกรมแปลงตัวเลข 1 - 1000 ให้แสดงเป็นข้อความ



23 พฤศจิกายน 2554

[JAVA][Image]ส่วนของคำสั่งแก้ไขอาการ salt & paper noise

โค๊ดนี้จะดึงค่า RGB ออกมาและทำการเก็บข้อมูลลงในตัวแปร filter เพื่อหาค่าที่อยู่ตรงกลาง
นำไปใช้กับรูปภาพที่มีการเกิด salt & paper noise
                int filter[] =  new int[9];
                for(int z = 1 ; z<=5;z++)
                {
                    for(int i = 1;i<=image.getWidth()-2;i++)
                    {  
                        for(int j = 1;j<=image.getHeight()-2;j++)
                        {
                            filter [0] = rgb[i-1][j-1];
                            filter [1] = rgb[i][j-1];
                            filter [2] = rgb[i+1][j-1];
                            filter [3] = rgb[i-1][j];
                            filter [4] = rgb[i][j];
                            filter [5] = rgb[i+1][j];
                            filter [6] = rgb[i-1][j+1];
                            filter [7] = rgb[i][j+1];
                            filter [8] = rgb[i+1][j+1];

                            Arrays.sort(filter);
                            rgb[i][j]=filter[4];
                        }        
                    }
                }


[JAVA] ส่วนของคำสั่ง การเปิดไฟล์เองได้ (ListFile)


import java.util.*;
import java.io.*;
import javax.swing.*;
public class ListFile {

public static void main(String[] args) throws IOException{
String name = "";
File filename = new File("");
JFileChooser ch = new JFileChooser();
int status = ch.showOpenDialog(null);
if(status == JFileChooser.APPROVE_OPTION){
filename = ch.getSelectedFile();
FileReader inleader = new FileReader(filename);
Scanner x = new Scanner(inleader);

/*
*
* Statement
*
*
*/
}else{
System.out.println("Open File Dialog cancelled.");
}
}
}

08 ตุลาคม 2554

[ASSEMBLY] โปรแกรมควบคุม Steper Motor


.model small
.data
PDATA equ 3BCh

BUFF1   equ 01111111B
LATCH1  equ 10000000B

TAB1 db 01h,03h,02h,06h,04h,0Ch,08h,09h
    TAB2    db 09h,08h,0Ch,04h,06h,02h,03h,01h
    LEFT    EQU 4Bh
    RIGHT   EQU 4Dh
    ENTER   EQU 1Ch
    SCANKEY db  ?
    ASCII   db  ?
 
.code
mov ax,@data
mov ds,ax

Next: call RDKEY
call CHKKEY

cmp SCANKEY,ENTER
jne Next

Exit:
       mov ah,4Ch
       int 21h

WRT_DATA proc near
        push cx
     
        mov cx,10
and al,BUFF1
mov dx,PDATA
LD1:out dx,al
   loop LD1
 
   mov cx,10
or al,LATCH1
mov dx,PDATA
    LD2:out dx,al
   loop LD2
 
   pop cx  
        ret
WRT_DATA endp

RDKEY   proc near
        mov  ah,00h
   int  16h
   mov  SCANKEY,ah
   mov  ASCII,al

        ret
RDKEY   endp

CHKKEY  proc near
        cmp  SCANKEY,LEFT
        jne  RKEY
       
        mov cl,8
        mov di,offset TAB2
        call ROTATE
        jmp  EXT
     
RKEY:   mov cl,8
        mov di,offset TAB1
        call ROTATE
        jne  EXT
           
EXT:    ret
CHKKEY  endp

ROTATE  proc near      
;ST:    mov cl,8
; mov di,offset TAB1

NT:     call WRT_DATA
   call Delay
inc di
dec cl

jnz NT
; jmp ST
        ret
ROTATE  endp

Delay proc near
        push cx
        push dx
mov dx,1000

DL2:mov cx,50000
DL1:mov al,[di]

   loop DL1
dec dx
jnz DL2

pop dx
pop cx
ret
Delay endp

end

23 กันยายน 2554

[JAVA] การสร้างคลาสประเภท Exception แบบฝึกหัดที่ 9.2

======= TellerGUI =======

import Banking.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TellerGUI implements ActionListener{
private Button bn1,bn2,bn3;
private TextField tf1,tf2,tf3;
private Label l1,l2,l3;
private TextField err;
private Panel p1,p2,p3;
private Frame fr;
private Customer cust;
public static void main(String den[]){
Customer cust = new Customer("Sommai","Sommut");
Account ac = new SavingAccount(5000);
cust.addAccount(ac);
TellerGUI tl = new TellerGUI(cust);
tl.init();
}
public void init(){
fr = new Frame("Button Sample");
fr.setLayout(new FlowLayout());

tf1 = new TextField();
tf2 = new TextField();
tf3 = new TextField();
err = new TextField();

l1 = new Label("Name:");
l2 = new Label("Balance:");
l3 = new Label("Amount");

bn1 = new Button("Deposit");
bn2 = new Button("Withdraw");
bn3 = new Button("Exit");

bn1.addActionListener(this);
bn2.addActionListener(this);
bn3.addActionListener(this);

p1 = new Panel();
p2 = new Panel();
p3 = new Panel();

p1.setLayout(new GridLayout(3,2));
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);

p2.add(bn1);
p2.add(bn2);
p2.add(bn3);

p3.setLayout(new BorderLayout());
p3.add(p1,BorderLayout.CENTER);
p3.add(p2,BorderLayout.SOUTH);

fr = new Frame("Bank");
fr.add(p3,BorderLayout.CENTER);
fr.add(err,BorderLayout.SOUTH);


tf1.setEditable(false);
tf2.setEditable(false);
err.setEditable(false);

tf2.setText(cust.getAccount(0).getBalance()+"");
tf1.setText(cust.getFirstName()+" "+cust.getLastName());

fr.pack();
fr.show();
}
public TellerGUI(Customer c){
cust = c;
}

private void deposit()
{
double amt=Double.parseDouble(tf3.getText());
cust.getAccount(0).deposit(amt);
tf2.setText(cust.getAccount(0).getBalance()+ "" );
err.setText("Deposit "+amt+" baht");
}
public void withdraw()
{
double amt=Double.parseDouble(tf3.getText());
try
{
cust.getAccount(0).withdraw(amt);
err.setText("Withdraw "+amt+" baht");
} catch(WithdrawException ex) {
err.setText("Not Enought Monry");
}
tf2.setText(cust.getAccount(0).getBalance()+"");

}

@Override
public void actionPerformed(ActionEvent ev)
{
String cmd = ev.getActionCommand();
if ("Exit".equals(cmd)) System.exit(0);
else if ("Withdraw".equals(cmd)) withdraw();
else if ("Deposit".equals(cmd)) deposit();
}


}

======= Account =======
package Banking;
public class Account {
protected double balance;
public Account(double amount){
balance = amount;
}
public void deposit(double amount){
balance = balance + amount;
}
public void withdraw(double amount) throws WithdrawException
{
if(balance>=amount)
{
balance -= amount;
}
else
{
throw new WithdrawException();
}
}
public double getBalance(){
return balance;
}
public void showBalance(){
System.out.println(balance);
}
public Account(){

}
}
======= Customer =======
package Banking;
import java.util.*;
public class Customer {
private String firstName;
private String lastName;
private Account acct[];
private Vector v = new Vector();
private Enumeration e = v.elements();
private int NumAccount=0;
public Customer(String fName,String lName){
firstName = fName;
lastName = lName;
acct = new Account[20];
}
public void setFirstName(String fName){
firstName = fName;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String lName){
lastName = lName;
}
public String getLastName(){
return lastName;
}
public void setAccount(Account acct){
acct = acct;
}
public Account getAccount(int index){
return acct[index];
}
public void addAccount(Account acct){
this.acct[NumAccount]=acct;
NumAccount++;
}
public int getNumAccount(){
return NumAccount;
}
public void setVector(Account acct){
this.v.add(acct.getBalance());
}
public void printReport()
{
for(int i=0;i<getNumAccount();i++)
System.out.println("Account"+(i+1)+" Balance :"+getAccount(i).getBalance());
}
public void printReport2(){
int i=0;
while(e.hasMoreElements()){
System.out.println("Account"+(i + 1)+" Balance :"+e.nextElement());
i++;
}
}
}
======= SavingAccount =======
package Banking;
public class SavingAccount extends Account {
public SavingAccount(double amount) {
balance = amount;
}
}

[JAVA] การจัดการกับข้อผิดพลาด แบบฝึกหัดที่ 9.1

public class ExceptionDemo {
public static void main(String[] args){
try{
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
double c = Double.parseDouble(args[2]);
double x = (-b + Math.sqrt(b*b-4*a*c))/(2*a);
System.out.print("x = "+x);
x = (-b - Math.sqrt(b*b-4*a*c)) / (2*a);
System.out.println(" or x = "+x);
}catch(ArithmeticException ex){
System.out.println(ex.toString());
}catch(NumberFormatException ex){
System.out.println("Invalid Numeric format");
}

}
}

[JAVA] การเขียนโปรแกรมจัดการกับเหตุการณ์ แบบฝึกหัดที่ 8.1

======= TellerGUI =======

import Banking.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TellerGUI implements ActionListener{
private Button bn1,bn2,bn3;
private TextField tf1,tf2,tf3;
private Label l1,l2,l3;
private TextField err;
private Panel p1,p2,p3;
private Frame fr;
private Customer cust;
public static void main(String den[]){
Customer cust = new Customer("Sommai","Sommut");
Account ac = new SavingAccount(5000);
cust.addAccount(ac);
TellerGUI tl = new TellerGUI(cust);
tl.init();
}
public void init(){
fr = new Frame("Button Sample");
fr.setLayout(new FlowLayout());

tf1 = new TextField();
tf2 = new TextField();
tf3 = new TextField();
err = new TextField();

l1 = new Label("Name:");
l2 = new Label("Balance:");
l3 = new Label("Amount");

bn1 = new Button("Deposit");
bn2 = new Button("Withdraw");
bn3 = new Button("Exit");

bn1.addActionListener(this);
bn2.addActionListener(this);
bn3.addActionListener(this);

p1 = new Panel();
p2 = new Panel();
p3 = new Panel();

p1.setLayout(new GridLayout(3,2));
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);

p2.add(bn1);
p2.add(bn2);
p2.add(bn3);

p3.setLayout(new BorderLayout());
p3.add(p1,BorderLayout.CENTER);
p3.add(p2,BorderLayout.SOUTH);

fr = new Frame("Bank");
fr.add(p3,BorderLayout.CENTER);
fr.add(err,BorderLayout.SOUTH);


tf1.setEditable(false);
tf2.setEditable(false);
err.setEditable(false);

tf2.setText(cust.getAccount(0).getBalance()+"");
tf1.setText(cust.getFirstName()+" "+cust.getLastName());

fr.pack();
fr.show();
}
public TellerGUI(Customer c){
cust = c;
}

private void deposit()
{
double amt=Double.parseDouble(tf3.getText());
cust.getAccount(0).deposit(amt);
tf2.setText(cust.getAccount(0).getBalance()+ "" );
err.setText("Deposit "+amt+" baht");
}
public void withdraw()
{
double amt=Double.parseDouble(tf3.getText());
try
{
cust.getAccount(0).withdraw(amt);
err.setText("Withdraw "+amt+" baht");
} catch(WithdrawException ex) {
err.setText("Not Enought Monry");
}
tf2.setText(cust.getAccount(0).getBalance()+"");

}

@Override
public void actionPerformed(ActionEvent ev)
{
String cmd = ev.getActionCommand();
if ("Exit".equals(cmd)) System.exit(0);
else if ("Withdraw".equals(cmd)) withdraw();
else if ("Deposit".equals(cmd)) deposit();
}


}

======= Account =======
package Banking;
public class Account {
protected double balance;
public Account(double amount){
balance = amount;
}
public void deposit(double amount){
balance = balance + amount;
}
public void withdraw(double amount) throws WithdrawException
{
if(balance>=amount)
{
balance -= amount;
}
else
{
throw new WithdrawException();
}
}
public double getBalance(){
return balance;
}
public void showBalance(){
System.out.println(balance);
}
public Account(){

}
}
======= Customer =======
package Banking;
import java.util.*;
public class Customer {
private String firstName;
private String lastName;
private Account acct[];
private Vector v = new Vector();
private Enumeration e = v.elements();
private int NumAccount=0;
public Customer(String fName,String lName){
firstName = fName;
lastName = lName;
acct = new Account[20];
}
public void setFirstName(String fName){
firstName = fName;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String lName){
lastName = lName;
}
public String getLastName(){
return lastName;
}
public void setAccount(Account acct){
acct = acct;
}
public Account getAccount(int index){
return acct[index];
}
public void addAccount(Account acct){
this.acct[NumAccount]=acct;
NumAccount++;
}
public int getNumAccount(){
return NumAccount;
}
public void setVector(Account acct){
this.v.add(acct.getBalance());
}
public void printReport()
{
for(int i=0;i<getNumAccount();i++)
System.out.println("Account"+(i+1)+" Balance :"+getAccount(i).getBalance());
}
public void printReport2(){
int i=0;
while(e.hasMoreElements()){
System.out.println("Account"+(i + 1)+" Balance :"+e.nextElement());
i++;
}
}
}
======= SavingAccount =======
package Banking;
public class SavingAccount extends Account {
public SavingAccount(double amount) {
balance = amount;
}
}

09 กันยายน 2554

[JAVA]การใช้แพคเก็จ javax.swing แบบฝึกหัดที่ 7.2


import Banking.*;
import javax.swing.*;
import java.awt.*;
public class TellerGUISwing {
    private JButton bn1,bn2,bn3;
    private JTextField tf1,tf2,tf3;
    private JLabel l1,l2,l3;
    private JTextField err;
    private JPanel p1,p2,p3;
    private JFrame fr;
    private Customer cust;
    public static void main(String den[]){
        Customer cust = new Customer("Sommai","Sommut");
        Account ac = new SavingAccount(5000);
        cust.addAccount(ac);
        TellerGUISwing tl = new TellerGUISwing(cust);
        tl.init();
    }
    public void init(){
        fr = new JFrame("Button Sample");
        fr.setLayout(new FlowLayout());
        tf1 = new JTextField();
        tf2 = new JTextField();
        tf3 = new JTextField();
        err = new JTextField();
        l1 = new JLabel("Name:");
        l2 = new JLabel("Balance:");
        l3 = new JLabel("Amount");
        bn1 = new JButton("Deposit");
        bn2 = new JButton("Withdraw");
        bn3 = new JButton("Exit");
        p1 = new JPanel();
        p2 = new JPanel();
        p3 = new JPanel();
        p1.setLayout(new GridLayout(3,2));
        p1.add(l1);
        p1.add(tf1);
        p1.add(l2);
        p1.add(tf2);
        p1.add(l3);
        p1.add(tf3);
        p2.add(bn1);
        p2.add(bn2);
        p2.add(bn3);
        p3.setLayout(new BorderLayout());
        p3.add(p1,BorderLayout.CENTER);
        p3.add(p2,BorderLayout.SOUTH);
        fr.getContentPane().add(p3,BorderLayout.CENTER);
        fr.getContentPane().add(err,BorderLayout.SOUTH);
        tf1.setEditable(false);
        tf2.setEditable(false);
        err.setEditable(false);
        tf2.setText(cust.getAccount(0).getBalance()+"");
        tf1.setText(cust.getFirstName()+""+cust.getLastName());
        fr.pack();
        fr.show();
    }
    public TellerGUISwing(Customer c){
        cust = c;
    }
}

[JAVA]การสร้างโปรแกรม GUI โดยใช้แพคเก็จ java.awt แบบฝึกหัดที่ 7.1


import Banking.*;
import java.awt.*;
public class TellerGUI {
    private Button bn1,bn2,bn3;
    private TextField tf1,tf2,tf3;
    private Label l1,l2,l3;
    private TextField err;
    private Panel p1,p2,p3;
    private Frame fr;
    private Customer cust;
    public static void main(String den[]){
        Customer cust = new Customer("Sommai","Sommut");
        Account ac = new SavingAccount(5000);
        cust.addAccount(ac);
        TellerGUI tl = new TellerGUI(cust);
        tl.init();
    }
    public void init(){
        fr = new Frame("Button Sample");
        fr.setLayout(new FlowLayout());
        tf1 = new TextField();
        tf2 = new TextField();
        tf3 = new TextField();
        err = new TextField();
        l1 = new Label("Name:");
        l2 = new Label("Balance:");
        l3 = new Label("Amount");
        bn1 = new Button("Deposit");
        bn2 = new Button("Withdraw");
        bn3 = new Button("Exit");
        p1 = new Panel();
        p2 = new Panel();
        p3 = new Panel();
        p1.setLayout(new GridLayout(3,2));
        p1.add(l1);
        p1.add(tf1);
        p1.add(l2);
        p1.add(tf2);
        p1.add(l3);
        p1.add(tf3);
        p2.add(bn1);
        p2.add(bn2);
        p2.add(bn3);
        p3.setLayout(new BorderLayout());
        p3.add(p1,BorderLayout.CENTER);
        p3.add(p2,BorderLayout.SOUTH);
        fr.add(p3,BorderLayout.CENTER);
        fr.add(err,BorderLayout.SOUTH);
        tf1.setEditable(false);
        tf2.setEditable(false);
        err.setEditable(false);
        tf2.setText(cust.getAccount(0).getBalance()+"");
        tf1.setText(cust.getFirstName()+""+cust.getLastName());
        fr.pack();
        fr.show();
    }
    public TellerGUI(Customer c){
        cust = c;
    }
}

20 สิงหาคม 2554

[Assembly]โปรแกรมแปลงค่าในตาราง


.model small
.data
TAB1  DB ‘0123456789’
VBCD DB 05h
VASC  DB 30h
.code
mov ax,@data
mov ds,ax

mov ax,0000h
mov al,VBCD
mov di,ax
mov al,TAB1[di]

mov VASC,al

mov ax,4c00h
int 21h
end

[Assembly]โปรแกรมแปลงค่าในตาราง โดยใช้การคูณ


.model small
.data
    TBCD    DB 'Even',13,10,'$'
   DB 'Odd ',13,10,'$'
   DB 'Even',13,10,'$'
   DB 'Odd ',13,10,'$'
   DB 'Even',13,10,'$'
   DB 'Odd ',13,10,'$'
   DB 'Even',13,10,'$'
   DB 'Odd ',13,10,'$'
   DB 'Even',13,10,'$'
   DB 'Odd ',13,10,'$'
STR1    DB 'ENTER NUMBER: $'
STR2    DB 13,10,'Number is $'
SCALE   DB 7
VDEC    DB '0'
VBCD    DB 00h
.code
mov ax,@data
mov ds,ax

    mov dx,offset STR1
mov ah,09h
int 21h

mov ah,01h
int 21h

mov VDEC,al
sub al,'0'
mov VBCD,al
mov dx,offset STR2
mov ah,09h
int 21h

mov dx,offset TBCD
mov al,VBCD
mul SCALE
add dx,ax
mov ah,09h
int 21h

mov ax,4c00h
int 21h
end

[Assembly]โปรแกรม บวก ลบ แบบ BCD (ป้อนค่าและแสดงผลได้)


.model small
.data
BALANCE    DB 'Balance: $'
RECEIVE    DB 13,10,'Receive: $'
SELL    DB 13,10,'Sell: $'
REMAIN    DB 13,10,'Remain = '
VRemain      DB '00000',13,10,'$'
VBalance      DB 5,?,'0000'
VReceive      DB 5,?,'0000'
VSell      DB 5,?,'0000'
.code
mov ax,@data
mov ds,ax

mov dx,offset BALANCE       ;input Balance    
mov ah,09h
int 21h
mov dx,offset VBalance
mov ah,0ah
int 21h

mov dx,offset RECEIVE       ;input Receive
mov ah,09h
int 21h
mov dx,offset VReceive
mov ah,0ah
int 21h

mov dx,offset SELL          ;input Sell
mov ah,09h
int 21h
mov dx,offset VSell
mov ah,0ah
int 21h
 
    mov al,VBalance+5           ;digit 4
add al,VReceive+5
aaa
add al,30h
sbb al,VSell+5
aas
mov bl,al

mov al,VBalance+4           ;digit 3
adc al,VReceive+4
aaa
add al,30h
sbb al,VSell+4
aas
mov bh,al

mov al,VBalance+3           ;digit 2
adc al,VReceive+3
aaa
add al,30h
sbb al,VSell+3
aas
mov dl,al

mov al,VBalance+2           ;digit 1
adc al,VReceive+2
aaa
mov dh,al

mov al,30h                  ;digit 0
adc al,VRemain
aaa
add al,30h
mov VRemain,al

mov al,dh                   ;digit 1
add al,30h
sbb al,VSell+2
aas
mov dh,al

mov al,VRemain              ;digit 0
sbb al,0
aas
add al,30h
mov VRemain,al

add bx,3030h                ;keep digit 1-4
mov VRemain+3,bh
mov VRemain+4,bl
add dx,3030h
mov VRemain+1,dh
mov VRemain+2,dl

mov dx,offset REMAIN        ;show digit 0-4
mov ah,09h
int 21h

mov ax,4c00h
int 21h
end

[Assembly]โปรแกรมที่ใช้คำสั่งเงื่อนไขและวนรอบ


.model small
.data
STR1 DB 'ENTER 1-9: $'
STR2 DB 13,10,'SUM: '
    VSUM DB '00',13,10,'$'
    VN   DB 13,10,'INVAID INPUT','$'
    NASC DB '0'
    KEY  DB '0'
.code
mov ax,@data
mov ds,ax

  mov dx,offset STR1
mov ah,09h
int 21h
 
  mov ah,01h
    int 21h
    mov KEY,al
    mov bx,ax
 
CHK:mov al,KEY
    cmp al,'0'
    jb NON
    cmp al,'9'
    ja NON
    mov ax,bx
    jmp L0
 
NON:mov dx,offset VN
    jmp EX
 
L0: mov NASC,al
    mov ax,3030h
mov cl,'1'

L1: add al,cl
    aaa
    add al,'0'
    inc cl
    cmp cl,NASC
jna L1

    mov VSUM,ah
    mov VSUM+1,al
 
    mov dx,offset STR2
EX: mov ah,09h
int 21h

mov ax,4c00h
int 21h
end

[Assembly]โปรแกรม บวก ลบ แบบ BCD


.model small
.data
    SUM    dw 0
    START  dw 1234H
    VAdd   dw 3099H
    VSub   dw 2099H
.code  
    MOV ax,@data
    MOV ds,ax
 
;-------------------- Addition
 
MOV DX,START     ;load START
MOV BX,VADD        ;load ValueAddition
    MOV AL,BL       ;sum BL with DL
    ADD AL,DL
    DAA             ;adjust
    MOV CL,AL       ;answer to CL
    MOV AL,BH       ;sum BH, DH, and carry
    ADC AL,DH
    DAA             ;adjust
    MOV CH,AL       ;answer to CH

;-------------------- Subtraction

    MOV DX,Vsub     ;load ValueAddition
    MOV BX,CX        ;load Answer of Addition
MOV AL,BL       ;subtract DL from BL
    SUB AL,DL
DAS             ;adjust
MOV CL,AL       ;answer to CL
MOV AL,BH       ;subtract DH, and carry
SBB AL,DH
DAS             ;adjust
    MOV CH,AL       ;answer to CH

    MOV ax,4c00h
    INT 21h
end

[JAVA]การใช้ตัวแปรอะเรย์ของข้อมูลชนิดคลาส แบบฝึกหัดที่ 6.2


------------------------------------
              Account
------------------------------------

package Banking;

public class Account {
    protected double balance;
    public Account(double amount){
        balance = amount;
    }
    public void deposit(double amount){
        balance = balance + amount;
    }
    public boolean withdraw(double amount){
        boolean result = true;
        if(amount < balance){
            balance = balance - amount;
        }
        else{
            result = false;
        }
        return result;
    }
    public double getBalance(){
        return balance;
    }
    public void showBalance(){
        System.out.println(balance);
    }
    public Account(){
     
    }
}

------------------------------------
              Customer
------------------------------------

package Banking;

public class Customer {
    private String firstName;
    private String lastName;
    private Account acct[];
    private int NumAccount=0;
    public Customer(String fName,String lName){
        firstName = fName;
        lastName = lName;
        acct = new Account[20];
    }
    public void setFirstName(String fName){
        firstName = fName;
    }
    public String getFirstName(){
        return firstName;
    }
    public void setLastName(String lName){
        lastName = lName;
    }
    public String getLastName(){
        return lastName;
    }
    public void setAccount(Account acct){
        acct = acct;
    }
    public Account getAccount(int index){
        return acct[index];
    }
    public void addAccount(Account acct){
        this.acct[NumAccount]=acct;
        NumAccount++;
    }
    public int getNumAccount(){
        return NumAccount;
    }
    public void printReport()
    {
        for(int i=0;i<getNumAccount();i++)
        System.out.println("Account"+(i+1)+" Balance :"+getAccount(i).getBalance());
    }
}

------------------------------------
               Teller
------------------------------------

import Banking.*;

public class Teller {
    public static void main(String args[]){
     
        Customer cust = new Customer("Somchai","Sommut");
        Account acct1 = new Account(5000);
        Account acct2 = new Account(3000);
     
        cust.addAccount(acct1);
        cust.addAccount(acct2);
 
        acct1.deposit(4200);
        acct1.withdraw(3000);
     
        acct2.deposit(3200);
        acct2.withdraw(1000);
     
        System.out.println(cust.getFirstName()+"  "+cust.getLastName());
        System.out.println("Number of Account : "+cust.getNumAccount());
     
        cust.printReport();
    }  
}

15 สิงหาคม 2554

[JAVA]โปรแกรมจำลองระบบธนาคาร แบบฝึกหัดที่ 5.1


---------- Account Class ----------

package Banking;

public class Account {
    private double balance;
    public Account(double amount){
        balance = amount;
    }
    public void deposit(double amount){
        balance = balance + amount;
    }
    public void withdraw(double amount){
            balance = balance - amount;
    }
    public double getBalance(){
        return balance;
    }
    public void showBalance(){
        System.out.println(balance);
    }
    public Account(){
     
    }
}


---------- Customer Class ----------

package Banking;

public class Customer {
    private String firstName;
    private String lastName;
    private Account acct;

    public Customer(String fName,String lName){
        firstName = fName;
        lastName = lName;
    }
    public void setFirstName(String fName){
        firstName = fName;
    }
    public String getFirstName(){
        return firstName;
    }
    public void setLastName(String lName){
        lastName = lName;
    }
    public String getLastName(){
        return lastName;
    }
    public void setAccount(Account acct){
        acct = acct;
    }
    public Account getAccount(){
        return acct;
    }
}

---------- Teller Class ----------

import Banking.*;

public class Teller {
    public static void main(String args[]){
        boolean result;
        Account myAccount = new Account(4000);
        Customer cust = new Customer("Somchai","Sommut");
        cust.setAccount(myAccount);
        result = myAccount.withdraw(3000);
        myAccount.deposit(4200);
        myAccount.showBalance();
        System.out.println("Withdraw = "+result);
        System.out.println(cust.getFirstName()+" "+cust.getLastName());
    }  
}

[JAVA]การเขียน Constructor และเมธอดแบบ Overload แบบฝึกหัดที่ 5.4


------------------------------------
                Main
------------------------------------

public class TestRectangle {
    public static void main(String args[]){
        Rectangle rect1 = new Rectangle();
        Rectangle rect2 = new Rectangle(10.5,8,"Red");
        rect1.setWidth(12.0);
        rect1.setHeight(5.5);
        rect1.setColor("Yellow");
        System.out.println("Rect1 : Width = "+rect1.getWidth()+", Height = "+rect1.getHeight()+", Color = "+rect1.getColor());
        System.out.println("Rect2 : Whith = "+rect2.getWidth()+", Height = "+rect2.getHeight()+", Color = "+rect2.getColor());  
        System.out.println("Area1 : "+rect1.findArea()+"\n"+"Area2 : "+rect2.findArea());
    }
}

------------------------------------
             Rectangle
------------------------------------

public class Rectangle {
    private double width =1;
    private double height = 1;
    private static String color = "white";
    public Rectangle(){
     
    }
    public Rectangle(double w,double h,String c){
        width=w;
        height=h;
        color=c;
    }
    public void setWidth(Double w){
        width=w;
    }
    public double getWidth(){
        return width;
    }
    public void setHeight(double h){
        height=h;
    }
    public double getHeight(){
        return height;
    }
    public void setColor(String c){
        color=c;
    }
    public String getColor(){
        return color;
    }
    public double findArea(){
        return width*height;
    }
}

[JAVA]การเขียนโปรแกรมที่ใช้หลักการเชิงออปเจ็ค แบบฝึกหัดที่ 5.3


------------------------------------
             Account
------------------------------------

package Banking;

public class Account {
    protected double balance;
    public Account(double amount){
        balance = amount;
    }
    public void deposit(double amount){
        balance = balance + amount;
    }
    public boolean withdraw(double amount){
        boolean result = true;
        if(amount < balance){
            balance = balance - amount;
        }
        else{
            result = false;
        }
        return result;
    }
    public double getBalance(){
        return balance;
    }
    public void showBalance(){
        System.out.println(balance);
    }
    public Account(){
     
    }
}

------------------------------------
             Customer
------------------------------------

package Banking;

public class Customer {
    private String firstName;
    private String lastName;
    private Account acct[];
    private int AccountNum=0;
    public Customer(String fName,String lName){
        firstName = fName;
        lastName = lName;
        acct = new Account[20];
    }
    public void setFirstName(String fName){
        firstName = fName;
    }
    public String getFirstName(){
        return firstName;
    }
    public void setLastName(String lName){
        lastName = lName;
    }
    public String getLastName(){
        return lastName;
    }
    public void setAccount(Account acct){
        acct = acct;
    }
    public Account getAccount(int index){
        return acct[index];
    }
    public void addAccount(Account acct){
        this.acct[AccountNum]=acct;
        AccountNum++;
    }
    public int getAccountNum(){
        return AccountNum;
    }
    public void printReport()
    {
        for(int i=0;i<getAccountNum();i++)
        System.out.println("Account"+(i+1)+" Balance :"+getAccount(i).getBalance());
    }
}

------------------------------------
           SavingAccount
------------------------------------

package Banking;

public class SavingAccount extends Account {

    public SavingAccount(double amount) {
        balance = amount;
    }
}

------------------------------------
            CheckingAccount
------------------------------------

package Banking;

public class CheckingAccount extends Account {
    private double credit;
    public CheckingAccount(double initBalance,double credit){
        balance = initBalance;
        credit=credit;
    }
    public double getCredit(){
        return credit;
    }
    @Override
    public boolean withdraw(double amount){
        boolean result = true;
        if(amount < balance){
            balance = balance - amount;
        }
        else{
            if(amount > (balance + credit)){
                result = false;
            }
            else{
                balance = 0;
                credit = (balance + credit) - amount;
            }    
        }
        return result;
    }
}

------------------------------------
              Tailer
------------------------------------

import Banking.*;

public class Teller {
    public static void main(String args[]){
     
        Customer cust = new Customer("Somchai","Sommut");
        SavingAccount acct1 = new SavingAccount(3000);
        CheckingAccount acct2 = new CheckingAccount(4000,2000);
     
        cust.addAccount(acct1);
        cust.addAccount(acct2);
     
        acct1.deposit(4200);
        acct1.withdraw(3000);
     
        acct2.deposit(3200);
        acct2.withdraw(1000);
     
        System.out.println(cust.getFirstName()+"  "+cust.getLastName());
        System.out.println("Number of Account : "+cust.getAccountNum());
     
        cust.printReport();
    }  
}

[JAVA]การเขียนเมธอด withdraw() แบบฝึกหัดที่ 5.2

---------- Account Class ----------

package Banking;

public class Account {
    private double balance;
    public Account(double amount){
        balance = amount;
    }
    public void deposit(double amount){
        balance = balance + amount;
    }
    public boolean withdraw(double amount){
        boolean result = true;
        if(amount < balance){
            balance = balance - amount;
        }
        else{
            result = false;
        }
        return result;
    }
    public double getBalance(){
        return balance;
    }
    public void showBalance(){
        System.out.println(balance);
    }
    public Account(){
     
    }
}


---------- Customer Class ----------

package Banking;

public class Customer {
    private String firstName;
    private String lastName;
    private Account acct;

    public Customer(String fName,String lName){
        firstName = fName;
        lastName = lName;
    }
    public void setFirstName(String fName){
        firstName = fName;
    }
    public String getFirstName(){
        return firstName;
    }
    public void setLastName(String lName){
        lastName = lName;
    }
    public String getLastName(){
        return lastName;
    }
    public void setAccount(Account acct){
        acct = acct;
    }
    public Account getAccount(){
        return acct;
    }
}

---------- Teller Class ----------

import Banking.*;

public class Teller {
    public static void main(String args[]){
        boolean result;
        Account myAccount = new Account(4000);
        Customer cust = new Customer("Somchai","Sommut");
        cust.setAccount(myAccount);
        result = myAccount.withdraw(3000);
        myAccount.deposit(4200);
        myAccount.showBalance();
        System.out.println("Withdraw = "+result);
        System.out.println(cust.getFirstName()+" "+cust.getLastName());
    }  
}

05 สิงหาคม 2554

[Assembly]โปรแกรม บวก ลบ โดยกำหนดค่าของตัวแปรอยู่ในรูปของเลขรหัส BCD (4หลัก)

.model small
.data
    SUM    dw 0
    START  dw 1234H
    VAdd   dw 3099H
    VSub   dw 2099H
.code  
    MOV ax,@data
    MOV ds,ax
 
;-------------------- Addition
 
MOV DX,START     ;load START
MOV BX,VADD        ;load ValueAddition
    MOV AL,BL       ;sum BL with DL
    ADD AL,DL
    DAA             ;adjust
    MOV CL,AL       ;answer to CL
    MOV AL,BH       ;sum BH, DH, and carry
    ADC AL,DH
    DAA             ;adjust
    MOV CH,AL       ;answer to CH

;-------------------- Subtraction

    MOV DX,Vsub     ;load ValueAddition
    MOV BX,CX        ;load Answer of Addition
MOV AL,BL       ;subtract DL from BL
    SUB AL,DL
DAS             ;adjust
MOV CL,AL       ;answer to CL
  MOV AL,BH       ;subtract DH, and carry
SBB AL,DH
DAS             ;adjust
    MOV CH,AL       ;answer to CH

    MOV ax,4c00h
    INT 21h
end

[Assembly]โปรแกรม บวก ลบ คูณ หาร แบบคิดตัวทด

.model small
.data
CSUM   DW 0
CREN   DW 0
COM    DB 0
CADD   DB 24
CSUB   DB 10
CMUL   DB 3
CDIV   DB 4
.code
mov ax,@data
mov ds,ax

mov ax,CSUM
mov al,COM
add al,CADD
adc ah,0
     
sub al,CSUB
sbb ah,0
 
 mov bl,CMUL
mov bh,0
mul bx
 
 mov bl,CDIV
div bx
mov CSUM,ax
mov ax,0
div bx
mov CREN,ax

mov ax,4c00h
int 21h
end

[Assembly]โปรแกรม บวก ลบ คูณ หาร แบบปัดเศษทศนิยม

.model small
.data
CSUM   DW 0
COM    DB 0
CADD   DB 24
CSUB   DB 8
CMUL   DB 4
CDIV   DB 3
.code
mov ax,@data
mov ds,ax

mov ax,CSUM
mov al,COM
add al,CADD
adc ah,0
     
sub al,CSUB
sbb ah,0
 
 mov bl,CMUL
mov bh,0
mul bx
 
 mov bl,CDIV
div bx
add ah,ah
cmp ah,bl
jb NEXT
inc al

NEXT:  mov CSUM,ax

 mov ax,4c00h
int 21h
end

17 กรกฎาคม 2554

โจทย์ พร้อมตัวอย่างเฉลยโปรแกรม พิรามิด ทั้ง 36 ข้อ ^ ^ (ชุดที่ 4)

ข้อที่ 25.

****5*****
***444****
**33333***
*2222222**
111111111*

public class SuperLoop25 {
    public static void main(String den[]){
        int z=5;
        for(int i=1;i<=z;i++){
            for(int j=i;j<z;j++){
                System.out.print("*");
            }
            for(int j=1;j<=i*2-1;j++){
                System.out.print(z-i+1);
            }
            for(int j=i;j<=z;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }   
}
ข้อที่ 26.

11111*
2222**
333***
44****
5*****

public class SuperLoop26 {
    public static void main(String den[]){
        int z=5;
        for(int i=1;i<=z;i++){
            for(int j=i;j<=z;j++){
                System.out.print(i);
            }
            for(int j=1;j<=i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
ข้อที่ 27.

1 *
2  **
3   ***
4    ****
5     *****

public class SuperLoop27 {
    public static void main(String den[]){
        int z=5;
        for(int i=1;i<=z;i++){
            System.out.print(i);
            for(int j=1;j<=i;j++){
                System.out.print(" ");
            }
            for(int j=1;j<=i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

ข้อที่ 28.

54321*12345
5432***2345
543*****345
54*******45
5*********5

public class SuperLoop28 {
    public static void main(String den[]){
        int z=5;
        for(int i=1;i<=z;i++){
            for(int j=z;j>=i;j--){
                System.out.print(j);
            }
            for(int j=1;j<=i*2-1;j++){
                System.out.print("*");
            }
            for(int j=i;j<=z;j++){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}
ข้อที่ 29.

1*3***
2*4****
3*5*****
4*6******
5*7*******

public class SuperLoop29 {
    public static void main(String den[]){
        int z=5;
        for(int i=1;i<=z;i++){
            System.out.print(i+"*"+(i+2));
            for(int j=1;j<=i+2;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
ข้อที่ 30.

1234567***3
123456****4
12345*****5
1234******6
123*******7

public class SuperLoop30 {
    public static void main(String den[]){
        int z=3;
        for(int i=1;i<=z*2-1;i++){
            for(int j=1;j<=(z*2-i+2);j++){
                System.out.print(j);
            }
            for(int j=z;j<=z+i+1;j++){
                System.out.print("*");
            }
            System.out.println(z-(z-2)+i);
        }
    }
}
ข้อที่ 31.

*********1
 *******21
  *****321
   ***4321
    *54321

public class SuperLoop31 {
    public static void main(String den[]){
        int z=5;
        for(int i=1;i<=z;i++){
            for(int j=1;j<i;j++){
                System.out.print(" ");
            }
            for(int j=i;j<=z*2-i;j++){
                System.out.print("*");
            }
            for(int j=i;j>=1;j--){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}
ข้อที่ 32.

11111*****
2222  ****
333    ***
44      **
5        *

public class SuperLoop32 {
    public static void main(String den[]){
        int z=5;
        for(int i=1;i<=z;i++){
            for(int j=i;j<=z;j++){
                System.out.print(i);
            }
            for(int j=1;j<i*2-1;j++){
                System.out.print(" ");
            }
            for(int j=i;j<=z;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

ข้อที่ 33.

    *
   ***
  *****
   ***
    *

public class SuperLoop33 {
    public static void main(String den[]){
        int z=4;
            for(int i=1;i<=z;i++){
                for(int j=i;j<=z+1;j++){
                    System.out.print(" ");
                }
                for(int j=1;j<=i*2-1;j++){
                    System.out.print("*");
                }
                System.out.println();
            }
            for(int i=1;i<z;i++){
                for(int j=1;j<=i+2;j++){
                    System.out.print(" ");
                }
                for(int j=i;j<=(z-1)*2-i;j++){
                    System.out.print("*");
                }
                System.out.println();
            }
      
    }
}
ข้อที่ 34.

    *
   *2*
  *232*
   *2*
    *

public class SuperLoop34 {
    public static void main(String den[]){
        int z=5;
        for(int i=1;i<=z;i++){
           for(int j=i;j<=z+1;j++){
               System.out.print(" ");
           }
           System.out.print("*");
           for(int j=1;j<i;j++){
               System.out.print(j+1);
           }
           for(int j=i;j>2;j--){
               System.out.print(j-1);
           }
           if (i!=1) System.out.print("*");
           /*for(int j=z;j<=z;j++){
               System.out.print("*");
           }*/
           System.out.println();
        }
        for(int i=1;i<=z-1;i++){
            for(int j=z-1;j<=z+i;j++){
                System.out.print(" ");
            }
            System.out.print("*");
            for(int j=2;j<=z-i;j++){
                System.out.print(j);
            }
            for(int j=z-i;j>2;j--){
                System.out.print(j-1);
            }
            if (i!=z-1) System.out.print("*");
            System.out.println();
        }
    }   
}

ข้อที่ 35.

   1*1*2
   2*3*4
   3*5*6
   4*7*8
   5*9*10

public class SuperLoop35 {
    public static void main(String den[]){
        int z=5;
        for(int i=1,j=1,k=2;i<=z;i++,j+=2,k+=2){
            System.out.println(i+"*"+j+"*"+k);
        }
    }
}
ข้อที่ 36.

123*321
23***32
3*****3
23***32
123*321

public class SuperLoop36 {
    public static void main(String den[]){
        int z=3;
        for(int i=1;i<=z;i++){
            for(int j=i;j<=z;j++){
                System.out.print(j);
            }
            for(int j=1;j<=i*2-1;j++){
                System.out.print("*");
            }
            for(int j=z;j>=i;j--){
                System.out.print(j);
            }
            System.out.println();
        }
        for(int i=z-1;i>=1;i--){
            for(int j=i;j<=z;j++){
                System.out.print(j);
            }
            for(int j=1;j<=i*2-1;j++){
                System.out.print("*");
            }
            for(int j=z;j>=i;j--){
                System.out.print(j);
            }
            System.out.println();
        }
    }   
}