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;
}