2009년 11월 26일 목요일
2009년 11월 25일 수요일
2009년 11월 19일 목요일
File 입출력 기능의 LinkedList
#define LINKEDLIST_H
#include <fstream>
using namespace std ;
template <typename T>
class Node {
public:
T element ;
Node * next ;
Node() {
next = NULL ;
}
Node(T element) {
this->element = element ;
next = NULL ;
}
} ;
template <typename T>
class Iterator {
Node<T> * current ;
public:
Iterator(Node<T> * p) {
current = p ;
}
Iterator & operator++() {
current = current->next ;
return * this ;
}
T operator*() {
return current->element ;
}
bool operator==(const Iterator<T> & iter) {
return current == iter.current ;
}
bool operator!=(const Iterator<T> & iter) {
return current != iter.current ;
}
} ;
template <typename T>
class LinkedList {
Node<T> * head , * tail ;
int size ;
fstream fio ;
public:
LinkedList() ;
void addFirst(T element) ; //첫번째 원소로 추가
void addLast(T element) ; //마지막 원소로 추가
T getFirst() ; //첫번째 원소 반환
T getLast() ; //마지막 원소 반환
T removeFirst() ; //첫번째 원소 제거
T removeLast() ; //마지막 원소 제거
void add(T element) ; //원소 추가
void add(int index , T element) ; //해당 위치에 원소 추가
void clear() ; //전체 삭제
T get(int index) ; //특정 위치 원소 반환
bool isEmpty() ; // 비어있는지 검사
int getSize() ; //크기 반환
Iterator<T> begin() {
return Iterator<T>(head) ;
}
Iterator<T> end() {
return Iterator<T>(tail->next);
}
//bool contains(T element) ; //T가 포함되어 있는지 검사
//int indexOf(T element) ; //T의 원소의 위치 반환
//int lastIndexOf(T element); //T의 마지막 원소 위치 반환
//void remove(T element); //T 원소 검색해서 제거
//T removeAt(int index) ; //특정 위치의 원소 제거
//T set(int index , T element) ; //특정 위치의 원소 교체
void writeToFile(char * file) ;
void readFromFile(char * file) ;
};
template<typename T>
LinkedList<T>::LinkedList() {
head = tail = NULL ;
size = 0 ;
}
template <typename T>
void LinkedList<T>::addLast(T element) {
if( tail == NULL) {
head = tail = new Node<T>(element) ;
} else {
tail->next = new Node<T>(element) ;
tail = tail->next ;
}
size++ ;
}
template <typename T>
void LinkedList<T>::addFirst(T element) {
if( head == NULL) {
head = tail = new Node<T>(element) ;
} else {
Node<T> * temp = new Node<T>(element) ;
temp->next = head;
head = temp ;
}
size++ ;
}
template <typename T>
T LinkedList<T>::getFirst() {
if( size > 0 ) {
return head->element ;
}
return NULL ;
}
template <typename T>
T LinkedList<T>::getLast() {
if( size > 0 ) {
return tail->element ;
}
return NULL ;
}
template <typename T>
T LinkedList<T>::removeFirst() {
if( size > 0) {
Node<T> * t = head;
head = head->next ;
if( head == NULL) tail = NULL ;
size -- ;
T element = t->element ;
delete t ;
return element ;
}
return NULL ;
}
template <typename T>
T LinkedList<T>::removeLast() {
if(size == 1 ) {
Node<T> * t = head ;
head = tail = NULL ;
size = 0 ;
T element = t->element ;
delete t ;
return element ;
} else if( size > 1 ) {
Node<T> * current = head ;
for(int i = 0 ; i < size -2 ; i++)
current = current->next ;
Node<T> * t = tail ;
tail = current ;
tail->next = NULL ;
size -- ;
T element = t->element ;
delete t ;
return element ;
}
return NULL ;
}
template <typename T>
void LinkedList<T>::add(T element) {
addLast(element) ;
}
template <typename T>
void LinkedList<T>::add(int index , T element) {
if( index == 0 ) addLast(element) ;
else if(index >= size) addLast(element) ;
else {
Node<T> * current = head ;
for(int i = 1 ; i < index ; i++)
current = current->next ;
Node<T> * t = current->next;
current->next = new Node<T>(element) ;
(current->next)->next = t;
size ++ ;
}
}
template <typename T>
void LinkedList<T>::clear() {
while(head != NULL) {
Node<T> * t = head;
head = head->next ;
delete t ;
}
tail = NULL ;
}
template <typename T>
T LinkedList<T>::get(int index) {
Node<T> * current = head ;
for(int i = 0 ; i < index ; i++)
current = current->next ;
return current->element ;
}
template <typename T>
bool LinkedList<T>::isEmpty() {
return head == NULL ;
}
template <typename T>
int LinkedList<T>::getSize() {
return size ;
}
template <typename T>
void LinkedList<T>::writeToFile(char * file) {
fio.open(file , ios::out | ios::binary) ;
Node<T> * c = head ;
fio.write(reinterpret_cast<char *>(&size) , sizeof(int)) ;
for(int i = 0 ; i < size ; i++) {
fio.write(reinterpret_cast<char *>(& c->element) , sizeof(T)) ;
c = c->next ;
}
fio.close() ;
cout<<"File was writen successfully"<<endl ;
}
template <typename T>
void LinkedList<T>::readFromFile(char * file) {
fio.open(file , ios::in | ios::binary) ;
clear() ;
T data ;
int maxsize ;
fio.read(reinterpret_cast<char *>(&maxsize) , sizeof(int)) ;
for(int i = 0 ; i < maxsize ; i++) {
fio.read(reinterpret_cast<char *>(&data) , sizeof(T)) ;
addLast(data) ;
}
fio.close() ;
cout<<"Data Read Done!"<<endl ;
}
#endif
2009년 11월 6일 금요일
LinkedList Template
#define LINKEDLIST_H
template <typename T>
class Node {
public:
T element ;
Node * next ;
Node() {
next = NULL ;
}
Node(T element) {
this->element = element ;
next = NULL ;
}
} ;
template <typename T>
class LinkedList {
Node<T> * head , * tail ;
int size ;
public:
LinkedList() ;
void addFirst(T element) ; //첫번째 원소로 추가
void addLast(T element) ; //마지막 원소로 추가
T getFirst() ; //첫번째 원소 반환
T getLast() ; //마지막 원소 반환
T removeFirst() ; //첫번째 원소 제거
T removeLast() ; //마지막 원소 제거
void add(T element) ; //원소 추가
void add(int index , T element) ; //해당 위치에 원소 추가
void clear() ; //전체 삭제
T get(int index) ; //특정 위치 원소 반환
bool isEmpty() ; // 비어있는지 검사
int getSize() ; //크기 반환
//bool contains(T element) ; //T가 포함되어 있는지 검사
//int indexOf(T element) ; //T의 원소의 위치 반환
//int lastIndexOf(T element); //T의 마지막 원소 위치 반환
//void remove(T element); //T 원소 검색해서 제거
//T removeAt(int index) ; //특정 위치의 원소 제거
//T set(int index , T element) ; //특정 위치의 원소 교체
};
template<typename T>
LinkedList<T>::LinkedList() {
head = tail = NULL ;
size = 0 ;
}
template <typename T>
void LinkedList<T>::addLast(T element) {
if( tail == NULL) {
head = tail = new Node<T>(element) ;
} else {
tail->next = new Node<T>(element) ;
tail = tail->next ;
}
size++ ;
}
template <typename T>
void LinkedList<T>::addFirst(T element) {
if( head == NULL) {
head = tail = new Node<T>(element) ;
} else {
Node<T> * temp = new Node<T>(element) ;
temp->next = head;
head = temp ;
}
size++ ;
}
template <typename T>
T LinkedList<T>::getFirst() {
if( size > 0 ) {
return head->element ;
}
return NULL ;
}
template <typename T>
T LinkedList<T>::getLast() {
if( size > 0 ) {
return tail->element ;
}
return NULL ;
}
template <typename T>
T LinkedList<T>::removeFirst() {
if( size > 0) {
Node<T> * t = head;
head = head->next ;
if( head == NULL) tail = NULL ;
size -- ;
T element = t->element ;
delete t ;
return element ;
}
return NULL ;
}
template <typename T>
T LinkedList<T>::removeLast() {
if(size == 1 ) {
Node<T> * t = head ;
head = tail = NULL ;
size = 0 ;
T element = t->element ;
delete t ;
return element ;
} else if( size > 1 ) {
Node<T> * current = head ;
for(int i = 0 ; i < size -2 ; i++)
current = current->next ;
Node<T> * t = tail ;
tail = current ;
tail->next = NULL ;
size -- ;
T element = t->element ;
delete t ;
return element ;
}
return NULL ;
}
template <typename T>
void LinkedList<T>::add(T element) {
addLast(element) ;
}
template <typename T>
void LinkedList<T>::add(int index , T element) {
if( index == 0 ) addLast(element) ;
else if(index >= size) addLast(element) ;
else {
Node<T> * current = head ;
for(int i = 1 ; i < index ; i++)
current = current->next ;
Node<T> * t = current->next;
current->next = new Node<T>(element) ;
(current->next)->next = t;
size ++ ;
}
}
template <typename T>
void LinkedList<T>::clear() {
while(head != NULL) {
Node<T> * t = head;
head = head->next ;
delete t ;
}
tail = NULL ;
}
template <typename T>
T LinkedList<T>::get(int index) {
Node<T> * current = head ;
for(int i = 0 ; i < index ; i++)
current = current->next ;
return current->element ;
}
template <typename T>
bool LinkedList<T>::isEmpty() {
return head == NULL ;
}
template <typename T>
int LinkedList<T>::getSize() {
return size ;
}
#endif
2009년 10월 21일 수요일
2009년 10월 16일 금요일
2009년 10월 15일 목요일
2009년 10월 9일 금요일
2009년 9월 25일 금요일
2009년 9월 18일 금요일
2009년 9월 11일 금요일
2009년 9월 5일 토요일
2009년 6월 12일 금요일
2009년 6월 5일 금요일
MYSQL 데이터 베이스와 테이블 구성 방법
(C) Copyright 1985-2005 Microsoft Corp.
C:\Users\david>
C:\Users\david>mysql -uwww -pwww
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.34-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
C:\Users\david>mysql -uroot -padmin
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.1.34-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases ;
+--------------------+
Database
+--------------------+
information_schema
mysql
shoppingmall
test
+--------------------+
4 rows in set (0.00 sec)
mysql> create database shopppingmall ;
Query OK, 1 row affected (0.01 sec)
mysql> show databases ;
+--------------------+
Database
+--------------------+
information_schema
mysql
shop
shoppingmall
test
+--------------------+
5 rows in set (0.00 sec)
mysql> grant all privileges on shoppingmall.* to www@localhost identified by 'www' ;
Query OK, 0 rows affected (0.06 sec)
mysql> exit
Bye
C:\Users\david>mysql -uwww -pwww
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.1.34-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use shoppingmall ;
Database changed
mysql> show tables ;
Empty set (0.00 sec)
mysql> create table users(userid varchar(12) not null primary key ,
-> username varchar(30) not null ,
-> email varchar(30) , phone varchar(30) ,
-> address varchar(250) ) ;
Query OK, 0 rows affected (0.06 sec)
mysql> desc users ;
+----------+--------------+------+-----+---------+-------+
Field Type Null Key Default Extra
+----------+--------------+------+-----+---------+-------+
userid varchar(12) NO PRI NULL
username varchar(30) NO NULL
email varchar(30) YES NULL
phone varchar(30) YES NULL
address varchar(250) YES NULL
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.04 sec)
mysql> insert into users values('sss','df','fdsfdsf','1111','ffds') ;
Query OK, 1 row affected (0.00 sec)
mysql> select * from users ;
+--------+----------+---------+-------+---------+
userid username email phone address
+--------+----------+---------+-------+---------+
sss df fdsfdsf 1111 ffds
+--------+----------+---------+-------+---------+
1 row in set (0.00 sec)
mysql>
자바 데이터 베이스 프로그래밍 소스
import java.sql.*;
public class DBConnector {
public static Connection getConnection() {
Connection conn = null ;
try {
Class.forName("com.mysql.jdbc.Driver") ;
String url = "jdbc:mysql://localhost:3306/shoppingmall" ;
String username = "www" ;
String password = "www" ;
conn = DriverManager.getConnection(url , username , password) ;
}catch(Exception e) { e.printStackTrace() ; }
return conn ;
}
}
데이터 베이스 작업을 실제로 수행하게 되는 클래스 - DataHandler
import java.sql.*;
import java.util.*;
public class DataHandler {
public Vector
Vector
Connection connection = DBConnector.getConnection() ;
Statement st = null ;
try {
st = connection.createStatement() ;
ResultSet rs = st.executeQuery("select * from users") ;
while(rs.next()) {
User user = new User(rs.getString("userid") , rs.getString("username") ,
rs.getString("email") , rs.getString("phone") ,
rs.getString("address")) ;
list.add(user) ;
}
}catch(Exception e) { e.printStackTrace() ; }
finally {
try {
st.close(); connection.close() ;
}catch(Exception e) {}
}
return list;
}
public void addUser(User user) {
Connection conn = DBConnector.getConnection() ;
Statement st = null ;
try {
st = conn.createStatement() ;
String sql = "insert into users values('" + user.getUserid() + "','" ;
sql += user.getUsername() + "','" ;
sql += user.getEmail() + "','" ;
sql += user.getPhone() + "','" ;
sql += user.getAddress() + "')" ;
st.executeUpdate(sql) ;
st.close() ;
conn.close();
}catch(Exception e) {
e.printStackTrace() ;
}
}
public void removeUser(User user) {
Connection conn = DBConnector.getConnection() ;
Statement st = null ;
try {
st = conn.createStatement() ;
String sql = "delete from users where userid='" + user.getUserid() + "'" ;
st.executeUpdate(sql) ;
st.close() ;
conn.close();
}catch(Exception e) {
e.printStackTrace() ;
}
}
public void updateUser(User user) {
Connection conn = DBConnector.getConnection() ;
Statement st = null ;
try {
st = conn.createStatement() ;
String sql = "update users set username='" + user.getUsername() + "'," ;
sql += " email='" + user.getEmail() + "'," ;
sql += " phone='" + user.getPhone() + "'," ;
sql += " address='" + user.getAddress() +
"' where userid ='" + user.getUserid() + "'" ;
System.out.println(sql) ;
st.executeUpdate(sql) ;
st.close() ;
conn.close();
}catch(Exception e) {
e.printStackTrace() ;
}
}
}
사용자 정보를 담고 있어야 할 클래스 - User
public class User {
public User(String userid, String username, String email, String phone,
String address) {
super();
this.userid = userid;
this.username = username;
this.email = email;
this.phone = phone;
this.address = address;
}
String userid , username , email , phone , address ;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
실제로 사용자 관리를 위해 필요한 구동 애플리케이션 클래스 - UserManager
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
import java.util.*;
import java.awt.* ;
public class UserManager extends JFrame implements ActionListener {
private DataHandler handler ;
private JTable list;
private DefaultTableModel model ;
private static int NEW = 0 ;
private static int MODIFY = 1 ;
private static int STANDBY = -1 ;
private int mode = STANDBY ;
private int currentEditRow = -1 ;
private JButton newButton , updateButton , deleteButton ;
private JTextField userid , username , email , phone , address ;
public UserManager() {
handler = new DataHandler() ;
String [] columnNames = {"userid" , "username" , "email" ,
"phone","address"};
model = new DefaultTableModel(columnNames , 0) ;
list = new JTable(model) ;
add(list , BorderLayout.NORTH) ;
fillUserList(handler.getUserList()) ;
JPanel panel = new JPanel() ;
panel.setLayout(new GridLayout(5,2)) ;
JLabel lbl_id = new JLabel("User ID") ;
panel.add(lbl_id) ;
userid = new JTextField(40) ;
panel.add(userid) ;
JLabel lbl_name = new JLabel("User Name") ;
panel.add(lbl_name) ;
username = new JTextField(40) ;
panel.add(username) ;
JLabel lbl_email = new JLabel("Email") ;
panel.add(lbl_email) ;
email = new JTextField(40) ;
panel.add(email) ;
JLabel lbl_phone = new JLabel("Phone") ;
panel.add(lbl_phone) ;
phone = new JTextField(40) ;
panel.add(phone) ;
JLabel lbl_address = new JLabel("Address") ;
panel.add(lbl_address) ;
address = new JTextField(40) ;
panel.add(address) ;
add(panel , BorderLayout.CENTER) ;
JPanel buttonPanel = new JPanel() ;
buttonPanel.setLayout(new GridLayout(1,3)) ;
newButton = new JButton("New") ;
updateButton = new JButton("Update") ;
deleteButton = new JButton("Delete") ;
buttonPanel.add(newButton) ; buttonPanel.add(updateButton) ;
buttonPanel.add(deleteButton) ;
add(buttonPanel , BorderLayout.SOUTH) ;
newButton.addActionListener(this) ;
updateButton.addActionListener(this) ;
deleteButton.addActionListener(this) ;
list.setColumnSelectionAllowed(false) ;
list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int rownum = list.getSelectedRow() ;
if( rownum >= 0 ) {
userid.setText(model.getValueAt(rownum , 0).toString()) ;
username.setText(model.getValueAt(rownum , 1).toString()) ;
email.setText(model.getValueAt(rownum, 2).toString()) ;
phone.setText(model.getValueAt(rownum, 3).toString()) ;
address.setText(model.getValueAt(rownum,4).toString()) ;
mode = MODIFY ;
userid.setEditable(false) ;
currentEditRow = rownum ;
}
}
}) ;
}
public static void main(String[] args) {
UserManager app = new UserManager() ;
app.setSize(800,600) ;
app.setVisible(true) ;
}
public void fillUserList(Vector list) {
Iterator
while(iter.hasNext()) {
User current = iter.next();
Object[] o = new Object[5] ;
o[0] = current.getUserid() ;
o[1] = current.getUsername() ;
o[2] = current.getEmail() ;
o[3] = current.getPhone() ;
o[4] = current.getAddress() ;
model.addRow(o) ;
}
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand() ;
if( command.equals("New")) {
mode = NEW ;
clearFields() ;
} else if( command.equals("Update")) {
if( mode == NEW ) {
User current= getUserFromFields() ;
handler.addUser( current ) ;
model.addRow(getObjectFromUser(current)) ;
userid.setEditable(true) ;
clearFields() ;
} else if( mode == MODIFY) {
User current= getUserFromFields() ;
model.setValueAt(current.userid, currentEditRow, 0) ;
model.setValueAt(current.username, currentEditRow, 1) ;
model.setValueAt(current.email, currentEditRow, 2) ;
model.setValueAt(current.phone,currentEditRow,3) ;
model.setValueAt(current.address , currentEditRow , 4) ;
handler.updateUser(current) ;
}
} else if( command.equals("Delete")) {
if( mode == MODIFY ) {
handler.removeUser(getUserFromFields()) ;
model.removeRow(currentEditRow) ;
clearFields() ;
mode = STANDBY ;
}
}
}
public void clearFields() {
username.setText("") ; userid.setText("") ; email.setText("") ;
phone.setText("") ; address.setText("") ;
}
public Object [] getObjectFromUser(User user) {
Object [] o = new Object[5] ;
o[0] = user.getUserid() ;
o[1] = user.getUsername() ;
o[2] = user.getEmail() ;
o[3] = user.getPhone() ;
o[4] = user.getAddress() ;
return o ;
}
public User getUserFromFields() {
User user = new User(userid.getText(), username.getText(),
email.getText(), phone.getText(),
address.getText()) ;
return user ;
}
}