2010년/4월

4월23일 ATMEGA C코딩/ 두더지게임초안

뽀얀햄스터 2010. 4. 23. 13:43

1. 크기가 5 x 7 인 int 동적 배열을 생성한 다음, 0부터 순서대로 값을 저장하세요.
#include<stdio.h>
int main()
{
  int X=5;     //배열의크기
  int Y=7;   //배열의크기  
  int ixcnt;  //X증가량  
  int iycnt;  //Y증가량  
  int **arry;  //2차원배열을 사용하기 위한 더블포인터

  arry=(int **)malloc(sizeof(int *)*Y);  //(int *)*7의 크기만큼 만든다.

  for(iycnt=0;iycnt<Y;++iycnt)  // 배열로 나누고 각 배열에 다시 동적 할당을  할 것
  { 
    arry[iycnt]=(int *)malloc(sizeof(int)*X);
    //기존 arry[7]까지에  다시 int*5만큼 동적할당    
  }

  for(iycnt=0;iycnt<Y;++iycnt)
  {
    for(ixcnt=0;ixcnt<X;++ixcnt)
    {

      arry[iycnt][ixcnt]=X*iycnt+ixcnt; //0~34까지 넣기
    }
  }
  
  for(iycnt=0;iycnt<Y;++iycnt)
  {
    for(ixcnt=0;ixcnt<X;++ixcnt)
    {

      printf("%4d",arry[iycnt][ixcnt]); //출력
    }
    printf("\n");
  }
  for(iycnt=0;iycnt<Y;++iycnt)
  {
    free(arry[iycnt]);   //첫번째   반환
        
  }
  free(arry); //두번째  반환
return 0;
}
2. 초기 배열 크기가 10인 int 자료형 동적 배열을 생성해서 0부터 순서대로 채우세요. 사용자가 입력한 숫자가 100이라면, 0부터 99까지의 값을 배열에 저장합니다.
#include<stdio.h>
int main()
{
  int x=10;
  int ic;
  int *array;

  array=(int*)malloc(sizeof(int )*x);

  for(ic=0;ic<x;ic++)
  {
    array[ic]=ic;
  }
  printf("입력하세요~\n");
  scanf("%d",&x);
  free(array);
  array=(int*)malloc(sizeof(int )*x);

  for(ic=0;ic<x;ic++)
  {
    array[ic]=ic;
  }
  
  for(ic=0;ic<x;ic++)
  {
    printf("%2d",array[ic]);
    if((array[ic]%10==9)&&(array[ic]>0))
    {
      printf("\n");  
    }
  }

  free(array);

  return  0;
}




3. 초 입력시 분과 초로 변환하는 프로그램을 작성하세요.
출력)  Input Seconds :  100
  Output :  1 min 40 sec
#include<stdio.h>
int main()
{
  int inum;
  printf("시간의 초를 입력하세요 \n");
  scanf("%d",&inum);
  printf("[%d]분\t[%d]초",inum/60,inum%60);
}
4. 입력된 정수의 2의 보수를 구하여 10진수, 16진수 형태로 출력하세요.
출력)  Input Number :  1
        2's complement(10진수) :  -1
        2's complement(16진수) :  ffffffff
#include<stdio.h>
int main()
{
  int  inum;
  printf("입력하세요 2의보수값을 넣어드려요\n");
  scanf("%d",&inum);

  printf("2's complement(10진수) : %d\n",~inum+1);
  printf("2's complement(16진수) : %p\n",~inum+1);



  return 0;
}


---------------------------------------------------------------------
두더지
---------------------------------------------------------------------

#include<avr/io.h>
#include<avr/signal.h>

#include<avr/interrupt.h>
#include<stdlib.h>


#define CPU_CLOCK     16000000  //CPU clock=16000000Hz
#define TICKS_PER_SEC  1000    //Ticks per sec =1000
#define PRESCALER    128      //클럭의  배수
#define BAUD_RATE 19200    //통신시 이용할 속도
#define BAUD_RATE_L (CPU_CLOCK/(16l*BAUD_RATE))-1 
#define BAUD_RATE_H ((CPU_CLOCK/(16l*BAUD_RATE))-1)>>8

unsigned char USART_Receive(void);   //recv
void Protocall(void);        //직렬포트설정 
//unsigned char PINCN(void);
volatile unsigned int  g_elapsed_time;//시간변수 
unsigned char Pnum;              //PINC 번호 
void uart_send_byte(unsigned char byte); // send 부분 
void intiLED(void);            //LED초기화
void setTCCR0(void);          //TCCR0설정
void initTCNT0(void);          //TCNT0초기화
void setTIMSK(void);          //TIMSK 설정
void sleep(unsigned int elapsed_time);  //1초  대기
SIGNAL(SIG_OVERFLOW0);          //timer0의 오버플로우  함수
unsigned char count=0;
unsigned char USART_Receive(void)
{
  while(!(UCSR1A&(1<<RXC)));
  return UDR1;
}

void Protocall(void)
{

  UBRR1L = (unsigned char)BAUD_RATE_L; //baud rate 설정
  UBRR1H =(unsigned  char)BAUD_RATE_H;
  //parity설정 1stop bit설정,문자사이즈 8bit 설정
  UCSR1C=(0<<UPM1)|(0<<UPM0)|(0<<USBS)|
    (1<<UCSZ1)|(1<<UCSZ0);
  //송신수신 interrupt설정(PORTD27번28번사용), 문자사이즈 8bit설정
  UCSR1B=(1<<TXEN)|(1<<RXEN)|(0<<UCSZ2);

}
void uart_send_byte(unsigned char byte)
{
  while(!(UCSR1A&(1<<UDRE)));
  UDR1=byte ; 
}

void intiLED(void)
{
  DDRF=0xFF;
  PORTF=0xFF;
  DDRC=0x00;
  DDRE=0xFF;
  PORTF=0x00;

}
void setTCCR0(void)
{
  TCCR0=(TCCR0|(1<<2));
  TCCR0=(TCCR0|(1<<0));
  TCCR0=(TCCR0&(~(1<<1)));
  
}
void initTCNT0(void)
{
  TCNT0 =256-(CPU_CLOCK/TICKS_PER_SEC/PRESCALER);
}          
void setTIMSK(void)  
{
  TIMSK=(TIMSK)|(1<<0);
}        
void sleep(unsigned int elapsed_time)
{
  g_elapsed_time=0;
  while(g_elapsed_time<elapsed_time);
  
  
}
SIGNAL(SIG_OVERFLOW0)
{
  initTCNT0();
  
  g_elapsed_time++;
}

int main()
{  
  unsigned char i;    //게임 20번할 횟수
  // 정답을 맞춘 갯수 
  int random; //입력받을 난수 값
  unsigned char Lnum=0;  //LED번호 
  unsigned char buf[20];
  unsigned char  j,fnd1,cnt;
  unsigned char ch=0;
    //PINC번호  
  Protocall();
  intiLED();
  setTCCR0();
  initTCNT0();
  setTIMSK();
  SREG=0x80;
  
  ch=USART_Receive();
  while(1)
  {
    if('y'==ch)
    {
      for(j=0;20>j;++j)
      {
        PORTF=USART_Receive();
        sleep(800);  
        uart_send_byte((255-PINC));
        PORTF=0xFF;
        fnd1=USART_Receive();
        if(fnd1==1)
        {
          cnt=cnt+0x05;
          if(cnt&0x0A==0x0A)
          {
            cnt=cnt+6;
          }
          PORTE=cnt;
          
        }
        sleep(200);
            
      }
      
      
      if(20==USART_Receive())
      {
        PORTF=0xFF;
        sleep(250);
        PORTF=0x00;
        PORTF=0xFF;
        sleep(250);
        PORTF=0x00;
        PORTF=0xFF;
        sleep(250);
        PORTF=0x00;
        PORTF=0xFF;
        sleep(250);
        PORTF=0x00;
      }
    }
  }
  return  0;
}

---------------------------------------------------------------------window.h
---------------------------------------------------------------------
#include<stdio.h>
#include<windows.h>

#include<stdlib.h>
#include<time.h>
#include<conio.h>
int main()
{
  char szPort[15];  //포트명을 저장할 변수
  
  wsprintf(szPort,"COM%d",1); //포트 2번으로 통신
  
  HANDLE  m_hComn =NULL;  //HANDLE생성후   초기값 NULL
  m_hComn = CreateFile(szPort,GENERIC_READ|GENERIC_WRITE,0,NULL,
    OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  
  if(m_hComn==INVALID_HANDLE_VALUE)  //포트접속실패시 에러
  {
    printf("(!) Failed to create a  Comm Device file \n");
    return FALSE;
  }
  
  DCB dcb;        //DCB 구조체생성
  dcb.DCBlength=sizeof(DCB);    //포트의 크기 비트레이 바이트크기 등을
  GetCommState(m_hComn,&dcb);    
  dcb.BaudRate=19200;
  dcb.ByteSize=8;
  dcb.Parity=0;
  dcb.StopBits=0;
  
  SetCommState(m_hComn,&dcb);    
  OVERLAPPED  osWrite,osRead;      //송신용 객체 생성
  osWrite.Offset=0;
  osWrite.OffsetHigh=0;
  osWrite.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
  osRead.Offset=0;
  osRead.OffsetHigh=0;
  osRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
  //unsigned char buf[2]; 
  
  unsigned char i,j=0,znum=1;
  unsigned char jnum,result=1;
  unsigned char conum,swit;
  
  
  printf("게임을 시작하시겠습니까?(y)\n");
  i=getche();
  printf("\n");
  fflush(stdout);
  if(i=='y')
  {
      while(1)
    {
      j=0;
      srand((int)time(NULL));
      WriteFile(m_hComn,&i,1,NULL,&osWrite);
      for(jnum=0;jnum<20;++jnum)
      {  
        conum=rand()%7;
        
        result=~(1<<conum);
        WriteFile(m_hComn,&result,1,NULL,&osWrite);
        Sleep(100);
        ReadFile(m_hComn,&swit,1,NULL,&osRead);
        if(swit==128)
        {
          swit=8;
        }
        else if(swit==64)
        {
          swit=7;
        }
        else if(swit==32)
        {
          swit=6;
        }
        else if(swit==16)
        {
          swit=5;
        }
        else if(swit==8)
        {
          swit=4;
        }
        else if(swit==4)
        {
          swit=3;
        }
        else if(swit==2)
        {
          swit=2;
        }
        else if(swit==1)
        {
          swit=1;
        }
        else
        {
          swit=0;
        }
      if(conum+1==swit)
        {
          printf("LED[%d]\tSWITCH[%d]성공 \n",conum+1,swit);
          j++;
          WriteFile(m_hComn,&znum,1,NULL,&osWrite);
        }
        else if (conum!=swit)
        {
          printf("LED[%d]\tSWITCH[%d]실패  \n",conum+1,swit);
        }
        
      }
      if(j==20)
      {
        WriteFile(m_hComn,&j,1,NULL,&osWrite);
      }
      printf("게임을 다시 시작하시겠습니까?(y/n)\n");
      i=getche();
      if(i=='n')
      {
        break;
      }
    }
  }
  CloseHandle(m_hComn);
  
  return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------C++
interrupt<->polling
#include<iostream.h>
int
 main()
{
/*  int A;
  int C;
  int B;
  cout<<"구구단입력 어디서  어디까지"<<endl;
   cin>>A;
  cin>>C;
  for(A;A<C+1;++A)
  {
    for(B=1;B<10;++B)
    {
      cout<<A<<"*"<<B<<"="<<A*B<<endl;
    }
    cout<<endl;
  }

  cout<<"하이"<<endl;  
  cout<<"["<<A<<"]"<<endl; 
  cin>>A;      
  cout<<"["<<A<<"]"<<endl; 
*/

  int num=2;
  int mul;
  for(num;9>=num;num=num+3)
  {
    for(mul=0;27>mul;mul++)
    {
      if(9>=num+(mul%3))
      {
        cout<<num+mul%3<<"*"<<mul/3+1<<"="<<num+mul%3*mul/3+1<<"\t";
      }
      if(2==mul%3)
      {
        cout<<endl;
      }

    }
    cout<<endl;

  }
  return 0;
}
``