반응형

j-kit-128을 이용하여 전체적인 시스템을 구현중입니다.

현재까지

1.led
2.buzzer(PWM적용 필요)
3.fnd
4.timer
5.switch(Ext interrupt 구현 필요)
6.serial통신

//미구현
7.adc_dimmer
8.cds
9.i2c
10. 추가로 wifi모듈을 달아서 wifi 통신도 해 볼 예정입니다.

미구현 항목들은 기능에 대한 숙지가 부족하여 좀더 공부 할 필요가 있을 것 같습니다.
j-kit-128을 이용하여 복습하고, 해당 키트를 이용하여 공부하시는 분들에게 도움이 되고자? 만들게 되었습니다.

기능 구현 및 개선이 되는대로 업로드 하겠습니다.
저도 부족한 점이 많기에 많은 가르침 부탁드립니다.


=======================================================================================

추가적으로 기능 구현한 내용


1. CDS (adc값 읽기)

2. extSwitch (외부 키 인터럽트)


// 추가 구현 예정

void init_pwmDimmer();//pwm dimmer

void init_i2c();//i2c 온도센서

void init_pwmMotor(); //pwm motor

void init_pwmBuzzer(); //pwm buzzer

void init_INTSerial(); // uart인터럽트


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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
 
#define FND_NUM0 0x3f
#define FND_NUM1 0x06
#define FND_NUM2 0x5b
#define FND_NUM3 0x4f
#define FND_NUM4 0x66
#define FND_NUM5 0x6d
#define FND_NUM6 0x7d
#define FND_NUM7 0x27
#define FND_NUM8 0x7f
#define FND_NUM9 0x6f
 
#define FND_SEL1 0x01
#define FND_SEL2 0x02
#define FND_SEL3 0x04
#define FND_SEL4 0x08
 
unsigned int fnd[10]= {FND_NUM0,FND_NUM1,FND_NUM2,FND_NUM3,FND_NUM4,FND_NUM5,FND_NUM6,FND_NUM7,FND_NUM8,FND_NUM9};
unsigned int fnd_sel[4= {FND_SEL1,FND_SEL2,FND_SEL3,FND_SEL4};
unsigned int count=0, sec=0;
 
char textBuff[100];
unsigned int extCount=0;
 
void init_led();
void init_buzzer();
void init_fnd();
void init_switch();
void init_timer();
void init_serial();
void init_adc();
void init_extSwitch();
 
/* 미구현
void init_pwmDimmer();//pwm dimmer
void init_i2c();//i2c 온도센서
void init_pwmMotor(); //pwm motor
void init_pwmBuzzer(); //pwm buzzer
void init_INTSerial(); // uart인터럽트
*/
 
void fnd_display(int num);
void SendByte(char data);
void SendLine(char *string);
char ReceiveByte();
uint16_t read_adc(uint8_t channel);
 
void test_led();
void test_buzzer();
void test_fnd();
void test_switch();
void test_serial();
void test_timer();
void test_adc();
void test_extSwitch();
 
int main(void)
{
    init_led();
    init_buzzer();
    init_fnd();
    init_switch();
    init_timer();
    init_serial();
    init_adc();
    init_extSwitch();
    sei();
    while (1)
    {
        //test_led();
        //test_buzzer();
        //test_fnd();
        //test_switch();
        //test_timer();
        //test_serial();
        //test_adc();
        //test_extSwitch();
    }
}
 
void init_led(){
    DDRA = 0xff;
    PORTA = 0x00;
}
void test_led(){
    PORTA = 0x00;
    _delay_ms(1000);
    PORTA = 0xff;
    _delay_ms(1000);
}
void init_buzzer(){
    DDRB = 0x10;
}
void test_buzzer(){
    PORTB = 0x10;
    _delay_ms(10);
    PORTB = 0x10;
    _delay_ms(10);
}
void init_fnd(){
    DDRC = 0xff;
    DDRG = 0x00;
}
void test_fnd(){
    fnd_display(1234);
}
void fnd_display(int num){
    int fnd1,fnd2,fnd3,fnd4;
    fnd1 = num%10;
    fnd2 = (num/10)%10;
    fnd3 = (num/100)%10;
    fnd4 = num/1000;
 
    PORTC = fnd[fnd1];
    PORTG = fnd_sel[0];
    _delay_ms(1);
    PORTC = fnd[fnd2];
    PORTG = fnd_sel[1];
    _delay_ms(1);
    PORTC = fnd[fnd3];
    PORTG = fnd_sel[2];
    _delay_ms(1);
    PORTC = fnd[fnd4];
    PORTG = fnd_sel[3];
    _delay_ms(1);
}
void init_switch(){
    // pull-up
    DDRE = 0x00;
    PORTE = 0x30;
}
void test_switch(){
    init_led();
    if((PINE & 0x20)== 0x00)
    PORTA = 0xff;
    if((PINE & 0x10)== 0x00)
    PORTA = 0x00;
}
void init_timer(){
    TIMSK = 0x01// R/W 선택 TIMER 0 사용
    TCCR0 = 0x04// 분주비 64
    TCNT0 =256-5// 0에서 시작 255가되어 256이 되면 OVF가 되어 인터럽트 구문을 실행한다.
    /*
    Timer set
    1/16000000 = 0.0000000625
    64분주
    0.0000000625* 64 = 0.000004
    0부터 250회 돌면 0.000004 *250 = 0.001
    OVF 발생 count 증가
    1000번 발생하면 1초 가 된다.
    */
}
void test_timer(){
    init_fnd();
    init_serial();
    fnd_display(sec);
    sprintf(textBuff,"%d\r\n",sec);
    SendLine(textBuff);
}
void init_serial(long BaudRate){
    UBRR0H = 0;
    switch(BaudRate)
    {
        case 115200:
        UBRR0L = 8;
        break;
        
        case 57600:
        UBRR0L = 16;
        break;
        
        case 38400:
        UBRR0L = 25;
        break;
        
        case 19200:
        UBRR0L = 51;
        break;
        
        case 14400:
        UBRR0L = 68;
        break;
        
        case 9600:
        UBRR0L = 103;
        break;
        
        // Default 115200
        default:
        UBRR0L = 8;
        break;
    }
    UCSR0A = 0x00;
    UCSR0B = 0x18;
    UCSR0C = 0x06;  // 8 bit
}
void SendByte(char data)
{
    while((UCSR0A & 0x20== 0x00);
    UDR0 = data;
}
/*** Function for sending line ***/
void SendLine(char *string)
{
    while(*string != '\0')
    {
        SendByte(*string);
        string++;
    }
}
 
char ReceiveByte(){
    while(!(UCSR0A & (1<<RXC0)));
    return UDR0;
}
void test_serial(){
    SendByte('A');
    _delay_ms(10);
    SendLine("Hello World");
    
    if((ReceiveByte() == 'a'&& (ReceiveByte() =='b')){
        SendByte('o');
        SendByte('k');
    }
}
void init_adc(){
        ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)); //16Mhz/128 = 125Khz
        ADMUX |= (1<<REFS0);       //AVCC(5V)
        ADCSRA |= (1<<ADEN);      //ADC 인에이블
}
uint16_t read_adc(uint8_t channel){
    ADMUX &= 0xF0;
    ADMUX |= channel;
    
    ADCSRA |= (1<<ADSC);      //변환 시작
    while(ADCSRA&(1<<ADSC));//변환 완료되기를 기다림.
    
    return ADCW;  //ADC값 반환
}
void test_adc(){
    init_adc();
    init_fnd();
    uint16_t adcValue =0;
    adcValue = read_adc(0);
    sprintf(textBuff,"adcValue : %d\r\n",adcValue);
    SendLine(textBuff);
    fnd_display(adcValue);
    _delay_ms(100);
}
void init_extSwitch(){
    DDRE = 0x00;
    PORTE = 0xf0;
    EICRB =0x0a;
    
    EIMSK = 0x30// INT4, INT5 사용
}
void test_extSwitch(){
    init_fnd();
    fnd_display(extCount);
}
 
ISR(INT4_vect){
    extCount++;
}
ISR(INT5_vect){
    extCount--;
}
ISR(TIMER0_OVF_vect){
    TCNT0 =256-(256-5);
    count++;
    if (count >=1000){
        sec++; count=0;
    }
}
cs



jkit_190122.zip


반응형

'Project > j-kit-128-1실습' 카테고리의 다른 글

[j-kit-128] jkit 128 기능구현하기  (0) 2018.12.10
Sonar_GPS - C#Window Form  (0) 2018.10.21
Sonar_GPS  (0) 2018.10.11
Serial 통신_ 문자열 전송  (0) 2018.10.01
Serial 통신_Uart0_문자 1개  (0) 2018.10.01
반응형

Atmega8a 

8MHz Ext.OSC



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Study_Atmega8A.c
 *
 * Created: 2018-12-18 오후 10:30:25
 * Author : HeungSik
 */ 
 
#include <avr/io.h>
#define F_CPU 8000000
#define FOSC 8000000
#include <util/delay.h>
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
 
void init_serial();
void USART_Transmit(unsigned char data);
unsigned char USART_Receive( void );
 
void init_serial(unsigned int ubrr){
    UBRRH = (unsigned char)(ubrr >>8);
    UBRRL = (unsigned char)ubrr;
    //UBRRL = 25;
    /* Enable receiver and transmitter */
    UCSRB = (1<<RXEN)|(1<<TXEN);
    /* Set frame format: 8data, 2stop bit */
    UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
void USART_Transmit( unsigned char data )
{
    /* Wait for empty transmit buffer */
    while ( !( UCSRA & (1<<UDRE)) )
    ;
    /* Put data into buffer, sends the data */
    UDR = data;
}
unsigned char USART_Receive( void )
{
    /* Wait for data to be received */
    while ( !(UCSRA & (1<<RXC)) )
    ;
    /* Get and return received data from buffer */
    return UDR;
}
void Transmit_Line(char *string){
    int i =0;
    while (*string != 0x00)
    {
        USART_Transmit(*string);
        i++;
    }
}
int main(void)
{
    
    init_serial(MYUBRR);
    
    while (1
    {
        USART_Transmit('a');
        
        _delay_ms(50);
    }
}
 
 
cs


다음은 BLE모듈 사용하여 LED 제어하기


반응형
반응형
#include <stdlib.h>    // qsort 함수가 선언된 헤더 파일


int compare(const void *a, const void *b)    // 오름차순 비교 함수 구현
{
    int num1 = *(int *)a;    // void 포인터를 int 포인터로 변환한 뒤 역참조하여 값을 가져옴
    int num2 = *(int *)b;    // void 포인터를 int 포인터로 변환한 뒤 역참조하여 값을 가져옴

    if (num1 < num2)    // a가 b보다 작을 때는
        return -1;      // -1 반환
    
    if (num1 > num2)    // a가 b보다 클 때는
        return 1;       // 1 반환
    
    return 0;    // a와 b가 같을 때는 0 반환
}

int main()
{
    int numArr[10] = { 8, 4, 2, 5, 3, 7, 10, 1, 6, 9 };    // 정렬되지 않은 배열

    // 정렬할 배열, 요소 개수, 요소 크기, 비교 함수를 넣어줌
    qsort(numArr, sizeof(numArr) / sizeof(int), sizeof(int), compare);

    for (int i = 0; i < 10; i++)
    {
        printf("%d ", numArr[i]);    // 1 2 3 4 5 6 7 8 9 10
    }

    printf("\n");

    return 0;
}



// 내림차순


int compare(const void *a, const void *b)    // 내림차순 비교 함수 구현
{
    int num1 = *(int *)a;    // void 포인터를 int 포인터로 변환한 뒤 역참조하여 값을 가져옴
    int num2 = *(int *)b;    // void 포인터를 int 포인터로 변환한 뒤 역참조하여 값을 가져옴

    if (num1 > num2)    // a가 b보다 클 때는
        return -1;      // -1 반환
    
    if (num1 < num2)    // a가 b보다 작을 때는
        return 1;       // 1 반환
    
    return 0;           // a와 b가 같을 때는 0 반환
}



// 좀더 간단하게 구현 할 경우


int compare(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;    // 오름차순
}
int compare(const void *a, const void *b)
{
    return *(int *)b - *(int *)a;    // 내림차순
}


반응형

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

Sprintf에 예제와 사용 예  (0) 2019.01.04
itoa, atoi 함수에 대하여  (0) 2019.01.04
C언어 - 자연수의 조합  (0) 2018.09.14
C언어 - 자판기  (0) 2018.09.14
C언어 - 포인터 이해  (0) 2018.09.07
반응형

// 어떤 자연수 X의 세 자연수의 합으로 만들어 진다.

오름차순으로 중복 없이 조합되는 수를 출력하여라

 

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
46
47
48
49
// prob.04.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
 
#include "stdafx.h"
#include <stdio.h>
 
 
int main()
{
    int count1 = 0int count2 = 1int count3 = 2;
    int num = 0
    int num1[100= { 0 }; int num2[100= { 0 }; int num3[100= { 0 };
    
    printf("입력받을 자연수 N의 개수 : ");
    scanf_s("%d"&num);
    
    int x[100= { 0 };
    for (int i = 0; i < num; i++) {
        printf("\n자연수 X를 입력 해 주세요 : ");
        scanf_s("%d"&x[i]);
    }
 
    for (int i = 0; i < num; i++) {
        printf("\n자연수 X %d : %d",i, x[i]);
    }
    
    for (int l = 0; l < num;l++ ) {
        if (x[l] < 4)printf("\nNULL");
        else {
            for (int i = 1; i < x[l]; i++) {
                count1 = i;
                for (int j = i + 1; j < x[l]; j++) {
                    count2 = j;
                    for (int k = j + 1; k < x[l]; k++) {
                        count3 = k;
                        if ((count1 + count2 + count3) == x[l]) {
                            printf("\ncount1 = %d  ", count1);
                            printf("count2 = %d  ", count2);
                            printf("count3 = %d  ", count3);
                        }
                    }
                }
            }
        }
    }
    return 0;
}
 
 
cs

 

 

 

 

반응형

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

Sprintf에 예제와 사용 예  (0) 2019.01.04
itoa, atoi 함수에 대하여  (0) 2019.01.04
C언어 - 자판기  (0) 2018.09.14
C언어 - 포인터 이해  (0) 2018.09.07
C언어 - EOF  (0) 2018.09.07
반응형

 

// 자판기

어떤 동전 가격 종류 상관 없이 계산하려 한다.

최적의 해를 구하라

예) 동전종류 50원, 100원, 500원, 1000원

  자판기 600원

 > 500원 1개,100원1개

 

 

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
46
47
48
49
50
51
52
53
// jhs0914.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
 
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    printf("동전의 종류는 몇개 인가요?  : ");
    int cointype = 0;
    scanf_s("%d"&cointype);
    printf("동전의 종류 : %d", cointype);
    int cointypeSave[100= { 0 };
 
 
    for (int i = 0; i < cointype; i++) {
        printf("\n%d번째 동전 가격 : ",i+1);
        scanf_s("%d",&cointypeSave[i]);
    }
 
    for (int i = 0; i < cointype; i++) {
        printf("\n%d번째 동전 : %d",i,cointypeSave[i]);
    }
 
    printf("\n자판기에는 얼마가 들어 있나요?  : ");
    int coinpay = 0;
    scanf_s("%d"&coinpay);
    
    int count[100= { 0 };
    int count1 = 0;
    
 
        for (int i = cointype-1; i >=0; i--) {
            count1 = 0;
            while ((coinpay - cointypeSave[i]) >= 0) {
                coinpay = coinpay - cointypeSave[i];
                count1++;
                count[i] = count1;
            }
            
        }
    for (int i = 0; i < cointype; i++) {
        if (count[i] == 0){}
        else 
            printf("\n %d원 짜리 동전 %d개", cointypeSave[i], count[i]);
    }
 
    
    return 0;
}
 
 
cs

 

 

 

반응형

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

itoa, atoi 함수에 대하여  (0) 2019.01.04
C언어 - 자연수의 조합  (0) 2018.09.14
C언어 - 포인터 이해  (0) 2018.09.07
C언어 - EOF  (0) 2018.09.07
C언어 - 학생 Table(총점, 평균, 석차)  (0) 2018.09.07
반응형

* Dot Matrix

 

1. 하트를 그리고, 그 하트를 방향키를 이용하여 움직인다.

 

2. 불켜기 게임을 Dotmatrix로 구현한다. 25개를 다 켜면 HLED가 ON/OFF

 

3. 폭탄 피하기 게임

 

* Key Matrix

 

1. 핸드폰 영문자판 흉내내기

 - 키매트릭스를 핸드폰 자판이라 생각하고 LCD에 글을 써보자

 

* HLED 녹화하기

 - 타이머 인터럽트를 이용해서 구현하기

 

 

 

** 고난도 프로젝트

 

LCD에 Key입력을 받아 명령을 수행한다.

 

MTR RI 30 모터를 오른쪽으로 30도 이동 

MTR LE 15 모터를 왼쪽으로 15도 이동

 

...

반응형
반응형
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
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * avr-0910.cpp
 *
 * Created: 2018-09-10 오전 9:20:31
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <avr/interrupt.h>
#include <stdio.h>
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
unsigned int count =0, sec=0;
 
ISR(TIMER0_OVF_vect){
    TCNT0 =256-25;
    count++;
    if (count >625){
        sec++; count=0;
    }
}
int main(void)
{
    /* Replace with your application code */
    io_init();
    lcd_init();
    
    // 타이머 인터럽트 레지스터
    TIMSK = 0x01// R/W 선택 TIMER 0 사용
    TCCR0 =0x07// 분주비 1024
    TCNT0 =256-25// 0에서 시작 255가되어 256이 되면 OVF가 되어 인터럽트 구문을 실행한다.
    
    /*
    1/16000000 = 0.0000000625
    분주비가 1024
    0.0000000625 *1024 = 0.000064 // TCNT0가 1올라가는 속도.
    ISR이 발생하는 시간 = 0.016384
    약 61번이 OVF 인터럽트가 발생하면 1초가 된다.
    */
    
    sei();
    char temp[16];
    
    
    while (1
    {
        sprintf(temp,"%d",sec);
        lcd_putsf(0,0,temp);        
    }
}
cs

 

// 해당 소스는 정확한 1초가 아니다.

 

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * avr-0910.cpp
 *
 * Created: 2018-09-10 오전 9:20:31
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <avr/interrupt.h>
#include <stdio.h>
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
unsigned int count =0, sec=0;
 
ISR(TIMER0_OVF_vect){
    TCNT0 =256-5;
    count++;
    if (count >500){
        sec++; count=0;
    }
}
int main(void)
{
    /* Replace with your application code */
    io_init();
    lcd_init();
    
    // 타이머 인터럽트 레지스터
    TIMSK = 0x01// R/W 선택 TIMER 0 사용
    TCCR0 =0x04// 분주비 64
    TCNT0 =256-5// 0에서 시작 255가되어 256이 되면 OVF가 되어 인터럽트 구문을 실행한다.
    
    /*
    1/16000000 = 0.0000000625
    분주비가 64
    0.0000000625 *64 = 0.000004 // TCNT0가 1올라가는 속도.
    ISR이 발생하는 시간 = 0.000004
    TCNT 250 회로 OVF 발생시 걸리는 시간 0.001
    500번이 OVF 인터럽트가 발생하면 1초가 된다.
    */
    
    sei();
    char temp[16];
    
    
    while (1
    {
        sprintf(temp,"%d",sec);
        lcd_putsf(0,0,temp);        
    }
}
cs
반응형
반응형
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
46
47
// pointer.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
 
#include "stdafx.h"
void swap(int a, int b) {
    int t;
    t = a; a = b; b = t;
}
void swap_point(int *a, int*b) {
    int t;
    t = *a; *= *b; *= t;
}
// 배열과 포인터 이해
int main()
{
    int A[100= { 5,6,7,8 };
 
    printf("%d\n"*A);
    printf("%d\n"*(A+1));
    printf("%d\n"*+11);
    printf("\n");
    int *p;
    p = A;
    
    printf("%d\n"*p);
    printf("%d\n"*(p+1));
    printf("%d\n"*(p + 2));
    printf("%d\n"*(p + 3));
    printf("%d\n"*(++p));
    printf("\n");
    // & 기호 + 변수 A == > 변수 A 의 위치
    int a = 10
    int *p1 = &a;
    printf("%d\n"*p1);
    printf("%d\n"&a);
 
    //포인터 사용
    int i=50, j=70;
    swap(i, j);
    printf("%d %d\n", i, j);
    swap_point(&i, &j);
    printf("%d %d\n", i, j);
 
    return 0;
}
 
 
cs

 

반응형

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

C언어 - 자연수의 조합  (0) 2018.09.14
C언어 - 자판기  (0) 2018.09.14
C언어 - EOF  (0) 2018.09.07
C언어 - 학생 Table(총점, 평균, 석차)  (0) 2018.09.07
C언어 - 탐색, 정렬, 순위  (0) 2018.09.07
반응형
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// student_table.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
 
#include "stdafx.h"
 
int score[20][7= { 0 };
int student = 0, i = 0, j = 0;
int student_no = 0;
int rank[20= { 0 };
 
void print_score() {
    int i, j;
    i = j = 0;
    printf("  학번  국어  영어  수학  총점  평균  석차\n");
    for (i = 0; i < student; i++) {
        for (j = 0; j < 7; j++) {
            printf("%6d", score[i][j]);
        }
        printf("\n");
    }
}
int main()
{
    printf("학생 수를 입력 해 주세요 = ");
    scanf_s("%d"&student);
 
    for (int i = 0; i < student; i++) {
        printf("%d번 학생의 학번은 ?", i);
        scanf_s("%d"&(score[i][0]));
            
        printf("%d번 학생의 국어 점수 :", i);
        scanf_s("%d"&(score[i][1]));
 
        printf("%d번 학생의 영어 점수 :", i);
        scanf_s("%d"&(score[i][2]));
 
        printf("%d번 학생의 수학 점수 :", i);
        scanf_s("%d"&(score[i][3]));
    }
    print_score();
    //총점 계산
    for (int i = 0; i < student; i++) {
        for (int j = 1; j <= 3; j++) {
            score[i][4+= score[i][j];
        }
    }
    print_score();
    //평균계산
    for (int i = 0; i < student; i++) {
            score[i][5]=score[i][4/ 3;
    }
    print_score();
    //rank 배열에 입력
    for (int i = 0; i < student; i++) {
        rank[i]=score[i][4];
    }
    //rank 배열 정렬
    for (int i = 0; i < student; i++) {
        for (int j = i + 1; j < student; j++) {
            if (rank[i] < rank[j]) {
                int tmp = rank[i];
                rank[i] = rank[j];
                rank[j] = tmp;
            }
 
        }
    }
    // rank배열 출력
    for (int i = 0; i < student; i++) {
        printf("%d ", rank[i]);
    }
    printf("\n");
    // 석차 배열에 입력
    for (int i = 0; i < student; i++) {
        for (int j = 0; j < student; j++) {
            if (rank[j] == score[i][4])
                score[i][6= j+1;
        }
    }
    print_score();
    return 0;
}
 
 
cs

 

 

 

//석차 구하는 함수

for (int i=0; i<student; i++){

score[i][6] =1; // 초기화

for (int j=0;j<student; j++){

if (score[i][5]<score[j][5]){

score[i][6]++;

}

}

// 이 함수를 쓰는게 좀더 편한 것 같다..

 


 // 학번 순으로 정렬
 for (int i = 0; i < student; i++) {
  for (int j = i + 1; j < student; j++) {
   if (score[i][0] > score[j][0]) {
    for (int k = 0; k < 7; k++) {
     int tmp = score[i][k];
     score[i][k] = score[j][k];
     score[j][k] = tmp;

    }
   }
  }
 }

// 정렬부분 누락 됨 추가 필요.

 

 

반응형

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

C언어 - 포인터 이해  (0) 2018.09.07
C언어 - EOF  (0) 2018.09.07
C언어 - 탐색, 정렬, 순위  (0) 2018.09.07
C언어 - Stack으로 구현한 자동차 주차장  (0) 2018.09.06
C언어(자료구조) - Stack 구현  (0) 2018.09.06
반응형

/* 출력결과

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

+ Recent posts