2013년 8월 6일 화요일

[ 알고리즘 ] 유클리드 알고리즘

유클리드 알고리즘 최대공약수 샘플

int gcd(int u , int v)
{
    int t;
    while( u>0 )
    {
       if(u < v){
          t=u; u=v; v=t;
       }
       u=u-v;
     }
     return v;
 }

2013년 6월 19일 수요일

[ C ] 함수 포인터 샘플

더하기 빼기 샘플

int add( int a, int b ) {
        return a + b;
}

int sub( int a, int b ) {
        return a - b;
}

함수포인터 미사용
void main() {
        int x, y;
        printf( "result = %d\n", add( x, y ) );
        printf( "result = %d\n", sub( x, y ) );
}

함수 포인터 사용시
void main() {
        int (*func[2])(int, int);  
        int x, y;
        func[0] = add; 
        func[1] = sub;
        printf( "result = %d\n", (*func[0])( x, y ) );
        printf( "result = %d\n", (*func[1])( x, y ) );
}

2013년 5월 28일 화요일

[C++] 문자열 자르기 샘플

문자열 자르기 간단 샘플

#include <string>
#include <iostream>

int main()
{
using namespace std;

string str( "1234567890" );
str.substr( 5 );    // "67890"
str.substr( 5, 3 ); // "678"

return 0;
}

2013년 5월 14일 화요일

[ C ][ 수학 ] 팩토리알 구하기 factorial

C언어 간단 샘플

팩토리알 구하기


#include <stdio.h>

int fact(int n)
{
if (n <= 1) return 1;
return n * fact(n - 1);
}

int main(void)
{
int n;
printf("n: ");
scanf("%d", &n);
printf("fact = %d\n", fact(n));
return 0;
}

2013년 5월 7일 화요일

[ C++ ] 파일 쓰기 샘플

C++
파일 쓰기 간단 샘플


#include <fstream>

int main()
{
std::ofstream ofs;
ofs.open( "test.txt" );

ofs << "testmessage" << 777 << std::endl;

ofs.close();
return 0;
}

2013년 4월 30일 화요일

[ C ] 최대공약수 구하기

최대 공약수 샘플 소스


int gcd (int m, int n)
{
/ / 인수에 0이 있으면 0을 반환
if ((0 == m) | | (0 == n))
return 0;

/ / 유클리드 방법
while (m! = n)
{
if (m> n) m = m - n;
else n = n - m;
}
return m;
} / / gcd

2013년 4월 29일 월요일

[ C ] 구구단


초간단 구구단

#include <stdio.h>
int main(void)
{
        int i,j;
     
        for( i=1 ; i < 10 ; i++ ) {
                for(j=1 ; j <10 ; j++ ) {
                        printf("%3d ",i*j);
                }
                printf("\n");
        }
        return 0;
}

[ C++ ]쓰레드 샘플 생성, 종료


쓰레드 생성 종료 간단 샘플

# include <iostream>
# include <boost/thread.hpp>
using namespace std;
using namespace boost;

/ / 인수없이 함수를 다중 스레드에서 실행
const int kCount = 1000;
const int kInterval = 100;

void PrintHello () {
   for (int i = 0; i! = kCount; + + i)
     if (! (i % kInterval)) cout << "Hello";
}
int main () {
   thread t_hello (& PrintHello); / / PrintHello 함수를 실행하는 스레드의 생성 및 실행

   t_hello.join (); / / t_hello가 실행하고있는 스레드 종료를 기다릴
   return 0;
}

2013년 4월 24일 수요일

[ C++ ] 파일 읽기 샘플


파일읽기 간단 샘플


#include <fstream>
#include <string>
#include <iostream>

using namespace std;

int main( )
{
  ifstream ifs("data.txt");
  string buf;

  while(ifs && getline(ifs, buf)) {
    cout << buf << endl;
  }

  return 0;
}

[ C++] 날짜 시분초 구하기


날짜 시분초 구하기샘플

#include <stdio.h>
#include <time.h>

int main(){

     time_t now = time(NULL);
     struct tm *pnow = localtime(&now);

     char buf[128]="";
     sprintf(buf,"%d:%d:%d",pnow->tm_hour,pnow->tm_min,pnow->tm_sec);
     printf(buf);
     getchar();
     return 0;
}

[ C++] 날짜 구하기

년, 월, 일 구하기 샘플


#include <stdio.h>
#include <time.h>
int main()
{
     time_t now = time(NULL);
     struct tm *pnow = localtime(&now);
     char week[][3] = {"日","月","火","水","木","金","土"};

     printf("今日は%2d年%2d月%2d日(%s)\n",
     pnow->tm_year+1900,
     pnow->tm_mon + 1,
     pnow->tm_mday,
     week[pnow->tm_wday]);

     return 0;
}