Tao file header file trong C++ nhu the nao

http://blog.sangnd.com/2013/01/cc-tach-chuong-trinh-lon-thanh-cac-file-rieng.html

Vi du:

File: nguyento.h

int nguyento(int n);

File: nguyento.cpp

#include "nguyento.h"
#include <math.h>

int nguyento(int n) {
    if (n<2) return -1;
    int k = sqrt(n);
    for (int i=2; i<=k; i++)
        if (n%i == 0) return i;
    return 0;
}

File: main.cpp

#include "nguyento.h"
#include <iostream>

using namespace std;

int main() {
    int n;
    cout << "Nhap N = "; cin >> n;

    int k = nguyento(n);

    if (k > 0) cout << n << " khong phai so nguyen to vi chia het cho " << k << endl;
        else if (k == 0) cout << n << " la so nguyen to" << endl;
            else cout << n << " khong phai so nguyen to vi nho hon 2" << endl;
}

Cach bien dich:

g++ nguyento.cpp main.cpp
./a.out

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: