Showing posts with label trong. Show all posts
Showing posts with label trong. Show all posts
Wednesday, May 3, 2017
define trong láºp trình c là gì
define trong láºp trình c là gì
?ây là t? khóa ti?n x? lý, dùng ?? ??nh ngh?a tên m?t h?ng ho?c ki?u gì ?ó
. nó giúp code "nhanh g?n" h?n nh?ng nh?ng beginner c?ng ??ng quá l?m d?ng !
Có l? ?ã quen ki?u #define PI 3.14 .. ho?c #define m?t giá tr? nào ??y.
Có l? ?ã quen ki?u #define PI 3.14 .. ho?c #define m?t giá tr? nào ??y.
#define FOR(i,a,b) for (int i = a; i < b; i++)?ây là m?t vòng l?p for và b?n có th? s? d?ng nó ??n gi?n v? sau b?ng cú pháp: FOR(i,3,5); (ch?y t? 3 -> 5)
.. ho?c FOR(i, 1, 100); (ch?y t? 1->100)
#define for(i,n) for(int i=0;i<n; i++)ch?y t? 0 ??n bé h?n n. dùng for(i, 5); t?c s? cho ch?y t? 0 ??n <5. thay vì c? ph?i code l?i dài dòng, nh? v?y ng?n h?n ph?i không?
#define R1 return 1th?m chí define cho c? giá tr? tr? v? luôn.
#define CL(a,x) memset(x, a, sizeof(x));nhìn hàm memset ch?c c?ng bi?t r?i nh?. (c?n có th? vi?n string).
#define filein freopen("in.txt","r",stdin)Có th? dùng cho c? thao tác m? file.(t?t nhiên là ?óng c?ng ???c).
#define pr(x) printf("Case %d: ",x) in m?t s? nguyên x b?ng cú pháp pr(x);
#define sc(n) scanf("%d",&n) nh?p m?t s? nguyên n b?ng cú pháp sc(n);
#define C1(x) cin >> x // gõ C1(x); có v? nhanh h?n cin >> x;
#define C2(x, y) cin >> x >> y // t??ng t? nh?ng nh?p 2 s?
#define C3(x, y, z) cin >> x >> y >> z // nh?p 3 s?nh?p xu?t thông th??ng..
#define pb push_backpush_back c?ng ng?n h?n nhi?u r?i.(c?n có th? vi?n vector)
#define max(a, b) ((a) > (b) ? (a) : (b))tìm max 2 s? a và b b?ng cú pháp max(a, b);
Và còn nhi?u n?a .Nh?ng ?? tìm max cho b?t k? ki?u nào (int, float, double..) thì sao?
B?n có nh? template là gì? là m?t t? khóa c?a C++ ??c tr?ng cho vi?c t?ng quát hóa vi?c x? lý v?i các ki?u d? li?u khác nhau.
Và còn nhi?u n?a .Nh?ng ?? tìm max cho b?t k? ki?u nào (int, float, double..) thì sao?
B?n có nh? template là gì? là m?t t? khóa c?a C++ ??c tr?ng cho vi?c t?ng quát hóa vi?c x? lý v?i các ki?u d? li?u khác nhau.
template <class T> inline T max(T a, T b){ return a > b ? a : b;}tìm max 2 s? a và b thì ch? c?n dùng cú pháp max(a, b); là xong
// tính a^2 b?ng cú pháp sq(a);
// ??c s? chung l?n nh?t c?a 2 s? a, b b?ng cú pháp gcd(a, b);
// .nh? nh?t
// ch? là m?t hàm Pow thôi.
// ki?m tra s? nguyên t?.
.. còn r?t nhi?u n?a và b?n có th? t? ngh? ra. tham kh?o thêm typedef
có th? m?t s? không c?n thi?t l?m nh?ng m?c ?ích c?a bài này là gi?i thi?u cho b?n bi?t nh?ng kh? n?ng c?a #define và template có th? làm ???c mà b?n ch?a bi?t.

template<class T>T sq(T a){ return (a*a); }// tính a^2 b?ng cú pháp sq(a);
template<class T>T gcd(T a,T b){ return (b==0) ? a : gcd(b,a%b); }// ??c s? chung l?n nh?t c?a 2 s? a, b b?ng cú pháp gcd(a, b);
template<class T>T lcm(T a,T b){ return (a/gcd(a,b))*b; }// .nh? nh?t
template<class T>T Pow(T n,T p) { T res=n; for(T i=1;i<p; i++){ res *= n; } return res; }// ch? là m?t hàm Pow thôi.
template<class T>bool isPrime(T n){ for(T i=2; i*i<=n; i++){ if(n%i==0) return false; } return true; }// ki?m tra s? nguyên t?.
.. còn r?t nhi?u n?a và b?n có th? t? ngh? ra. tham kh?o thêm typedef
có th? m?t s? không c?n thi?t l?m nh?ng m?c ?ích c?a bài này là gi?i thi?u cho b?n bi?t nh?ng kh? n?ng c?a #define và template có th? làm ???c mà b?n ch?a bi?t.

Go to link Download
Wednesday, March 22, 2017
Delete with CheckBox Java swing gui Xóa dữ liệu bảng bằng ChẹckBox trong Java Swing
Delete with CheckBox Java swing gui Xóa dữ liệu bảng bằng ChẹckBox trong Java Swing

by: http://www.thaicreate.com/java/java-gui-workshop-delete-multiple-rows-checkbox.html
Delete with CheckBox Java swing
Java 2016
package com.java.myapp;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MyForm extends JFrame {
static JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
MyForm frame = new MyForm();
frame.setVisible(true);
}
});
}
/**
* Create the frame.
*/
public MyForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 516, 319);
setTitle("ThaiCreate.Com Java GUI Tutorial");
getContentPane().setLayout(null);
// Customer List
JLabel lblCustomerList = new JLabel("Customer List");
lblCustomerList.setBounds(207, 44, 87, 14);
getContentPane().add(lblCustomerList);
// ScrollPane
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(28, 84, 440, 89);
getContentPane().add(scrollPane);
// Table
table = new JTable();
scrollPane.setViewportView(table);
// Button Delete
JButton btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] options = { "Yes", "No" };
int n = JOptionPane
.showOptionDialog(null, "Do you want to Delete data?",
"Confirm to Delete?",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[1]);
if (n == 0) // Confirm Delete = Yes
{
for (int i = 0; i < table.getRowCount(); i++) {
Boolean chkDel = Boolean.valueOf(table.getValueAt(i, 0).toString()); // Checked
if(chkDel) // Checked to Delete
{
String strCustomerID = table.getValueAt(i, 1)
.toString(); // get CustomerID
DeleteData(strCustomerID); // Delete Data
}
}
JOptionPane.showMessageDialog(null, "Delete Data Successfully");
PopulateData(); // Reload Table
}
}
});
btnDelete.setBounds(205, 202, 89, 23);
getContentPane().add(btnDelete);
PopulateData();
}
private void PopulateData() {
// Clear table
table.setModel(new DefaultTableModel());
// Model for Table
DefaultTableModel model = new DefaultTableModel() {
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return Boolean.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
case 4:
return String.class;
case 5:
return String.class;
case 6:
return String.class;
default:
return String.class;
}
}
};
table.setModel(model);
// Add Column
model.addColumn("Select");
model.addColumn("CustomerID");
model.addColumn("Name");
model.addColumn("Email");
model.addColumn("CountryCode");
model.addColumn("Budget");
model.addColumn("Used");
Connection connect = null;
Statement s = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost/mydatabase"
+ "?user=root&password=root");
s = connect.createStatement();
String sql = "SELECT * FROM customer ORDER BY CustomerID ASC";
ResultSet rec = s.executeQuery(sql);
int row = 0;
while ((rec != null) && (rec.next())) {
model.addRow(new Object[0]);
model.setValueAt(false, row, 0); // Checkbox
model.setValueAt(rec.getString("CustomerID"), row, 1);
model.setValueAt(rec.getString("Name"), row, 2);
model.setValueAt(rec.getString("Email"), row, 3);
model.setValueAt(rec.getString("CountryCode"), row, 4);
model.setValueAt(rec.getFloat("Budget"), row, 5);
model.setValueAt(rec.getFloat("Used"), row, 6);
row++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Delete
private void DeleteData(String strCustomerID) {
Connection connect = null;
Statement s = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost/mydatabase"
+ "?user=root&password=root");
s = connect.createStatement();
String sql = "DELETE FROM customer WHERE " + "CustomerID = "
+ strCustomerID + " ";
s.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Go to link Download
Saturday, March 4, 2017
Create an array of 10 000 random interger number elements array A Tạo Radom Array Integer trong C
Create an array of 10 000 random interger number elements array A Tạo Radom Array Integer trong C
Create an array of 10,000 random interger number elements (array A)
=========================================================================
27212 23947 7446 19659 1052 5771 21256 11110 32057 10666
26250 12670 13145 3925 31148 5874 25774 7043 9429 5536
6763 20016 21858 23934 7301 2061 32655 3815 21174 30582
10139 12082 14024 20900 1575 19337 15376 11300 19424 15864
20537 2695 18525 18629 22851 29788 30794 31744 21706 10075
T?o Radom v?i m?ng Integer trong C
C 2016
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int i;
int arraysize = 100; //cho chay 100 vong test!
int numbers[arraysize]; //Khai bao mang kich thuoc 100
srand(time(NULL));
//Gan moi so Random vao mang thu i
for(i=0; i<arraysize ; i++){
numbers[i] = rand()%10000;
}
//In ra man hinh mang vua Random
for (i=0;i< arraysize;i++)
{
printf("%d ",numbers[i]);
}
return 0;
}

Go to link Download
Subscribe to:
Posts (Atom)