পৃষ্ঠাসমূহ

বৃহস্পতিবার, ২১ এপ্রিল, ২০১১

JDBC Connection

For Adding drivers go to Services then drivers(right click), add drivers then give address
Mysql connector:C:\Program Files\NetBeans 6.8\ide12\modules\ext\mysql-connector-java-5.1.6-bin.jar
oracle connectior:E:\app\1251\product\11.2.0\dbhome_2\jdbc\lib\ojdbc6_g.jar

Under project at Library (right click) add jar forlder then give the above address

Connection class:


import java.sql.*;

public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}

মঙ্গলবার, ১২ এপ্রিল, ২০১১

Euler path

#include iostream
using namespace std;
int graph[12][12];
int visited[12][12],n;
void traverse(int node);
int main(){
freopen("sam.txt","r",stdin);
int i,j,flag=0;
cin>>n;
int count=0;
for(i=0;i>graph[i][j];
count+=graph[i][j];
}

if(count%2){flag=1;break;}

}
if(flag){cout<<"\n No euler path is there\n";}
else traverse(0);
return 0;

}
void traverse(int node){
int i;
for(i=0;i

Huffman Coding

#include
#include "stdio.h"
#include "string.h"

using namespace std;

int freq[256];
int numc, numn = 0;
char code[32];
bool found;
char letter;

// Object for each node in the Huffman tree
class node
{
public:
int id;
char ch;
int frequency;
node *left;
node *right;

node()
{
id = 0;
ch = '\0';
frequency = 0;
left = NULL;
right = NULL;
}
};

// Element in the priority queue
class list
{
public:
node *n;
list* next;
};

// Finds the element in the queue with a frequency less than
// the paramter passed to the function
list *find(list *p, int a)
{
if(p->next == NULL) return p;
else
{
if(a <= p->next->n->frequency) return p;
else return find(p->next, a);
}
}

// Inserts a new node into the priority queue in order of increasing frequency
list *insert(list *h, node *in)
{
list *n1;

list *p = new list();
p->n = in;

if(p->n->frequency <= h->n->frequency)
{
p->next = h;
h = p;
}
else
{
n1 = find(h, p->n->frequency);

p->next = n1->next;
n1->next = p;
}
return h;
}

// Deletes a node from the front of the queue
list *ldel(list *h)
{
list *n = new list();
n = h;
h = n->next;
return h;
}

// Traverses the queue and prints out the data of each node
// Not required in the code. Used for testing purposes
void traverseList(list *h)
{
cout << h->n->ch << "(" << h->n->id << "): " << h->n->frequency << ", ";
if(h->next != NULL)
{
traverseList(h->next);
}
}

// Takes characters from an input and places them in an array
void input()
{
int num;

for(int i = 0; i < 256; i++)
{
freq[i] = 0;
}

freopen("sample3.txt", "r", stdin);
//freopen("output.txt", "w", stdout);

while(scanf("%c", &letter) == 1)
{
if(letter == '\n') continue;
else
{
num = letter;
freq[num]++;
}
}

for(int i = 0; i < 256; i++)
{
if(freq[i] > 0) numc++;
}
}

// Displays the frquency of each character from the input
void showFreq()
{
char letter;

for(int i = 0; i < 256; i++)
{
if(freq[i] > 0)
{
letter = i;
cout << letter << ": " << freq[i] << ", ";
}
}

cout << endl;
}

// Makes the priority queue using the characters taken as input
// List is ordered by increasing frequency of the character
list *makeList(list *h)
{
int j;

for(int i = 0; i < 256; i++)
{
if(freq[i] > 0)
{
node *m = new node();
numn++;
m->id = numn;
m->ch = i;
m->frequency = freq[i];
m->left = NULL;
m->right = NULL;
h->n = m;
h->next = NULL;
j = i;
break;
}
}

for(int i = j + 1; i < 256; i++)
{
if(freq[i] > 0)
{
node *m = new node();
numn++;
m->id = numn;
m->ch = i;
m->frequency = freq[i];
m->left = NULL;
m->right = NULL;
h = insert(h, m);
}
}
return h;
}

// Creates a Huffman tree using the Huffman algorithm
list *huffman(list *h)
{
for(int i = 0; i < numc - 2; i++)
{
node *n = new node();
n->ch = NULL;
n->frequency = 0;
numn++;
n->id = numn;
n->left = h->n;
n->frequency += h->n->frequency;
h = ldel(h);
n->right = h->n;
n->frequency += h->n->frequency;
h = ldel(h);
h = insert(h, n);
}
node *n = new node();

n->ch = NULL;
n->frequency = 0;
numn++;
n->id = numn;
n->left = h->n;
n->frequency += h->n->frequency;
h = ldel(h);
n->right = h->n;
n->frequency += h->n->frequency;
h->n = n;
return h;
}

// The following functions traverse the tree to find the code for each letter
// Backtracking is used
void goRight(node *r, char a);

void goLeft(node *r, char a)
{
if(r->ch == a)
{
found = true;
}
else
{
if(r->left != NULL) goLeft(r->left, a);
if(!found && r->right != NULL) goRight(r->right, a);
}
if(found) strcat(code, "1");
}

void goRight(node *r, char a)
{
if(r->ch == a)
{
found = true;
}
else
{
if(r->left != NULL) goLeft(r->left, a);
if(!found && r->right != NULL) goRight(r->right, a);
}
if(found) strcat(code, "0");
}

void printCode()
{
for(int i = strlen(code) -1; i >= 0; i--) cout << code[i];
}

void getCode(node *r, char a)
{
found = false;
strcpy(code, "");

if(r->left != NULL)
{
goLeft(r->left, a);
}
if(!found && r->right != NULL)
{
goRight(r->right, a);
}
printCode();
}

void showAllCodes(node *r)
{
for(int i = 0; i < 256; i++)
{
if(freq[i] > 0)
{
letter = i;
if(letter == 32) cout << "SPACE: ";
else cout << letter << ": ";
getCode(r, letter);
cout << endl;
}
}
}

// Main function
int main()
{
list *head = new list(); // Creates the head of the priority queue
node *root = new node(); // Creates the root node of the Huffman tree

input();
head = makeList(head);
head = huffman(head);
root = head->n; // Root points to the last node left in queue
showAllCodes(root);
}