Timer class for C/C++ – run on all Linux/Windows systems

File  header timer.h:

#ifndef TIMER_H
#define TIMER_H
#include <time.h>
class Timer
{
public:
Timer();
~Timer();
void start();
float Get_Elapsed_restart();
float Get_Elapsed();
float Get_Elapsed_s();
float Get_Elapsed_s_restart();
private:
clock_t starttime, finaltime;
};
#endif
File timer.cpp:

#include “timer.h”
Timer::Timer()
{
}
Timer::~Timer()
{
}

void Timer::start()
{
starttime=clock();
}

float Timer::Get_Elapsed_restart()
{
finaltime=clock();
float val=(float)(finaltime-starttime);
starttime=finaltime;
return val;
}

float Timer::Get_Elapsed()
{
finaltime=clock();
return (float)(finaltime-starttime);
}

float Timer::Get_Elapsed_s()
{
return (float)Get_Elapsed()/CLOCKS_PER_SEC;
}

float Timer::Get_Elapsed_s_restart()
{
return (float)Get_Elapsed_restart()/CLOCKS_PER_SEC;
}

Chương trình C/C++ hiển thị các file bị ẩn do virus – C/C++ program that shows hidden file by virus

// chuong trinh chuyen doi cac thu muc bi virus bien thanh an (hidden)
// sang dang binh thuong
// Ngay 10/06/2007
// Tac gia: Nguyen Huu Tuan
// De test chuong trinh, copy file .exe vao thu muc nao do
// tao thu muc an bang lenh: attrib +h +r +s <ten thu muc>
// Sau do chay thu chuong trinh
// Chuong trinh duoc dich bang C-Free, DevCpp, Visual C++ 2005

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main()
{
system(“dir /b /a > list.txt”);
ifstream fi(“list.txt”);
ofstream fo(“done.bat”);
char str[255];
fo << “@echo off” << endl;
while(fi.getline(str,255))
fo << “attrib -a -s -h -r \”” << str << “\”” << endl;
fi.close();
fo.close();
system(“done.bat”);
system(“del done.bat”);
system(“del list.txt”);
return 0;
}

Sắp xếp cơ bản – Basic sorting

Chương trình cài đặt các thuật toán sắp xếp cơ bản (sắp xếp nổi bọt (bubble sort), sắp xếp chọn (selection sort), sắp xếp chèn (insertion sort), sắp xếp nhanh (quick sort), sắp xếp trộn (merge sort), sắp xếp vun đống (heap sort)), so sánh thời gian thực hiện giữa các thuật toán và 1 hidden operation (can you find it out?).
Hình minh họa:
Vsort's image
Chương trình: download

Danh sách liên kết với ngôn ngữ C – Linked list in C programming language

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

// khai bao cau truc cho mot nut cua danh sach
typedef struct Node
{
// truong du lieu
int data;
struct Node * next;
} NodeType;

// khai bao kieu danh sach
typedef struct
{
NodeType * head;
NodeType * tail;
// so phan tu cua danh sach
int spt;
}LList;

// ham khoi tao danh sach
void init(LList * list);
// ham them 1 phan tu vao dau danh sach
void addFirst(LList *list, int d);
// ham them mot phan tu vao cuoi danh sach
void addLast(LList *list, int d);
// ham xoa phan tu o cuoi danh sach
int delLast(LList * list);
// ham xoa phan tu o dau danh sach
int delFirst(LList * list);
void insertAfter(LList * list, NodeType * p);
void insertBefore(LList * list, NodeType * p);
void removeAfter(LList * list, NodeType * p);
void removeBefore(LList * list, NodeType * p);
// ham in danh sach
void printList(LList list);
// ham sap xep danh sach
void sort(LList *list);
// ham tim kiem trong danh sach
NodeType * search(LList *, int d);
// giai phong toan bo danh sach
void freeAll(LList * list);

int main()
{
LList myList;
init(&myList);
addFirst(&myList, 10);
addFirst(&myList, 1);
addFirst(&myList, 12);
addLast(&myList, 20);
addLast(&myList, 23);
addLast(&myList, 25);
sort(&myList);
printList(myList);
freeAll(&myList);
printList(myList);
return 0;
}
void init(LList * list)
{
list->head = list->tail = NULL;
list->spt = 0;
}

void printList(LList list)
{
NodeType * tmp;
tmp = list.head;
while(tmp!=NULL)
{
printf(“%d “, tmp->data);
tmp = tmp->next;
}
printf(“\n”);
}

void addFirst(LList * list, int d)
{
NodeType * tmp = (NodeType *)malloc(sizeof(NodeType));
tmp->data = d;
if(list->spt==0)
{
tmp->next = NULL;
list->head = list->tail = tmp;
}
else
{
tmp->next = list->head;
list->head = tmp;
}
list->spt = list->spt+1;
}

void addLast(LList * list, int d)
{
NodeType * tmp = (NodeType *)malloc(sizeof(NodeType));
tmp->data = d;
tmp->next = NULL;
if(list->spt==0)
list->head = list->tail = tmp;
else
{
list->tail->next = tmp;
list->tail = tmp;
}
list->spt = list->spt+1;
}

NodeType * search(LList * list, int d)
{
NodeType * tmp = list->head;
while(tmp!=NULL)
{
if (tmp->data==d)
break;
tmp = tmp->next;
}
return tmp;
}

int delLast(LList * list)
{
NodeType * p, * q;
int ret = -1;

if(list->spt>0)
{
p = list->head;
q = NULL;
while(p->next!=NULL)
{
q = p;
p = p->next;
}
if(q!=NULL)
{
// danh sach chi co 1 phan tu
q->next = NULL;
list->tail = q;
}
else
list->head = list->tail = NULL;
ret = p->data;
free(p);
list->spt = list->spt-1;
}
return ret;
}

int delFirst(LList * list)
{
int ret=-1;
NodeType * tmp;
if(list->spt>0)
{
tmp = list->head;
if(list->spt==1)
{
// danh sach chi co 1 phan tu
ret = list->head->data;
list->head = list->tail = NULL;
}else
list->head = list->head->next;
free(tmp);
list->spt = list->spt – 1;
}
return ret;
}

// sap xep dung thuat toan doi cho truc tiep (interchange sort)
void sort(LList * list)
{
NodeType * p, * q;
int tmp;

p = list->head;
while(p!=NULL)
{
q = p->next;
while(q!=NULL)
{
if(q->data < p->data)
{
tmp = q->data;
q->data = p->data;
p->data = tmp;
}
q = q->next;
}
p = p->next;
}
}

void freeAll(LList * list)
{
NodeType * p, * q;
if(list->spt>0)
{
p = list->head;
list->head = list->tail = NULL;
list->spt = 0;
while(p)
{
q = p->next;
free(p);
p = q;
}
}
}

GSL 1.15 and 1.16 building with Visual Studio 2010

Update 05/02/2014: A good book for GSL (updated with 1.16 version) is GNU Scientific Library Reference Manual – Third Edition.

Update 14-11-2013: Building GSL 1.16 with Visual Studio 2010 (VC++ 10): In July, GSL 1.16 was released, the source code files can be obtained from this. If one wants to build it with Visual Studio 2012 (VC++ 11), the VS solution file can be downloaded from this URL and the same steps (from 1 to 6) as doing with GSL 1.15 and VS 2010 are needed. Based on VC++ 11 solution files, I have created solution for some one who still works with Visual Studio 2010 (VC++ 10), and that file can be downloaded from this address.

Today I build GSL 1.15 for using with Visual Studio 2010. After downloading GSL 1.15 source code, I download VS 2010 solution files and start building GSL’s .dll and .lib for my own. And these are the steps:
1. Exatracting file gsl-1.15-vc10.zip into a folder (GSL_build for example). You should have the folder GSL_build\gsl-1.15-vc10 after this step.
2. Exatracting file gsl-1.15.tar.gz into folder GSL_build\gsl-1.15.
3. Copy all gsl_*.h files from GSL_build\gsl-1.15 folder to GSL_build\gsl-1.15-vc10\gsl folder (you should do a search here and make a copy on the found files).
4. Copy all files from folder GSL_build\gsl-1.15 to GSL_build\gsl-1.15-vc10 folder, do not overwrite the two file templates_on.h and env.c or you will encounter errors with Release build.
5. Open file GSL_build\gsl-1.15-vc10\build.vc10\gsl.lib.sln with VS 2010 and start Debug and Release build.
6. Open file GSL_build\gsl-1.15-vc10\build.vc10\gsl.dll.sln with VS 2010 and start Debug and Release build. You may need to copy cblas.lib file from folder build.vc10\lib\Win32 to folder build.vc10\dll\Win32 (Debug or Release sub folder) to make this step runs.
7. If you encounter errors (like me), then these are the fixings:
+ file bspline.c: moving variables declarations to the first part of the function: gsl_bspline_greville_abscissa(size_t i, gsl_bspline_workspace *w)
+ file rk4imp.c: replacing sqrt(3) with M_SQRT3.
It’s done now. You should have your .dll in gsl_build\gsl-1.15-vc10\build.vc10\dll and .lib in the folder gsl_build\gsl-1.15-vc10\build.vc10\lib.
8. Notes:
+ When copying these above files and folders, do not over write any files.
+ You can build GSL with other blas library like Goto Blas, Atlas. Just replacing the cblas.lib with the suitable .lib file in building gsl.dll.
+ For using GSL, you need both .lib file in the gsl_build\gsl-1.15-vc10\build.vc10\lib folder and .dll file in the gsl_build\gsl-1.15-vc10\build.vc10\dll folder. Do not use .lib files from gsl_build\gsl-1.15-vc10\build.vc10\dll folder.
+ You can use examples from Physics 780.20 Computational Physics course for testing your .dll and .lib files.
+ Another source you can use is located at: Data Analysis with Open source Tools. This is a good book with good examples.

+ Even though I did not test the above process of building GSL with other Visual Studio versions (VS Express, VS 2008, VS 2012) but I strongly believe that the same details can be applied.

Rock – the music I like. Dead to the world – Nightwish.

Người (trong thiên hạ) có thể chia thành hai loại: một loại thích nhạc Rock và một loại chưa từng biết đến nhạc Rock.
Link 1 (original): Dead to the world – Nightwish
 
Link 2 (Anette vocal – better performance I think): Dead to the world – Nightwish

All the same take me away
We’re dead to the world

The child gave thought to the poet’s words
Gave comfort to the Fallen
(Heartfelt, lovelorn)

Remaining, yet still uninvited
Those words scented my soul
(Lonely soul, Ocean soul)

It’s not the monsters under your bed
It is the Man next door
That makes you fear, makes you cry,
Makes you cry for the child
All the wars are fought among those lonely men
Unarmed, unscarred

I don’t want to die a scarless man
A lonely soul
(Tell me now what to do)

I studied silence to learn the music
I joined the sinful to regain innocence

Heaven queen, cover me
In all that blue
Little boy, such precious joy
Is dead to the world

Heaven queen, carry me
Away from all pain
All the same take me away
We’re dead to the world

Dead, silent, constant
Yet always changing
– My favorite view of this world

As he died, he will return to die in me again
Weaving the cloth, giving birth to the Century Child
Who gave his life not for the world but for me
Innocence reborn once more

Heaven queen, carry me
Away from all pain
All the same take me away
We’re dead to the world

Heaven queen, cover me
In all that blue
Little boy, such precious joy
Is dead to the world

Heaven queen, carry me
Away from all pain
All the same take me away
We’re dead to the world

We’re dead to the world

Windows 7 services optimize or ‘Too many services will kill your machine’

Windows 7, cũng giống như các bậc đàn anh của nó là Windows Vista, XP, … được Microsoft attached với rất nhiều services, trên máy tính của tôi là 182 services. Tuy nhiên là một người dùng bình thường với các nhu cầu cơ bản: ứng dụng văn phòng (Office), duyệt web qua Wifi hoặc LAN, nghe nhạc, in ấn và chia sẻ với các máy tính khác (nhu cầu share càng ngày càng ít đi), kết nối với các thiết bị khác (máy ảnh số, Ipad,Iphone), lập trình bằng VS 2010 chúng ta cần không tới 1/5 trong số các dịch vụ đó. Việc đưa ra các dịch vụ này tất nhiên là có vai trò và mục đích riêng của chúng (bạn có thể send email hỏi các chuyên gia của MS). Theo tôi để đảm bảo các yêu cầu cơ bản và không tốn tài nguyên vào các dịch vụ mà bạn chẳng bao giờ dùng tới, chúng ta chỉ cần các dịch vụ sau:

1. Application Experience

2.Base Filtering Engine (nếu bạn không dùng IDM để download file thì cũng có thể say goodbye dịch vụ này).

3. CNG Key Isolation

4. COM+Event System

5. Cryptographic Services

6. DCOM Server Process Launcher

7. Desktop Windows Manager Session Manager

8. DHCP Client

9. Extensible Authentication Protocol

10. Group Policy Client

11. Multimedia Class Scheduler

12. Network Store Interface Service

13. Office Software Protection Platform

14. Plug and Play

15. Power

16. Remote Procedure Call (RPC)

17. RPC Endpoint Mapper

18. Security Accounts Manager

19. Superfetch

20. System Event Notification Service

21. Task Scheduler

22. Themese (nếu bạn không thích màu mè thì bỏ đi cũng vẫn OK)

23. User Profile Service

24. Windows Audio

25. Windows Audio Endpoint Builder

26. Windows Driver Foundation – User-mode Driver Framework

27. Windows Event Log

28. Windows Font Cache Service

29. Windows Management Instrumentation

30. WLAN AutoConfig

Các dịch vụ trên được để nguyên cấu hình sau khi cài đặt, các dịch vụ khác nếu chạy sau khi khởi động thì thiết lập chúng là Disable. Bạn cũng có thể dùng Task manager để kiểm tra bộ nhớ mà Windows sử dụng ngay sau khi máy khởi động xong. Nếu trên laptop mà chỉ mất khoảng 600 MB là OK.

Posted in Hệ điều hành Windows. Comments Off on Windows 7 services optimize or ‘Too many services will kill your machine’