반응형

** 배열에는 마지막에 0이 들어간다. **

data [0] [1] [2] [3] [4] [5] ...

: 1을 입력할 경우 1과 0 이 data[0], data[1]에 들어 간다.

 

** 따라서 배열의 크기를 알 수 있다.

 

 


#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

int atoi(char data1[]) {


 int pow = 1;
 int len = 0;

 int result1 = 0;
 int result2 = 0;
 int size1 = 0;
 int size2 = 0;

 for (int l = 0; l < 100; l++) {
  if (data1[l] == 0) {
   len = l;
   break;
  }
 }

 for (int i = 0; i < 100; i++) {
  if (data1[i] == 0) break;

  for (int j = 1 + i; j <len; j++) pow = pow * 10;
  result1 = result1 + ((data1[i] - 48) * pow);
  pow = 1;
 }
 return result1;

}
int main()
{

 char data1[100];
 char data2[100];

 

 printf("First Number : ");
 scanf_s("%s", data1, sizeof(data1));
 
 

 
 
 printf("Second Number : ");
 scanf_s("%s", data2, sizeof(data2));


 printf("Sum First Number & Second Number : %d \n", atoi(data1));
 printf("Sum First Number & Second Number : %d \n", atoi(data2));
 
    return 0;
}

 

반응형
반응형

 



#include "stdafx.h"
#include <stdio.h>
#include "myfunc.h"
#include <conio.h>
#include <windows.h>

#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define SPACE 32


#define ON 1
#define OFF 0
#define MAX 5

#define BLACK 0
#define BLUE 1
#define GREEN 2
#define IVORY 3
#define RED 4
#define PURPLE 5
#define YELLOW 6
#define WHITE 7
#define GRAY 8
#define SKYBLUE 9
#define YELLOWGREEN 10
#define WHITEIVORY 11
#define PINK 12
#define WHITEPURPLE 13
#define WHITEYELLOW 14
#define BOLDWHITE 15

#define BOARD_SIZE 5

void setColor(int background, int foreground)
{
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (background << 4) | foreground);
}

int main()
{
 
 int board[MAX][MAX] = { 0 };

 int player_x = 0, player_y = 0;
 int inputKey = 0;
 int led_count = 0;

 int boardsize = 0;
 while (1) {
  system("cls");
  
  setColor(BLACK, WHITE);
  printf(" %d X %d LED 켜기 \n", MAX, MAX);

  // 배열 출력
  for (int y = 0; y < MAX; y++) {
   for (int x = 0; x < MAX; x++)
   {
    if (player_x == x && player_y == y) {
     setColor(BLACK, PINK);
    }
    else if ((board[y][x]) == OFF) {

     setColor(BLACK, WHITE);
    }
    else{
     setColor(BLACK, BLUE);
    }
    printf("■"); 
   }
   printf("\n");
  }

  setColor(BLACK, WHITE);
  printf("켜진 LED 갯수 : %d\n",led_count);
  
  if (led_count == 25) {
   setColor(BLACK, WHITE);
   printf("축하합니다 모든 불이 켜졌습니다.");
  }
  //Sleep(100); // delay 함수
  inputKey = _getch(); // 키입력 함수

  // 좌우이동 및 제한
  if (inputKey == LEFT) {
   player_x--;
   if (player_x < 0) player_x = 0;
  }
  else if (inputKey == RIGHT) {
   player_x++;
   if (player_x > MAX - 1) player_x = MAX - 1;
  }
  else if (inputKey == UP) {
   player_y--;
   if (player_y < 0) player_y = 0;
  }
  else if (inputKey == DOWN) {
   player_y++;
   if (player_y > MAX - 1) player_y = MAX - 1;
  }

  else if (inputKey == SPACE){
 
   if (board[player_y][player_x] == ON) {
    board[player_y][player_x] = OFF;
    led_count--;
   }
   else {
    board[player_y][player_x] = ON;
    led_count++;
   }
   if (player_y < MAX - 1) {
    if (board[player_y + 1][player_x] == ON) {
     board[player_y + 1][player_x] = OFF;
     led_count--;
    }
    else {
     board[player_y + 1][player_x] = ON;
     led_count++;
    }
   }
   if (player_x < MAX - 1) {
    if (board[player_y][player_x + 1] == ON) {
     board[player_y][player_x + 1] = OFF;
     led_count--;
    }
    else {
     board[player_y][player_x + 1] = ON;
     led_count++;
    }
   }

   if (player_y > 0) {
    if (board[player_y - 1][player_x] == ON) {
     board[player_y - 1][player_x] = OFF;
     led_count--;
    }
    else {
     board[player_y - 1][player_x] = ON;
     led_count++;
    }
   }
   
   if (player_x > 0) {
    if (board[player_y][player_x - 1] == ON) {
     board[player_y][player_x - 1] = OFF;
     led_count--;
    }
    else {
     board[player_y][player_x - 1] = ON;
     led_count++;
    }
   }
   setColor(BLACK, BLUE);
  } 
 }
    return 0;
}

 

**

Window.h 헤더 파일 안에 Sleep()함수가 존재하며, 딜레이를 줄 수 있다.

반응형
반응형

/* 4x4 빙고판 만들기

 

*/

 

#include "stdafx.h"
#include <stdio.h>

#include <random>
using namespace std;

int main()
{
 random_device rn;
 mt19937_64 rnd(rn());
 uniform_int_distribution<int> range(0, 4);

 int bingo[5][5] = { 0 };
 int dx = 0, dy = 0;
 int dx2 = 0, dy2 = 0;
 int count = 1;
 int tmp = 0;
 int inputkey = 0;
 int finish = 0;
 int bingoline = 0;

  // 배열에 숫자 넣기
  for (int i = 0; i < 5; i++) {
   for (int j = 0; j < 5; j++) {
    bingo[i][j] = count++;
   }
  }

  // 빙고판 섞기
  for (int k = 0; k < 10000; k++) {
   dx = range(rnd);
   dy = range(rnd);
   dx2 = range(rnd);
   dy2 = range(rnd);

   tmp = bingo[dx][dy];
   bingo[dx][dy] = bingo[dx2][dy2];
   bingo[dx2][dy2] = tmp;
  }

  while (1) {
   system("cls");

   printf("■■■■■■■■");
   printf("\n■   Bingo    ■");
   printf("\n■■■■■■■■\n");

   // 빙고판 출력
   for (int x = 0; x < 5; x++) {
    for (int y = 0; y < 5; y++) {
     if (inputkey == bingo[x][y]) {
      bingo[x][y] = 0;
      printf(" ■");
     }
     
     else
     {
      if (bingo[x][y] == 0)
      {
       printf(" ■");
      }
      else
      {
       printf("%3d", bingo[x][y]);
      }
     }
    }
    printf("\n");
   }
   printf("■■■■■■■■\n");
   printf("※ 마킹 할 숫자를 입력하세요 : ");
   scanf_s("%d", &inputkey);
   
   //빙고 라인 체크
   printf("%d 라인, 빙고 완성 ※");
   
   //빙고 승리! (5라인)
   if (finish == 1) {
    system("cls");
    printf("※ 축하합니다. 빙고 완성 ※");
   }
   
  }
 return 0;
 
}

반응형
반응형

/* 출력결과

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

*/

 

#include "stdafx.h"
#include <stdio.h>

 

int main()
{

 int a[5][5] = { 0 };
 int cnt = 1;

 for (int i = 1; i < 6; i++) {
  for (int j = 1; j < 6; j++) {
   a[i-1][j-1] = cnt++;
   printf("%3d", a[i - 1][j - 1]);
   }
  printf("\n");
 }

 


 return 0;
}

****************************************************************************************************************************************

 

 

/*

1 1 1 1 1

2 2 2 2 2

3 3 3 3 3

4 4 4 4 4

5 5 5 5 5

*/


#include "stdafx.h"
#include <stdio.h>


int main()
{
 int a[5][5] = { 0 };
 
 for (int i = 1; i < 6; i++) {
   for (int j = 1; j < 6; j++) {
    a[i - 1][j - 1] = i;
    printf("%d ", a[i - 1][j - 1]);
   }
   printf("\n");
 }
 
   
 
    return 0;
}

 

****************************************************************************************************************************************

 

 

 

/*

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

*/


#include "stdafx.h"
#include <stdio.h>


int main()
{
 
 int a[5][5] = { 0 };
 
 for (int i = 1; i < 6; i++) {
  for (int j = 1; j < 6; j++) {
   a[i-1][j-1] = j;
   printf("%d ", a[i-1][j-1]);
  }
  printf("\n");
 }

 
   
 
    return 0;
}

 

 

****************************************************************************************************************************************

 

/*

1 2 3 4 5

10 9 8 7 6

11 12 13 14 15

20 19 18 17 16

21 22 23 24 25

*/


#include "stdafx.h"
#include <stdio.h>

 

int main()
{
 int a[5][5] = { 0 };
 int cnt = 1;

 for (int i = 1; i < 6; i++) {
  for (int j = 1; j < 6; j++) {
   a[i - 1][j - 1] = cnt++;
   
  }
  
 }


 for (int i = 1; i < 6; i++) {
  if (i % 2 == 1) {
   for (int j = 1; j < 6; j++) {
    

     printf("%3d", a[i - 1][j - 1]);
    }
    
   }
  else
   for (int j = 5; j > 0; j--) {

 

    printf("%3d", a[i - 1][j - 1]);
   }
 
  printf("\n");
 }

 return 0;
}

****************************************************************************************************************************************

 

/*

1 2 3 4 5

16 17 18 19 6

15 24 25 20 7

14 23 22 21 8

13 12 11 10 9

*/

 

 

#include "stdafx.h"
#include <stdio.h>

 

int main()
{
 int a[5][5] = { 0 };
 int cnt = 0;
 int x = 0, y = 0, dir = 0;

 //dir : 0오른쪽 1아래 2왼쪽 3 위
 while (cnt < 26) {
  cnt++;
  a[y][x] = cnt;

  if (dir == 0) {
   if (a[y][x + 1] == 0 && x < 4)
    x++; 
   else
    dir++;
  }

  if (dir == 1) {
   if (a[y + 1][x] == 0 && y < 4)
    y++;
   else
    dir++;
  }

  if (dir == 2) {
   if (a[y][x - 1] == 0 && x > 0)
    x--;
   else
    dir++;
  }

  if (dir == 3) {
   if (a[y - 1][x] == 0 && y > 0)
    y--;
   else {
    dir = 0;
    x++;
   }
  }
 }


 for (int i = 1; i < 6; i++) {
  for (int j = 1; j < 6; j++) {
   printf("%3d", a[i-1][j-1]);
  }
  printf("\n");
 }
  return 0;
 
}

 

 

반응형

'Study > C' 카테고리의 다른 글

C언어 - 배열 (LED 켜기 게임)  (0) 2018.08.27
C언어 - 배열 (빙고판 만들기)  (0) 2018.08.24
C언어 - 배열 (로또번호 난수발생기)  (0) 2018.08.24
C언어 과제1  (0) 2018.08.21
C언어 과제2  (0) 2018.08.21
반응형

** 배열 사용하여 로또번호 6개 뽑기 **

 


#include "stdafx.h"
#include <stdio.h>
#include <random>
using namespace std;

 

//로또번호 생성기


int main()
{
 random_device rn;
 mt19937_64 rnd(rn());
 uniform_int_distribution<int> range(1, 45);

// 랜덤수 출력 함수

 


 int a[45] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45 };
 int tmp=0;
 
 int ran1 = 0;
 int ran2 = 0;

 

 for (int i = 0; i < 100000; i++) {
  
  ran1 = range(rnd);
  ran2 = range(rnd);
  
  tmp = a[ran1-1];
  a[ran1-1] = a[ran2-1];
  a[ran2-1] = tmp;
  
 } // 카드를 2장뽑아서 섞는다.
 
 for (int j = 0; j < 6; j++) {
  printf("%d ", a[j]);
 } // 배열의 1~6까지 출력

 

    return 0;
}

 

********************************************************************************************************************************************

 

** 로또 번호 출력하여 10장 뽑기 **


#include "stdafx.h"
#include <stdio.h>
#include <random>
using namespace std;

//로또번호 생성기
int main()
{
 random_device rn;
 mt19937_64 rnd(rn());
 uniform_int_distribution<int> range(1, 45);
 int a[45] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45 };
 int tmp=0;
 
 int ran1 = 0;
 int ran2 = 0;
 for (int k = 0; k < 10; k++) {
  for (int i = 0; i < 100000; i++) {

   ran1 = range(rnd);
   ran2 = range(rnd);

   tmp = a[ran1 - 1];
   a[ran1 - 1] = a[ran2 - 1];
   a[ran2 - 1] = tmp;

  }

  for (int j = 0; j < 6; j++) {
   if (0 < a[j] && a[j] < 10) {
    printf("  %d", a[j]);
   }
   else
    printf(" %d", a[j]);
  }
  printf("\n");
 }
    return 0;
}

 

********************************************************************************************************************************************

 

 

**  로또번호 생성기 OMR 마킹 방식  **

 


#include "stdafx.h"
#include <stdio.h>
#include <random>
using namespace std;

//로또번호 생성기 OMR 마킹 방식
int main()
{
 random_device rn;
 mt19937_64 rnd(rn());
 uniform_int_distribution<int> range(1, 45);
 
 int a[45];

 


 int ran1 = 0;
 int cnt = 0;

 for (int i = 0; i < 11; i++) {
  for (int q = 0; q < 45; q++)
  {
   a[q] = q + 1;
  }
  while (cnt < 6) {

   ran1 = range(rnd);
   if (a[ran1 - 1] != 0) {
    printf("%3d ", a[ran1 - 1]);
    a[ran1 - 1] = 0;
    cnt++;
   }

  }
  printf("\n");
  cnt = 0;
 }
   
   
 
    return 0;
}

 

********************************************************************************************************************************************

 

 

반응형

'Study > C' 카테고리의 다른 글

C언어 - 배열 (빙고판 만들기)  (0) 2018.08.24
C언어 - 배열 (2차원배열)  (0) 2018.08.24
C언어 과제1  (0) 2018.08.21
C언어 과제2  (0) 2018.08.21
연산자의 활용  (0) 2016.02.26
반응형

1. 1부터 입력받은 숫자까지 다 더해서 출력
ex)
숫자를 입력하세요 : 10
1부터 다 더한 값 : 55

2. 입력받은 숫자까지 홀수, 또는 짝수만 출력
ex)
2 4 6 8 10 12 ... 100
1 3 5 7 9 ...

3. 구구단 중 입력받은 단만 출력
ex)
원하는 단을 입력하세요 : 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
.
.
3 x 9 = 27

4. 구구단 전체를 출력 (단, while문만 사용)
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
.
.
.

2 x 9 = 18

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
.
.
.
3 x 9 = 27

4 x 1 = 4
.
.

5. 피보나치 수열
0 1 1 2 3 5 8 13 21 34 55 ...

 

반응형

'Study > C' 카테고리의 다른 글

C언어 - 배열 (빙고판 만들기)  (0) 2018.08.24
C언어 - 배열 (2차원배열)  (0) 2018.08.24
C언어 - 배열 (로또번호 난수발생기)  (0) 2018.08.24
C언어 과제2  (0) 2018.08.21
연산자의 활용  (0) 2016.02.26
반응형

1. 별그리기
@@@@@
 @@@@@
 @@@@@
 @@@@@
 @@@@@

 *
 **
 ***
 ****
 *****

     *
    **
   ***
  ****
 *****

    *
   ***
  *****
 *******

    1
   123
  12345
 1234567


2. 피아노 연주기
*** Simple Piano ***
 1. 연주하기 2. 음악 재생

>> 1

 *** Play Mode ***

 *** Simple Piano ***
 1. 연주하기 2. 음악 재생 3. 종료

>> 2

 *** 학교종 재생 ***

 *** Simple Piano ***
 1. 연주하기 2. 음악 재생

3. 숫자 맞히기 (컴퓨터가 물어보는 입장)
당신이 생각하는 숫자는 50입니까?
 (1: 높음, 2: 낮음, 3: 정답)
 >> 1

당신이 생각하는 숫자는 75입니까?
 (1: 높음, 2: 낮음)
 >> 2

당신이 생각하는 숫자는 63입니까?
 (1: 높음, 2: 낮음, 3: 정답)
 >> 1

당신이 생각하는 숫자는 70입니다.


기타 등등
 소수찾기 함수, 전체 다 더하는 함수, 그외 등

추가 문제
 숫자 10개 배열에 입력받아서, 큰 수 작은 수 찾기
 문자열 배열 입력받아서, 대문자는 소문자로, 소문자는 대문자로 변경하는 문제
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18

반응형

'Study > C' 카테고리의 다른 글

C언어 - 배열 (빙고판 만들기)  (0) 2018.08.24
C언어 - 배열 (2차원배열)  (0) 2018.08.24
C언어 - 배열 (로또번호 난수발생기)  (0) 2018.08.24
C언어 과제1  (0) 2018.08.21
연산자의 활용  (0) 2016.02.26
반응형

 연산자

기능 

결합방향 

 =

 연산자 오른쪽에 있는 값을 연산자 왼쪽에 있는 변수에 대힙한다 

 <- 

 +

 두 피연산자의 값을 더한다 

 ->

 -

 왼쪽의 피연산자 값에서 오른쪽의 피연산자 값을 뺀다 

 -> 

 *

 두 피연산자의 값을 곱한다 

 -> 

 /

 왼쪽의 피연산자 값을 오른쪽의 피연산자 값으로 나눈다 

 -> 

 %

 왼쪾의 피연산자 값을 오른쪽의 피연산자 값으로 나눴을 때 얻게 되는 나머지를 반환한다. 

 ->

 ++num

 값을 1 증가후, 속한 문장의 나머지를 진행(선증가 후 연산) 

 <- 

 num++

 속한 문장을 먼저 진행한 후 값을 1증가(선연산 후 증가) 

 ->

 --num

 값을 1 감소후, 속한 문장의 나머지를 진행(선 감소후 연산) 

 <-

 num--

  속한 문장을 진행한 후 값을 1 감소(선 연산후 감소)

 ->

 <

 예  n1<n2

 n1이 n2보다 작은가? 

 -> 

 >

 예 n1>n2

 n1이 n2보다 큰가? 

 ->

 ==

 예 n1==n2

 n1과 n2가 같은가?

 ->

 !=

 예 n1 != n2

 n1과 n2가 다른가?

 ->

 <=

 예 n1<=n2

 n1이 n2보다 같거나 작은가?

 ->

 >=

 예 n1>=n2

 n1이 n2보다 같거나 큰가?

 ->

 &&

 예 A&&B

 A와 B모두 참이면 연산결과로 참을 반환(논리 AND 

 ->

 ||

 예 A||B

 A와 B 둘중 하나라도 참이면 연산결과로 참을 반환(논리 OR) 

 ->

 !

 예 A!

 A가 참이면 거짓, A가 거짓이면 참을 반환(논리 NOT) 

 <-

 

반응형

'Study > C' 카테고리의 다른 글

C언어 - 배열 (빙고판 만들기)  (0) 2018.08.24
C언어 - 배열 (2차원배열)  (0) 2018.08.24
C언어 - 배열 (로또번호 난수발생기)  (0) 2018.08.24
C언어 과제1  (0) 2018.08.21
C언어 과제2  (0) 2018.08.21

+ Recent posts