hong.com
Final
2025-08-06
2025-08-07
2025-08-08
2025-08-11
2025-08-12
2025-08-13
2025-08-14
2025-08-18
2025-08-19
Project
C Language
game instructions
FFT
game instructions
Study
C Language
ARM System Programming
C Programming - Challenges
C programming
Linux
Day1
Day2
Day3
Day4
Day5
Day6
Day7
Day8
SKIN_Project copy
SKIN_Project
perceptron
System Verilog
Day1
Day2
Day3
VLSI Circuits Design
Day1
Subsubcategory 1
Subsubcategory 2
example
Home
Contact
Copyright ยฉ 2024 |
Yankos
Home
> Study
๐ ๆ้ใขใค (โโขแดโขโ)โก โง*ใ
Study
Day3 : Low-pass Filter
Project #1 MobaXterm Code : rrc_filter.sv // Created on 2025/07/15 by jaehong `timescale 1ns / 1ps module rrc_filter #( parameter WIDTH = 7 )( input clk, input rstn, input [WIDTH-1:0] data_in, // format : <1.6> output logic signed [WIDTH-1:0] data_out ); // coeff -3, -1.8, -7, -14, 24, 19, -62, -23, 225, 387, 225, -23, -62, 19, 24, -14, -7, 8, -1, -3 // format : <1.8> logic signed [WIDTH+9-1:0] mul_00, mul_01, mul_02, mul_03; logic signed [WIDTH+9-1:0] mul_04, mul_05, mul_06, mul_07; logic signed [WIDTH+9-1:0] mul_08, mul_09, mul_10, mul_11; logic signed [WIDTH+9-1:0] mul_12, mul_13, mul_14, mul_15; logic signed [WIDTH+9-1:0] mul_16, mul_17, mul_18, mul_19; logic signed [WIDTH+9-1:0] mul_20, mul_21, mul_22, mul_23; logic signed [WIDTH+9-1:0] mul_24, mul_25, mul_26, mul_27; logic signed [WIDTH+9-1:0] mul_28, mul_29, mul_30, mul_31; logic signed [WIDTH+9-1:0] mul_32; logic signed [WIDTH-1:0] shift_din [32:0]; integer i; always@(posedge clk or negedge rstn) begin if (~rstn) begin for(i = 32; i >= 0; i=i-1) begin shift_din[i] <= 0; end end else begin for(i = 32; i > 0; i=i-1) begin shift_din[i] <= shift_din[i-1]; end shift_din[0] <= data_in; end end always @(*) begin mul_00 = shift_din[00]*0; mul_01 = shift_din[01]*-1; mul_02 = shift_din[02]*1; mul_03 = shift_din[03]*0; mul_04 = shift_din[04]*-1; mul_05 = shift_din[05]*2; mul_06 = shift_din[06]*2; mul_07 = shift_din[07]*-2; mul_08 = shift_din[08]*2; mul_09 = shift_din[09]*0; mul_10 = shift_din[10]*-6; mul_11 = shift_din[11]*8; mul_12 = shift_din[12]*10; mul_13 = shift_din[13]*-28; mul_14 = shift_din[14]*-14; mul_15 = shift_din[15]*111; mul_16 = shift_din[16]*196; mul_17 = shift_din[17]*111; mul_18 = shift_din[18]*-14; mul_19 = shift_din[19]*-28; mul_20 = shift_din[20]*10; mul_21 = shift_din[21]*8; mul_22 = shift_din[22]*-6; mul_23 = shift_din[23]*0; mul_24 = shift_din[24]*2; mul_25 = shift_din[25]*-2; mul_26 = shift_din[26]*0; mul_27 = shift_din[27]*2; mul_28 = shift_din[28]*-1; mul_29 = shift_din[29]*0; mul_30 = shift_din[30]*1; mul_31 = shift_din[31]*-1; mul_32 = shift_din[32]*0; end logic signed [WIDTH+16-1:0] filter_sum; //always_comb begin always @(*) begin filter_sum = mul_00 + mul_01 + mul_02 + mul_03 + mul_04 + mul_05 + mul_06 + mul_07 + mul_08 + mul_09 + mul_10 + mul_11 + mul_12 + mul_13 + mul_14 + mul_15 + mul_16 + mul_17 + mul_18 + mul_19 + mul_20 + mul_21 + mul_22 + mul_23 + mul_24 + mul_25 + mul_26 + mul_27 + mul_28 + mul_29 + mul_30 + mul_31 + mul_32; end logic signed [WIDTH+8-1:0] trunc_filter_sum; assign trunc_filter_sum = filter_sum[WIDTH+16-1:8]; always_ff @(posedge clk or negedge rstn) begin if (~rstn) data_out <= 'h0; else if (trunc_filter_sum >= 63) data_out <= 63; else if (trunc_filter_sum < -64) data_out <= -64; else data_out <= trunc_filter_sum[WIDTH-1:0]; end endmodule code : tb_rrc_filter.sv `timescale 1ns/10ps module tb_rrc_filter(); logic clk, rstn; logic signed [6:0] data_in; logic signed [6:0] data_out; logic signed [6:0] adc_data_in [0:93695]; initial begin clk <= 1'b1; rstn <= 1'b0; #55 rstn <= 1'b1; // #500000 $finish; end always #5 clk <= ~clk; integer fd_adc_di; integer fd_rrc_do; integer i; int data; initial begin //always @(posedge clk) begin fd_adc_di = $fopen("./ofdm_i_adc_serial_out_fixed_30dB.txt", "r"); //fd_adc_di = $fopen("./ofdm_adc_serial_out_fixed_30dB.txt", "r"); fd_rrc_do = $fopen("./rrc_do.txt", "w"); i = 0; while (!$feof(fd_adc_di)) begin void'($fscanf(fd_adc_di,"%d\n",data)); adc_data_in[i] = data; i = i + 1; end #800000 $finish; $fclose(fd_rrc_do); end logic [23:0] adc_dcnt; always_ff @(posedge clk or negedge rstn) begin if (~rstn) adc_dcnt <= 'h0; else adc_dcnt <= adc_dcnt + 1'b1; end logic [6:0] tmp_data_in; assign tmp_data_in = adc_data_in[adc_dcnt]; logic [6:0] data_in; always_ff @(posedge clk or negedge rstn) begin if (~rstn) data_in <= 'h0; else data_in <= tmp_data_in; end always_ff @(negedge clk) begin //$fd_rrc_do = $fopen("./rrc_do.txt", "w"); $fwrite(fd_rrc_do, "%0d\n", data_out); end rrc_filter #(.WIDTH(7)) i_rrc_filter ( .clk(clk), .rstn(rstn), .data_in(data_in), .data_out(data_out) ); endmodule code : run_rcc vcs -sverilog -full64 -debug_all \ tb_rrc_filter.sv rrc_filter.sv \ -o simv && ./simv [aedu34@kccisynop2 /home/aedu34/asic_adu_red/verilog/project1] rrc_filter.sv tb_rrc_filter.sv run_rcc ofdm_i_adc_serial_out_fixed_30dB.txt source run_rcc rrc_do.txt ์์ฑ๋จ code : run_rrc_verdi vcs -full64 -sverilog -kdb -debug_access+all+reverse rrc_filter.sv tb_rrc_filter.sv ./simv -verdi & source run_rrc_verdi MATLAB C:\Users\kccistc\Documents\MATLAB rrc_do.txt Code : matlab graph y = load('rrc_do.txt'); % ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ plot(y); % ํํ ๊ทธ๋ฆฌ๊ธฐ xlabel('Sample'); ylabel('Amplitude'); title('RRC Filter Output'); grid on; Result : matlab graph Code : matlab_dump_analysis_stu.m % Created on 2025/07/02 by jihan clc; % fixed_mode = 0; % '0' = floating fixed_mode = 1; % '1' = fixed [FileName, PathName] = uigetfile('*.txt', 'select the capture binary file'); [FID, message] = fopen(FileName, 'r'); if (fixed_mode) waveform = fscanf(FID, '%d', [1 Inf]); else waveform = fscanf(FID, '%f', [1 Inf]); end Iwave = waveform(1, :); figure; pwelch(double(Iwave)); Result : matlab graph ๊ณ ์ ์์์ FIR ํํฐ์ ๋นํธํญ ๊ณ์ฐ ์ ๋ฆฌ ๐ ์ ๋ ฅ & ๊ณ์ ๋นํธํญ data ํฌ๋งท: <1.6> โ ์ด 7๋นํธ (๋ถํธ 1๋นํธ + ์์ 6๋นํธ) coeff ํฌ๋งท: <1.8> โ ์ด 9๋นํธ (๋ถํธ 1๋นํธ + ์์ 8๋นํธ) โด๏ธ ๊ณฑ์ ์ ๋นํธํญ ํ์ฅ <1.6> ร <1.8> = <2.14> 7bit ร 9bit = 16bit ์ ์๋ถ: 1๋นํธ + 1๋นํธ = 2๋นํธ ์์๋ถ: 6๋นํธ + 8๋นํธ = 14๋นํธ ๊ฒฐ๊ณผ: <2.14> = ์ด 16๋นํธ โ ๋ง์ (๋์ ) ์ ๋นํธํญ ํ์ฅ ๊ณ ์ ์์์ ๋ผ๋ฆฌ ๋ํ ๋๋ ์ ์๋ถ๊ฐ 1๋นํธ ์ฆ๊ฐํ ์ ์์ (Carry ๋ฐ์ ๋๋น) ๋์ ํด์ ๋ํ ์๋ก ์ ์๋ถ ๋นํธํญ์ ceil(log2(N))๋งํผ ๋์ด๋จ (N์ ๋ง์ ํ์ ๋๋ Tap ์) ๐ ์์ : ๊ฐ์ ๋นํธํญ <2.14> ๊ฐ์ ๋์ ํ ๊ฒฝ์ฐ Tap ์ ๋์ ๊ฒฐ๊ณผ ๋นํธํญ 2 tap <3.14> 16 tap <6.14> 32 tap <7.14> 33 tap <8.14> โป ์์๋ถ๋ ํญ์ 14๋นํธ๋ก ์ ์ง, ์ ์๋ถ๋ง ํ์ฅ๋จ ๐ค ์ถ๋ ฅ ๋ณํ ๋จ๊ณ ๋์ ๊ฒฐ๊ณผ: <8.14> โ ์ถ๋ ฅ ํฌ๋งท <1.6>์ผ๋ก ๋ณํํ๊ธฐ ์ํด ๋ค์ ์์ ์ํ: ์์๋ถ ์ ๋จ (Truncation) ํ์ 8๋นํธ(์์๋ถ)๋ฅผ ์ ๊ฑฐ <8.14> โ <8.6> ์์๋ถ 14๋นํธ โ 6๋นํธ๋ก ์ถ์ ์ ์๋ถ ํฌํ ์ฒ๋ฆฌ (Saturation) ์ถ๋ ฅ ์ ์๋ถ๋ 1๋นํธ๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ฏ๋ก ๊ฐ โฅ 63 โ 63 ๊ฐ โค -64 โ -64 ์ต์ข ์ถ๋ ฅ ํฌ๋งท ๊ฒฐ๊ณผ๋ <1.6> ํ์์ผ๋ก Flip-Flop์ ์ ์ฅ๋์ด ์ถ๋ ฅ ์์ ํ โ ํต์ฌ ์์ฝ ์ฐ์ฐ ์ ์๋ถ ๋ณํ ์์๋ถ ๋ณํ ๊ณฑ์ <a.b> ร <c.d> a + c b + d ๋ง์ <a.b> + <a.b> a + 1 b ์ ์ง ๋์ ๋ง์ N๋ฒ a + ceil(log2(N)) b ์ ์ง ์ถ๋ ฅ ๋ณํ ์ ์๋ถ ์๋ผ์ ํฌํ ์์๋ถ b โ 6 1000 -> 1250 1.25G ๋ฅผ ์ฃผ๊ณ timing check (main.c) logic์ผ๋ก system verilog floating point file fix point ์์ฑ ์ฑ๋ฅ ๋ง์กฑ ์ํค๋ฉด rtl์ ์ง๊ณ ํฉ์ฑํ๊ณ gate sim๊น์ง 20์ด ๋ค์ด๊ฐ๊ณ 17ํด๋ญ ๋ค์ ๋์ด 17๋ฒ์งธ ํด๋ญ์ tcl, sdc, los ๋งคํธ๋ฉ์
Study
ยท 2025-07-16
Day2 : Synthesis
Synthesis tool : synopsis design tool simulation ๊ฒฐ๊ณผ๋ ๋์๋ Synthesis ๋์ง ์๋ ๊ฒฝ์ฐ, ๋๋ถ๋ถ ์ฝ๋ ๋ฌธ์ Synthesis ์์ ๊ฐ์ฅ ์ค์ํ ๊ฒ์ Timing ์ฒดํฌ -> Setup, Hold ํ๋ฆฌํ๋กญ์์ ๋์ด๊ฐ๋ clock sqew ์๊ธธ ์๋ ์์ Clock Domain Crossing (CDC)๊ณผ Metastability ๐ก Clock Domain Crossing (CDC)๋? ์๋ก ๋ค๋ฅธ ํด๋ญ ๋๋ฉ์ธ์ ์ฌ์ฉํ๋ ํ๋ก ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ์ํฉ ์: A ๋๋ฉ์ธ์ 100MHz, B ๋๋ฉ์ธ์ 25MHz โ ์ฃผ๊ณ ๋ฐ๋ ํ์ด๋ฐ์ด ๋ค๋ฆ ๐ด Metastability(๋ฉํ์คํ ๋น๋ฆฌํฐ)๋? ํ๋ฆฝํ๋กญ์ด ์์ ๋ ์ํ(0 ๋๋ 1)๊ฐ ์๋ ์ ๋งคํ ์ํ์ ๋น ์ง๋ ํ์ ์ฃผ๋ก setup time / hold time์ ์๋ฐํ ๋ ๋ฐ์ ๊ฒฐ๊ณผ์ ์ผ๋ก ์ถ๋ ฅ์ด ์์ธก ๋ถ๊ฐ๋ฅํ๊ฑฐ๋ ๋ฆ๊ฒ ์์ ๋จ ๐ ๋์ ์ฐ๊ด์ฑ CDC ์ํฉ์์๋ ํ์ด๋ฐ์ด ๋ง์ง ์๋ ๋ฐ์ดํฐ๊ฐ ์ ์ก๋๊ธฐ ์ฝ๊ณ , ์ด๋ก ์ธํด ์์ ๋๋ฉ์ธ์ ํ๋ฆฝํ๋กญ์ด ๋ฉํ์คํ ์ด๋ธ ์ํ์ ๋น ์ง ์ ์์ ๋ฐ๋ผ์ CDC โ Metastability ์ํ ์ฆ๊ฐ ๐ ํด๊ฒฐ ๋ฐฉ๋ฒ (Synchronizer) ๋๊ธฐํ ํ๋ก๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉํ์คํ ๋น๋ฆฌํฐ๋ฅผ ์ํ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ๋ 2๋จ ํ๋ฆฝํ๋กญ ๋๊ธฐํ๊ธฐ always @(posedge clk_b) begin sync1 <= async_signal; sync2 <= sync1; // ์ฌ๊ธฐ์ metastability ์ํ end ํฉ์ฑํ๋ ์ด์ ? rtl ์ฝ๋์์ ์ ๋๊ณ ๋ฌธ์ ๊ฐ ์๋๋ผ๋ ํฉ์ฑ ์ ์๋ฌ๊ฐ ์๊ธธ ์ ์์. ํฉ์ฑ์ด ๋๋ ์ฝ๋๊ฐ ๋๋๋ก ์์ ํด์ค์ผ ํจ. ํฉ์ฑํ๊ธฐ ์ ์ rtl sim์ ๋ค์ ๋๋ ค๋ด์ผํจ run.log ํ์ผ์์ /Err, /Criticure warring ์ด ์๋ ์ผํจ ๋ง์ง๋ง gate sim์์ netlist๋ฅผ ๊ฐ๊ณ ์์ ๋๋ ธ์ ๋ rtl ๊ณผ ๊ฐ์์ผํจ. ๊ฐ๋ ๋ค๋ฅธ ๊ฒฝ์ฐ๋ xpro? ๊ฒ์ดํธ ๋ ๋ฒจ์์ x๊ฐ์ด ๋์ค๋ฉด ๊ฐ์ฅ ์์ ๋ฌ ์ (0๋ถํฐ)๋ฅผ ์ซ์๊ฐ์ผ๋จ. counter1_xpro ์์ ํ๊ณ ๋ค์ rtl sim syn setuo check (30~40% margin ์ฃผ๊ณ ) gate sim
Study
ยท 2025-07-15
Day1 : System Verilog Review
initial vs always ๐ข initial ์๋ฎฌ๋ ์ด์ ์๊ฐ 0์ ์์๋จ ๋ฑ ํ ๋ฒ๋ง ์คํ๋๊ณ ์ข ๋ฃ๋จ ๋ธ๋ก ์์ ์ฝ๋๊ฐ ์์ฐจ์ ์ผ๋ก ์คํ ์ฌ๋ฌ ๊ฐ ์์ผ๋ฉด ๋์์ ๋ณ๋ ฌ ์คํ ์ฃผ๋ก ํ ์คํธ๋ฒค์น, ์ด๊ธฐํ, ์ํ์ค ์ ์ด ๋ฑ์ ์ฌ์ฉ ๋ด๋ถ์ #10, sqen ๊ฐ์ ์ํ์ ํ๋ฆ ํํ ๊ฐ๋ฅ (์๋ฎฌ๋ ์ด์ ์ ์ฉ) โ ์: ์๋ฎฌ๋ ์ด์ ์ค ํ ๋ฒ๋ง ํน์ ๋์์ ์ํํ ๋ ๐ต always ์๋ฎฌ๋ ์ด์ ์๊ฐ 0๋ถํฐ ์์ ์กฐ๊ฑด์ด ์ฐธ์ผ ๋๋ง๋ค ๊ณ์ ์คํ๋จ (์๊ตฌ ๋ฃจํ์ฒ๋ผ ๋์) ํ๋ก ๋ด ๋์์ ์ง์์ ์ผ๋ก ๊ฐ์งํ๊ฑฐ๋ ์ํํ ๋ ์ฌ์ฉ ์ค๊ณ ๋ธ๋ก๊ณผ ํ ์คํธ๋ฒค์น ๋ชจ๋์์ ์ฌ์ฉ ๊ฐ๋ฅ ํ๋์จ์ด ํฉ์ฑ ๊ฐ๋ฅ (๋๊ธฐ/์กฐํฉ ํ๋ก ๊ตฌํ) โ ์: ํด๋ญ ์ฃ์ง๋ง๋ค ๋ ์ง์คํฐ ๊ฐ์ ๊ฐฑ์ ํ๊ฑฐ๋, ์ ๋ ฅ ๋ณํ์ ๋ฐ์ โ ์์ฝ initial์ ํ ๋ฒ๋ง ์คํ โ ์ฃผ๋ก ์๋ฎฌ๋ ์ด์ ์ ์ด always๋ ๊ณ์ ๋ฐ๋ณต ์คํ โ ์ฃผ๋ก ํ๋ก ๋์ ๊ตฌํ = vs <= (Blocking vs Non-blocking) โฑ๏ธ ์๊ฐ ํ๋ฆ ์ฐจ์ด ์ ๋ฆฌ = (Blocking): ์ฆ์ ํ ๋น๋จ โ ์๊ฐ ํ๋ฆ ์์ ๋ค์ ๋ฌธ์ฅ ์คํ ์ ์ ๊ฐ์ด ๋ฐ๋ก ๋ฐ์๋จ <= (Non-blocking): ์์ฝ ํ ๋์ค์ ๋ฐ์ โ ์๊ฐ ํ๋ฆ ์์ ๊ฐ์ ์๋ฎฌ๋ ์ด์ ์ฌ์ดํด ๋ด์์ ๊ฐ์ด ๋์์ ์ ๋ฐ์ดํธ๋จ ๋น๋๊ธฐ ๋ฆฌ์ vs ๋๊ธฐ ๋ฆฌ์ (Power / Area / Cost) ๐ข ๋น๋๊ธฐ ๋ฆฌ์ (Asynchronous Reset) ๐ ์ ๋ ฅ: ๋ฎ์ โ ํด๋ญ ์์ด ์ฆ์ ๋ฆฌ์ ๋๋ฏ๋ก ์ ๋ ฅ ์๋ชจ๊ฐ ์ ์ ๐ ๋ฉด์ : ์์ โ ๋ฆฌ์ ๋ก์ง์ด ๊ฐ๋จํด์ ํ๋์จ์ด ์์ ์ ๊ฒ ์ฌ์ฉ ๐ฐ ๋น์ฉ: ๋ฎ์ โ ๊ตฌํ์ด ๋จ์ํ์ฌ ๊ฒ์ดํธ ์๊ฐ ์ ๊ณ ๋น์ฉ ์ ๊ฐ ๊ฐ๋ฅ ๐ต ๋๊ธฐ ๋ฆฌ์ (Synchronous Reset) ๐ ์ ๋ ฅ: ๋์ โ ํด๋ญ ์ ํธ๊ฐ ํ์ํ์ฌ ํด๋ญ ๊ด๋ จ ์ ๋ ฅ ์๋ชจ ๋ฐ์ ๐ ๋ฉด์ : ํผ โ ์กฐ๊ฑด ์ ์ด ๋ก์ง ์ถ๊ฐ๋ก ์ธํด ํ๋์จ์ด ์์์ด ๋ ๋ค์ด๊ฐ ๐ฐ ๋น์ฉ: ๋์ โ ๋ ผ๋ฆฌ ํ๋ก๊ฐ ๋ณต์กํด์ง๋ฉฐ ์ค๊ณ ๋ฐ ๊ตฌํ ๋น์ฉ ์ฆ๊ฐ โ ์์ฝ Async ๋ฆฌ์ : ๋ฆฌ์์ค ์ ์ฝ (์ ์ ๋ ฅ, ์ ๋ฉด์ , ์ ๋น์ฉ), ํ์ง๋ง ํ์ด๋ฐ ๋ฏผ๊ฐ๋ โ Sync ๋ฆฌ์ : ์ค๊ณ ์์ ์ฑ โ, ๊ทธ๋ฌ๋ ์์ ์๋ชจ โ ์กฐํฉ ๋ ผ๋ฆฌ vs ์์ฐจ ๋ ผ๋ฆฌ ๐ข Combinational Logic (์กฐํฉ ๋ ผ๋ฆฌ) ์ถ๋ ฅ์ด ์ ๋ ฅ์๋ง ์์กด ๋ฉ๋ชจ๋ฆฌ(์ํ ์ ์ฅ) ์์ ์์ ํด๋ญ ์ ํธ ์์ด ๋์ ์ ๋ ฅ์ด ๋ฐ๋๋ฉด ์ฆ์ ์ถ๋ ฅ๋ ๋ฐ๋ ์: ๊ฐ์ฐ๊ธฐ, ์ธ์ฝ๋, ๋์ฝ๋, ๋ฉํฐํ๋ ์ ๋ฑ assign y = a & b; ๐ต Sequential Logic (์์ฐจ ๋ ผ๋ฆฌ) ์ถ๋ ฅ์ด ์ ๋ ฅ + ์ด์ ์ํ(๋ฉ๋ชจ๋ฆฌ)์ ์์กด ๋ ์ง์คํฐ, ํ๋ฆฝํ๋กญ ๋ฑ ์ํ ์ ์ฅ ์์ ์ฌ์ฉ ํด๋ญ ์ ํธ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ํ๊ฐ ๊ฐฑ์ ๋จ ์๊ฐ ๊ฐ๋ (์์, ํ์ด๋ฐ)์ด ์ค์ ์: ๋ ์ง์คํฐ, ์นด์ดํฐ, FSM ๋ฑ always @(posedge clk) begin q <= d; end โ ์์ฝ ์กฐํฉ ๋ ผ๋ฆฌ: ํ์ฌ ์ ๋ ฅ๋ง์ผ๋ก ๊ฒฐ์ โ ๋ฉ๋ชจ๋ฆฌ ์์, ํด๋ญ ํ์ ์์ ์์ฐจ ๋ ผ๋ฆฌ: ํ์ฌ ์ ๋ ฅ + ์ด์ ์ํ๋ก ๊ฒฐ์ โ ๋ฉ๋ชจ๋ฆฌ ์์, ํด๋ญ ํ์ Equality Operator ์์ฝ โฌ ==, != x, z ๋ฌด์ ๋ชปํจ โ ๊ฒฐ๊ณผ๊ฐ x ๋ ์ ์์ ์ผ๋ฐ์ ์ธ ๊ฐ ๋น๊ต์ ์ฌ์ฉ โฌ ===, !== x, z๊น์ง ํฌํจํด์ ์ ํํ ๋น๊ต ํ ์คํธ๋ฒค์น์์ ์์ฃผ ์ฌ์ฉ โ ํต์ฌ ์ ๋ฆฌ ==, != โ ๊ฐ ๋น๊ต, x/z ์์ผ๋ฉด ๊ฒฐ๊ณผ๋ x ===, !== โ ์์ ์ผ์น ๋น๊ต, x/z ํฌํจํด์ ๋น๊ต signed vs unsigned ๐ข unsigned ๋ถํธ ์๋ ์ โ 0 ์ด์๋ง ํํ MSB(์ต์์ ๋นํธ)๋ ๊ฐ์ ์ผ๋ถ ๊ธฐ๋ณธ๊ฐ (์๋ฌด๊ฒ๋ ์ ๋ถ์ด๋ฉด unsigned) ์: 4โb1000 = 8 (์์๊ฐ ์๋) ๐ต signed ๋ถํธ ์๋ ์ โ ์์/์์ ๋ชจ๋ ํํ ๊ฐ๋ฅ MSB๋ ๋ถํธ๋นํธ๋ก ์ฌ์ฉ๋จ (0: ์์, 1: ์์) ๋น๊ต, ์ฐ์ฐ ์ signed๋ผ๋ฆฌ ๊ณ์ฐ๋์ด์ผ ์ ํํจ reg signed [3:0] a = 4'b1000; // -8 โ ์์ฝ unsigned โ 0 ์ด์๋ง ํํ, ๊ธฐ๋ณธ๊ฐ signed โ ์์๊น์ง ํํ, MSB๋ ๋ถํธ๋นํธ reg vs wire ๐ข wire ๊ฐ ์ ์ฅ X, ์ฐ๊ฒฐ์ฉ assign์ผ๋ก๋ง ๊ฐ ์ง์ ์กฐํฉ ๋ ผ๋ฆฌ ํ๋ก์ ์ฌ์ฉ ๐ต reg ๊ฐ ์ ์ฅ O, ๋ด๋ถ ์ํ ์ ์ง always, initial ๋ธ๋ก์์ ๊ฐ ํ ๋น ์์ฐจ ๋ ผ๋ฆฌ ํ๋ก์ ์ฌ์ฉ โ ์์ฝ wire: ์ ์ฅ โ, ์ฐ๊ฒฐ๋ง reg: ์ ์ฅ โญ, ๋ธ๋ก ๋ด์์ ๊ฐ ์ ์ง Clock Gating, else ์ฌ์ฉ, power ๊ด๋ จ ๐ข local clock gating ํน์ block๋ง ์ ํ์ ์ผ๋ก ํด๋ญ ๊ณต๊ธํด์ ๋์์ ์ ํ ์ฌ์ฉ ๋ชฉ์ : ๋์ ์ ํ๋ ํ๋ก์ ์ ๋ ฅ ์ฐจ๋จ โ ์ ์ ๋ ฅ ์ค๊ณ ์ง์ if (~enable)๋ก ์ฒ๋ฆฌํ๋ ๊ฒ๋ณด๋ค ๋ ํจ์จ์ ์ผ ์ ์์ ๐ต Flip-Flop (seq logic)์์ else ์ฌ์ฉ ํญ์ else๋ฅผ ์ฐ๋ ๊ฒ ์์น์ ์๋ ํ์ง๋ง always @(posedge clk)์์ if๋ง ์ฐ๊ณ else๊ฐ ์์ผ๋ฉด: ์ด์ ๊ฐ ์ ์ง ์ ๋ ์ ์์ synthesis ํด์ด latch๋ก ์คํดํ ์๋ ์์ ๋ช ํํ ์ํ ์ ์ง ๋ชฉ์ ์ผ๋ก else๋ฅผ ๋ช ์ํ๋ ๊ฒ ์ข์ โ ํ์ง๋ง ๋ถํ์ํ ์ฐ์ฐ(=ํ ๊ธ)์ด ๋ฐ์ํ ์ ์์ด โ power ์ธก๋ฉด์์ ๋ถ๋ฆฌํ ์ ์์ ๐ท Combinational Logic์์๋? always @(*)์์๋ if์ else๋ฅผ ๋ฐ๋์ ์จ์ผ ํจ โ ๋ชจ๋ ์กฐ๊ฑด์์ ์ถ๋ ฅ์ด ์ ์๋์ง ์์ผ๋ฉด latch ๋ฐ์ ์ํ latch๋ฅผ ํผํ๋ ค๋ฉด ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๊ตฌ์กฐ (else ํฌํจ) ๊ฐ ์ค์ํจ โ ์์ฝ local clock gating: ํด๋ญ์ ์ฐจ๋จํด์ ์ ๋ ฅ ์ค์ด๋ ๊ธฐ๋ฒ FF์์ else: ์ํ ๋ณด์กด ๋ช ํํ ํ๋ ค๋ฉด ์ข์ง๋ง, ๋ถํ์ํ ์ ๋ ฅ ์๋ชจ ์ฃผ์ combinational์์ else ๋๋ฝ: latch ๋ฐ์ ๊ฐ๋ฅ์ฑ ์์ โ ๋ฐ๋์ ์ฒ๋ฆฌ ํ์ Quiz 1 ์ด ์ฝ๋๋ ํฉ๋ฒ์ ์ธ๊ฐ? ์ปดํ์ผ์ด ๋ ๊น? program automatic test; bit [31:0] count; logic [31:0] Count = 'x; initial begin count = Count; $display("Count = %0x count = %0d", Count, count); end endprogram: test ๋ต๋ณ : ํฉ๋ฒ์ ์ธ SystemVerilog ์ฝ๋์ด๋ฉฐ ์ปดํ์ผ๋ ๊ฐ๋ฅํจ. program automatic์ ์๋ฎฌ๋ ์ด์ ํ๊ฒฝ์์ ์ฌ์ฉ๋๋ ๊ตฌ์กฐ์ด๊ณ ๋ฌธ๋ฒ์ ์ผ๋ก ๋ฌธ์ ์์. logic ํ์ ์ ์ด๋ค ํ์ ์ ๋ค๋ฅธ ์ด๋ฆ์ธ๊ฐ? โx๋ Count๋ฅผ ์ด๋ป๊ฒ ์ด๊ธฐํํ๋๊ฐ? ๋ต๋ณ : logic์ 4-state ํ์ ์ธ reg์ ๋์ฒด ํํ โ 0, 1, x, z ์ ์ฅ ๊ฐ๋ฅ. โx๋ ๋ชจ๋ ๋นํธ๋ฅผ unknown(x) ๊ฐ์ผ๋ก ์ด๊ธฐํํจ. ํ๋ก๊ทธ๋จ์ ๋ฌด์์ ์ถ๋ ฅํ ๊น? ์ count์ Count์ ๊ฐ์ด ๋ค๋ฅธ๊ฐ? ์ถ๋ ฅ ์์ : Count = xxxxxxxx count = 0 (ํ์์ ์ผ๋ก ํํ) ์ด์ : Count๋ logic์ด๋ฏ๋ก โx ๊ฐ์ ์ ์งํจ. count๋ bit ํ์ ์ด๋ผ 2-state๋ง ๊ฐ๋ฅ โ x ๊ฐ์ 0์ผ๋ก ๊ฐ์ ๋ณํํด์ ์ ์ฅ๋จ shift_reg
Study
ยท 2025-07-14
SKIN_Project
3์ผ๊ป๋ก ํ ๊น๋ Google Colab Dataset ๊ตฌ๊ธ ๋๋ผ์ด๋ธ > ๋ด ๋๋ผ์ด๋ธ > SKIN > dataset_skin Code : SKIN_Project.ipynb import matplotlib.pyplot as plt import numpy as np import os from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.applications import MobileNetV2 from tensorflow.keras import models, layers from tensorflow.keras.optimizers import Adam from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint # โ Google Drive ๋ง์ดํธ from google.colab import drive drive.mount('/content/drive') # โ ๊ฒฝ๋ก ์ค์ dataset_path = '/content/drive/MyDrive/SKIN/dataset_skin' # ๋๊ฐ ์ฌ๋ฆฐ ๊ฒฝ๋ก๋ก ์์ model_save_path = '/content/drive/MyDrive/SKIN/skin_model.h5' # ์ํ๋ ์ ์ฅ ๊ฒฝ๋ก # โ ๋ฐ์ดํฐ ์ฆ๊ฐ ์ค์ (rotation_range๋ 15๋๋ง ์ค) datagen = ImageDataGenerator( rescale=1./255, validation_split=0.2, rotation_range=15, width_shift_range=0.05, height_shift_range=0.05, shear_range=0.1, zoom_range=0.1, brightness_range=[0.8, 1.2], horizontal_flip=True, fill_mode='nearest' ) # โ ๋ฐ์ดํฐ ๋ก๋ฉ train_generator = datagen.flow_from_directory( dataset_path, target_size=(96, 96), # MobileNetV2๋ ์ต์ 96x96๋ถํฐ ๊ฐ๋ฅ batch_size=32, class_mode='categorical', subset='training', shuffle=True ) val_generator = datagen.flow_from_directory( dataset_path, target_size=(96, 96), batch_size=32, class_mode='categorical', subset='validation', shuffle=True ) # โ ํด๋์ค ์ด๋ฆ ์๋ ์ถ์ถ class_names = list(train_generator.class_indices.keys()) print("ํด๋์ค ์ธ๋ฑ์ค:", train_generator.class_indices) # โ MobileNetV2 ๊ธฐ๋ฐ ๋ชจ๋ธ ๊ตฌ์ฑ base_model = MobileNetV2(input_shape=(96, 96, 3), include_top=False, weights='imagenet') base_model.trainable = False model = models.Sequential([ base_model, layers.GlobalAveragePooling2D(), layers.Dense(128, activation='relu'), layers.Dropout(0.5), layers.Dense(len(class_names), activation='softmax') ]) model.compile(optimizer=Adam(learning_rate=1e-4), loss='categorical_crossentropy', metrics=['accuracy']) # โ ์ฝ๋ฐฑ ์ค์ early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True) checkpoint = ModelCheckpoint('best_model.h5', save_best_only=True) # โ ํ์ต ์คํ history = model.fit( train_generator, validation_data=val_generator, epochs=50, callbacks=[early_stop, checkpoint], verbose=2 ) # โ ๊ฒฐ๊ณผ ์๊ฐํ acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(len(acc)) plt.plot(epochs, acc, 'bo', label='Training acc') plt.plot(epochs, val_acc, 'b', label='Validation acc') plt.title('Training and Validation Accuracy') plt.legend() plt.show() plt.plot(epochs, loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'b', label='Validation loss') plt.title('Training and Validation Loss') plt.legend() plt.show() # โ ํ์ต ์ด๋ฏธ์ง ์์ x_batch, y_batch = next(train_generator) plt.figure(figsize=(10, 10)) for i in range(25): plt.subplot(5, 5, i + 1) plt.xticks([]); plt.yticks([]); plt.grid(False) plt.imshow(x_batch[i]) label_idx = np.argmax(y_batch[i]) plt.xlabel(class_names[label_idx]) plt.tight_layout() plt.show() # โ ๋ชจ๋ธ ์ ์ฅ (.h5 ํ์ผ) model.save(model_save_path) print(f"๋ชจ๋ธ์ด ์ ์ฅ๋์์ต๋๋ค: {model_save_path}") Result Ubuntu Terminal # ์์ ํ ๋๋ ํ ๋ฆฌ ์์ฑ ํ ์ด๋ mkdir skin_project cd skin_project # ๊ฐ์ํ๊ฒฝ ์์ฑ ๋ฐ ํ์ฑํ python3 -m venv venv source venv/bin/activate # ํ์ํ ํจํค์ง ์ค์น pip install tensorflow opencv-python numpy # skin_names.json ์์ฑ vi skin_names.json // skin_names.json [ "๊ธฐ์ ์ธํฌ์", "ํํผ๋ญ์ข ", "ํ๊ด์ข ", "๋น๋ฆฝ์ข ", "ํ์์ ", "ํธํ์ธํฌ์", "์ฌ๋ง๊ท" ] # NanumGothic ์ค์น sudo apt install fonts-nanum fc-list | grep NanumGothic # Pillow ํจํค์ง ์ค์น pip install Pillow # predict_cam.py ์์ฑ vi predict_cam.py # predict_cam.py import cv2 import numpy as np import tensorflow as tf import json from PIL import ImageFont, ImageDraw, Image # ํด๋์ค ์ด๋ฆ (ํ๊ธ) ๋ถ๋ฌ์ค๊ธฐ with open("skin_names.json", "r") as f: class_names = json.load(f) # ๋ชจ๋ธ ๋ก๋ model = tf.keras.models.load_model("skin_model.h5") # ํ๊ธ ํฐํธ ๊ฒฝ๋ก (Ubuntu์์ ์ฌ์ฉ ๊ฐ๋ฅ) FONT_PATH = "/usr/share/fonts/truetype/nanum/NanumGothic.ttf" font = ImageFont.truetype(FONT_PATH, 32) # ์น์บ ์์ cap = cv2.VideoCapture(2) print("Press 'q' to quit.") while True: ret, frame = cap.read() if not ret: break # ์์ธก์ ์ํ ์ ์ฒ๋ฆฌ img = cv2.resize(frame, (96, 96)) img_array = np.expand_dims(img / 255.0, axis=0) pred = model.predict(img_array)[0] label = class_names[np.argmax(pred)] confidence = np.max(pred) # ์๋ณธ ํ๋ ์์ Pillow ์ด๋ฏธ์ง๋ก ๋ณํ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) pil_img = Image.fromarray(frame_rgb) draw = ImageDraw.Draw(pil_img) text = f"{label} ({confidence*100:.1f}%)" # ํ ์คํธ ์ถ๋ ฅ draw.text((10, 30), text, font=font, fill=(0, 255, 0)) # ์ด๋ก์ # ๋ค์ OpenCV ํ์์ผ๋ก ๋ณํํ์ฌ ์ถ๋ ฅ frame_bgr = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR) cv2.imshow("Skin Classifier", frame_bgr) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows() Comment ์น์บ ์ผ๋ก ์ธ์ํ ๊ฒฐ๊ณผ ํ์์ ์ ์ ๋๋ก ์ธ์ํ์ง ๋ชปํด์ ๋นผ์ผ๋ ๊ฒ ๊ฐ์ ๊ธฐ์ ์ธํฌ์ o ํํผ๋ญ์ข o ํ๊ด์ข o ๋น๋ฆฝ์ข o ํ์์ x ํธํ์ธํฌ์ o ์ฌ๋ง๊ท o
Study
ยท 2025-07-04
SKIN_Project
์ต์ข ํ๊ฑฐ ๋ ธ์ ์ ์ฒดํฌ Google Colab Dataset ๊ตฌ๊ธ ๋๋ผ์ด๋ธ > ๋ด ๋๋ผ์ด๋ธ > SKIN > dataset_skin Code : SKIN_Project.ipynb import matplotlib.pyplot as plt import numpy as np import os from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.applications import MobileNetV2 from tensorflow.keras import models, layers from tensorflow.keras.optimizers import Adam from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint # โ Google Drive ๋ง์ดํธ from google.colab import drive drive.mount('/content/drive') # โ ๊ฒฝ๋ก ์ค์ dataset_path = '/content/drive/MyDrive/SKIN/dataset_skin' # ๋๊ฐ ์ฌ๋ฆฐ ๊ฒฝ๋ก๋ก ์์ model_save_path = '/content/drive/MyDrive/SKIN/skin_model.h5' # ์ํ๋ ์ ์ฅ ๊ฒฝ๋ก # โ ๋ฐ์ดํฐ ์ฆ๊ฐ ์ค์ (rotation_range๋ 15๋๋ง ์ค) datagen = ImageDataGenerator( rescale=1./255, validation_split=0.2, rotation_range=15, width_shift_range=0.05, height_shift_range=0.05, shear_range=0.1, zoom_range=0.1, brightness_range=[0.8, 1.2], horizontal_flip=True, fill_mode='nearest' ) # โ ๋ฐ์ดํฐ ๋ก๋ฉ train_generator = datagen.flow_from_directory( dataset_path, target_size=(96, 96), # MobileNetV2๋ ์ต์ 96x96๋ถํฐ ๊ฐ๋ฅ batch_size=32, class_mode='categorical', subset='training', shuffle=True ) val_generator = datagen.flow_from_directory( dataset_path, target_size=(96, 96), batch_size=32, class_mode='categorical', subset='validation', shuffle=True ) # โ ํด๋์ค ์ด๋ฆ ์๋ ์ถ์ถ class_names = list(train_generator.class_indices.keys()) print("ํด๋์ค ์ธ๋ฑ์ค:", train_generator.class_indices) # โ MobileNetV2 ๊ธฐ๋ฐ ๋ชจ๋ธ ๊ตฌ์ฑ base_model = MobileNetV2(input_shape=(96, 96, 3), include_top=False, weights='imagenet') base_model.trainable = False model = models.Sequential([ base_model, layers.GlobalAveragePooling2D(), layers.Dense(128, activation='relu'), layers.Dropout(0.5), layers.Dense(len(class_names), activation='softmax') ]) model.compile(optimizer=Adam(learning_rate=1e-4), loss='categorical_crossentropy', metrics=['accuracy']) # โ ์ฝ๋ฐฑ ์ค์ early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True) checkpoint = ModelCheckpoint('best_model.h5', save_best_only=True) # โ ํ์ต ์คํ history = model.fit( train_generator, validation_data=val_generator, epochs=50, callbacks=[early_stop, checkpoint], verbose=2 ) # โ ๊ฒฐ๊ณผ ์๊ฐํ acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(len(acc)) plt.plot(epochs, acc, 'bo', label='Training acc') plt.plot(epochs, val_acc, 'b', label='Validation acc') plt.title('Training and Validation Accuracy') plt.legend() plt.show() plt.plot(epochs, loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'b', label='Validation loss') plt.title('Training and Validation Loss') plt.legend() plt.show() # โ ํ์ต ์ด๋ฏธ์ง ์์ x_batch, y_batch = next(train_generator) plt.figure(figsize=(10, 10)) for i in range(25): plt.subplot(5, 5, i + 1) plt.xticks([]); plt.yticks([]); plt.grid(False) plt.imshow(x_batch[i]) label_idx = np.argmax(y_batch[i]) plt.xlabel(class_names[label_idx]) plt.tight_layout() plt.show() # โ ๋ชจ๋ธ ์ ์ฅ (.h5 ํ์ผ) model.save(model_save_path) print(f"๋ชจ๋ธ์ด ์ ์ฅ๋์์ต๋๋ค: {model_save_path}") Result Ubuntu Terminal # ์์ ํ ๋๋ ํ ๋ฆฌ ์์ฑ ํ ์ด๋ mkdir skin_project cd skin_project # ๊ฐ์ํ๊ฒฝ ์์ฑ ๋ฐ ํ์ฑํ python3 -m venv venv source venv/bin/activate # ํ์ํ ํจํค์ง ์ค์น pip install tensorflow opencv-python numpy # skin_names.json ์์ฑ vi skin_names.json // skin_names.json [ "๊ธฐ์ ์ธํฌ์", "ํํผ๋ญ์ข ", "ํ๊ด์ข ", "๋น๋ฆฝ์ข ", "ํ์์ ", "ํธํ์ธํฌ์", "์ฌ๋ง๊ท" ] # NanumGothic ์ค์น sudo apt install fonts-nanum fc-list | grep NanumGothic # Pillow ํจํค์ง ์ค์น pip install Pillow # predict_cam.py ์์ฑ vi predict_cam.py # predict_cam.py import cv2 import numpy as np import tensorflow as tf import json from PIL import ImageFont, ImageDraw, Image # ํด๋์ค ์ด๋ฆ (ํ๊ธ) ๋ถ๋ฌ์ค๊ธฐ with open("skin_names.json", "r") as f: class_names = json.load(f) # ๋ชจ๋ธ ๋ก๋ model = tf.keras.models.load_model("skin_model.h5") # ํ๊ธ ํฐํธ ๊ฒฝ๋ก (Ubuntu์์ ์ฌ์ฉ ๊ฐ๋ฅ) FONT_PATH = "/usr/share/fonts/truetype/nanum/NanumGothic.ttf" font = ImageFont.truetype(FONT_PATH, 32) # ์น์บ ์์ cap = cv2.VideoCapture(2) print("Press 'q' to quit.") while True: ret, frame = cap.read() if not ret: break # ์์ธก์ ์ํ ์ ์ฒ๋ฆฌ img = cv2.resize(frame, (96, 96)) img_array = np.expand_dims(img / 255.0, axis=0) pred = model.predict(img_array)[0] label = class_names[np.argmax(pred)] confidence = np.max(pred) # ์๋ณธ ํ๋ ์์ Pillow ์ด๋ฏธ์ง๋ก ๋ณํ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) pil_img = Image.fromarray(frame_rgb) draw = ImageDraw.Draw(pil_img) text = f"{label} ({confidence*100:.1f}%)" # ํ ์คํธ ์ถ๋ ฅ draw.text((10, 30), text, font=font, fill=(0, 255, 0)) # ์ด๋ก์ # ๋ค์ OpenCV ํ์์ผ๋ก ๋ณํํ์ฌ ์ถ๋ ฅ frame_bgr = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR) cv2.imshow("Skin Classifier", frame_bgr) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows() Comment ์น์บ ์ผ๋ก ์ธ์ํ ๊ฒฐ๊ณผ ํ์์ ์ ์ ๋๋ก ์ธ์ํ์ง ๋ชปํด์ ๋นผ์ผ๋ ๊ฒ ๊ฐ์ ๊ธฐ์ ์ธํฌ์ o ํํผ๋ญ์ข o ํ๊ด์ข o ๋น๋ฆฝ์ข o ํ์์ x ํธํ์ธํฌ์ o ์ฌ๋ง๊ท o
Study
ยท 2025-07-04
Day8 : Project_Plan
๐งฑ ๋ผ์ฆ๋ฒ ๋ฆฌํ์ด ํ๊ฒฝ ๊ตฌ์ถ ctrl + alt + t โ ํฐ๋ฏธ๋ ์คํ ๋จ์ถํค htop โ ์ค์๊ฐ ์์คํ ๋ฆฌ์์ค(๋ฉ๋ชจ๋ฆฌ, CPU ์ฌ์ฉ๋ ๋ฑ) ํ์ธ ํด ์คํ sudo apt update โ ํจํค์ง ๋ชฉ๋ก ์ ๋ฐ์ดํธ (์ต์ ๋ฒ์ ํ์ธ์ฉ) sudo apt upgrade โ ์ค์น๋ ํจํค์ง๋ค์ ์ต์ ๋ฒ์ ์ผ๋ก ์ ๊ทธ๋ ์ด๋ sudo apt install ibus ibus-hangul โ ํ๊ธ ์ ๋ ฅ๊ธฐ(iBus + Hangul) ์ค์น sudo apt install fonts-nanum fonts-unfonts-core โ ๋๋๊ธ๊ผด, ์ค๊ธ๊ผด ๋ฑ ํ๊ธ ํฐํธ ์ค์น sudo update-alternatives --config x-www-browser โ ๊ธฐ๋ณธ ์น ๋ธ๋ผ์ฐ์ ์ค์ ๋ณ๊ฒฝ mkdir YOLO โ 'YOLO'๋ผ๋ ์ ํด๋ ์์ฑ cd YOLO/ โ YOLO ํด๋๋ก ์ด๋ python -m venv .yolo โ '.yolo'๋ผ๋ ์ด๋ฆ์ ๊ฐ์ํ๊ฒฝ(virtualenv) ์์ฑ source .yolo/bin/activate โ ๊ฐ์ํ๊ฒฝ ํ์ฑํ git clone https://github.com/ultralytics/yolov5 โ YOLOv5 ์ ์ฅ์๋ฅผ GitHub์์ ๋ณต์ cd yolov5/ โ ๋ณต์ ํ yolov5 ๋๋ ํ ๋ฆฌ๋ก ์ด๋ pip install -U pip โ pip(ํจํค์ง ๊ด๋ฆฌ์) ์ต์ ๋ฒ์ ์ผ๋ก ์ ๋ฐ์ดํธ pip install ultralytics โ Ultralytics ํจํค์ง ์ค์น (YOLOv5 ์คํ์ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํฌํจ) python detect.py --weights yolov5s.pt --source 0 --img 160 โ YOLOv5s ๋ชจ๋ธ๋ก ์น์บ (0๋ฒ)์ ์ค์๊ฐ ๊ฐ์ฒด ํ์ง (ํด์๋: 160) sudo apt install openssh-server โ SSH ์๋ฒ ์ค์น (๋ค๋ฅธ PC์์ ์๊ฒฉ ์ ์ ๊ฐ๋ฅํ๊ฒ ํจ) curl -fsSL https://ollama.com/install.sh | sh โ Ollama ์ค์น ์คํฌ๋ฆฝํธ ์คํ (๋ก์ปฌ AI ๋ชจ๋ธ ์คํ ๋๊ตฌ) ollama run gemma3:1b โ Gemma 3 1B ๋ชจ๋ธ ์คํ (๋ก์ปฌ LLM ํ ์คํธ) # PC์์ ์๊ฒฉ ์ ์ ssh hhhong@10.10.15.183 ๐ค ํ์๋ค๊ณผ ํ๋ก์ ํธ ๊ณํ ์๋ฆฝ ๋ผ์ฆ๋ฒ ๋ฆฌํ์ด๋ฅผ ํ์ฉํ On-Device AI ํ๋ก์ ํธ ๋ฐฉํฅ์ฑ ๋ ผ์ ๊น๋ฏผ๊ท: ์์ด ์ธ์ (Sign Language Recognition, SLR) ์์ฐฌํ: ๋จธ๋ฆฌ์นด๋ฝ ๋๊ป ๋ฐ ํ๋ชจ ์ด๊ธฐ ์ง๋จ + ๋ง์ถคํ ์๋ฐฉ ๊ด๋ฆฌ ๊ฐ์ด๋ ์์ฌํ: ๋ฅํ์ดํฌ ๊ฐ์ง ์์คํ ์์ฌํ: ๊ฑฐ์ง ๋ด์ค ํ๋ ์์คํ ๐ฌ ํ๊ณ ์ค์๊ฐ ๊ฐ์ฒด ํ์ง์ ๋ก์ปฌ AI ๋ชจ๋ธ์ด ๋์์ ๊ฐ๋ฅํจ์ ํ์ธํจ ๋ผ์ฆ๋ฒ ๋ฆฌํ์ด ํ๊ฒฝ ์ธํ ์ค ํ๋ธ๋ฅผ ํตํด LAN ์ผ์ด๋ธ์ ๋ถํ ํ๋ ๊ณผ์ ์์ PC์์ ๋คํธ์ํฌ๋ฅผ ์ก์ง ๋ชปํ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ผ๋, ์์ ์ข ๋ฃ ํ ์ ์์ ์ผ๋ก ์ฐ๊ฒฐ๋์ด IP ์ถฉ๋ ๋ฌธ์ ์์์ ํ์ ํจ ๐ก ๊ฐ์ฌ๋์ ์กฐ์ธ โCNN ๋ชจ๋ธ์ C++ ์์ค์ผ๋ก ๋ฝ์ผ๋ฉด ๊ฐ์ฅ ์ข์.โ ํ๋ก์ ํธ ์ฑ๊ณต ์ฌ๋ถ์ ์๊ด์์ด, ๊ตฌํ ์์ค์ ๋์ด๋ ๊ฒ์ด ๋ ์ค์ํ๋ค๋ ์ทจ์ง์ ๋ง์ โ๏ธ ํด์ ๋ฐ ๋์ ์ดํด ๋จ์ํ ๋ชจ๋ธ์ โ๋๋ฆฌ๋ ๊ฒโ์ ๋์ด์, ๊ตฌ์กฐ๋ฅผ ์ ํํ ์ดํดํ๊ณ ์ง์ ๊ตฌํํด๋ณด๋ ๊ฒฝํ์ด ์ค์ํ๋ค๋ ๋ป PyTorch, TensorFlow ๊ฐ์ ํ๋ ์์ํฌ๋ฅผ ์ธ ์๋ ์์ง๋ง, ๊ธฐ์ด ์ํ/๋ก์ง์ ๋ฐํ์ผ๋ก CNN์ ์ง์ ๊ตฌํ(C++ ์์ค์ ์ ์์ค ์ ๊ทผ)ํด๋ณด๋ ๊ฒ์ด ์ค๋ ฅ ํฅ์์ ๋์๋จ ์ค๋ฌด ๋๋ ์จ๋๋ฐ์ด์ค ํ๊ฒฝ(Raspberry Pi ๋ฑ)์์๋ ์ต์ ํ๋ ์ ์์ค ์ฝ๋๋ก ๋ชจ๋ธ์ ์ด์ํ๋ ๋ฅ๋ ฅ๋ ์๊ตฌ๋จ ๐ ์์ผ๋ก์ ํ์ต ๋ฐฉํฅ์ ๋ฐ์ CNN ๋ ์ด์ด ๊ตฌ์ฑ ๋ฐ ์ฐ์ฐ ๋ฐฉ์(ReLU, Conv, Pooling ๋ฑ)์ ์ง์ ๊ตฌํํด๋ณด๋ ์ฐ์ต Python์ผ๋ก ๊ตฌํ โ ๊ฐ๋ฅํ๋ค๋ฉด C++๋ก ๊ตฌ์กฐ๋ง์ด๋ผ๋ ์ฎ๊ฒจ๋ณด๊ธฐ PyTorch์์ .model.eval()์ฒ๋ผ ๋ด๋ถ ๋์์ด ์ด๋ป๊ฒ ๊ตฌ์ฑ๋๋์ง ์ญ์ผ๋ก ์ดํด๋ณด๊ธฐ
Study
ยท 2025-07-02
Day7 : HARIBO_Mini_Project
๐ Model Improvement Strategy ๊ธฐ์กด CNN ๋ชจ๋ธ์ ๋ค์ ์ ๋ต์ ํตํฉํ์ฌ ์ฑ๋ฅ์ ํฅ์์ํจ ๊ตฌ์กฐ๋ฅผ ๊ตฌํํจ: ๋ฐ์ดํฐ ์ฆ๊ฐ ์ ์ฉ: ๋ค์ํ ์ด๋ฏธ์ง ๋ณํ์ ํตํด ํ์ต ๋ฐ์ดํฐ ๋ค์์ฑ ํ๋ณด ์ ์ดํ์ต ๋์ : MobileNetV2์ ์ฌ์ ํ์ต๋ ํน์ง ์ถ์ถ๊ธฐ ์ฌ์ฉ Dropout ๋ฐ Dense ๋ ์ด์ด ์ถ๊ฐ: ์ค๋ฒํผํ ๋ฐฉ์ง ๋ฐ ๋ชจ๋ธ ํํ๋ ฅ ํฅ์ EarlyStopping, ModelCheckpoint ์ ์ฉ: ๊ณผ์ ํฉ ๋ฐฉ์ง ๋ฐ ์ต์ ๋ชจ๋ธ ์ ์ฅ ๋ฐ์ดํฐ ์ฆ๊ฐ ๊ฐํ: ํ์ , ์ด๋, ํ๋/์ถ์, ๋ฐ์ ๋ฑ ๋ณตํฉ์ ์ฆ๊ฐ ์ ์ฉ ๐ฌ HARIBO_Dataset Preparation 5๊ฐ์ง ํ๋ฆฌ๋ณด ์ ค๋ฆฌ ์ข ๋ฅ(bear, cola, egg, heart, ring)๋ฅผ ์ง์ ์ดฌ์ํ์ฌ ์ด๋ฏธ์ง ๋ฐ์ดํฐ์ ์์ฑ ๋ค์ํ ๊ฐ๋ยท์กฐ๋ช ยท๋ฐฐ๊ฒฝ์์ ์์ง๋ ์ด๋ฏธ์ง ์ด 500์ฅ (๊ฐ ํด๋์ค๋น 100์ฅ ๋ด์ธ) ๊ตฌ๊ธ ๋๋ผ์ด๋ธ์ ์ ๋ก๋ ํ, Google Colab ํ๊ฒฝ์์ ์ค์ต์ฉ์ผ๋ก ์ฐ๋ ๐ก Code : CNN with Transfer Learning & Augmentation import matplotlib.pyplot as plt import numpy as np import os from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.applications import MobileNetV2 from tensorflow.keras import models, layers from tensorflow.keras.optimizers import RMSprop from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint # โ ๊ตฌ๊ธ ๋๋ผ์ด๋ธ ๋ง์ดํธ from google.colab import drive drive.mount('/content/drive') # โ ๊ฒฝ๋ก ์ค์ dataset_path = '/content/drive/MyDrive/haribo_dataset' model_save_path = '/content/drive/MyDrive/haribo_model.h5' # โ ๋ฐ์ดํฐ ์ฆ๊ฐ ์ค์ datagen = ImageDataGenerator( rescale=1./255, # ํฝ์ ๊ฐ์ 0~1 ๋ฒ์๋ก ์ ๊ทํ validation_split=0.2, # ์ ์ฒด ๋ฐ์ดํฐ ์ค 20%๋ฅผ ๊ฒ์ฆ์ฉ์ผ๋ก ์ฌ์ฉ rotation_range=90, # ์ต๋ ยฑ90๋ ๋ฒ์ ๋ด์์ ๋ฌด์์ ํ์ width_shift_range=0.1, # ์ ์ฒด ๋๋น์ 10%๋งํผ ์ข์ฐ ์ด๋ height_shift_range=0.1, # ์ ์ฒด ๋์ด์ 10%๋งํผ ์ํ ์ด๋ shear_range=0.1, # ์ ๋จ ๋ณํ (์ด๋ฏธ์ง๋ฅผ ๊ธฐ์ธ์ด๋ ํจ๊ณผ) zoom_range=0.1, # 10% ๋ฒ์ ๋ด ๋ฌด์์ ํ๋/์ถ์ horizontal_flip=True, # ์ด๋ฏธ์ง๋ฅผ ์ข์ฐ๋ก ๋ฌด์์ ๋ฐ์ fill_mode='nearest' # ๋ณํ ํ ์๊ธด ๋น ์์ญ์ ๊ฐ์ฅ ๊ฐ๊น์ด ํฝ์ ๋ก ์ฑ์ ) # โ ๋ฐ์ดํฐ ๋ก๋ฉ train_generator = datagen.flow_from_directory( dataset_path, target_size=(96, 96), batch_size=32, class_mode='categorical', subset='training', shuffle=True ) val_generator = datagen.flow_from_directory( dataset_path, target_size=(96, 96), batch_size=32, class_mode='categorical', subset='validation', shuffle=True ) # โ ํด๋์ค ์ด๋ฆ ์๋ ์ถ์ถ class_names = list(train_generator.class_indices.keys()) print("ํด๋์ค ์ธ๋ฑ์ค:", train_generator.class_indices) # โ MobileNetV2 ๊ธฐ๋ฐ ๋ชจ๋ธ ๊ตฌ์ฑ base_model = MobileNetV2(input_shape=(96, 96, 3), include_top=False, weights='imagenet') base_model.trainable = False model = models.Sequential([ base_model, layers.GlobalAveragePooling2D(), layers.Dense(128, activation='relu'), layers.Dropout(0.5), layers.Dense(len(class_names), activation='softmax') # ํด๋์ค ์ ์๋ ๋ฐ์ ]) model.compile(optimizer=Adam(learning_rate=1e-4), loss='categorical_crossentropy', metrics=['accuracy']) # โ ์ฝ๋ฐฑ ์ค์ early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True) checkpoint = ModelCheckpoint('best_model.h5', save_best_only=True) # โ ํ์ต ์คํ history = model.fit( train_generator, validation_data=val_generator, epochs=50, callbacks=[early_stop, checkpoint], verbose=2 ) # โ ๊ฒฐ๊ณผ ์๊ฐํ acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(len(acc)) plt.plot(epochs, acc, 'bo', label='Training acc') plt.plot(epochs, val_acc, 'b', label='Validation acc') plt.title('Training and Validation Accuracy') plt.legend() plt.show() plt.plot(epochs, loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'b', label='Validation loss') plt.title('Training and Validation Loss') plt.legend() plt.show() # โ ํ์ต ์ด๋ฏธ์ง ์์ x_batch, y_batch = next(train_generator) plt.figure(figsize=(10, 10)) for i in range(25): plt.subplot(5, 5, i + 1) plt.xticks([]); plt.yticks([]); plt.grid(False) plt.imshow(x_batch[i]) label_idx = np.argmax(y_batch[i]) plt.xlabel(class_names[label_idx]) plt.tight_layout() plt.show() # โ ๋ชจ๋ธ ์ ์ฅ (.h5 ํ์ผ) model.save(model_save_path) print(f"๋ชจ๋ธ์ด ์ ์ฅ๋์์ต๋๋ค: {model_save_path}") โ Result : ํ์ต ๊ฒฐ๊ณผ ์๊ฐํ ๋ฐ ์์ธก ํ์ธ ๐ Summary MobileNetV2๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ ์ ์ดํ์ต ๋ชจ๋ธ์ด ์ ์ ๋ฐ์ดํฐ์ ์์๋ ์ข์ ์ฑ๋ฅ์ ๋ณด์ ์ค์๊ฐ ์์ธก ํ๊ฒฝ์๋ ์ต์ ํ๋ ๋ชจ๋ธ ๊ตฌ์กฐ๋ก ์ ํ ๊ฐ๋ฅ (On-Device AI ์ ์ฉ ๊ฐ๋ฅ) ๐ป Real-Time Inference Setup on Terminal ๐ 1. ๋๋ ํ ๋ฆฌ ๊ตฌ์ฑ mkdir haribo_cam_classifier cd haribo_cam_classifier ๐ 2. ๊ฐ์ํ๊ฒฝ ์์ฑ ๋ฐ ํจํค์ง ์ค์น python3 -m venv venv source venv/bin/activate pip install tensorflow opencv-python-headless numpy ๐ฅ 3. ํ์ตํ ๋ชจ๋ธ(.h5)์ Google Drive์์ ๋ค์ด๋ก๋ํ์ฌ ๋ณต์ฌ haribo_model.h5 ํ์ผ์ Google Drive์์ ๋ค์ด๋ฐ์ haribo_cam_classifier ๋๋ ํ ๋ฆฌ์ ์์น์ํด ๐ผ๏ธ 4. ํด๋์ค ์ด๋ฆ ํ์ผ ์์ฑ (class_names.json) ["bear", "cola", "egg", "heart", "ring"] ๐ก 5. ์ค์๊ฐ ๋ถ๋ฅ ์ฝ๋ ์์ฑ (predict_cam.py) import cv2 import numpy as np import tensorflow as tf import json # ๋ชจ๋ธ๊ณผ ํด๋์ค ์ด๋ฆ ๋ก๋ model = tf.keras.models.load_model('haribo_model.h5') with open('class_names.json', 'r') as f: class_names = json.load(f) def preprocess(frame): img = cv2.resize(frame, (96, 96)) img = img.astype('float32') / 255.0 return np.expand_dims(img, axis=0) cap = cv2.VideoCapture(2) if not cap.isOpened(): print("์นด๋ฉ๋ผ๋ฅผ ์ด ์ ์์ต๋๋ค.") exit() print("์ ค๋ฆฌ ๋ถ๋ฅ ์์! (Q ํค๋ฅผ ๋๋ฅด๋ฉด ์ข ๋ฃ)") while True: ret, frame = cap.read() if not ret: break input_img = preprocess(frame) pred = model.predict(input_img) label = class_names[np.argmax(pred)] # ์์ธก ๊ฒฐ๊ณผ ํ๋ฉด์ ์ถ๋ ฅ cv2.putText(frame, f'Prediction: {label}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.imshow('Haribo Classifier', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() ๐งฉ 6. OpenCV ์ค์น (GUI ์ง์ ํฌํจ) pip install opencv-python โถ๏ธ 7. ์ค์๊ฐ ์์ธก ์คํ python3 predict_cam.py โ 8. ๊ฒฐ๊ณผ ์ ๋ฆฌ ๐ฌ ๋ชจ๋ธ ์์ธก์ ์ํ 5๊ฐ ํด๋์ค ํ๋ฆฌ๋ณด ์ํ ์ ์ฒด ์ด๋ฏธ์ง ๐งช ์์ธก ์์: heart ๐งช ์์ธก ์์: ring ๐งช ์์ธก ์์: cola ๐งช ์์ธก ์์: egg ๐งช ์์ธก ์์: bear
Study
ยท 2025-07-01
Day6 : CNN
๐ CNN๋? CNN(Convolutional Neural Network)์ ์ด๋ฏธ์ง ์ธ์๊ณผ ๋ถ๋ฅ์ ํนํ๋ ์ธ๊ณต์ ๊ฒฝ๋ง์ผ๋ก, ์ฌ๋์ ์๊ฐ ์ฒ๋ฆฌ ๋ฐฉ์๊ณผ ์ ์ฌํ๊ฒ ๊ตญ์์ ์ธ ์์ญ์ ์ค์ฌ์ผ๋ก ํน์ง(feature)์ ์ถ์ถํ๊ณ ํ์ตํ๋ค. ๊ธฐ์กด์ MLP๋ณด๋ค ์ด๋ฏธ์ง ๊ตฌ์กฐ๋ฅผ ๋ ์ ๋ฐ์ํ๋ฉฐ, ์ปดํจํฐ ๋น์ (CV) ๋ถ์ผ์์ ๋๋ฆฌ ์ฌ์ฉ๋๋ค. <ํฉ์ฑ๊ณฑ ์ธต - Convolution Layer> ์ ๋ ฅ ์ด๋ฏธ์ง์ ํํฐ(์ปค๋)๋ฅผ ์ ์ฉํด ํน์ง ๋งต(Feature Map) ์์ฑ ๋ณดํต 3ร3 ํฌ๊ธฐ์ ํํฐ ์ฌ์ฉ (VGGNet ๋ฑ) โ ์์์๋ก ๋ค์ํ feature ์ถ์ถ ๊ฐ๋ฅ ํํฐ์ ๋๊ป๋ ์ ๋ ฅ ๋ฐ์ดํฐ(์: RGB โ 3)์ ์๋ ๋ง์ถฐ์ง Stride: ํํฐ ์ด๋ ๊ฐ๊ฒฉ, ์์์๋ก ์ ๋ฐํ๊ณ ํด์๋ก ๋น ๋ฅด๊ฒ ์ฒ๋ฆฌ๋จ Padding: ์ถ๋ ฅ feature map ํฌ๊ธฐ๋ฅผ ์ ์งํ๋ ค๋ฉด padding=same ์ค์ ํฉ์ฑ๊ณฑ ์ฐ์ฐ ๋ค์๋ ํ์ฑํ ํจ์(ReLU)๋ฅผ ์ ์ฉํด ๋น์ ํ์ฑ ๋์ <ํ๋ง ์ธต - Pooling Layer> MaxPooling: ํ๋ง ์์ญ์ ์ต๋๊ฐ โ ์ฃผ์ ํน์ง๋ง ๊ฐ์กฐ AveragePooling: ์์ญ ๋ด ํ๊ท ๊ฐ ์ฌ์ฉ GlobalAveragePooling: Flatten ์์ด ์ ์ฒด ํ๊ท ๋ง ๋ฝ์๋ด๋ ๋ฐฉ์ (GoogLeNet) ์ฐ์ฐ๋ ๊ฐ์ + ๊ณผ์ ํฉ ๋ฐฉ์ง + ๊ณต๊ฐ ๊ตฌ์กฐ ์์ฝ <๋ฐ์ง์ธต - Fully Connected Layer> Flatten ๋ ์ด์ด๋ก feature map์ 1์ฐจ์ ๋ฒกํฐ๋ก ๋ณํ ์ดํ Fully Connected Layer๋ฅผ ๊ฑฐ์ณ ํด๋์ค๋ณ ์ถ๋ ฅ๊ฐ ์์ฑ ์ฃผ๋ก softmax๋ฅผ ์ถ๋ ฅ์ธต ํ์ฑํ ํจ์๋ก ์ฌ์ฉํด ํ๋ฅ ๊ฐ ๋์ถ ๐งฎ example flow ์: ์ต์ข ์ถ๋ ฅ๊ฐ์ด (0.7, 0)์ด๊ณ ์ ๋ต์ด (1, 0)์ธ ๊ฒฝ์ฐ โ 0.3 ์ค์ฐจ ์ด ์ค์ฐจ๋ฅผ ์ญ์ ํ(backpropagation)๋ก ์ ํํ๋ฉฐ ๊ฐ์ค์น ์ ๋ฐ์ดํธ ๊ฒฝ์ฌํ๊ฐ๋ฒ(gradient descent) ๋ฑ ์ต์ ํ ์๊ณ ๋ฆฌ์ฆ ์ฌ์ฉ ๐๏ธ CNN ๋ชจ๋ธ๊ณผ ์ธ๊ฐ ์๊ฐ ์ฒ๋ฆฌ์ ์ ์ฌ์ฑ ์ธ๊ฐ์ ์๊ฐ ํผ์ง๋ ๋จ์ํ ์๊ฐ ์ ๋ณด โ ๋ณต์กํ ํน์ง ์์ผ๋ก ์ฒ๋ฆฌ CNN๋ ์ธต์ด ๊น์ด์ง์๋ก ๋ณต์กํ feature๋ฅผ ์ถ์ถ ์ ์ฐจ์ edge โ ๊ณ ์ฐจ์ ํจํด ์ถ์ถ ํ๋ฆ์ด ์๊ฐ ์ ๋ณด ์ฒ๋ฆฌ์ ๋ฎ์ ๐ง ๋ํ CNN ๊ตฌ์กฐ๋ค AlexNet (2012): CNN์ ์ ๋ช ํ๊ฒ ๋ง๋ ์ต์ด์ ๊ตฌ์กฐ, 8์ธต ๊ตฌ์ฑ VGGNet (2014): 3ร3 ํํฐ ๋ฐ๋ณต ์ฌ์ฉ, ๊ตฌ์กฐ ๋จ์ & ํจ๊ณผ์ GoogLeNet: Inception ๊ตฌ์กฐ + Global Average Pooling ์ฌ์ฉ ResNet: Residual Block ์ฌ์ฉ โ ์ธต์ด ๊น์ด์ ธ๋ ์ฑ๋ฅ ์ ์ง ๐ ์ ์ดํ์ต (Transfer Learning) ๊ธฐ์กด ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ๊ฐ์ค์น๋ฅผ ์ฌ์ฌ์ฉํ์ฌ ์ ์ ๋ฐ์ดํฐ๋ก๋ ํ์ต ๊ฐ๋ฅ Feature Extraction: ๊ธฐ์กด ๊ตฌ์กฐ ์ ์ง, ์ถ๋ ฅ์ธต๋ง ์๋ก ํ์ต Fine-Tuning: ์ผ๋ถ ์ธต์ ๊ณ ์ , ๋๋จธ์ง๋ ์ฌํ์ต ์ ์ ๋ฐ์ดํฐ ์ํฉ์์ ๊ฐ๋ ฅํ ์ฑ๋ฅ ๋ฐํ ๊ฐ๋ฅ ๐จโ๐ป ์ค์ต ๐ก Code : CNN Layer ๊ตฌํ import numpy as np from PIL import Image import matplotlib.pyplot as plt # ํฉ์ฑ๊ณฑ ํจ์ ๊ตฌํ def conv(a, b): c = np.array(a) * np.array(b) return np.sum(c) # MaxPooling ํจ์ ๊ตฌํ(ํ ๊ฐ์ map ๊ณ์ฐ) def MaxPooling(nimg): # 2d input nimg = np.array(nimg) i0, j0 = nimg.shape # i0 = nimg.shape[0], j0 = nimg.shape[1] i1 = int((i0 + 1) / 2) j1 = int((j0 + 1) / 2) output = np.zeros((i1, j1)) if i0 % 2 == 1: i0 += 1 tmp = np.zeros((1, j0)) nimg = np.concatenate([nimg, tmp], axis=0) if j0 % 2 == 1: j0 += 1 tmp = np.zeros((i0, 1)) nimg = np.concatenate([nimg, tmp], axis=1) for i in range(output.shape[0]): for j in range(output.shape[1]): a = np.array(nimg[2*i:2*i+2, 2*j:2*j+2]) output[i, j] = a.max() return output # ํฉ์ฑ๊ณฑ ์ถ๋ ฅ ์ธต(reature map) ํจ์ ๊ตฌํ(ํ ๊ฐ์ filter ๊ณ์ฐ) def featuring(nimg, filters): feature = np.zeros((nimg.shape[0] - 2, nimg.shape[1] - 2)) for i in range(feature.shape[0]): for j in range(feature.shape[1]): a = nimg[i:i+3, j:j+3] feature[i, j] = conv(a, filters) return feature # MaxPooling ์ถ๋ ฅ ์ธต ํจ์ ๊ตฌํ(์ฌ๋ฌ map ๊ณ์ฐ) def Pooling(nimg): nimg = np.array(nimg) pool0 = [] for i in range(len(nimg)): pool0.append(MaxPooling(nimg[i])) return pool0 # ๋ฐฐ์ด์ ๊ทธ๋ฆผ์ผ๋ก ๋ณํ def to_img(nimg): nimg = np.array(nimg) nimg = np.uint8(np.round(nimg)) fimg = [] for i in range(len(nimg)): fimg.append(Image.fromarray(nimg[i])) return fimg # feature map ์์ฑ(์ฌ๋ฌ filter ๊ณ์ฐ) def ConvD(nimg, filters): nimg = np.array(nimg) feat0 = [] for i in range(len(filters)): feat0.append(featuring(nimg, filters[i])) return feat0 # ReLU ํ์ฑํ ํจ์ def ReLU(fo): fo = np.array(fo) fo = (fo > 0) * fo return fo # CNN Layer ํจ์ : Conv + ReLU + MaxPooling def ConvMax(nimg, filters): nimg = np.array(nimg) f0 = ConvD(nimg, filters) f0 = ReLU(f0) fg = Pooling(f0) return f0, fg # ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ : ํฉ์ฑ๊ณฑ ํ์ ์ํ์ MaxPooling ํ์ ์ํ๋ฅผ ๊ทธ๋ฆผ์ผ๋ก ๊ทธ๋ฆฌ๊ธฐ def draw(f0, fg0, size=(12, 8), k=-1): # size์ k๋ ๊ธฐ๋ณธ๊ฐ ์ค์ plt.figure(figsize=size) for i in range(len(f0)): plt.subplot(2, len(f0), i + 1) plt.gca().set_title('Conv' + str(k) + '-' + str(i)) plt.imshow(f0[i]) for i in range(len(fg0)): plt.subplot(2, len(fg0), len(f0) + i + 1) plt.gca().set_title('MaxP' + str(k) + '-' + str(i)) plt.imshow(fg0[i]) if k != -1: # k=-1์ด ์๋๋ฉด ๊ทธ๋ฆผ์ ์ ์ฅ plt.savefig('conv' + str(k) + '.png') # 3๊ฐ์ activation map ํฉ์น๊ธฐ : MaxPooling ํ์ ๊ฒฐ๊ณผ map๋ค์ ํ๋์ ๋ฐ์ดํฐ๋ก ํตํฉ def join(mm): mm = np.array(mm) m1 = np.zeros((mm.shape[1], mm.shape[2], mm.shape[0])) for i in range(mm.shape[1]): for j in range(mm.shape[2]): for k in range(mm.shape[0]): m1[i][j][k] = mm[k][i][j] return m1 # CNN Layer ๊ณผ์ ์ ๊ณ์ฐํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋ฆผ์ผ๋ก ์ถ๋ ฅ def ConvDraw(p0, filters, size=(12, 8), k=-1): f0, fg0 = ConvMax(p0, filters) f0_img = to_img(f0) fg1_img = to_img(fg0) draw(f0, fg0, size, k) p1 = join(fg0) return p1 # ํ ์คํธ ์คํ nimg31 = np.random.rand(10, 10) filters = [np.ones((3, 3))] * 3 m0 = ConvDraw(nimg31, filters, (12, 10), 0) โ Result : CNN Layer ๊ตฌํ ๐ ๋ผ์ฆ๋ฒ ๋ฆฌํ์ด ํ๊ฒฝ ๊ตฌ์ถ โ ์ค์น ๋ฐ ์ด๋ฏธ์ง ์ค์ sudo apt install rpi-imager : ๋ผ์ฆ๋ฒ ๋ฆฌํ์ด ์ด๋ฏธ์ง ๋๊ตฌ ์ค์น rpi-imager : GUI ์คํ ํ OS ์ด๋ฏธ์ง ๋ค์ด๋ก๋ ๋ฐ ์ค์น ๊ฐ๋ฅ โ๏ธ ์ค์ ์ ๋ณด ์ด์์ฒด์ : Raspberry Pi OS (64-bit) ์ ์ฅ์: Mass Storage Device - 62.5 GB
Study
ยท 2025-06-30
Day5 : ์ ํ๋ชจ๋ธ, ์ ๊ฒฝ๋ง๋ชจ๋ธ
๐ ์ ํ ๋ชจ๋ธ ์์ฝ ๐ง 1. ์ ํ ๋ชจ๋ธ ๊ฐ์ ์ ๋ ฅ ๋ฐ์ดํฐ๋ฅผ ๋ฒกํฐ ํํ๋ก ์ฒ๋ฆฌํ๋ ๊ฐ์ฅ ๋จ์ํ ํํ์ ๋จธ์ ๋ฌ๋ ๋ชจ๋ธ ์ ํ ๋ณํ + ๊ฐ๋จํ ๊ฒฐ์ ํจ์๋ก ๋ถ๋ฅ ์ํ ๐งฑ 2. ๋ฒกํฐํ (Vectorization) ์ ํ ๋ชจ๋ธ์ 1์ฐจ์ ๋ฒกํฐ ํํ์ ์ ๋ ฅ๋ง ์ฒ๋ฆฌ ๊ฐ๋ฅ ๋ฐ๋ผ์, 2D/3D ์ด๋ฏธ์ง๋ฅผ 1D ๋ฒกํฐ๋ก ๋ณํํด์ผ ํจ ์: 4x4 ํฝ์ ์ด๋ฏธ์ง๋ฅผ (1,16) ๋ฒกํฐ๋ก ๋ณํ ๐งฎ 3. ์ ํ ๋ถ๋ฅ๊ธฐ - Score ํจ์ ์ ๋ ฅ ๋ฒกํฐ์ ๊ฐ์ค์น ํ๋ ฌ์ ๊ณฑ์ผ๋ก ๊ฐ ํด๋์ค์ ์ ์(score) ๊ณ์ฐ Score ๊ณ์ฐ์ ํ๋ ฌ ๊ณฑ์ ํตํด ๋ณ๋ ฌ ์ฒ๋ฆฌ ๊ฐ๋ฅ ๐ 4. Softmax ๋ถ๋ฅ๊ธฐ ๊ฐ ํด๋์ค์ score๋ฅผ ํ๋ฅ ๊ฐ์ผ๋ก ๋ณํ softmax ์ถ๋ ฅ์ ๊ฐ ํด๋์ค์ ๋ํ ํ๋ฅ ๋ถํฌ๋ฅผ ๋ํ๋ ๐ 5. ์์ค ํจ์ - Cross Entropy Loss ์์ธก ํ๋ฅ ๊ณผ ์ ๋ต ํด๋์ค ๊ฐ์ ๊ฑฐ๋ฆฌ ๊ณ์ฐ ์ ๋ต ํด๋์ค์ ํด๋นํ๋ softmax ๊ฐ์ -log๋ฅผ ์ทจํด ์์ค ๊ณ์ฐ โ๏ธ 6. ์ต์ ํ - SGD (Stochastic Gradient Descent) ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ํ ๋ฒ์ ํ์ตํ์ง ์๊ณ , ๋ฏธ๋ ๋ฐฐ์น ๋จ์๋ก ๊ฒฝ์ฌ ํ๊ฐ๋ฒ ์ ์ฉ ๊ณ์ฐ ํจ์จ์ฑ๊ณผ ๋น ๋ฅธ ์๋ ด์ ์ํด ์ฌ์ฉ๋จ ๐งช 7. ์ค์ต ๊ฐ์ (MNIST) MNIST ์ซ์ ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ์ ํ ๋ชจ๋ธ์ ์ ์ฉํ์ฌ ํ์ต ์ ํ๋ ๋ฐ ์์ค ๊ณก์ ์ ์๊ฐํํ์ฌ ํ์ต ๊ฒฐ๊ณผ ๋ถ์ โ ์์ฝ ์ ํ ๋ชจ๋ธ์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ถ๋ฅ๊ธฐ์ด๋ฉฐ, ๊ธฐ์ด ๊ฐ๋ (๋ฒกํฐํ, softmax, cross entropy, SGD ๋ฑ)์ ํ์ตํ๋ ๋ฐ ์ค์ํจ ์ดํ ์ ๊ฒฝ๋ง ๋ชจ๋ธ์ ์ดํดํ๊ธฐ ์ํ ๊ธฐ๋ฐ ์ง์์ ์ ๊ณตํจ ๐จโ๐ป ์ค์ต ๐ก Code : ๋ฒกํฐํ ์ฝ๋ ์ด๋ฏธ์ง๋ฅผ ๋ฒกํฐํํ ๋, numpy๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ flatten ๋๋ reshape์ ์ฌ์ฉํด ๋ฒกํฐํ ํ ์ ์๋ค. import numpy as np # random ํจ์๋ก 0~255 ์ฌ์ด์ ์์์ ์ ์๋ฅผ ์ฑ๋ถ์ผ๋ก ๊ฐ๋ 4x4 ํ๋ ฌ์ ๋ง๋ ๋ค. a = np.random.randint(0, 255, (4, 4)) a array([[ 38, 223, 157, 213], [104, 79, 231, 31], [117, 10, 48, 72], [128, 41, 6, 178]]) # flatten์ ์ฌ์ฉํด 1์ฐจ์ ํ๋ ฌ(๋ฒกํฐ)๋ก ๋ง๋ ๋ค. b = a.flatten() b array([ 38, 223, 157, 213, 104, 79, 231, 31, 117, 10, 48, 72, 128, 41, 6, 178]) # reshape๋ฅผ ์ฌ์ฉํด ํ๋ ฌ ํฌ๊ธฐ๋ฅผ ๋ฐ๊พผ๋ค. -1์ ์๋์ผ๋ก ๊ณ์ฐํ๋ค๋ ์๋ฏธ์ด๊ณ ์ด ๊ฒฝ์ฐ 16์ ์ ๋ ๊ฒ๊ณผ ๊ฐ๋ค. # ๋ง์ฝ (2, 8)์ ํ๋ ฌ๋ก ๋ฐ๊พธ๋ ค ํ๋ค๋ฉด reshape(2, -1) ๋๋ reshape(2, 8) ๋ ๋ค ๊ฐ์ ๊ฒฐ๊ณผ์ด๋ค. c = a.reshape(-1) c array([ 38, 223, 157, 213, 104, 79, 231, 31, 117, 10, 48, 72, 128, 41, 6, 178]) ๐ก Code : Mnist ์ค์ต # 1. ๊ธฐ๋ณธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ถ๋ฌ์ค๊ธฐ import numpy as np import pandas as pd # 2 ๋ฐ์ดํฐ์ ๋ถ๋ฌ์ค๊ธฐ from tensorflow.keras.datasets.mnist import load_data (train_x, train_y), (test_x, test_y) = load_data() # 2-1 ๋ฐ์ดํฐ ํ์ธํ๊ธฐ train_x.shape, train_y.shape # train ๋ฐ์ดํฐ ํฌ๊ธฐ ํ์ธ test_x.shape, test_y.shape # test ๋ฐ์ดํฐ ํฌ๊ธฐ ํ์ธ ((10000, 28, 28), (10000,)) # 2-2 ์ด๋ฏธ์ง ํ์ธํ๊ธฐ from PIL import Image img = train_x[0] import matplotlib.pyplot as plt img1 = Image.fromarray(img, mode='L') plt.imshow(img1) train_y[0] # ์ฒซ๋ฒ์งธ ๋ฐ์ดํฐ ํ์ธ np.uint8(5) # 3 ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ # 3-1 ์ ๋ ฅ ํํ ๋ณํ: 3์ฐจ์ โ 2์ฐจ์ # ๋ฐ์ดํฐ๋ฅผ 2์ฐจ์ ํํ๋ก ๋ณํ: ์ ๋ ฅ ๋ฐ์ดํฐ๊ฐ ์ ํ๋ชจ๋ธ์์๋ ๋ฒกํฐ ํํ train_x1 = train_x.reshape(60000, -1) test_x1 = test_x.reshape(10000, -1) # 3-2 ๋ฐ์ดํฐ ๊ฐ์ ํฌ๊ธฐ ์กฐ์ : 0~1 ์ฌ์ด ๊ฐ์ผ๋ก ๋ณํ train_x2 = train_x1 / 255 test_x2 = test_x1 / 255 # 4 ๋ชจ๋ธ ์ค์ # 4-1 ๋ชจ๋ธ ์ค์ ์ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ถ๋ฌ์ค๊ธฐ from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense # 4-2 ๋ชจ๋ธ ์ค์ md = Sequential() md.add(Dense(10, activation='softmax', input_shape=(28*28,))) md.summary() # ๋ชจ๋ธ ์์ฝ /usr/local/lib/python3.11/dist-packages/keras/src/layers/core/dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead. super().__init__(activity_regularizer=activity_regularizer, **kwargs) Model: "sequential_6" โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโณโโโโโโโโโโโโโโโโโโโโโโโโโณโโโโโโโโโโโโโโโโ โ Layer (type) โ Output Shape โ Param # โ โกโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฉ โ dense_6 (Dense) โ (None, 10) โ 7,850 โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโ Total params: 7,850 (30.66 KB) Trainable params: 7,850 (30.66 KB) Non-trainable params: 0 (0.00 B) # 5 ๋ชจ๋ธ ํ์ต ์งํ # 5-1 ๋ชจ๋ธ compile: ์์ค ํจ์, ์ต์ ํ ํจ์, ์ธก์ ํจ์ ์ค์ md.compile(loss = 'sparse_categorical_crossentropy', optimizer = 'sgd', metrics = ['acc']) # 5-2 ๋ชจ๋ธ ํ์ต: ํ์ต ํ์, batch_size, ๊ฒ์ฆ์ฉ ๋ฐ์ดํฐ ์ค์ hist = md.fit(train_x2, train_y, epochs=30, batch_size=64, validation_split=0.2) acc = hist.history['acc'] val_acc = hist.history['val_acc'] epoch = np.arange(1, len(acc) + 1) # ํ์ต๊ฒฐ๊ณผ ๋ถ์ : ํ์ต ๊ณก์ ๊ทธ๋ฆฌ๊ธฐ plt.figure(figsize=(10,8)) plt.plot(epoch, acc, 'b', label='Training accuracy') plt.plot(epoch, val_acc, 'r', label='Validation accuracy') plt.title('Training and validation accuracy') plt.xlabel('Epochs') plt.ylabel('Accuracy') plt.legend() plt.show() # 6 ํ ์คํธ์ฉ ๋ฐ์ดํฐ ํ๊ฐ md.evaluate(test_x2, test_y) # 7 ๊ฐ์ค์น ์ ์ฅ weight = md.get_weights() weight [array([[-0.01809908, 0.04423299, -0.00407743, ..., 0.06332525, -0.00251734, 0.00196751], [-0.050407 , -0.05998353, -0.07465094, ..., -0.01433843, 0.01071206, 0.03646336], [ 0.06986522, -0.0116923 , -0.07076468, ..., 0.02445704, -0.05563192, 0.0041509 ], ..., [ 0.03118712, -0.04921252, 0.00195412, ..., -0.00605295, -0.00202944, 0.07754893], [ 0.08052733, -0.0327304 , 0.02389491, ..., 0.00695625, 0.06758214, 0.03055982], [-0.05485652, 0.03522244, 0.03506895, ..., 0.00976416, -0.06175685, 0.04081956]], dtype=float32), array([-0.23029914, 0.30129498, 0.01726046, -0.17140533, 0.06494795, 0.772551 , -0.05629169, 0.3972938 , -0.94089097, -0.15446219], dtype=float32)] # Model Loss ์๊ฐํ plt.plot(hist.history['loss'], label='loss') plt.plot(hist.history['val_loss'], label='val_loss') plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.show() CIFAR10 ์ค์ต 2025-06-27_2 ๋ณด๊ณ ์ ์ฐธ๊ณ
Study
ยท 2025-06-27
Day4 : Deep Learning
๐ง ๋ฅ๋ฌ๋ ๊ฐ์ ๋ฅ๋ฌ๋์ ์ธ๊ฐ์ ๋๋ฅผ ๋ชจ๋ฐฉํ ์ธ๊ณต์ ๊ฒฝ๋ง(ANN)์ ๊ธฐ๋ฐ์ผ๋ก ํ ๊ธฐ๊ณํ์ต ๊ธฐ์ ๋ก, ๋ค์ธต ๊ตฌ์กฐ์ ์ ๊ฒฝ๋ง์ ํตํด ๋ณต์กํ ํจํด์ ํ์ตํ๊ณ ์์ธกํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ๐ ์ธ๊ณต์ ๊ฒฝ๋ง(ANN)์ ๊ฐ๋ ์๋ฌผํ์ ๋ด๋ฐ ๊ตฌ์กฐ์์ ์ฐฉ์. ์ ๋ ฅ โ ๊ฐ์ค์น โ ํ์ฑํ ํจ์ โ ์ถ๋ ฅ ํ๋ฆ์ผ๋ก ๋์. ๊ฐ ์ ํธ์ ๊ฐ๋๋ ๊ฐ์ค์น(Weight)๋ก ํํ๋จ. ๐งฌ ๋ฅ๋ฌ๋(Deep Learning)์ด๋? ์๋์ธต์ด ์ฌ๋ฌ ๊ฐ์ธ ์ฌ์ธต ์ ๊ฒฝ๋ง(Deep Neural Network, DNN)์ ํตํด ํ์ตํ๋ ๋ฐฉ์. ์ฌ์ธต ํ์ต(Deep Learning)์ด๋ผ๊ณ ๋ ํจ. ๐ ๏ธ ์ ๊ฒฝ๋ง ๊ตฌ์ฑ ์์ ๊ตฌ์ฑ ์์ ์ค๋ช ์ ๋ ฅ์ธต ํ์ต ๋ฐ์ดํฐ๊ฐ ๋ค์ด์ค๋ ์ธต ์๋์ธต ๊ฐ์คํฉ ๊ณ์ฐ ๋ฐ ๋น์ ํ ๋ณํ ์ํ ์ถ๋ ฅ์ธต ์ต์ข ์์ธก๊ฐ์ ์ถ๋ ฅ ๊ฐ์ค์น ์ ๋ ฅ์ ์ค์๋๋ฅผ ๊ฒฐ์ ํธํฅ ๊ฐ์คํฉ์ ๋ํด์ง๋ ์์๋ก ์ถ๋ ฅ ์กฐ์ โ ๊ฐ์คํฉ (Weighted Sum) ๊ฐ ์ ๋ ฅ๊ฐ ร ๊ฐ์ค์น + ํธํฅ ์์: z = wโxโ + wโxโ + โฆ + b โ๏ธ ํ์ฑํ ํจ์ (Activation Function) ํจ์๋ช ํน์ง Sigmoid S์ ํํ, ์ถ๋ ฅ๊ฐ [0, 1], ๊ธฐ์ธ๊ธฐ ์์ค ๋ฌธ์ Tanh ์ถ๋ ฅ [-1, 1], ํ๊ท 0, sigmoid๋ณด๋ค ์ฐ์ ReLU 0 ์ดํ โ 0, 0 ์ด๊ณผ โ ๊ทธ๋๋ก ์ถ๋ ฅ, ๋น ๋ฅธ ํ์ต LeakyReLU ReLU์ ์์ ์ ๋ ฅ ๋ฌด๋ฐ์ ๋ฌธ์ ํด๊ฒฐ Softmax ํ๋ฅ ๋ถํฌ ์ถ๋ ฅ, ๋ค์ค ํด๋์ค ๋ถ๋ฅ์ ์ฌ์ฉ ๐งญ ํ์ต ๊ณผ์ (Training Flow) 1๏ธโฃ ์์ ํ (Forward Propagation) ์ ๋ ฅ โ ์๋์ธต โ ์ถ๋ ฅ์ธต์ผ๋ก ์์ธก๊ฐ ๋์ถ 2๏ธโฃ ์์ค ํจ์ (Loss Function) ์์ธก๊ฐ๊ณผ ์ค์ ๊ฐ์ ์ฐจ์ด๋ฅผ ๊ณ์ฐ ํ๊ท: MSE ๋ถ๋ฅ: Cross Entropy 3๏ธโฃ ์ตํฐ๋ง์ด์ (Optimizer) ๊ฒฝ์ฌํ๊ฐ๋ฒ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ค์น ์ต์ ํ ์ ์ฒด/๋ฏธ๋ ๋ฐฐ์น ๋ฐฉ์ ์ฌ์ฉ 4๏ธโฃ ์ญ์ ํ (Backpropagation) ์ค์ฐจ๋ฅผ ์ญ๋ฐฉํฅ ์ ํํด ๊ฐ์ค์น ์ ๋ฐ์ดํธ ๊ฐ ์ธต์ ๊ฐ์ค์น์ ๋ํด ๋ฏธ๋ถ๊ฐ ๊ธฐ๋ฐ ๋ณด์ ๐งฑ ๋ฅ๋ฌ๋ ๋ชจ๋ธ์ ์ ํ ์ ํ ์ค๋ช DFN (์๋ฐฉํฅ ์ ๊ฒฝ๋ง) ๊ธฐ๋ณธ ๊ตฌ์กฐ, ๊ณ ์ ์ ๋ ฅ ์ฒ๋ฆฌ RNN (์ํ ์ ๊ฒฝ๋ง) ์๊ณ์ด ๋ฐ์ดํฐ ์ฒ๋ฆฌ, ๊ณผ๊ฑฐ ์ ๋ณด ๋ฐ์ LSTM RNN ๊ฐ์ , ์ฅ๊ธฐ ๊ธฐ์ต ์ ์ง CNN ์ด๋ฏธ์ง ๋ถ์ ํนํ, ํฉ์ฑ๊ณฑ ๋ฐ ํ๋ง ํ์ฉ ๐ง CNN์ ๊ตฌ์กฐ ํฉ์ฑ๊ณฑ์ธต: ํํฐ๋ฅผ ํตํด ํน์ง ์ถ์ถ ํ๋ง์ธต: ๋ฐ์ดํฐ ํฌ๊ธฐ ์ถ์, ํต์ฌ ์ ๋ณด ๋ณด์กด ์์ ์ฐ๊ฒฐ์ธต: ์ต์ข ๋ถ๋ฅ ์ํ ๐ ๋น๊ต ์์ฝ (DFN vs RNN vs CNN) ํญ๋ชฉ DFN RNN CNN ์ ๋ ฅ ์ ์ ์๊ณ์ด ์ด๋ฏธ์ง/์๊ณ์ด ํน์ง ๋จ๋ฐฉํฅ ์ํ ์ฐ๊ฒฐ ์ง์ญ์ ํน์ง ํ์ต ์ฌ์ ์ด๋ ค์ ์ค๊ฐ ํจ์จ ๋ฎ์ ๋ฎ์ ๋์ ๐ฌ ์๋ ์๋ฒ ๋ฉ (Word Embedding) ๋ฐฉ์ ์ค๋ช One-hot Encoding ํฌ์ ๋ฒกํฐ, ๋จ์ ๊ตฌ์กฐ Word2Vec ์ฃผ๋ณ ๋ฌธ๋งฅ โ ์ค์ฌ ๋จ์ด ์์ธก (CBOW/Skip-gram) TF-IDF ๋จ์ด ์ค์๋ ๊ฐ์ค์น ๋ถ์ฌ FastText ๋ถ๋ถ ๋จ์ด ๊ธฐ๋ฐ, OOV ๋ฌธ์ ํด๊ฒฐ GloVe ๋จ์ด ๋์ ๋ฑ์ฅ ํต๊ณ ๊ธฐ๋ฐ ELMo ๋ฌธ๋งฅ์ ๋ฐ๋ผ ๋ฒกํฐ๊ฐ ๋ฌ๋ผ์ง๋ ๋์ ์๋ฒ ๋ฉ ๐จ ์ ๋์ ์์ฑ ์ ๊ฒฝ๋ง (GAN) ๋ ๋คํธ์ํฌ๊ฐ ๊ฒฝ์: Generator: ์ง์ง ๊ฐ์ ๊ฐ์ง ๋ฐ์ดํฐ ์์ฑ Discriminator: ์ง์ง์ ๊ฐ์ง ๊ตฌ๋ณ ์์ , ์ด๋ฏธ์ง ์์ฑ ๋ฑ์์ ๊ฐ๋ ฅํ ์ฑ๋ฅ โ ์์ฝ ๋ฅ๋ฌ๋์ ์ธ๊ณต์ ๊ฒฝ๋ง์ ํ์ฅํ ๊ตฌ์กฐ๋ก, ๋ค์ํ ๋ฌธ์ ํด๊ฒฐ์ ์ ์ฉ ๊ฐ๋ฅ ํ์ฑํ ํจ์, ํ์ต ์๊ณ ๋ฆฌ์ฆ, ๋ชจ๋ธ ๊ตฌ์กฐ์ ๋ฐ๋ผ ์ฑ๋ฅ์ด ์ข์ฐ๋จ CNN, RNN, GAN, Word Embedding ๋ฑ์ ์ค์ ๋ฌธ์ ์ ๋ง๋ ๋ฅ๋ฌ๋ ๊ธฐ๋ฒ ์ ํ์ ๊ธฐ์ค์ด ๋๋ค. ๐ ๏ธ ์์ ํ ๋๋ ํ ๋ฆฌ ์์ฑ ๋ฐ ํ๊ฒฝ ์ค์ # 1. ์์ ๋๋ ํ ๋ฆฌ ์์ฑ mkdir F_MNIST # ๋๋ ํ ๋ฆฌ ์ด๋ฆ: F_MNIST cd F_MNIST # ํด๋น ๋๋ ํ ๋ฆฌ๋ก ์ด๋ # 2. ๊ฐ์ ํ๊ฒฝ ์์ฑ ๋ฐ ํ์ฑํ python3 -m venv .fmnist # ๊ฐ์ ํ๊ฒฝ ์์ฑ (ํด๋ ์ด๋ฆ: .fmnist) source .fmnist/bin/activate # ๊ฐ์ ํ๊ฒฝ ํ์ฑํ # 3. ํจํค์ง ์ค์น pip install -U pip # pip ์ต์ ๋ฒ์ ์ผ๋ก ์ ๊ทธ๋ ์ด๋ pip install tensorflow # TensorFlow (๋ฅ๋ฌ๋ ํ๋ ์์ํฌ) pip install matplotlib # Matplotlib (์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ) pip install PyQt5 # PyQt5 (Matplotlib GUI ๋ฐฑ์๋์ฉ) pip install scikit_learn # scikit-learn (๋จธ์ ๋ฌ๋ ๋ฐ ํ๊ฐ ๋๊ตฌ) # 4. Qt GUI ๋ฐฑ์๋ ์ค์ (Wayland ํ๊ฒฝ์์ ํ์) export QT_QPA_PLATFORM=wayland # Qt GUI๋ฅผ Wayland์์ ์ ์ ๋์ํ๊ฒ ์ค์ ๐จโ๐ป ์ค์ต ๐ก Code : Fashion MNIST import tensorflow as tf from tensorflow import keras import numpy as np import matplotlib import matplotlib.pyplot as plt # dataset load fashion_mnist = keras.datasets.fashion_mnist # spilt data (train / test) (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() print(train_images.shape) print(train_labels.shape) print(test_images.shape) print(test_labels.shape) class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] matplotlib.use('Qt5Agg') NUM=20 plt.figure(figsize=(15,15)) plt.subplots_adjust(hspace=1) for idx in range(NUM): sp = plt.subplot(5,5,idx+1) plt.imshow(train_images[idx]) plt.title(f'{class_names[train_labels[idx]]}') plt.show() plt.figure() plt.imshow(train_images[0]) plt.colorbar() plt.grid(False) plt.show() # ๊ฐ๋จํ ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ (for ANN) train_images = train_images / 255.0 test_images = test_images / 255.0 class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] plt.figure(figsize=(10,8)) for i in range(20): plt.subplot(4,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(train_images[i], cmap=plt.cm.binary) plt.xlabel(class_names[train_labels[i]]) plt.show() model = keras.Sequential ([ keras.layers.Flatten(input_shape=(28,28)), keras.layers.Dense(128, activation='relu'), keras.layers.Dense(10, activation='softmax'), ]) model.summary() model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(train_images, train_labels, epochs=20) predictions = model.predict(test_images) predictions[0] np.argmax(predictions[0]) test_labels[0] def plot_image(i, predictions_array, true_label, img): predictions_array, true_label, img = predictions_array[i], true_label[i], img[i] plt.grid(False) plt.xticks([]) plt.yticks([]) plt.imshow(img, cmap=plt.cm.binary) predicted_label = np.argmax(predictions_array) if predicted_label == true_label: color = 'blue' else: color = 'red' plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label], 100*np.max(predictions_array), class_names[true_label]), color=color) def plot_value_array(i, predictions_array, true_label): predictions_array, true_label = predictions_array[i], true_label[i] plt.grid(False) plt.xticks([]) plt.yticks([]) thisplot = plt.bar(range(10), predictions_array, color="#777777") plt.ylim([0, 1]) predicted_label = np.argmax(predictions_array) thisplot[predicted_label].set_color('red') thisplot[true_label].set_color('blue') num_rows = 5 num_cols = 3 num_images = num_rows*num_cols plt.figure(figsize=(2*2*num_cols, 2*num_rows)) for i in range(num_images): plt.subplot(num_rows, 2*num_cols, 2*i+1) plot_image(i, predictions, test_labels, test_images) plt.subplot(num_rows, 2*num_cols, 2*i+2) plot_value_array(i, predictions, test_labels) plt.show() from sklearn.metrics import accuracy_score print('accuracy score : ', accuracy_score(tf.math.argmax(predictions, -1), test_labels))
Study
ยท 2025-06-26
Day3 : Perceptron
Report : Perceptron 1. ํผ์ ํธ๋ก ์ด๋? ์๋ฌผํ์ ๋ด๋ฐ์ ๋ชจ๋ฐฉํ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์ธ๊ณต์ ๊ฒฝ๋ง ์ ๋ ฅ ๊ฐ ร ๊ฐ์ค์น + ๋ฐ์ด์ด์ค๋ฅผ ํตํด ์ ํ๊ฒฐํฉ์ ๋ง๋ค๊ณ , ๋จ์ ๊ณ๋จ ํจ์(step function) ๋ก 0 ๋๋ 1์ ์ถ๋ ฅ ๊ฐ๋จํ ๊ตฌ์กฐ์ง๋ง, ์ ํ ๋ถ๋ฆฌ ๊ฐ๋ฅํ ๋ฌธ์ ๋ ์๋ฒฝํ๊ฒ ํด๊ฒฐํ ์ ์์ ํ๋์ ์ง์ ์ผ๋ก ๋๋ ์ ์์ผ๋ฉด โ ์ ํ ๋ถ๋ฆฌ ๊ฐ๋ฅ ๋จ์ ๊ณ์ฐ ํจ์ : ์ ๋ ฅ๊ฐ์ด 0๋ณด๋ค ํฌ๋ฉด 1์ ์ถ๋ ฅํ๊ณ , ๊ทธ ์ธ์ 0์ ์ถ๋ ฅํ๋ ํจ์ 2. ํผ์ ํธ๋ก ํ์ต ๋ฐฉ์ (์ฝ๋ ์ค์ฌ) # AND & OR & NAND & XOR Gate Perceptron import numpy as np import matplotlib.pyplot as plt class Perceptron: def __init__(self, input_size, lr=0.1, epochs=10): self.weights = np.zeros(input_size) self.bias = 0 self.lr = lr self.epochs = epochs self.errors = [] def activation(self, x): return np.where(x > 0, 1, 0) def predict(self, x): linear_output = np.dot(x, self.weights) + self.bias return self.activation(linear_output) def train(self, X, y): for epoch in range(self.epochs): total_error = 0 for xi, target in zip(X, y): prediction = self.predict(xi) update = self.lr * (target - prediction) self.weights += update * xi self.bias += update total_error += int(update != 0.0) self.errors.append(total_error) print(f"Epoch {epoch+1}/{self.epochs}, Errors: {total_error}") # AND ๊ฒ์ดํธ ๋ฐ์ดํฐ ๋ฐ ํ์ต X_and = np.array([[0,0],[0,1],[1,0],[1,1]]) y_and = np.array([0,0,0,1]) print(" AND Gate Training") ppn_and = Perceptron(input_size=2) ppn_and.train(X_and, y_and) print("\n AND Gate Test:") for x in X_and: print(f"Input: {x}, Predicted Output: {ppn_and.predict(x)}") # OR ๊ฒ์ดํธ ๋ฐ์ดํฐ ๋ฐ ํ์ต X_or = np.array([[0,0],[0,1],[1,0],[1,1]]) y_or = np.array([0,1,1,1]) print("\n OR Gate Training") ppn_or = Perceptron(input_size=2) ppn_or.train(X_or, y_or) print("\n OR Gate Test:") for x in X_or: print(f"Input: {x}, Predicted Output: {ppn_or.predict(x)}") # NAND ๊ฒ์ดํธ ๋ฐ์ดํฐ ๋ฐ ํ์ต X_nand = np.array([[0,0],[0,1],[1,0],[1,1]]) y_nand = np.array([1,1,1,0]) # AND์ ๋ฐ๋ print("\n NAND Gate Training") ppn_nand = Perceptron(input_size=2) ppn_nand.train(X_nand, y_nand) print("\n NAND Gate Test:") for x in X_nand: print(f"Input: {x}, Predicted Output: {ppn_nand.predict(x)}") # XOR ๊ฒ์ดํธ ๋ฐ์ดํฐ ๋ฐ ํ์ต X_xor = np.array([[0,0],[0,1],[1,0],[1,1]]) y_xor = np.array([0,1,1,0]) # ์ ํ ๋ถ๋ฆฌ ๋ถ๊ฐ๋ฅ print("\n XOR Gate Training") ppn_xor = Perceptron(input_size=2) ppn_xor.train(X_xor, y_xor) print("\n XOR Gate Test:") for x in X_xor: print(f"Input: {x}, Predicted Output: {ppn_xor.predict(x)}") 2-1. train() ํจ์ ํธ์ถ ppn_and.train(X_and, y_and) X_and: ์ ๋ ฅ ๋ฐ์ดํฐ ๋ฐฐ์ด (์: [0,0], [1,1] ๋ฑ) y_and: ๊ฐ ์ ๋ ฅ์ ๋ํ ์ ๋ต ์ถ๋ ฅ๊ฐ 2-2. ์ ์ฒด ๋ฐ๋ณต (epoch) ์์ for epoch in range(self.epochs): # ์ด 10๋ฒ ๋ฐ๋ณต ํ epoch๋ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ํ ๋ฒ ํ์ตํ๋ ์ฃผ๊ธฐ ์ด 10๋ฒ ๋ฐ๋ณตํ๋ฉด์ ์กฐ๊ธ์ฉ ๊ฐ์ค์น๋ฅผ ์กฐ์ 2-3. ํ epoch ๋ด ์ํ ๋ฐ๋ณต for xi, target in zip(X, y): ๊ฐ ๋ฐ์ดํฐ xi์ ์ ๋ต target์ ํ๋์ฉ ๊บผ๋ด ์์ฐจ ํ์ต 2-4. ์์ธก ๊ณผ์ prediction = self.predict(xi) predict() ๋ด๋ถ์์ ๋ค์ ์์๋ก ์๋: linear_output = wยทx + b activation() โ 0 ๋๋ 1 ๋ฐํ 2-5. ์ค์ฐจ ๊ณ์ฐ ๋ฐ ๊ฐ์ค์น/๋ฐ์ด์ด์ค ์ ๋ฐ์ดํธ update = self.lr * (target - prediction) ์์ธก์ด ์ ๋ต๋ณด๋ค ์์ผ๋ฉด โ update > 0: ๊ฐ์ค์น ์ฆ๊ฐ ์์ธก์ด ์ ๋ต๋ณด๋ค ํฌ๋ฉด โ update < 0: ๊ฐ์ค์น ๊ฐ์ ์์ธก์ด ์ ํํ๋ฉด โ update == 0: ๊ฐ์ค์น ๋ณํ ์์ self.weights += update * xi self.bias += update ๊ฐ ์ ๋ ฅ ๊ฐ์ ๋ฐ๋ผ ๊ฐ์ค์น ์กฐ์ ํญ์ ๋ฐ์ด์ด์ค๋ ๊ฐ์ด ์ ๋ฐ์ดํธ 2-6. ์๋ฌ ์นด์ดํธ total_error += int(update != 0.0) ์์ธก์ด ํ๋ ธ์ ๋๋ง ์๋ฌ๋ก ์ง๊ณ 2-7. ํ์ต ๊ฒฐ๊ณผ ์ถ๋ ฅ self.errors.append(total_error) print(f"Epoch {epoch+1}/{self.epochs}, Errors: {total_error}") ํ์ต์ด ์งํ๋ ์๋ก Errors๊ฐ ์ค์ด๋๋์ง ํ์ธ ๊ฐ๋ฅ ํ์ง๋ง XOR์ ์ค์ง ์์ โ ์ ํ ๋ถ๋ฆฌ ๋ถ๊ฐ๋ฅ ๋ฌธ์ 2-8. ์ต์ข ์์ธก ๊ฒฐ๊ณผ ํ์ธ ๊ฐ ๊ฒ์ดํธ์ ๋ํด ํ์ต์ด ๋๋๋ฉด ๋ค์์ ์ํ: for x in X_and: print(f"Input: {x}, Predicted Output: {ppn_and.predict(x)}") ํ์ต๋ ๊ฐ์ค์น๋ก ์๋ก์ด ์ ๋ ฅ์ ํ ์คํธํด๋ณด๋ ๊ณผ์ 2-9. ์์ฝ: ํผ์ ํธ๋ก ํ์ต ํ๋ฆ ์ ๋ ฅ X, ์ ๋ต y โ ๊ฐ์คํฉ ๊ณ์ฐ (wยทx + b) โ ๊ณ๋จ ํจ์๋ก ์์ธก โ ์ค์ฐจ ๊ณ์ฐ (target - predict) โ w, b ์ ๋ฐ์ดํธ โ ์๋ฌ ๊ธฐ๋ก โ epoch ๋ฐ๋ณต โ ํ์ต ์๋ฃ ํ ํ ์คํธ 3. XOR ๊ฒ์ดํธ๊ฐ ํผ์ ํธ๋ก ์ผ๋ก ์ ๋๋ ์ด์ ํผ์ ํธ๋ก ์ ์ง์ ํ๋๋ก ์ถ๋ ฅ์ ๋๋๋ ๋ชจ๋ธ XOR์ ์ด๋ค ์ง์ ์ผ๋ก๋ 0๊ณผ 1์ ๋๋ ์ ์์ ์ฆ, ์ ํ ๋ถ๋ฆฌ ๋ถ๊ฐ๋ฅ ๋ฌธ์ โ ํผ์ ํธ๋ก ํ๊ณ ํด๊ฒฐ์ฑ : ๋ค์ธต ํผ์ ํธ๋ก (MLP) ๋น์ ํ์ฑ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์๋์ธต + ๋น์ ํ ํ์ฑํ ํจ์ (์: sigmoid, ReLU)๋ฅผ ํ์ฉํ๊ณ , ์ค์ฐจ ์ญ์ ํ(Backpropagation)๋ก ํ์ตํจ
Study
ยท 2025-06-25
Day3 : Perceptron
๐ Perceptron๋? ํผ์ ํธ๋ก (Perceptron)์ ์๋ฌผํ์ ๋ด๋ฐ์ ์ํ์ ์ผ๋ก ๋ชจ๋ธ๋งํ ์ธ๊ณต ๋ด๋ฐ ๋ชจ๋ธ๋ก, ์ฌ๋ฌ ์ ๋ ฅ ์ ํธ๋ฅผ ๋ฐ์ ๊ฐ ์ ๋ ฅ์ ๋ํ ๊ฐ์ค์น(Weight)๋ฅผ ๊ณฑํ ํ, ์ด๋ค์ ๊ฐ์คํฉ(Weighted Sum)์ ๊ณ์ฐํ๊ณ , ํ์ฑํ ํจ์(Activation Function)๋ฅผ ํตํด ์ต์ข ์ถ๋ ฅ์ ๊ฒฐ์ ํ๋ ๊ตฌ์กฐ์ด๋ค. ๐ง ๊ตฌ์กฐ (Perceptron Structure) ์ ๋ ฅ(x) โ ๊ฐ์ค์น(w) โ ๊ฐ์คํฉ(โ) โ ํ์ฑํ ํจ์(f) โ ์ถ๋ ฅ(y) ์ ๋ ฅ (Input): AND, OR ๋ฑ ๋ ผ๋ฆฌ ์ฐ์ฐ์ ์ํ ์ ๋ ฅ ์ ํธ. ๊ฐ์ค์น (Weight): ์ ๋ ฅ ์ ํธ์ ์ค์๋๋ฅผ ๊ฒฐ์ ํ๋ฉฐ, ํ์ต์ ํตํด ์กฐ์ ๋จ. ๊ฐ์คํฉ (Weighted Sum): ๊ฐ ์ ๋ ฅ๊ณผ ๊ทธ์ ๋์ํ๋ ๊ฐ์ค์น์ ๊ณฑ์ ๋ชจ๋ ๋ํ ๊ฐ. ํ์ฑํ ํจ์ (Activation Function): ๊ฐ์คํฉ์ด ์๊ณ๊ฐ์ ๋์ผ๋ฉด 1, ๋์ง ๋ชปํ๋ฉด 0์ ์ถ๋ ฅํ๋ ํจ์. ๋ํ์ ์ผ๋ก ๋จ์ ๊ณ๋จ ํจ์ ์ฌ์ฉ. ์ถ๋ ฅ (Output): ์ต์ข ๊ฒฐ๊ณผ๊ฐ (๋ณดํต 0 ๋๋ 1์ ์ด์ง ์ถ๋ ฅ). ๐ฏ ์์ฝ ํผ์ ํธ๋ก ์ ์ด์ง ๋ถ๋ฅ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์ ๊ฒฝ๋ง ๊ตฌ์กฐ์ด๋ค. ํ์ต์ ํตํด ์ ๋ ฅ ์ ํธ์ ์ค์๋๋ฅผ ๋ํ๋ด๋ ๊ฐ์ค์น๊ฐ ์กฐ์ ๋๋ค. ๋จ์ธต ํผ์ ํธ๋ก ์ ์ ํ ๋ถ๋ฆฌ ๊ฐ๋ฅํ ๋ฌธ์ ๋ง ํด๊ฒฐํ ์ ์๋ค. ๐จโ๐ป ์ค์ต ๐ก Code : AND & OR & NAND & XOR Gate Perceptron # AND & OR & NAND & XOR Gate Perceptron import numpy as np import matplotlib.pyplot as plt class Perceptron: def __init__(self, input_size, lr=0.1, epochs=10): self.weights = np.zeros(input_size) self.bias = 0 self.lr = lr self.epochs = epochs self.errors = [] def activation(self, x): return np.where(x > 0, 1, 0) def predict(self, x): linear_output = np.dot(x, self.weights) + self.bias return self.activation(linear_output) def train(self, X, y): for epoch in range(self.epochs): total_error = 0 for xi, target in zip(X, y): prediction = self.predict(xi) update = self.lr * (target - prediction) self.weights += update * xi self.bias += update total_error += int(update != 0.0) self.errors.append(total_error) print(f"Epoch {epoch+1}/{self.epochs}, Errors: {total_error}") # AND ๊ฒ์ดํธ ๋ฐ์ดํฐ ๋ฐ ํ์ต X_and = np.array([[0,0],[0,1],[1,0],[1,1]]) y_and = np.array([0,0,0,1]) print(" AND Gate Training") ppn_and = Perceptron(input_size=2) ppn_and.train(X_and, y_and) print("\n AND Gate Test:") for x in X_and: print(f"Input: {x}, Predicted Output: {ppn_and.predict(x)}") # OR ๊ฒ์ดํธ ๋ฐ์ดํฐ ๋ฐ ํ์ต X_or = np.array([[0,0],[0,1],[1,0],[1,1]]) y_or = np.array([0,1,1,1]) print("\n OR Gate Training") ppn_or = Perceptron(input_size=2) ppn_or.train(X_or, y_or) print("\n OR Gate Test:") for x in X_or: print(f"Input: {x}, Predicted Output: {ppn_or.predict(x)}") # NAND ๊ฒ์ดํธ ๋ฐ์ดํฐ ๋ฐ ํ์ต X_nand = np.array([[0,0],[0,1],[1,0],[1,1]]) y_nand = np.array([1,1,1,0]) # AND์ ๋ฐ๋ print("\n NAND Gate Training") ppn_nand = Perceptron(input_size=2) ppn_nand.train(X_nand, y_nand) print("\n NAND Gate Test:") for x in X_nand: print(f"Input: {x}, Predicted Output: {ppn_nand.predict(x)}") # XOR ๊ฒ์ดํธ ๋ฐ์ดํฐ ๋ฐ ํ์ต X_xor = np.array([[0,0],[0,1],[1,0],[1,1]]) y_xor = np.array([0,1,1,0]) # ์ ํ ๋ถ๋ฆฌ ๋ถ๊ฐ๋ฅ print("\n XOR Gate Training") ppn_xor = Perceptron(input_size=2) ppn_xor.train(X_xor, y_xor) print("\n XOR Gate Test:") for x in X_xor: print(f"Input: {x}, Predicted Output: {ppn_xor.predict(x)}") โ Result : AND & OR & NAND & XOR Gate Perceptron AND Gate Training Epoch 1/10, Errors: 1 Epoch 2/10, Errors: 3 Epoch 3/10, Errors: 3 Epoch 4/10, Errors: 2 Epoch 5/10, Errors: 1 Epoch 6/10, Errors: 0 Epoch 7/10, Errors: 0 Epoch 8/10, Errors: 0 Epoch 9/10, Errors: 0 Epoch 10/10, Errors: 0 AND Gate Test: Input: [0 0], Predicted Output: 0 Input: [0 1], Predicted Output: 0 Input: [1 0], Predicted Output: 0 Input: [1 1], Predicted Output: 1 OR Gate Training Epoch 1/10, Errors: 1 Epoch 2/10, Errors: 2 Epoch 3/10, Errors: 1 Epoch 4/10, Errors: 0 Epoch 5/10, Errors: 0 Epoch 6/10, Errors: 0 Epoch 7/10, Errors: 0 Epoch 8/10, Errors: 0 Epoch 9/10, Errors: 0 Epoch 10/10, Errors: 0 OR Gate Test: Input: [0 0], Predicted Output: 0 Input: [0 1], Predicted Output: 1 Input: [1 0], Predicted Output: 1 Input: [1 1], Predicted Output: 1 NAND Gate Training Epoch 1/10, Errors: 2 Epoch 2/10, Errors: 3 Epoch 3/10, Errors: 3 Epoch 4/10, Errors: 0 Epoch 5/10, Errors: 0 Epoch 6/10, Errors: 0 Epoch 7/10, Errors: 0 Epoch 8/10, Errors: 0 Epoch 9/10, Errors: 0 Epoch 10/10, Errors: 0 NAND Gate Test: Input: [0 0], Predicted Output: 1 Input: [0 1], Predicted Output: 1 Input: [1 0], Predicted Output: 1 Input: [1 1], Predicted Output: 0 XOR Gate Training Epoch 1/10, Errors: 2 Epoch 2/10, Errors: 3 Epoch 3/10, Errors: 4 Epoch 4/10, Errors: 4 Epoch 5/10, Errors: 4 Epoch 6/10, Errors: 4 Epoch 7/10, Errors: 4 Epoch 8/10, Errors: 4 Epoch 9/10, Errors: 4 Epoch 10/10, Errors: 4 XOR Gate Test: Input: [0 0], Predicted Output: 1 Input: [0 1], Predicted Output: 1 Input: [1 0], Predicted Output: 0 Input: [1 1], Predicted Output: 0 ๐ก Code : ๊ฒฝ๊ณ ๊ฒฐ์ ์๊ฐํ ํจ์ (AND, OR, NAND, XOR) # ๊ฒฝ๊ณ ๊ฒฐ์ ์๊ฐํ ํจ์ (AND, OR, NAND, XOR) from matplotlib.colors import ListedColormap import matplotlib.pyplot as plt import numpy as np def plot_decision_boundary(X, y, model, title='Perceptron Decision Boundary'): cmap_light = ListedColormap(['#FFDDDD', '#DDDDFF']) # ๋ฐฐ๊ฒฝ ์์ cmap_bold = ListedColormap(['#FF0000', '#0000FF']) # ์ ์์ h = .02 # mesh grid ๊ฐ๊ฒฉ x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.figure(figsize=(6, 5)) plt.contourf(xx, yy, Z, cmap=cmap_light) plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold, edgecolor='k', s=100, marker='o') plt.xlabel('Input 1') plt.ylabel('Input 2') plt.title(title) plt.grid(True) plt.show() # AND ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ plot_decision_boundary(X_and, y_and, ppn_and, title='AND Gate Decision Boundary') # OR ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ plot_decision_boundary(X_or, y_or, ppn_or, title='OR Gate Decision Boundary') # NAND ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ plot_decision_boundary(X_nand, y_nand, ppn_nand, title='NAND Gate Decision Boundary') # XOR ๊ฒ์ดํธ ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ plot_decision_boundary(X_xor, y_xor, ppn_xor, title='XOR Gate Decision Boundary') โ Result : ๊ฒฝ๊ณ ๊ฒฐ์ ์๊ฐํ ํจ์ (AND, OR, NAND, XOR) ๐ก Code : # ์ค๋ฅ ์๊ฐํ (AND, OR, NAND, XOR) # ์ค๋ฅ ์๊ฐํ (AND, OR, NAND, XOR) plt.figure(figsize=(8, 5)) plt.plot(range(1, len(ppn_and.errors) + 1), ppn_and.errors, marker='o', label='AND Gate') plt.plot(range(1, len(ppn_or.errors) + 1), ppn_or.errors, marker='s', label='OR Gate') plt.plot(range(1, len(ppn_nand.errors) + 1), ppn_nand.errors, marker='^', label='NAND Gate') plt.plot(range(1, len(ppn_xor.errors) + 1), ppn_xor.errors, marker='x', label='XOR Gate') plt.xlabel('Epochs') plt.ylabel('Number of Errors') plt.title('Perceptron Learning Error Over Epochs') plt.legend() plt.grid(True) plt.show() โ Result : ์ค๋ฅ ์๊ฐํ (AND, OR, NAND, XOR) ๐ฌ Comment ํผ์ ํธ๋ก : ์ ๋ ฅ ๋ฒกํฐ์ ๊ฐ์ค์น๋ฅผ ๊ณฑํ ํฉ์ด ๊ธฐ์ค(0)์ ๋๋์ง ํ๋จํ๊ณ , ํ์ต ๊ณผ์ ์์๋ ํ๋ฆฐ ๋งํผ๋ง ์กฐ์ ํ๋ฉฐ ์ ํ ๋ถ๋ฆฌ๋ฅผ ๋ฐฐ์ฐ๋ ๊ตฌ์กฐ XOR์ ์ ํ ๋ถ๋ฆฌ ๋ถ๊ฐ๋ฅํ ๋ฌธ์ ์ด๊ธฐ ๋๋ฌธ์ ๋จ์ธต ํผ์ ํธ๋ก ์ผ๋ก๋ ํด๊ฒฐํ ์ ์๋ค. ์ด๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด ๋ค์ธต ํผ์ ํธ๋ก (MLP)์ด๋ ๋น์ ํ ๋ณํ์ด ํ์ํ๋ค. ๐ก Code : MLP๋ก XOR ๋ฌธ์ ํด๊ฒฐ import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap class MultiLayerPerceptron: def __init__(self, input_size=2, hidden_size=4, output_size=1, lr=0.5, epochs=1000): self.W1 = np.random.uniform(-1, 1, (input_size, hidden_size)) self.b1 = np.zeros((1, hidden_size)) self.W2 = np.random.uniform(-1, 1, (hidden_size, output_size)) self.b2 = np.zeros((1, output_size)) self.lr = lr self.epochs = epochs self.losses = [] def sigmoid(self, x): return 1 / (1 + np.exp(-np.clip(x, -250, 250))) def sigmoid_derivative(self, x): return x * (1 - x) def forward(self, X): self.z1 = np.dot(X, self.W1) + self.b1 self.a1 = self.sigmoid(self.z1) self.z2 = np.dot(self.a1, self.W2) + self.b2 self.a2 = self.sigmoid(self.z2) return self.a2 def backward(self, X, y, output): m = X.shape[0] dZ2 = output - y dW2 = (1 / m) * np.dot(self.a1.T, dZ2) db2 = (1 / m) * np.sum(dZ2, axis=0, keepdims=True) dZ1 = np.dot(dZ2, self.W2.T) * self.sigmoid_derivative(self.a1) dW1 = (1 / m) * np.dot(X.T, dZ1) db1 = (1 / m) * np.sum(dZ1, axis=0, keepdims=True) self.W2 -= self.lr * dW2 self.b2 -= self.lr * db2 self.W1 -= self.lr * dW1 self.b1 -= self.lr * db1 def train(self, X, y): for epoch in range(self.epochs): output = self.forward(X) loss = np.mean((output - y) ** 2) self.losses.append(loss) self.backward(X, y, output) #if epoch % 200 == 0: # print(f"Epoch {epoch}/{self.epochs}, Loss: {loss:.6f}") def predict(self, X): output = self.forward(X) return (output > 0.5).astype(int) def predict_prob(self, X): return self.forward(X).ravel() # ๊ฒฐ์ ๊ฒฝ๊ณ์ฉ # === XOR ๋ฐ์ดํฐ === X_xor = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y_xor = np.array([[0], [1], [1], [0]]) # === ํ์ต === print("\n=== XOR Gate Multi-Layer Perceptron Training ===") mlp = MultiLayerPerceptron(input_size=2, hidden_size=2, lr=0.5, epochs=10000) mlp.train(X_xor, y_xor) # === ์์ธก ๊ฒฐ๊ณผ ์ถ๋ ฅ === print("\nXOR GATE Test (Multi-Layer Perceptron):") xor_predictions = mlp.predict(X_xor) for i, x in enumerate(X_xor): predicted = xor_predictions[i][0] actual = y_xor[i][0] result = "โ" if predicted == actual else "โ" print(f"Input: {x}, Predicted: {predicted}, Actual: {actual}, {result}") # === ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ ํจ์ === def plot_decision_boundary(X, y, model, title="Decision Boundary"): cmap_light = ListedColormap(['#FFDDDD', '#DDDDFF']) cmap_bold = ListedColormap(['#FF0000', '#0000FF']) h = .01 x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5 y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) grid = np.c_[xx.ravel(), yy.ravel()] Z = model.predict_prob(grid) Z = Z.reshape(xx.shape) plt.figure(figsize=(6, 5)) plt.contourf(xx, yy, Z > 0.5, cmap=cmap_light) plt.scatter(X[:, 0], X[:, 1], c=y.ravel(), cmap=cmap_bold, edgecolor='k', s=120) plt.title(title) plt.xlabel("Input 1") plt.ylabel("Input 2") plt.grid(True) plt.show() # === ๊ฒฐ์ ๊ฒฝ๊ณ ์๊ฐํ === plot_decision_boundary(X_xor, y_xor, mlp, title="XOR MLP Decision Boundary") # === ์์ค ๊ณก์ ์๊ฐํ === plt.figure(figsize=(8, 5)) plt.plot(range(mlp.epochs), mlp.losses, color='purple') plt.title("MLP Training Loss on XOR Problem") plt.xlabel("Epochs") plt.ylabel("Mean Squared Error") plt.grid(True) plt.show() โ Result : MLP๋ก XOR ๋ฌธ์ ํด๊ฒฐ === XOR Gate Multi-Layer Perceptron Training === XOR GATE Test (Multi-Layer Perceptron): Input: [0 0], Predicted: 0, Actual: 0, โ Input: [0 1], Predicted: 1, Actual: 1, โ Input: [1 0], Predicted: 1, Actual: 1, โ Input: [1 1], Predicted: 0, Actual: 0, โ
Study
ยท 2025-06-25
Day2 : OpenCV
๐ OpenCV๋? OpenCV(Open Source Computer Vision Library)๋ ์ค์๊ฐ ์ปดํจํฐ ๋น์ ๋ฐ ๋จธ์ ๋ฌ๋์ ์ํ ์คํ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค. ๋ค์ํ ์ด๋ฏธ์ง/๋น๋์ค ์ฒ๋ฆฌ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ฉฐ, Python, C++, Java ๋ฑ ๋ค์ํ ์ธ์ด์์ ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค. ๐ CUDA ๋ชจ๋์ ์ญํ GPU ๊ฐ์์ ํ์ฉํ ๊ณ ์ ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ํ OpenCV์ ์ผ๋ถ ํจ์๋ค์ CUDA๋ฅผ ํตํด ๋ณ๋ ฌ ์ฒ๋ฆฌ๋์ด ์ฑ๋ฅ์ ํฅ์์ํด ์ฌ์ฉ ์: cv2.cuda.GpuMat, cv2.cuda.filter2D(), cv2.cuda.resize() ๋ฑ ๐ ๏ธ ์์ ํ ๋๋ ํ ๋ฆฌ ์์ฑ ๋ฐ ํ๊ฒฝ ์ค์ # 1. ์์ ๋๋ ํ ๋ฆฌ ์์ฑ mkdir opencv # ๋๋ ํ ๋ฆฌ ์ด๋ฆ: opencv cd opencv # ํด๋น ๋๋ ํ ๋ฆฌ๋ก ์ด๋ # 2. ๊ฐ์ ํ๊ฒฝ ์์ฑ ๋ฐ ํ์ฑํ python3 -m venv .env # ๊ฐ์ ํ๊ฒฝ ์์ฑ (ํด๋ ์ด๋ฆ: .env) source .env/bin/activate # ๊ฐ์ ํ๊ฒฝ ํ์ฑํ # 3. ํจํค์ง ์ค์น pip install opencv-python # OpenCV ๊ธฐ๋ณธ ๊ธฐ๋ฅ(core, imgproc ๋ฑ) pip install opencv-contrib-python # ์ถ๊ฐ ๋ชจ๋(contrib ํฌํจ) pip install -U pip # pip ์ต์ ๋ฒ์ ์ผ๋ก ์ ๊ทธ๋ ์ด๋ โ ์ค์น ํ์ธ (Python ์ธํฐํ๋ฆฌํฐ ์คํ) >>> import numpy as np >>> import cv2 >>> np.__version__ '2.2.6' # ์ค์น๋ NumPy ๋ฒ์ ์ถ๋ ฅ >>> cv2.__version__ '4.11.0' # ์ค์น๋ OpenCV ๋ฒ์ ์ถ๋ ฅ >>> exit() # Python ์ธํฐํ๋ฆฌํฐ ์ข ๋ฃ ๐จ ์์ ์ ๋ณด ๐ ์ฐธ๊ณ ์ฌ์ดํธ W3Schools - RGB Colors ๐ RGB (Red, Green, Blue) ๊ฐ ์์ ์ฑ๋: 0~255 (8bit) R (Red): 8bit G (Green): 8bit B (Blue): 8bit ํฝ์ 1๊ฐ = 24bit (8bit ร 3) ๐จ HSL (Hue, Saturation, Lightness) H: ์์ (Hue) โ 0 ~ 360ยฐ S: ์ฑ๋ (Saturation) โ 0 ~ 100% L: ๋ฐ๊ธฐ (Lightness) โ 0 ~ 100% ๐ RGB vs HSL ์ฐจ์ด์ ํญ๋ชฉ RGB HSL ๊ตฌ์ฑ Red, Green, Blue (๊ฐ 0~255) Hue (0~360), Saturation & Lightness (0~100%) ์ง๊ด์ฑ ์ปดํจํฐ์์ ์ฌ์ฉํ๊ธฐ ์ ํฉ ์ฌ๋์ด ์์ ์ดํดํ๊ธฐ ์ฌ์ ์ ์กฐ์ ์์ ์กฐ์ ์ด ๋ณต์กํจ ์ฑ๋/๋ฐ๊ธฐ ์กฐ์ ์ด ์ฉ์ดํจ ์ฉ๋ ๋์คํ๋ ์ด, ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋ฑ ๋์์ธ, ์์ ์ ํ ๋๊ตฌ ๋ฑ์ ์ ์ฉ โ ์์ฝ: RGB๋ ํ๋ฉด ์ถ๋ ฅ/์ฒ๋ฆฌ์ ์ ํฉํ ๋์งํธ ์ ํํ ๋ฐฉ์ HSL์ ์์ ๊ตฌ์ฑ์์๋ฅผ ๋ถ๋ฆฌํด ์ฌ๋์ด ์ดํดํ๊ฑฐ๋ ์กฐ์ ํ๊ธฐ ์ฌ์ด ๋ฐฉ์ ๐ ๋ฉ๋ชจ vi ex1.py : python ์คํฌ๋ฆฝํธ ์์ฑ python ex1.py : ์์ฑ๋ ์คํฌ๋ฆฝํธ ์คํ jpg : ํ์ผ์ด ์๊ณ ์๋๊ฐ ๋น ๋ฅด๋ฉฐ, ์ฃผ๋ก ์ฌ์ง์ด๋ ์น ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง์ ์ฌ์ฉ png : ํ์ง ๋ณด์กด, ํฌ๋ช ๋ฐฐ๊ฒฝ์ด ํ์ํ ๊ฒฝ์ฐ ์ฌ์ฉ ๐จโ๐ป ์ค์ต ๐ก Code : ์ด๋ฏธ์ง Read / Write / Display # ex1.py import numpy as np import cv2 # ์ด๋ฏธ์ง ํ์ผ์ Read img = cv2.imread("Rengoku.jpg") # Image ๋ ์ด๋ฆ์ Display ์ฐฝ ์์ฑ cv2.namedWindow("image", cv2.WINDOW_NORMAL) # Numpy ndarray H/W/C order print(img.shape) # Read ํ ์ด๋ฏธ์ง ํ์ผ์ Display cv2.imshow("image", img) # ๋ณ๋ ํค ์ ๋ ฅ์ด ์์๋ ๊น์ง ๋๊ธฐ cv2.waitKey(0) # ex1_output.jpg ๋ก ์ฝ์ ์ด๋ฏธ์ง ํ์ผ์ ์ ์ฅ cv2.imwrite("ex1_output.jpg", img) # Destory all windows cv2.destroyAllWindows() โ Quiz: ์ด๋ฏธ์ง Read / Write / Display 1. print(img.shape)์ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ ๋ฌด์จ ์๋ฏธ์ผ๊น? 2. ๋ณธ์ธ์ด ์ข์ํ๋ ์ฌ์ง์ web ์์ ๋ค์ด๋ฐ์์ OpenCV API๋ฅผ ์ฌ์ฉํด์ Display ๋ฐ ํ์ผ๋ก ์ ์ฅํด๋ณด์. 3. ํ์ฌ๋ ๋ณ๋์ ํค ์ ๋ ฅ์ด ์์ ๋๊น์ง cv2.waitKey(0) ํจ์์์ ๋๊ธฐํ๊ฒ ๋๋ค. ์ฝ๋๋ฅผ ์ถ๊ฐํด์ ์๋ฌธ์ โsโ ํค๋ฅผ ์ ๋ ฅ๋ฐ์ ๋๋ง ์ด๋ฏธ์ง ํ์ผ์ ์ ์ฅํ๊ณ ๋ค๋ฅธ ํค๊ฐ ์ ๋ ฅ๋๋ฉด ์ด๋ฏธ์ง ํ์ผ์ ์ ์ฅํ์ง ์๊ฒ ์์ ํด๋ณด์. ๐ก Code : RGB/HSV Color Space (์ ๊ณต๊ฐ) # ex2.py import numpy as np import cv2 # ์ด๋ฏธ์ง ํ์ผ์ Read ํ๊ณ Color space ์ ๋ณด ์ถ๋ ฅ color = cv2.imread("Rengoku.jpg", cv2.IMREAD_COLOR) print(color.shape) height,width,channels = color.shape cv2.imshow("Original Image", color) # Color channel ์ B,G,R ๋ก ๋ถํ ํ์ฌ ์ถ๋ ฅ b,g,r = cv2.split(color) rgb_split = np.concatenate((b,g,r),axis=1) cv2.imshow("BGR Channels",rgb_split) # ์๊ณต๊ฐ์ BGR ์์ HSV ๋ก ๋ณํ hsv = cv2.cvtColor(color, cv2.COLOR_BGR2HSV) # Channel ์ H,S,V ๋ก ๋ถํ ํ์ฌ ์ถ๋ ฅ h,s,v = cv2.split(hsv) hsv_split = np.concatenate((h,s,v),axis=1) cv2.imshow("Split HSV", hsv_split) โ Quiz : RGB/HSV Color Space (์ ๊ณต๊ฐ) 1. ์ ์๊ณต๊ฐ ์ด๋ฏธ์ง์ ๋งํฌ๋ก ์ด๋ํด์ ๊ฐ ์ ๊ณต๊ฐ์ ํํ ๋ฐฉ๋ฒ์ ์ดํดํด ๋ณด์. 2. HSV color space๊ฐ ์ด๋ค ๊ฒฝ์ฐ์ ํจ๊ณผ์ ์ผ๋ก ์ฌ์ฉ๋ ๊น? 3. HSV๋ก ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ BGR์ด ์๋ RGB๋ก ๋ค์ ๋ณํํด์ ์ถ๋ ฅํด ๋ณด์. 4. COLOR_RGB2GRAY๋ฅผ ์ฌ์ฉํด์ ํ๋ฐฑ์ผ๋ก ๋ณํํด ์ถ๋ ฅํด ๋ณด์. ๐ก Code : Crop / Resize (์๋ฅด๊ธฐ / ํฌ๊ธฐ ์กฐ์ ) # ex3.py import numpy as np import cv2 # ์ด๋ฏธ์ง ํ์ผ์ Read img = cv2.imread("Rengoku.jpg") # Crop 300x400 from original image from (100, 50)=(x, y) # ์ธ๋ก(y): 100:500 โ 500 - 100 = 400ํฝ์ # ๊ฐ๋ก(x): 500:1200 โ 1200 - 500 = 700ํฝ์ cropped = img[100:500, 500:1200] # Resize cropped image from 300x400 to 400x200 resized = cv2.resize(cropped, (800,200)) # Display all cv2.imshow("Original", img) cv2.imshow("Cropped image", cropped) cv2.imshow("Resized image", resized) cv2.imwrite("ex3_cropped.jpg", cropped) cv2.imwrite("ex3_resized.jpg", resized) cv2.waitKey(0) cv2.destroyAllWindows() โ Quiz : Crop / Resize (์๋ฅด๊ธฐ / ํฌ๊ธฐ ์กฐ์ ) 1. Input image ๋ฅผ ๋ณธ์ธ์ด ์ข์ํ๋ ์ธ๋ฌผ ์ฌ์ง์ผ๋ก ๋ณ๊ฒฝํด์ ์ ์ฉํ์. ๊ทธ๋ฆฌ๊ณ ๋ณธ์ธ์ด ์ฌ์ฉํ input image ์ size ๋ฅผ ํ์ธํด ๋ณด์. 2. ๋ณธ์ธ์ด ์ฌ์ฉํ ์ด๋ฏธ์ง์ ์ผ๊ตด ์์ญ๋ง crop ํด์ display ํด ๋ณด์. 3. ์๋ณธ ์ด๋ฏธ์ง์ ์ ํํ 1.5๋ฐฐ๋งํผ ์ด๋ฏธ์ง๋ฅผ ํ๋ํด์ ํ์ผ๋ก ์ ์ฅํด ๋ณด์. 4. openCV ์ rotate API ๋ฅผ ์ฌ์ฉํด์ ์ฐ์ธก์ผ๋ก 90๋๋งํผ ํ์ ๋ ์ด๋ฏธ์ง๋ฅผ ์ถ๋ ฅํด ๋ณด์. ๐ก Code : ์ญ์ (Reverse Image) # ex4.py import numpy as np import cv2 src = cv2.imread("Rengoku.jpg", cv2.IMREAD_COLOR) dst = cv2.bitwise_not(src) cv2.imshow("src", src) cv2.imshow("dst", dst) cv2.imwrite("ex4_reverse.jpg", dst) cv2.waitKey() cv2.destroyAllWindows() โ Quiz : ์ญ์ (Reverse Image) 1. AND, OR, XOR ์ฐ์ฐ์ ๋ํด์ ํ์ธํด ๋ณด์. ๐ก Code : ์ด์งํ (Binary) # ex5.py import numpy as np import cv2 src = cv2.imread("Rengoku.jpg", cv2.IMREAD_COLOR) gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) ret, dst = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) cv2.imshow("dst", dst) cv2.imwrite("ex5_binary.jpg", dst) cv2.waitKey() cv2.destroyAllWindows() โ Quiz : ์ด์งํ (Binary) 1. ์๊ณ๊ฐ์ ๋ณํ์์ผ ๋ณด์. ๐ก Code : ํ๋ฆผํจ๊ณผ (Blur) # ex6.py import numpy as np import cv2 src = cv2.imread("Rengoku.jpg", cv2.IMREAD_COLOR) dst = cv2.blur(src, (9, 9), anchor=(-1,- 1), borderType=cv2.BORDER_DEFAULT) cv2.imshow("dst", dst) cv2.imwrite("ex6_blur.jpg", dst) cv2.waitKey() cv2.destroyAllWindows() โ Quiz : ํ๋ฆผํจ๊ณผ (Blur) 1. Kernel Size๋ฅผ ๋ณ๊ฒฝํ์ฌ ๋ณด์. 2. borderType์ ๋ณ๊ฒฝํ์ฌ ๋ณด์.(cv2.BORDER_REFLECT) ๐ก Code : ๊ฐ์ฅ์๋ฆฌ ๊ฒ์ถ (Edge) # ex7.py import numpy as np import cv2 src = cv2.imread("Rengoku.jpg", cv2.IMREAD_COLOR) gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, 3) cv2.imshow("sobel", sobel) cv2.imwrite("ex7_edge.jpg", sobel) cv2.waitKey() cv2.destroyAllWindows() โ Quiz : ๊ฐ์ฅ์๋ฆฌ ๊ฒ์ถ (Edge) 1. Laplacian ๋ณํ์ ์ ์ฉํด ๋ณด์. 2. Canny Edge Detection์ ์ ์ฉํด ๋ณด์. ๐ก Code : ๋ฐฐ์ด ๋ณํฉ (add Weighted) # ex8.py import numpy as np import cv2 src = cv2.imread("RGB.png", cv2.IMREAD_COLOR) hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV) # 1. Red ๋ง์คํฌ ์์ฑ lower_red = cv2.inRange(hsv, (0, 100, 100), (5, 255, 255)) upper_red = cv2.inRange(hsv, (170, 100, 100), (180, 255, 255)) mask_red = cv2.addWeighted(lower_red, 1.0, upper_red, 1.0, 0.0) # 2. Green ๋ง์คํฌ ์์ฑ mask_green = cv2.inRange(hsv, (40, 100, 100), (85, 255, 255)) # 3. Blue ๋ง์คํฌ ์์ฑ mask_blue = cv2.inRange(hsv, (100, 100, 100), (130, 255, 255)) # 4. ๊ฐ ์์ ์ถ์ถ (HSV โ BGR ๋ณํ ํฌํจ) red = cv2.bitwise_and(hsv, hsv, mask=mask_red) green = cv2.bitwise_and(hsv, hsv, mask=mask_green) blue = cv2.bitwise_and(hsv, hsv, mask=mask_blue) red = cv2.cvtColor(red, cv2.COLOR_HSV2BGR) green = cv2.cvtColor(green, cv2.COLOR_HSV2BGR) blue = cv2.cvtColor(blue, cv2.COLOR_HSV2BGR) # 5. ํ๋ฉด ์ถ๋ ฅ cv2.imshow("Original", src) cv2.imshow("Red", red) cv2.imshow("Green", green) cv2.imshow("Blue", blue) cv2.imwrite("ex8_original.png", src) cv2.imwrite("ex8_red.png", red) cv2.imwrite("ex8_green.png", green) cv2.imwrite("ex8_blue.png", blue) cv2.waitKey() cv2.destroyAllWindows() โ Quiz : ๋ฐฐ์ด ๋ณํฉ (add Weighted) 1. lower_red ๊ฐ์ ๋ฒ์๋ฅผ ๋ณ๊ฒฝํด ๋ณด์. 2. upper_red ๊ฐ์ ๋ฒ์๋ฅผ ๋ณ๊ฒฝํด ๋ณด์. 3. addWeighted์ gamma ๊ฐ์ ๋ณ๊ฒฝํด ๋ณด์. ๐ก Code : ์ฑ๋ ๋ถ๋ฆฌ ๋ฐ ๋ณํฉ # ex9.py import numpy as np import cv2 # ์ด๋ฏธ์ง ์ฝ๊ธฐ src = cv2.imread("RGB.png", cv2.IMREAD_COLOR) # ์ฑ๋ ๋ถ๋ฆฌ b, g, r = cv2.split(src) # ์ฑ๋ ์์ ๋ณ๊ฒฝ (RGB์ฒ๋ผ ๋ณด์ด๊ฒ) inverse = cv2.merge((r, g, b)) # ํ๋ฉด ์ถ๋ ฅ cv2.imshow("b", b) cv2.imshow("g", g) cv2.imshow("r", r) cv2.imshow("inverse", inverse) # ์ด๋ฏธ์ง ์ ์ฅ cv2.imwrite("ex9_blue_gray.png", b) cv2.imwrite("ex9_green_gray.png", g) cv2.imwrite("ex9_red_gray.png", r) cv2.imwrite("ex9_inverse.png", inverse) cv2.waitKey() cv2.destroyAllWindows() โ Quiz : ์ฑ๋ ๋ถ๋ฆฌ ๋ฐ ๋ณํฉ 1. Numpy ํํ์ ์ฑ๋ ๋ถ๋ฆฌ๋ฅผ ์ ์ฉํด ๋ณด์. b = src[:, :, 0] g = src[:, :, 1] r = src[:, :, 2] 2. ๋น ์ด๋ฏธ์ง๋ฅผ ์ ์ฉํด ๋ณด์. height, width, channel = src.shape zero = np.zeros((height, width, 1), dtype=np.uint8) bgz = cv2.merge((b, g, zero)) ๐ก Code : ๋์์ ํ์ผ์ ์ฝ๊ณ ๋ณด์ฌ์ฃผ๊ธฐ # ex10.py import numpy as np import cv2 cap = cv2.VideoCapture("son.mp4") save_count = 1 # ์ ์ฅํ ์ด๋ฏธ์ง ๋ฒํธ ์ด๊ธฐํ while cap.isOpened(): ret, frame = cap.read() # (2) ํ๋ ์ ์ฝ๊ธฐ ์คํจ โ ์์ ๋ โ ์ฒ์๋ถํฐ ๋ค์ if not ret: print("Restarting video...") cap.set(cv2.CAP_PROP_POS_FRAMES, 0) continue # (3) ํ๋ ์ ํฌ๊ธฐ 50% ์ถ์ resized = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5) # ์ถ๋ ฅ cv2.imshow("Resized Frame", resized) # (1) ๊ณ ์ ๋ ์๋๋ก ์ฌ์ (์ฝ 30fps) key = cv2.waitKey(90) # (4) 'c' ํค ์ ๋ ฅ ์ ์ด๋ฏธ์ง ์ ์ฅ if key & 0xFF == ord('c'): filename = f"{save_count:03}.jpg" cv2.imwrite(filename, resized) print(f"Saved {filename}") save_count += 1 # 'q' ํค ์ ๋ ฅ ์ ์ข ๋ฃ if key & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() โ Quiz : ๋์์ ํ์ผ์ ์ฝ๊ณ ๋ณด์ฌ์ฃผ๊ธฐ 1. ๋์์์ด ๋๋ฌด ๋น ๋ฅด๊ฒ ์ฌ์๋๋ค. ์ด์ ๋ฅผ ์ฐพ์๋ณด๊ณ ์ ์์ ์ธ ์๋๋ก ์ฌ์๋ ์ ์๋๋ก ์์ ํด ๋ณด์. 2. ๋์์์ด ๋๊น์ง ์ฌ์๋๋ฉด ๋ ์ด์ frame์ ์ฝ์ง ๋ชปํด ์ข ๋ฃ๋๋ค. ๋์์์ด ๋๊น์ง ์ฌ์๋๋ฉด ๋ค์ ์ฒ์๋ถํฐ ๋ฐ๋ณต๋ ์ ์๋๋ก ์์ ํด ๋ณด์. 3. ๋์์ ํฌ๊ธฐ๋ฅผ ๋ฐ์ผ๋ก resizeํด์ ์ถ๋ ฅํด ๋ณด์. 4. ๋์์ ์ฌ์ ์ค 'c' ํค ์ ๋ ฅ์ ๋ฐ์ผ๋ฉด ํด๋น ํ๋ ์์ ์ด๋ฏธ์ง ํ์ผ๋ก ์ ์ฅํ๋ ์ฝ๋๋ฅผ ์์ฑํด ๋ณด์. ํ์ผ ์ด๋ฆ์ 001.jpg, 002.jpg ๋ฑ์ผ๋ก overwrite ๋์ง ์๊ฒ ํ์. ๐ก Code : ์นด๋ฉ๋ผ๋ก๋ถํฐ input ์ ๋ฐ์ ๋ณด์ฌ์ฃผ๊ณ ๋์์ ํ์ผ๋ก ์ ์ฅํ๊ธฐ # ex11.py import numpy as np import cv2 # Read from the first camera device cap = cv2.VideoCapture(0) w = 640 #1280#1920 h = 480 #720#1080 cap.set(cv2.CAP_PROP_FRAME_WIDTH, w) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, h) # ์ฑ๊ณต์ ์ผ๋ก video device ๊ฐ ์ด๋ ธ์ผ๋ฉด while ๋ฌธ ๋ฐ๋ณต while(cap.isOpened()): # ํ ํ๋ ์์ ์ฝ์ด์ด ret, frame = cap.read() if ret is False: print("Can't receive frame (stream end?). Exiting ...") break # Display cv2.imshow("Camera", frame) # 1 ms ๋์ ๋๊ธฐํ๋ฉฐ ํค ์ ๋ ฅ์ ๋ฐ๊ณ 'q' ์ ๋ ฅ ์ ์ข ๋ฃ key = cv2.waitKey(1) if key & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() โ Quiz : ์นด๋ฉ๋ผ๋ก๋ถํฐ input ์ ๋ฐ์ ๋ณด์ฌ์ฃผ๊ณ ๋์์ ํ์ผ๋ก ์ ์ฅํ๊ธฐ 1. ๊ฐ์ง๊ณ ์๋ ์นด๋ฉ๋ผ์ ์ง์ ๊ฐ๋ฅํ ํด์๋๋ฅผ ํ์ธ ํ ์นด๋ฉ๋ผ ํด์๋๋ฅผ ๋ณ๊ฒฝํด ๋ณด์. 2. ์นด๋ฉ๋ผ Input์ "output.mp4" ๋์์ ํ์ผ๋ก ์ ์ฅํ๋๋ก ์ฝ๋๋ฅผ ์ถ๊ฐํด ๋ณด์. ๐ ๋ฉ๋ชจ sudo apt install v4l-utils : ์นด๋ฉ๋ผ ์ง์ ํด์๋ ํ์ธ์ฉ ๋๊ตฌ ์ค์น v4l2-ctl -d /dev/video0 โlist-formats-ext : ํด๋น ์นด๋ฉ๋ผ์ ํด์๋ ๋ฐ ํฌ๋งท ๋ชฉ๋ก ์ถ๋ ฅ ๐ก Code : Text / Line / Ractangle # ex12.py import numpy as np import cv2 cap = cv2.VideoCapture(0) # ๋๊ทธ๋ผ๋ฏธ๋ฅผ ๊ทธ๋ฆด ์ขํ๋ฅผ ์ ์ฅํ ๋ฆฌ์คํธ circle_centers = [] def draw_circle(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: # ๋ง์ฐ์ค ์ผ์ชฝ ๋ฒํผ ํด๋ฆญ ์ ์ขํ ์ ์ฅ circle_centers.append((x, y)) cv2.namedWindow("Camera") cv2.setMouseCallback("Camera", draw_circle) topLeft = (50+100, 50) bottomRight = (300+100, 300) while cap.isOpened(): ret, frame = cap.read() if not ret: break # Line cv2.line(frame, (topLeft[0] + 80, topLeft[1]), (bottomRight[0] + 80, bottomRight[1]), (0, 255, 0), 3) # Rectangle cv2.rectangle(frame, [pt+80 for pt in topLeft], [pt+50 for pt in bottomRight], (255, 0, 255), 3) # Text font = cv2.FONT_ITALIC cv2.putText(frame, 'hhhong', [pt-180 for pt in bottomRight], font, 2, (0, 255, 255), 5) # ์ ์ฅ๋ ์ขํ์ ๋๊ทธ๋ผ๋ฏธ ๊ทธ๋ฆฌ๊ธฐ for center in circle_centers: cv2.circle(frame, center, 30, (255, 255, 0), 3) # ๋ฐ์ง๋ฆ 30, ๋๊ป 3, ์์ (BGR) cv2.imshow("Camera", frame) key = cv2.waitKey(1) if key & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() โ Quiz : Text / Line / Ractangle 1. Text ๋ฌธ๊ตฌ / Font / ์์ / ํฌ๊ธฐ / ๊ตต๊ธฐ / ์ถ๋ ฅ์์น ๋ฑ ๋ชจ๋ ๊ฐ์ ๋ณ๊ฒฝํด ๋ณด์. 2. ๋๊ทธ๋ผ๋ฏธ๋ฅผ ๊ทธ๋ฆฌ๋ ํจ์๋ฅผ ์ฐพ์์ ์ ์ฉํด ๋ณด์. 3. ๋ง์ฐ์ค ์ผ์ชฝ ๋ฒํผ์ click ํ๋ฉด ํด๋น ์์น์ ๋๊ทธ๋ผ๋ฏธ๊ฐ ๊ทธ๋ ค์ง๋๋ก ์ฝ๋๋ฅผ ์ถ๊ฐํด ๋ณด์. (Reference : cv2.EVENT_LBUTTONDOWN) ๐ก Code : Trackbar # ex13.py import cv2 topLeft = (50, 50) bold = 0 r, g, b = 255, 255, 0 # ์ด๊ธฐ ํ ์คํธ ์์: ๋ ธ๋์ (BGR = (0, 255, 255)) # bold ํธ๋๋ฐ ์ฝ๋ฐฑ def on_bold_trackbar(value): global bold bold = value global r r = value def on_g_trackbar(value): global g g = value def on_b_trackbar(value): global b b = value # ์นด๋ฉ๋ผ ์ฐ๊ฒฐ cap = cv2.VideoCapture(0) # ์๋์ฐ ๋ฐ ํธ๋๋ฐ ์์ฑ cv2.namedWindow("Camera") cv2.createTrackbar("bold", "Camera", bold, 30, on_bold_trackbar) cv2.createTrackbar("R", "Camera", r, 255, on_r_trackbar) cv2.createTrackbar("G", "Camera", g, 255, on_g_trackbar) cv2.createTrackbar("B", "Camera", b, 255, on_b_trackbar) # ๋ฃจํ ์์ while cap.isOpened(): ret, frame = cap.read() if not ret: print("Can't receive frame (stream end?). Exiting ...") break # ํ ์คํธ ์ถ๋ ฅ (ํธ๋๋ฐ์์ ๋ฐ์์จ bold, color ๊ฐ ์ฌ์ฉ) cv2.putText(frame, "hhhong", topLeft, cv2.FONT_HERSHEY_SIMPLEX, 2, (b, g, r), # BGR 1 + bold) # ํ๋ ์ ์ถ๋ ฅ cv2.imshow("Camera", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # ์ข ๋ฃ ์ฒ๋ฆฌ cap.release() cv2.destroyAllWindows() โ Quiz: Trackbar 1. Trackbar๋ฅผ controlํด์ TEXT์ ๊ตต๊ธฐ๊ฐ ๋ณํ๋ ๊ฒ์ ํ์ธํด ๋ณด์. 2. Trackbar๋ฅผ ์ถ๊ฐํด์ font size๋ฅผ ๋ณ๊ฒฝ / ์ ์ฉํด ๋ณด์. 3. R/G/B Trackbar๋ฅผ ๊ฐ๊ฐ ์ถ๊ฐํด์ ๊ธ์์ font color๋ฅผ ๋ณ๊ฒฝํด ๋ณด์.
Study
ยท 2025-06-24
Day1 : Linux start
๐ 1. ๋ฆฌ๋ ์ค ์์คํ ๊ธฐ๋ณธ ๋ช ๋ น์ด ctrl + alt + T : ํฐ๋ฏธ๋์ ์ ์ฐฝ์ผ๋ก ์คํ python3 : Python 3 ๋ฒ์ ํ์ธ (์: 3.10.12) df -h : ๋์คํฌ ํํฐ์ ์ฌ์ฉ๋ ํ์ธ (์ฌ์ด์ฆ ๋จ์ human-readable) ifconfig : IP ์ฃผ์ ๋ฐ ๋คํธ์ํฌ ์ ๋ณด ํ์ธ (net-tools ํ์) htop : CPU ๋ฐ ๋ฉ๋ชจ๋ฆฌ ์ค์๊ฐ ๋ชจ๋ํฐ๋ง clear : ํฐ๋ฏธ๋ ํ๋ฉด์ ๊นจ๋ํ๊ฒ ์ง์ echo : ๋ฌธ์์ด์ด๋ ๋ณ์ ๊ฐ์ ์ถ๋ ฅ (์คํฌ๋ฆฝํธ ๋๋ฒ๊น ์ ์์ฃผ ์ฌ์ฉ) uname -a : ์ปค๋ ๋ฐ ์์คํ ์ ๋ณด ์ ์ฒด ์ถ๋ ฅ sudo : ๊ด๋ฆฌ์ ๊ถํ(superuser)์ผ๋ก ๋ช ๋ น์ด ์คํ ๐งฐ 2. ํจํค์ง ์ค์น ๋ฐ ํ๊ฒฝ ๊ตฌ์ฑ sudo apt install python3-venv : venv ๋ชจ๋ ์ค์น sudo apt install net-tools htop : ifconfig์ htop ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ ํจํค์ง ์ค์น sudo apt install vim : vim ํ ์คํธ ํธ์ง๊ธฐ ์ค์น sudo apt install curl : URL ํต์ ๋๊ตฌ curl ์ค์น sudo apt install plocate : locate ๋ช ๋ น์ด ๋์ฒด ํจํค์ง ์ค์น sudo apt install ncal : ๋ฌ๋ ฅ ์ถ๋ ฅ ๋ช ๋ น์ด(ncal) ์ค์น (ํ์ฅ๋ cal) ๐ 3. Python ๊ฐ์ํ๊ฒฝ ๋ฐ pip ๊ด๋ จ ๋ช ๋ น์ด python3 -m venv .env : ๊ฐ์ ํ๊ฒฝ ์์ฑ (.env ํด๋์) source .env/bin/activate : ๊ฐ์ ํ๊ฒฝ ํ์ฑํ deactivate : ๊ฐ์ ํ๊ฒฝ ์ข ๋ฃ pip install -U pip : pip์ ์ต์ ๋ฒ์ ์ผ๋ก ์ ๊ทธ๋ ์ด๋ โ๏ธ 4. ์์คํ ์กฐ์ ๋ช ๋ น์ด ps : ํ์ฌ ์คํ ์ค์ธ ํ๋ก์ธ์ค ์ ๋ณด ํ์ธ kill : ํน์ ํ๋ก์ธ์ค๋ฅผ ์ข ๋ฃ (์: kill PID) service : ๋ฐฑ๊ทธ๋ผ์ด๋ ์๋น์ค ์์/์ค์ง/์ฌ์์ ๋ฑ ๊ด๋ฆฌ batch : ์์คํ ๋ถํ๊ฐ ์ ์ ๋ ๋ช ๋ น์ด๋ฅผ ์คํ (์ง์ฐ ์คํ) shutdown : ์์คํ ์ข ๋ฃ ๋๋ ์ฌ๋ถํ ์์ฝ (์: shutdown -h now) ๐ 5. ํ์ผ ๊ด๋ฆฌ ๋ช ๋ น์ด touch : ์๋ก์ด ๋น ํ์ผ ์์ฑ cat : ํ์ผ ๋ด์ฉ์ ์ถ๋ ฅํ๊ฑฐ๋ ์ฌ๋ฌ ํ์ผ์ ์ฐ๊ฒฐ head : ํ์ผ์ ์ฒ์ ๋ช ์ค์ ์ถ๋ ฅ (๊ธฐ๋ณธ 10์ค) tail : ํ์ผ์ ๋ง์ง๋ง ๋ช ์ค์ ์ถ๋ ฅ (๊ธฐ๋ณธ 10์ค) cp : ํ์ผ์ด๋ ๋๋ ํ ๋ฆฌ๋ฅผ ๋ณต์ฌ mv : ํ์ผ์ด๋ ๋๋ ํ ๋ฆฌ๋ฅผ ์ด๋ํ๊ฑฐ๋ ์ด๋ฆ ๋ณ๊ฒฝ comm : ๋ ๊ฐ์ ์ ๋ ฌ๋ ํ์ผ์ ๋น๊ตํ์ฌ ๊ณตํต/์ฐจ์ด ์ถ๋ ฅ cmp : ๋ ํ์ผ์ ๋ฐ์ดํธ ๋จ์๋ก ๋น๊ต (๋ค๋ฅธ ์์น ์ถ๋ ฅ) dd : ์ ์์ค ๋ณต์ฌ ๋ช ๋ น์ด (๋์คํฌ ๋ฐฑ์ , ISO ๊ตฝ๊ธฐ ๋ฑ ํ์ฉ๋จ) ln : ๋งํฌ ํ์ผ ์์ฑ (๊ธฐ๋ณธ์ ํ๋๋งํฌ, `-s`๋ ์ฌ๋ณผ๋ฆญ ๋งํฌ) less : ๊ธด ํ์ผ์ ํ ํ๋ฉด์ฉ ์ฝ๊ธฐ (์คํฌ๋กค ๊ฐ๋ฅ, cat๋ณด๋ค ํธํจ) sort : ํ์ผ ๋ด์ฉ์ ์ํ๋ฒณ ๋๋ ์ซ์ ์์ผ๋ก ์ ๋ ฌ chmod : ํ์ผ ๊ถํ ์ค์ (์: ์คํ ๊ถํ ๋ถ์ฌ) chown : ํ์ผ์ด๋ ๋๋ ํ ๋ฆฌ์ ์์ ์(user)๋ ๊ทธ๋ฃน ๋ณ๊ฒฝ ๐ 6. ๋คํธ์ํฌ ๊ด๋ จ ๋ช ๋ น์ด wget : URL๋ก๋ถํฐ ํ์ผ ๋ค์ด๋ก๋ (์: ์ด๋ฏธ์ง, ๋ฌธ์ ๋ฑ) curl : URL๋ก๋ถํฐ ๋ฐ์ดํฐ ์ ์ก ๋๋ ์์ (API ํ ์คํธ์ ์์ฃผ ์ฌ์ฉ) traceroute : ๋ชฉ์ ์ง๊น์ง ๊ฑฐ์น๋ ๋ผ์ฐํฐ ๊ฒฝ๋ก ์ถ์ (๋คํธ์ํฌ ๋ฌธ์ ์ง๋จ์ ์ฌ์ฉ) iptables : ๋ฆฌ๋ ์ค ๋ฐฉํ๋ฒฝ ์ค์ ๋ฐ ํจํท ํํฐ๋ง ์ ์ด ๐ 7. ๊ฒ์ ๋ฐ ์ ๊ท ํํ์ find : ํ์ผ์ด๋ ๋๋ ํ ๋ฆฌ๋ฅผ ์กฐ๊ฑด ๊ธฐ์ค์ผ๋ก ๊ฒ์ which : ๋ช ๋ น์ด์ ์คํ ํ์ผ ๊ฒฝ๋ก ํ์ธ (PATH์์ ๊ฒ์) locate : ์ธ๋ฑ์ค ๊ธฐ๋ฐ์ผ๋ก ๋น ๋ฅด๊ฒ ํ์ผ ๊ฒฝ๋ก ๊ฒ์ (`updatedb` ํ์) grep : ํ ์คํธ์์ ํน์ ๋ฌธ์์ด ๊ฒ์ ๋ฐ ์ถ๋ ฅ (์ ๊ท ํํ์ ์ง์) sed : ๋ฌธ์์ด ์นํ/์ญ์ ๋ฑ ์คํธ๋ฆผ ํธ์ง์ ์ฌ์ฉ ๐งฉ 8. ๊ธฐํ ๋ช ๋ น์ด man : ๋ช ๋ น์ด ์ฌ์ฉ๋ฒ, ์ต์ ๋ฑ์ ์ค๋ช ํ๋ ๋งค๋ด์ผ ํ์ด์ง ๋ณด๊ธฐ whatis : ๋ช ๋ น์ด์ ์งง์ ์ค๋ช ์ถ๋ ฅ (man ํ์ด์ง ์์ฝ) cal : ์๊ฐ ๋ฌ๋ ฅ ์ถ๋ ฅ (์: cal 2025 6) banner : ์ ๋ ฅํ ๋ฌธ์์ด์ ํฐ ์์คํค ์ํธ ํ ์คํธ๋ก ์ถ๋ ฅ rev : ๋ฌธ์์ด์ด๋ ํ์ผ ๋ด์ฉ์ ๋ฐ๋๋ก ์ถ๋ ฅ tar : ํ์ผ ๋ฐ ๋๋ ํ ๋ฆฌ๋ฅผ ์์นด์ด๋ธํ๊ฑฐ๋ ์์ถ ํด์ ๐ 9. Vim ํ์ ๋ฐ ๊ฒ์ ๋จ์ถํค Shift + G : ํ์ผ์ ๋ง์ง๋ง ์ค๋ก ์ด๋ /๋ฌธ์์ด : ๋ฌธ์์ด ๊ฒ์ (์: /pip) n : ๋ค์ ๊ฒ์ ๊ฒฐ๊ณผ๋ก ์ด๋ N : ์ด์ ๊ฒ์ ๊ฒฐ๊ณผ๋ก ์ด๋ ๐ก 10. ๋ฉ๋ชจ Ctrl + w + v : Vim์์ ์์ง ์ฐฝ ๋ถํ :e . : Vim์์ ํ์ผ ํ์๊ธฐ ์ด๊ธฐ locate python3 > search.txt : locate ๊ฒฐ๊ณผ๋ฅผ ํ์ผ๋ก ์ ์ฅ grep -rn 'pip' > result.txt : ํ์ฌ ๋๋ ํ ๋ฆฌ ๋ด 'pip' ํฌํจ ์ค์ ์ฌ๊ท์ ์ผ๋ก ๊ฒ์ํด ์ ์ฅ # Home Work ํฐ ๋ ธ์ด๋ง ์ํคํ ์ฒ ํฐ ๋ ธ์ด๋ง ๋ณ๋ชฉ ํ์ ์บ์ ๋ฉ๋ชจ๋ฆฌ ํ๋ฒ๋ ์ํคํ ์ฒ ํ์ดํ๋ผ์ด๋ ๋ฉํฐ์ฝ์ด ํ๋ก์ธ์ ๊ตฌ๊ธ ์ ๋ฏธ๋์ด: PPT ์ ์ ์ฉ์ด (.html)
Study
ยท 2025-06-23
2025-06-17 study
What is Lorem Ipsum? This is an example post โ<โ. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industryโs standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. ์ฐฌํ๊บผ๋๋ก Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using โContent here, content hereโ, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for โlorem ipsumโ will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of โde Finibus Bonorum et Malorumโ (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, โLorem ipsum dolor sit amet..โ, comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from โde Finibus Bonorum et Malorumโ by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. Where can I get some? There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which donโt look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnโt anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc. Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Heading 6 This is the bold text and this is the italic text and letโs do strikethrough. Donโt forget to code your dream. Fruits: ๐ ๐ Other fruits: ๐ ๐ Numbers: first second third Click here | Header | Description | | :โ: | :โ: | | Cell1 | Cell2 | | Cell1 | Cell2 | | Cell1 | Cell2 | | Cell1 | Cell2 | To print message in the console, use console.log('your message') and .. console.log('your message') ํ์
Study
ยท 2025-06-17
2025-06-16 study
What is Lorem Ipsum? This is an example post โ<โ. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industryโs standard dummy text ever since the ์์ wogjd Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Heading 6 This is the bold text and this is the italic text and letโs do strikethrough. Donโt forget to code your dream. Fruits: ๐ ๐ Other fruits: ๐ ๐ Numbers: first second third Click here | Header | Description | | :โ: | :โ: | | Cell1 | Cell2 | | Cell1 | Cell2 | | Cell1 | Cell2 | | Cell1 | Cell2 | To print message in the console, use console.log('your message') and .. console.log('your message') 1์ผ์ฐจ ๊ธฐ๋ก source /home/env/env_tools.csh custom_compiler& Custom Compiler ์คํ ๋ฐ ์ด๊ธฐํ๋ฉด ๋์ ์์ ๊ณต๊ฐ์ธ Library ์์ฑ (name, tech_library ์ ํ) schematic ์์ฑ์ ์ํด CellView ์ถ๊ฐ (schematic name: NOT) schematic ์ํ ํ ์์ ์ ์ํด Design Options setting (Editing modify) Key Roll I istance W wire L label(wire name) P pin U back C copy Q property F2 save M move E inside cell ctrl + E outside cell ๋จ์ถํค ๋ฐ ์ญํ ๊ธฐ๋ก Check and Save ํตํด Error ํ์ธ
Study
ยท 2025-06-16
[ex78] ๊ณต์ฉ์ฒด ํ์ฉ ์ : ์๋์ ๋ชจ๋ ๋ณ๊ฒฝ
๋ฌธ์ ์ค๋ช Little Big ์๋์ ๋ชจ๋๋ฅผ ๋ณ๊ฒฝํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ int Change_Endian(int a); ๏จ a ๊ฐ์ ๋ฐ์ผ๋ฉด ์๋์ ๋ชจ๋๋ฅผ ๋ฐ๊ฟ์ ๋ฆฌํด ํ๋ค #include <stdio.h> union uni { int a; char b[4]; }; int Change_Endian(int data) { char tmp; union uni x; x.a = data; // ์ฝ๋ ์์ฑ return x.a; } void main(void) { int a = 0x12345678; printf("0x%.8x => 0x%.8x\n", a, Change_Endian(a)); } ์ถ๋ ฅ ์์ 0x12345678 => 0x78563412 ์ ๋ต ์ฝ๋ #include <stdio.h> union uni { int a; char b[4]; }; int Change_Endian(int data) { char tmp; union uni x; x.a = data; // ์ฝ๋ ์์ฑ tmp = x.b[0]; x.b[0] = x.b[3]; x.b[3] = tmp; tmp = x.b[1]; x.b[1] = x.b[2]; x.b[2] = tmp; return x.a; } void main(void) { int a = 0x12345678; printf("0x%.8x => 0x%.8x\n", a, Change_Endian(a)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex77] 1๊ฐ์ ์ ์๋ฅผ ์ถ๊ฐํ๊ณ ๋ฐฐ์ด๋ฅผ ์ธ์ํ์์ค
๋ฌธ์ ์ค๋ช ์ฒซ์ค์ ์ ๋ ฅ๋๋ N(1 <= N <= 17)๊ฐ ๋งํผ ๋ค์์ค์ ์ ๋ ฅ๋๋ ์ ์(์์, 0, ์์ ๋ชจ๋ ๊ฐ๋ฅ)๋ฅผ ์ ๋ ฅ๋ฐ์ ๋ฐฐ์ด์ ์ ์ฅํ๋ค. ๋จ, ์ ๋ ฅ๋๋ ๊ฐ์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋ ์ํ๋ก ์ฃผ์ด์ง๋ฉฐ ๊ฐ์ ๊ฐ์ด ์ค๋ณต๋์ด ์์์๋ ์๋ค. ๋ง์ง๋ง ์ค์ ์ถ๊ฐํ ๊ฐ์ ์ ์๋ก ์ ๋ ฅ ๋ฐ๋๋ค. ์ถ๊ฐ๋ ๊ฐ์ด ์ค๋ฆ์ฐจ์์ด ๋๋๋ก ๋ฐฐ์ด์ ์ถ๊ฐํ ํ ๊ฒฐ๊ณผ๋ฅผ ์ธ์ํ๋ค. #include <stdio.h> int main(void) { return 0; } ์ ๋ ฅ ์์ 10 10 20 30 40 50 60 70 80 90 100 65 ์ถ๋ ฅ ์์ 10 20 30 40 50 60 65 70 80 90 100 ์ ๋ต ์ฝ๋ #include <stdio.h> int N; int A[17 + 1]; int B; void InputData(void) { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &A[i]); } scanf("%d", &B); } void Solve(void) { int i; for (i = N - 1; i >= 0; i--) { if (A[i] <= B) { A[i + 1] = B; break; } else { A[i + 1] = A[i]; } } if (i == -1) { A[0] = B; } } void OutputData(void) { for (int i = 0; i < N + 1; i++) { printf("%d ", A[i]); } } int main(void) { InputData(); Solve(); OutputData(); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex76] ์ต๋๊ฐ
๋ฌธ์ ์ค๋ช 9๊ฐ์ ์๋ก ๋ค๋ฅธ ์์ฐ์๊ฐ ์ฃผ์ด์ง ๋ ์ด๋ค ์ค ์ต๋๊ฐ์ ์ฐพ๊ณ ๊ทธ ์ต๋๊ฐ์ด ๋ช ๋ฒ์งธ ์์ธ์ง๋ฅผ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. ์๋ฅผ ๋ค์ด ์๋ก ๋ค๋ฅธ 9๊ฐ์ ์์ฐ์ 3, 29, 38, 12, 57, 74, 40, 85, 61 ์ด ์ฃผ์ด์ง๋ฉด ์ด๋ค ์ค ์ต๋๊ฐ์ 85์ด๊ณ ์ด ๊ฐ์ 8๋ฒ์งธ ์์ด๋ค. #include <stdio.h> int main(void) { return 0; } ์ ๋ ฅ ์ค๋ช ์ฒซ ์งธ ์ค๋ถํฐ ์ํ ๋ฒ์งธ ์ค๊น์ง ํ ์ค์ ํ๋์ ์์ฐ์๊ฐ ์ฃผ์ด์ง๋ค. ์ฃผ์ด์ง๋ ์์ฐ์๋ 100 ๋ณด๋ค ์๋ค. ์ถ๋ ฅ ์ค๋ช ์ฒซ์งธ ์ค์ ์ต๋๊ฐ์ ์ถ๋ ฅํ๊ณ ๋์งธ ์ค์ ์ต๋๊ฐ์ด ๋ช ๋ฒ์งธ ์์ธ์ง๋ฅผ ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์์ 3 29 38 12 57 74 40 85 61 ์ถ๋ ฅ ์์ 85 8 ์ ๋ต ์ฝ๋ #include <stdio.h> int A[9]; void InputData(void) { for (int i = 0; i < 9; i++) { scanf("%d", &A[i]); } } int Solve(void) { int maxidx = 0; for (int i = 1; i < 9; i++) { if (A[maxidx] < A[i]) maxidx = i; } return maxidx; } int main(void) { InputData(); int ans = Solve(); printf("%d\n%d\n", A[ans], ans + 1); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex75] ์ฌ๋ ์ฐพ๊ธฐ
๋ฌธ์ ์ค๋ช ์ฐพ๋ ์ฌ๋์ด ์๋ ์๋ฆฌ ๋ฒํธ๋ฅผ ๋ชจ๋ ์ธ์ํ๋ ์ฝ๋๋ฅผ ์ค๊ณํ๋ผ ์ฒซ ์ค์ ์ ์ n(1<=n<=10), ๋ค์ ์ค๋ถํฐ n๋ช ์ด๋ฆ์ด ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋์ด ์ ๋ ฅ๋๋ค ๋จ, ์ ๋ ฅ๋๋ ์ด๋ฆ์ ๊ณต๋ฐฑ์ด ์๋ ๊ฒ์ผ๋ก ๊ฐ์ ํ๋ค ๋ง์ง๋ง ์ค์ ์ค์ ์ฐพ๊ณ ์ ํ๋ ์ฌ๋์ ์ด๋ฆ์ด ์ ๋ ฅ๋๋ค N๋ช ์ฌ๋๋ค ์ค์์ ์ฐพ๋ ์ฌ๋์ ๋ชจ๋ ์ฐพ์ ๊ทธ ์๋ฆฌ ๋ฒํธ๋ฅผ ํ ์ค์ ํ๋์ฉ ์ธ์ํ๋ผ ๋จ, ์๋ฆฌ ๋ฒํธ๋ ์ฒ์ ์ฌ๋์ด 1๋ฒ๋ถํฐ ์์ํ๋ฉฐ ์ฐพ๋ ์ฌ๋์ ๋ฐ๋์ 1๋ช ์ด์ ์กด์ฌํ๋ค ๋ฌธ์์ด ๋น๊ต๋ ๊ธฐ์กด์ ์ค๊ณํ๋ ๋ฌธ์์ด ๋น๊ต ํ๋ก๊ทธ๋จ์ ์ฐธ๊ณ ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์ค๋ช ์ด๋ฆ์ ์ต๋ 20์ ์ดํ์ ์ ๋ ฅ ์์ 5 kim lee park kim song kim ์ถ๋ ฅ ์์ 1 4 ์ ๋ต ์ฝ๋ #include <stdio.h> char a[10][21]; char b[21]; int str_cmp(int n) { int i = 0; for (;;) { if (a[n][i] != b[i]) return 0; if (a[n][i] == '\0' && b[i] == '\0') return 1; i++; } #if 0 while (a[n][i] != '\0' || b[i] != '\0') { if (a[n][i] != b[i]) return 0; i++; } return 1; #endif } void main(void) { int i; int n; scanf("%d", &n); for (i = 0; i < n; i++) { scanf(" %s", &a[i][0]); } scanf(" %s", &b[0]); // scanf(" %s", b); for (i = 0; i < n; i++) { if(str_cmp(i) == 1) printf("%d\n", i + 1); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex74] ๋ค๋ฅธ ๋ชจ์์ ์ด์ฐจ์ ๋ฐฐ์ด Transpose
๋ฌธ์ ์ค๋ช 3x4 ๋ฐฐ์ด a ์ ๊ฐ๋ค์ b ๋ฐฐ์ด(4x3)์ ์ฎ๊ฒจ ์ ์ฅํ๋ผ #include <stdio.h> int a[3][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }; int b[4][3]; void transpose2(void) { // ์ฝ๋ ๊ตฌํ } void main(void) { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { printf("%d ", a[i][j]); } printf("\n"); } transpose2(); for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { printf("%d ", b[i][j]); } printf("\n"); } } ์ถ๋ ฅ ์์ 1 2 3 4 5 6 7 8 9 10 11 12 12 8 4 11 7 3 10 6 2 9 5 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int a[3][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }; int b[4][3]; void transpose2(void) { // ์ฝ๋ ๊ตฌํ int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { b[4 - 1 - j][3 - 1 - i] = a[i][j]; } } } void main(void) { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { printf("%d ", a[i][j]); } printf("\n"); } transpose2(); for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { printf("%d ", b[i][j]); } printf("\n"); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex73] ์ด์ฐจ์ ๋ฐฐ์ด Transpose
๋ฌธ์ ์ค๋ช 4x4 ๋ฐฐ์ด์ ๊ฐ๋ค์ ์ถ๋ ฅ ์์์ ๊ฐ์ด b ๋ฐฐ์ด(4x4)์ ์ฎ๊ฒจ ์ ์ฅํ๋ผ #include <stdio.h> int a[4][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13, 14, 15, 16} }; int b[4][4]; void transpose1(void) { // ์ฝ๋ ๊ตฌํ } void main(void) { int i, j; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { printf("%d ", a[i][j]); } printf("\n"); } transpose1(); for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { printf("%d ", b[i][j]); } printf("\n"); } } ์ถ๋ ฅ ์์ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 16 12 8 4 15 11 7 3 14 10 6 2 13 9 5 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int a[4][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13, 14, 15, 16} }; int b[4][4]; void transpose1(void) { // ์ฝ๋ ๊ตฌํ int i, j; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { b[4 - 1 - j][4 - 1 - i] = a[i][j]; } } } void main(void) { int i, j; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { printf("%d ", a[i][j]); } printf("\n"); } transpose1(); for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { printf("%d ", b[i][j]); } printf("\n"); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex72] ํฉ์ด ๊ฐ์ฅ ํฐ ํ๊ณผ ์ด ์ฐพ๊ธฐ
๋ฌธ์ ์ค๋ช 2์ฐจ์ ๋ฐฐ์ด์์ ํฉ์ด ๊ฐ์ฅ ํฐ ํ๊ณผ ์ด์ ๋ฒํธ๋ฅผ ๊ฐ๊ฐ ์ธ์ํ๋ผ #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์ค๋ช ์ฒซ ์ค์ ์ ์ N(2<=N<=5)์ด ๋ค์ ์ค๋ถํฐ N๊ฐ์ ์ ์๊ฐ N์ค์ ๊ฑธ์ณ์ ์ ๋ ฅ๋๋ค ์ ๋ ฅ ์์ 5 3 -5 12 3 -21 -2 11 2 -7 -11 21 -21 -34 -93 -11 9 14 39 -98 -1 -2 -2 -2 -2 -2 ์ถ๋ ฅ ์์ 2 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int N; int a[5][5]; void input(void) { int i, j; scanf("%d", &N); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { scanf("%d", &a[i][j]); } } } void main(void) { int i, j; int sum = 0, max_sum = 0; int max_r, max_c; input(); // ์ฝ๋ ๊ตฌํ for (i = 0; i < N; i++) { sum = 0; for (j = 0; j < N; j++) { sum += a[i][j]; } if (i == 0) { max_sum = sum; max_r = 0; } else if (sum > max_sum) { max_sum = sum; max_r = i; } } for (j = 0; j < N; j++) { sum = 0; for (i = 0; i < N; i++) { sum += a[i][j]; } if (j == 0) { max_sum = sum; max_c = 0; } else if (sum > max_sum) { max_sum = sum; max_c = j; } } printf("%d %d", max_r + 1, max_c + 1); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex71] ๊ฐ์ฅ ์์ ๊ฐ์ ํ๊ณผ ์ด ๋ฒํธ ์ฐพ๊ธฐ
๋ฌธ์ ์ค๋ช 2์ฐจ์ ๋ฐฐ์ด๋ก ์ ๋ ฅ ๋ฐ์ ๊ฐ์ฅ ๊ฐ์ด ์์ ํ๊ณผ ์ด ๋ฒํธ๋ฅผ ์ธ์ํ๋ผ ํ, ์ด ๋ฒํธ๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌํ์ฌ ์ธ์ํ๋ผ ๋จ, ํ๊ณผ ์ด์ ๋ฒํธ๋ 1๋ถํฐ ์์ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 1 3 5 7 9 3 5 8 9 10 2 8 7 6 3 -1 -2 -9 -10 -11 11 22 33 67 90 ์ถ๋ ฅ ์์ 4 5 ์ ๋ต ์ฝ๋ #include <stdio.h> int a[5][5]; void input(void) { int i, j; for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { scanf("%d", &a[i][j]); } } } void main(void) { int i, j; int min = 0x7fffffff; int r, c; input(); // ์ฝ๋ ๊ตฌํ for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { if (min > a[i][j]) { min = a[i][j]; r = i + 1; c = j + 1; } } } printf("%d %d", r, c); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex70] 2์ฐจ์ ๋ฐฐ์ด๋ก ์ ์ ์ ๋ ฅ ๋ฐ์์ ๊ฐ์ฅ ํฐ ๊ฐ ์ธ์
๋ฌธ์ ์ค๋ช 2์ฐจ์ ๋ฐฐ์ด์ ์ ๋ ฅ ๋ฐ์์ ๊ฐ์ฅ ๊ฐ์ด ํฐ ๊ฐ์ ์ธ์ํ๋ผ #include <stdio.h> int a[5][5]; void input(void) { int i, j; for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { scanf("%d", &a[i][j]); } } } void main(void) { int i, j; input(); // ์ฝ๋ ๊ตฌํ } ์ ๋ ฅ ์ค๋ช 5์ค์ ๊ฑธ์ณ์ ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋ ์ ์๊ฐ 5๊ฐ์ฉ ์ ๋ ฅ๋๋ค ์ถ๋ ฅ ์ค๋ช ์ด ๋ค ์ค ๊ฐ์ฅ ๊ฐ์ด ํฐ ์ ์๋ฅผ ์ธ์ํ๋ผ ์ ๋ ฅ ์์ 1 3 5 7 9 3 5 8 9 10 2 8 7 6 3 -1 -2 -9 -10 -11 11 22 33 67 90 ์ถ๋ ฅ ์์ 90 ์ ๋ต ์ฝ๋ #include <stdio.h> int a[5][5]; void input(void) { int i, j; for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { scanf("%d", &a[i][j]); } } } void main(void) { int i, j; int max; input(); // ์ฝ๋ ๊ตฌํ max = a[0][0];//์ต๋๊ฐ ์ฐพ๊ธฐ ์ํด์๋ ์ต์๊ฐ์ผ๋ก ์ค์ (0x80000000 ํน์ ์ฒซ์์๊ฐ) for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { if (max < a[i][j]) max = a[i][j]; } } printf("%d\n", max); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex69] ๋ฌธ์์ด ๋น๊ต
๋ฌธ์ ์ค๋ช ๋ฌธ์์ด 2๊ฐ๋ฅผ ์ ๋ ฅ ๋ฐ์ ๋์๋ฅผ ํ๋จํ๋ ์ฝ๋๋ฅผ ์ค๊ณํ๋ผ ๋ฌธ์์ด์ ๋์ ๋น๊ต ๊ท์ : ๊ธ์๋ค์ ์์๋๋ก ๋น๊ตํ๋ฉด์ ๋์๋ฅผ ํ๋จํ๋ค ๋์ ํ๋จ ์ : (1) KIM == KIM (2) Kim > KIM (3) abcd > abc (4) abx > abcdex ์ ๋ฌธ์์ด์ด ํฌ๋ฉด โBIGโ, ์ ๋ฌธ์์ด์ด ์์ผ๋ฉด โSMALLโ, ๊ฐ์ผ๋ฉด โSAMEโ์ ์ถ๋ ฅํ๋ค ๋จ, ์ ๋ ฅ๋๋ ๋ ๊ฐ์ ๋ฌธ์์ด์ ์๋ฌธ์ ์ต๋ 10๊ธ์์ ๊ณต๋ฐฑ์ด ์๋ ๋ฌธ์์ด์ด๋ค #include <stdio.h> void main(void) { char a[11]; char b[11]; scanf(" %s", a); scanf(" %s", b); // ์ฝ๋ ๊ตฌํ } ์ ๋ ฅ ์์ KIM KIM ์ถ๋ ฅ ์์ SAME ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { char a[11]; char b[11]; int i = 0; scanf(" %s", a); scanf(" %s", b); // ์ฝ๋ ๊ตฌํ while (a[i] != 0 || b[i] != 0) { if (a[i] > b[i]) { printf("BIG\n"); return; } if (a[i] < b[i]) { printf("SMALL\n"); return; } i++; } printf("SAME\n"); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex68] ์ฑ์ ์ฒ๋ฆฌ์ฉ ์ํผ ์ปดํจํฐ
๋ฌธ์ ์ค๋ช ์ฑ์ ์ฒ๋ฆฌ ํ๋ก๊ทธ๋จ์ ๋ฐฐ์ด์ ์ด์ฉํ์ฌ ํจ์๋ก ์ค๊ณํ๋ผ Parameter๋ int์ด๊ณ ํจ์๋ช ๊ณผ return ํ์ ์ ์์๋ก ์ง์ ํ๋ผ ์ฑ์ ๊ธฐ์ค : 100~91 -> A, 90~81 -> B, 80~71 -> C, 70~61 -> D, 60์ดํ -> F ์ฑ์ ์ ์ ๋ ฅํ๋ฉด ํ์ ์ ๋ฆฌํดํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ (๋ฌธ์ A,B,C,D,F ์ค ํ๋) ๋จ, ์ง์ ๋ ๋ฒ์์ ๊ฐ(0~100)์ด ์๋๋ฉด ๋ฐ๋์ ๋ฌธ์ X๋ฅผ ๋ฆฌํด ํ๋ผ ์ด ํ๋ก๊ทธ๋จ์ ๋ฐฐ์ด์ ์ด์ฉํ์ฌ ์ต์ ์ ํ๋ก๊ทธ๋จ์ผ๋ก ๊ตฌํํ๋ผ #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 92 ์ถ๋ ฅ ์์ A ์ ๋ต ์ฝ๋ #include <stdio.h> char s[] = "FFFFFFDCBA"; char grade(int score) { if (score < 0 || score > 100) return 'X'; return s[(score - 1) / 10]; } void main(void) { int score; scanf("%d", &score); printf("%c\n", grade(score)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex67] ์ํธํ ํ๋ก๊ทธ๋จ
๋ฌธ์ ์ค๋ช ๋ค์๊ณผ ๊ฐ์ด ์ํธํ๋ฅผ ๋ง๋ค์๋ค ์ํธํ ํ๋ก๊ทธ๋จ์ ์ค๊ณํ๋ผ ๊ท์ : A~Z ๋๋ฌธ์๋ฅผ ๋ฃ์ผ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ท์น์ผ๋ก ์ํ๋ฒณ์ด ๋ณ๊ฒฝ๋์ด ๋์จ๋ค ABCDEFGHIJKLMNOPQRSTUVWXYZ ๏จ QWERTYUIOPLKJHGFDSAZXCVBNM scanf๋ก ๊ธ์๋ฅผ ์ ๋ ฅ ๋ฐ์์ ์ํธํ๋ ๋ฌธ์๋ฅผ ์ธ์ํ๋๋ก ํ๋ค ๋จ, ๋๋ฌธ์ A~Z ์ด์ธ์ ๊ธ์๋ฅผ ์ ๋ ฅํ๋ฉด ์ข ๋ฃํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ W I L L T E K 5 ์ถ๋ ฅ ์์ V O K K Z T L ์ ๋ต ์ฝ๋ #include <stdio.h> char sec[] = "QWERTYUIOPLKJHGFDSAZXCVBNM"; void main(void) { char ch; for (;;) { scanf(" %c", &ch); if ((ch < 'A') || (ch > 'Z')) break; else printf("%c\n", sec[ch - 'A']); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex66] ํค ์์๋๋ก ์งํฉ
๋ฌธ์ ์ค๋ช ์ ๋ ฅ ๋ฐ์ 20๊ฐ์ ์ ์๋ค์ ์์ ๊ฐ์์ ํฐ ๊ฐ์ผ๋ก ์ ๋ ฌํ๋ผ ๊ฐ์ ์ ๋ ฅ ๋ฐ์ 20๊ฐ์ ๋ฐฐ์ด ์์์ ์ ์ฅ์ ํ๋ค ์ ๋ ฅ ๊ฐ์ ์์, ์์ ์๊ด ์๋ค ๋จ, 20๊ฐ ์ ์๋ ํ ์ค์ ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋์ด ์ ๋ ฅ ๋๋ค a[0]์๋ ๊ฐ์ฅ ์์ ๊ฐ, a[19]์๋ ๊ฐ์ฅ ํฐ ๊ฐ์ด ์ ์ฅ๋๋๋ก ์ ๋ ฌ(sorting) ํ๋ค ์ ๋ ฌ๋ 20๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ธ์ํ๋ค ๏จ ๊ฐ์ฅ ์์ ๊ฐ๋ถํฐ ์ธ์๊ฐ ๋๊ฒ ํ๋ค ์ด๋ป๊ฒ ๋ฐฐ์ด์ ์๋ ๊ฐ ๋ค์ ์ ๋ ฌ ํ ์ ์์๊น? a[0]๋ฅผ a[1] ~ a[19]๊ณผ ๋น๊ตํ๋ฉด์ a[0]๊ฐ ๋ ํฌ๋ฉด ์๋ฆฌ๋ฅผ ๋ฐ๊พผ๋ค ์ด๋ ๊ฒ ํ๋ฉด a[0]์ ๊ฐ์ฅ ์์ ๊ฐ์ด ๋ค์ด์ค๊ฒ ๋๊ณ ์ด๊ฒ์ a[1] ~ a[19]์ ๋ํ์ฌ ๋ฐ๋ณตํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ -38 291 92 0 239 4879287 281 -274 2989 4892 88 -1 -1 24 2982 78974983 97994 -5839 398593 398593 ์ถ๋ ฅ ์์ -5839 -274 -38 -1 -1 0 24 88 92 239 281 291 2982 2989 4892 97994 398593 398593 4879287 78974983 ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int i, j, num; int a[20]; for (i = 0; i < 20; i++) { scanf("%d", &num); a[i] = num; } for (i = 0; i < 20 - 1; i++) { for (j = i + 1; j < 20; j++) { if (a[i] > a[j]) { num = a[i]; a[i] = a[j]; a[j] = num; } } } for (i = 0; i < 20; i++) { printf("%d ", a[i]); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex65] ๋ฐค์ ๋ถ๋ฅํ์ฌ ๋ด๋ ๋ฐ๊ตฌ๋
๋ฌธ์ ์ค๋ช ์๋ฐค์ ์ข ๋ฅ๋ณ๋ก ๋ถ๋ฅํ์ฌ ๊ฐ๊ฐ์ ๋ฐ๊ตฌ๋์ ๋ด๊ณ ์ ํ๋ค ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋ 100 ์ดํ์ ์์ ์ ์ m, n(m < n)์ ์ ๋ ฅ ๋ฐ๋๋ค m ~ n ๋ฒ์์ ์ ์ค์์ ๋ค์ ์กฐ๊ฑด์ ์๋ฐค๋ค์ ์ ์ฅ ํ๋ค. (์ฃผ์) n๋ ํฌํจ ๋๋ค ๊ณต์ฃผ ์๋ฐค : 3์ ๋ฐฐ์, ์์ ์๋ฐค : 5์ ๋ฐฐ์, ์ญ์ ์ด : 3 ๋๋ 5์ ๋ฐฐ์๊ฐ ์๋ ์ ๋จ, 3๊ณผ 5์ ๊ณต๋ฐฐ์์ด๋ฉด ๊ณต์ฃผ ์๋ฐค์ด๋ฉฐ ๊ฐ์ ์ ํ ์์ด ๋ชจ๋ ๊ฐ์ ๋ค ์ ์ฅํด์ผ ํ๋ค ๋จผ์ ๊ณต์ฃผ ์๋ฐค ์์ ๊ฐ๋ค์ ์ธ์ํ๊ณ ๋ค์์ ์์ ์๋ฐค ์์ ๊ฐ๋ค์ ์ธ์ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 1 20 ์ถ๋ ฅ ์์ 6 3 6 9 12 15 18 3 5 10 20 ์ ๋ต ์ฝ๋ #include <stdio.h> int a[100 + 2]; int b[100 + 2]; void main(void) { int i, m, n; int cnt1 = 0, cnt2 = 0; scanf("%d %d", &m, &n); for (i = m; i <= n; i++) { if ((i % 3) == 0) { a[cnt1] = i; cnt1++; } else if ((i % 5) == 0) { b[cnt2] = i; cnt2++; } } printf("%d\n", cnt1); for (i = 0; i < cnt1; i++) { printf("%d ", a[i]); } if (cnt1) printf("\n"); printf("%d\n", cnt2); for (i = 0; i < cnt2; i++) { printf("%d ", b[i]); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex64] ๊ทค ํ๋งค
๋ฌธ์ ์ค๋ช ๊ทค ํ๋งค : ์ต๋ 10๊ฐ์ ๊ทค ์ค์์ ํฌ๊ธฐ๊ฐ ํฐ ๊ทค๋ง ํ๋งคํ๊ณ ์ ํ๋ค ์ฒซ ์ค์ 10์ดํ ์์ ์ ์ n, ๋ค์ ์ค์ ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋ n๊ฐ์ ์ ์๋ฅผ ๋ฐ๋๋ค ์ด n๊ฐ์ ์ ์ ์ค ํ ์ ์๋ ๊ทค(๊ฐ์ด 10 ์ด์)์ ์ ๋ณํ์ฌ ๊ฐ์์ ๊ฐ ๋ค์ ์ธ์ํ๋ผ #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 5 8 10 25 4 30 ์ถ๋ ฅ ์์ 3 10 25 30 ์ ๋ต ์ฝ๋ #include <stdio.h> int a[10]; int b[10]; void main(void) { int i, n, cnt = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] >= 10) { b[cnt] = a[i]; cnt++; } } printf("%d\n", cnt); for (i = 0; i < cnt; i++) { printf("%d ", b[i]); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex63] ์๋ฐค๋ง ๋ด๋ ๋ฐ๊ตฌ๋
๋ฌธ์ ์ค๋ช ์ต๋ 10๊ฐ๋ฅผ ๋ด์ ์ ์๋ ์๋ฐค ๋ฐ๊ตฌ๋์ ์๋ฐค๋ค์ ๋ด์ผ๋ ค ํ๋ค ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋ ์ ์ m, n์ ์ ๋ ฅ ๋ฐ๋๋ค m์ด n๋ณด๋ค ํฌ๊ฑฐ๋ m์ด ์์๊ฑฐ๋ n์ด ์์์ผ ๊ฒฝ์ฐ๋ โErrorโ๋ฅผ ์ถ๋ ฅํ๊ณ ์ข ๋ฃํ๋ค m ~ n ๋ฒ์์ ์ ์ค์์ ๋ค์ ์กฐ๊ฑด์ ์๋ฐค์ ์ ์ฅ ํ๋ค. (์ฃผ์) n๋ ํฌํจ ๋๋ค ์๋ฐค์ 3๊ณผ 5์ ๊ณตํต ๋ฐฐ์(15์ ๋ฐฐ์๋ก ์ฐพ์ง ๋ง๊ณ )์ด๊ณ ๊ทธ ์ธ๋ ๋ค ์ญ์ ์ด๋ก ํ๋จํ๋ค ๋ฐ๊ตฌ๋๋ ๋ฐฐ์ด 10๊ฐ ์ง๋ฆฌ๋ฅผ ์ค๊ณํ๊ณ ์๋ฐค์ ๋ฐ๊ฒฌํ ๋๋ง๋ค ๊ฐ์ ์ถ๊ฐํ๋ค ๋จ, ์๋ฐค์ ์ต๋ 10๊ฐ๊น์ง๋ง ๋ด์ ์ ์์ผ๋ฉฐ 10๊ฐ๋ฅผ ๋ด์ผ๋ฉด ๋ฐ๊ตฌ๋์ ์ ์ฅ์ ์ข ๋ฃํด์ผ ํ๋ค ๋ฐ๊ตฌ๋์ ๋ด๊ธด ์๋ฐค ์๋ฅผ ์ธ์ํ๊ณ ๋ค์ ์ค์ ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌํ์ฌ ๊ฐ๋ค์ ์ธ์ํ๋ค ๋จ, ์๋ฐค์ด 10๊ฐ๋ณด๋ค ์ ์ ์๋ ์์ผ๋ฉฐ ์ด ๊ฒฝ์ฐ ์ ์ฅ๋ ์๋ฐค ๊ฐ๋ค๋ง ์ธ์๋์ด์ผ ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 1 50 ์ถ๋ ฅ ์์ 3 15 30 45 ์ ๋ต ์ฝ๋ #include <stdio.h> int a[10]; void main(void) { int i, m, n, cnt = 0; scanf("%d %d", &m, &n); if (m > n || m < 0 || n < 0) { printf("Error\n"); return; } for (i = m; i <= n; i++) { if (((i % 3) == 0) && ((i % 5) == 0)) { a[cnt] = i; cnt++; if (cnt == 10) break; } } printf("%d\n", cnt); for (i = 0; i < cnt; i++) { printf("%d ", a[i]); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex62] ๋๋ฌธ์์ ๊ฐ์
๋ฌธ์ ์ค๋ช ์ฐ์์ผ๋ก ์ ๋ ฅ๋ 10๊ธ์ ์ค์์ ๋๋ฌธ์์ ๊ฐ์๋ฅผ ์ถ๋ ฅํ๋ผ #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ abCDeFGHiz ์ถ๋ ฅ ์์ 5 ์ ๋ต ์ฝ๋ #include <stdio.h> char a[11]; void main(void) { int i, cnt = 0; scanf("%s", a); for (i = 0; i < 10; i++) { if ('A' <= a[i] && a[i] <= 'Z') cnt++; } printf("%d\n", cnt); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex61] ํ์ด๊ถ ์ถ์ฒจ
๋ฌธ์ ์ค๋ช ๋ค์ ์กฐ๊ฑด์ ๋ง๋ ํ์ด๊ถ ์ถ์ฒจ ํ๋ก๊ทธ๋จ์ ์ค๊ณํ๋ผ ์ฒซ ์ค์ ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋ ์ ์ 5๊ฐ๊ฐ ์ ๋ ฅ๋๊ณ ๋ค์ ์ค์ ์ ์ 1๊ฐ๊ฐ ์ ๋ ฅ ๋๋ค ๋ ๋ฒ์งธ ์ค์์ ์ ๋ ฅ ๋ฐ์ ์ ์๊ฐ 5๊ฐ์ ์ ๋ ฅ ๋ฐ์ ์ ์๋ค ์ค ์์ผ๋ฉด โBINGOโ๋ฅผ ์ธ์ํ๋ค 5๊ฐ์ ์ ์์ ํฌํจ๋์ด ์์ง ์์ผ๋ฉด โFAILโ์ ์ธ์ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 3 7 -4 9 8 9 ์ถ๋ ฅ ์์ BINGO ์ ๋ต ์ฝ๋ #include <stdio.h> int a[5]; void main(void) { int i, num; for (i = 0; i < 5; i++) { scanf("%d", &a[i]); } scanf("%d", &num); for (i = 0; i < 5; i++) { if (a[i] == num) break; } if (i < 5) printf("BINGO\n"); else printf("FAIL\n"); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex60] ์๊ธฐ์
๋ฌธ์ ์ค๋ช ์๊ธฐ์ ํ๋ก๊ทธ๋จ์ ์ค๊ณํ๋ผ scanf๋ก ์ ์ 5๊ฐ๋ฅผ ์ ๋ ฅ ๋ฐ๋๋ค ๏จ for ๋ฃจํ ํ์ฉ 5๊ฐ๋ฅผ ๋ค ์ ๋ ฅ ๋ฐ์ผ๋ฉด ์ ๋ ฅ ๋ฐ์ 5๊ฐ์ ์๋ฅผ ๋ชจ๋ ์ธ์ํ๋ค ๏จ for ๋ฃจํ ํ์ฉ #include <stdio.h> void main(void) { int a[5]; // ์ฝ๋ ๊ตฌํ } ์ ๋ ฅ ์์ 3 7 -4 9 8 ์ถ๋ ฅ ์์ 3 7 -4 9 8 ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int a[5]; // ์ฝ๋ ๊ตฌํ int i; for (i = 0; i < 5; i++) { scanf("%d", &a[i]); } for (i = 0; i < 5; i++) { printf("%d\n", a[i]); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex59] ํผํ๊ณ ๋ผ์ค ์
๋ฌธ์ ์ค๋ช ํผํ๊ณ ๋ผ์ค๋ ๊ฐ ๋ณ์ ๊ธธ์ด๊ฐ a, b, c์ธ ์ง๊ฐ์ผ๊ฐํ์์ a2+b2=c2๊ฐ ์ฑ๋ฆฝํจ์ ๋ฐ๊ฒฌํ๊ณ ์ ์ ๋ ์นด๋ฅผ ์ธ์น๋ฉฐ ๋ชฉ์ํ์ ๋ฐ์ณ๋์๋ค. (ํ์ ์๋ฅดํค๋ฉ๋ฐ์ค๊ฐ ์ด๋ฅผ ๋ฐ๋ผ ํ๋ค๋ ์๊ธฐ๊ฐ ์์) ์์ฐ์ n์ด ์ฃผ์ด์ง๋ฉด, 1โคaโคbโคcโคn์ ๋ง์กฑํ๋ฉด์ a2+b2=c2 ๋ํ ์ฑ๋ฆฝํ๋ a, b, c๊ฐ ์ผ๋ง๋ ์๋์ง ๊ณ์ฐํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์. ๋น์ฐํ a, b, c๋ ๋ชจ๋ ์ ์์ฌ์ผ ํ๋ค. ์๋ฅผ ๋ค์ด, n์ด 15์ธ ๊ฒฝ์ฐ ์๋์ ๊ฐ์ 4๊ฐ์ง์ (a, b, c)๊ฐ ์กด์ฌํ๋ค. (3, 4, 5), (5, 12, 13), (6, 8, 10), (9, 12, 15) #include <stdio.h> int Solve(int n) { //์ฌ๊ธฐ์ ๋ถํฐ ์์ฑ } int main(void) { int n; scanf("%d", &n); printf("%d\n", Solve(n)); return 0; } ์ ๋ ฅ ์ค๋ช ์ฒซ ๋ฒ์งธ ์ค์ 300 ์ดํ์ ์์ฐ์ n์ด ์ ๋ ฅ๋๋ค. ์ถ๋ ฅ ์ค๋ช ์ฒซ ๋ฒ์งธ ์ค์ ๊ฐ๋ฅํ ํผํ๊ณ ๋ผ์ค ์์ ๊ฐ์๋ฅผ ์ถ๋ ฅํ๋ค ์ ๋ ฅ ์์ 15 ์ถ๋ ฅ ์์ 4 ์ ๋ต ์ฝ๋ #include <stdio.h> int Solve(int n) { int cnt = 0; for (int c = 2; c <= n; c++) { for (int b = 1; b < c; b++) { for (int a = 1; a <= b; a++) { if (a*a + b * b == c * c) cnt++; } } } return cnt; } int main(void) { int n; scanf("%d", &n); printf("%d\n", Solve(n)); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex58] ์ง์ฝ์์ ๊ฐ์
๋ฌธ์ ์ค๋ช 2๊ฐ์ ์ ์ N, M์ ์ ๋ ฅ ๋ฐ์ N๋ถํฐ M๊น์ง ์ฐจ๋ก๋ก ์ฆ๊ฐํ๋ฉด์ ๊ฐ ์ ์๋ค์ ์ง์ฝ์๋ฅผ ๊ตฌํ์ฌ ์ง์ฝ์์ ๊ฐ์๊ฐ ๊ฐ์ฅ ๋ง์ ์ ์๋ฅผ ์ฐพ์ ์ธ์ํ๋ค. ์ง์ฝ์๋ ์์ ์ ์ ์ธํ ์ฝ์๋ค์ ๋งํ๋ค. ์๋ก 6์ ์ฝ์๋ 1, 2, 3, 6์ธ๋ฐ ์์ ์ ์ ์ธํ ์ง์ฝ์๋ 1, 2, 3์ธ 3๊ฐ์ด๋ค. ๋จ ํจ์๋ฅผ ์ค๊ณํ์ฌ ๊ตฌํ ํ๋ค. #include <stdio.h> int Solve(int n, int m) { //์ฌ๊ธฐ์ ๋ถํฐ ์์ฑ } int main(void) { int N, M; scanf("%d %d", &N, &M); printf("%d\n", Solve(N, M)); return 0; } ์ ๋ ฅ ์ค๋ช ์์๋๋ก N๊ณผ M์ ์ ๋ ฅ ๋ฐ๋๋ค ( 1โฆ N, M โฆ 10,000 ) ์ถ๋ ฅ ์ค๋ช N๋ถํฐ M๊น์ง ์์ค์์ ์ง์ฝ์์ ๊ฐ์๊ฐ ๊ฐ์ฅ ๋ง์ ์ ์๋ฅผ ์ถ๋ ฅํ๋ค. ๋์ผํ ๊ฐ์ด ์ฌ๋ฌ ๊ฐ์ผ ๊ฒฝ์ฐ ๊ฐ์ฅ ์์ ๊ฐ์ ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์์ 1 10 ์ถ๋ ฅ ์์ 6 ์ ๋ต ์ฝ๋ #include <stdio.h> int Solve(int n, int m) { int num = 0, maxv = 0; for (int i = n; i <= m; i++) { int cnt = 0; for (int j = 1; j < i; j++) { if (i%j == 0) cnt++; } if (cnt > maxv) { maxv = cnt; num = i; } } return num; } int main(void) { int N, M; scanf("%d %d", &N, &M); printf("%d\n", Solve(N, M)); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex57] ๋๊ณ ์ฐ์ฐ
๋ฌธ์ ์ค๋ช 3๋ณด๋ค ํฐ ์์ n์ ์ ๋ ฅ ๋ฐ์ 1 + (12) + โฆ + (12โฆn) ์ ๋๊ณ๋ฅผ ์ธ์ํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. ์๋ฅผ ๋ค์ด, 5์ ๋ ฅ ์ 1 + 2 + 6 + 24+ 120 = 153์ ์ธ์ํ๋ค #include <stdio.h> int Solve(int n) { //์ฌ๊ธฐ์ ๋ถํฐ ์์ฑ } int main(void) { int n; scanf("%d", &n); printf("%d\n", Solve(n)); return 0; } ์ ๋ ฅ ์ค๋ช ์ฒซ์งธ ์ค์ ์์ ์ ์ n (1<= n <= 10)์ด ์ ๋ ฅ๋๋ค ์ถ๋ ฅ ์ค๋ช ๋๊ณ ์ฐ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค ์ ๋ ฅ ์์ 5 ์ถ๋ ฅ ์์ 153 ์ ๋ต ์ฝ๋ #include <stdio.h> int Solve(int n) { int sum = 0; for (int i = 1; i <= n; i++) { int temp = 1; for (int j = 1; j <= i; j++) { temp *= j; } sum += temp; } return sum; } int main(void) { int n; scanf("%d", &n); printf("%d\n", Solve(n)); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex56] ๋ง์ง๋ง ์ ์ ์ฐพ๊ธฐ
๋ฌธ์ ์ค๋ช 1๊ฐ์ ์ ์ n์ ์ ๋ ฅ ๋ฐ์ 1+2+3โฆ +x ์ ํฉ๊ณ๊ฐ n์ด์์ด ๋๋ฉด ๋ง์ง๋ง ๋ํ ์ x๋ฅผ ๊ตฌํ์ฌ ์ธ์ํ๋ค. ( 1+2+3โฆ +x >= n ์ ์กฐ๊ฑด์ด๋ฉฐ n์ ์์ ์ ์์ ) #include <stdio.h> int Solve(int n) { //์ฌ๊ธฐ์ ๋ถํฐ ์์ฑ } int main(void) { int n; scanf("%d", &n); printf("%d\n", Solve(n)); return 0; } ์ ๋ ฅ ์ค๋ช ์ฒซ์งธ ์ค์ ์์ ์ ์ n(1<=n<=10์ต)์ด ์ ๋ ฅ๋๋ค ์ถ๋ ฅ ์ค๋ช 1+2+3โฆ +x >= n์ ๋ง์กฑํ๋ ์ ์ผ ์์ x๋ฅผ ์ถ๋ ฅํ๋ค ์ ๋ ฅ ์์ 2500 ์ถ๋ ฅ ์์ 71 ์ ๋ต ์ฝ๋ #include <stdio.h> /* int Solve(int n) { int sum = 0, i; for (i = 1; sum < n; i++) { sum += i; } return i - 1; } */ int Solve(int n) { int sum = 0; int v = 1; for(;;) { sum += v; if(sum >= n) return v; v++; } } int main(void) { int n; scanf("%d", &n); printf("%d\n", Solve(n)); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex55] ์ํ๋ ์๋ฆฌ๊ฐ ์์๋ด๊ธฐ
๋ฌธ์ ์ค๋ช ์ฒซ ์ค์ ์์ ์ ์ ํ๋(n)๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ๋ค์ ์ค์ ์ถ์ถํ๊ณ ์ถ์ ์๋ฆฌ ๋ฒํธ(d)๋ฅผ ํ๋ ์ ๋ ฅ ๋ฐ๋๋ค. ๋จ, ํญ์ d๋ n์ ์๋ฆฌ์๋ณด๋ค ์์ ๊ฐ์ด๋ค. ์ ๋ ฅ๋ฐ์ n์ d๋ฒ์งธ ์๋ฆฌ์ ๊ฐ์ ์ธ์ํ๋ ์ฝ๋๋ฅผ ์ค๊ณํ๋ผ. d๊ฐ 0์ด๋ฉด 1์ ์๋ฆฌ, d๊ฐ 1์ด๋ฉด 10์ ์๋ฆฌ, d๊ฐ 2์ด๋ฉด 100์ ์๋ฆฌ๋ฅผ ์ถ์ถํ๋ฉด ๋๋ค. #include <stdio.h> int Solve(int n, int d) { //์ฌ๊ธฐ์ ๋ถํฐ ์์ฑ } int main(void) { int n, d; scanf("%d", &n); scanf("%d", &d); printf("%d\n", Solve(n, d)); return 0; } ์ ๋ ฅ ์์ 7825612 2 ์ถ๋ ฅ ์์ 6 ์ ๋ต ์ฝ๋ #include <stdio.h> int pow(int x) { int r = 1; for (int i = 0; i < x; i++) { r *= 10; } return r; } int Solve(int n, int d) { return n / pow(d) % 10; } int main(void) { int n, d; scanf("%d", &n); scanf("%d", &d); printf("%d\n", Solve(n, d)); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex54] ์ค๊ฐ๊ฐ ์ฐพ๊ธฐ
๋ฌธ์ ์ค๋ช ๋ ์์ ์ค๊ฐ ๊ฐ์ ์ฐพ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ ์ ์ 2๊ฐ๋ฅผ ์ ๋ ฅ ๋ฐ์ ๋ ์์ ์ค๊ฐ ๊ฐ์ ๋ฆฌํดํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ค scanf๋ก ์ ๋ ฅ ๋ฐ์ผ๋ฉฐ ์์ ์ ์๋ ์์ ์ ์๋ ๋ชจ๋ ๊ฐ๋ฅํ๋ค ๋ํ ์ ๋ ฅ๋ 2๊ฐ์ ์ ์๋ ์์ ๊ฒ๊ณผ ๋ค์ ๊ฒ ์ค ์ด๋ ๊ฒ์ด ํฐ์ง๋ ํ์ ํ ์ ์๋ค 1๊ณผ 6์ด ์ ๋ ฅ๋๋ฉด ์ค๊ฐ ๊ฐ์ด 3, 4๊ฐ ๋ค ๊ฐ๋ฅํ๋ฏ๋ก ์ด๋ฌํ ๊ฐ๋ค์ ์ ๋ ฅ๋์ง ์๋๋ค ๋ํ, ๋ ๊ฐ์ด ๊ฐ์ ๊ฐ์ด ์ ๋ ฅ๋๋ ๊ฒฝ์ฐ๋ ์๋ ๊ฒ์ผ๋ก ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 10 20 ์ถ๋ ฅ ์์ 15 ์ ๋ต ์ฝ๋ #include <stdio.h> int Search_Middle(int num1, int num2) { for(; num1 != num2; num1++, num2--); return num1; } void main(void) { int min = 0, max = 0, temp; scanf("%d %d", &min, &max); if (min > max) { temp = min; min = max; max = temp; } printf("%d\n", Search_Middle(min, max)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex53] Factorial - for ์ด์ฉ
๋ฌธ์ ์ค๋ช ํฉํ ๋ฆฌ์ผ์ ๊ตฌํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ ์ ๋ ฅ ๋ฐ์ ์์ ์ ์์ ํฉํ ๋ฆฌ์ผ(!)์ ๊ตฌํ๋ ํจ์ parameter๋ int, return์ unsigned long long int ํฉํ ๋ฆฌ์ผ: 3! = 3 * 2 * 1 for ๋ฌธ์ผ๋ก ์ค๊ณํ๋ผ #include <stdio.h> unsigned long long int Factorial(int num) { // for ์ด์ฉ ์ฝ๋ ์์ฑ } void main(void) { int value; scanf("%d", &value); printf("%llu\n", Factorial(value)); } ์ ๋ ฅ ์์ 3 ์ถ๋ ฅ ์์ 6 ์ ๋ต ์ฝ๋ #include <stdio.h> unsigned long long int Factorial(int num) { // for ์ด์ฉ ์ฝ๋ ์์ฑ int i; unsigned long long int answer = 1; for (i = 2; i <= num; i++) { answer *= i; } return answer; } void main(void) { int value; scanf("%d", &value); printf("%llu\n", Factorial(value)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex52] X๊ฐ ์ ๋ ฅ๋ ๋๊น์ง ์ ๋ ฅ ๋ฐ๊ธฐ
๋ฌธ์ ์ค๋ช ์กฐ๊ฑด์ ๋ง๋ ์ ๋ ฅ์ด ๋ค์ด์ฌ ๋ ๊น์ง ์ ๋ ฅ ๋ฐ๊ธฐ ๊ธ์๋ฅผ ์ ๋ ฅ ๋ฐ์์ ๋๋ฌธ์ โXโ๊ฐ ๋ค์ด์ค๋ฉด while์ ํ์ถํ ํ โEXITโ๋ฅผ ์ธ์ํ๋ผ ๋จ, ๋ค๋ฅธ ๊ธ์๊ฐ ์ ๋ ฅ๋๋ฉด โERRORโ๋ฅผ ์ธ์ํ๊ณ ๋ค์ ์ ๋ ฅ์ ๋ฐ์์ผ ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 3 x X ์ถ๋ ฅ ์์ ERROR ERROR EXIT ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { char x; for (;;) { scanf(" %c", &x); if (x == 'X') { printf("EXIT\n"); break; } printf("ERROR\n"); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex51] ๋ฐ์ ์๋ถํฐ 0๊น์ง 3์ ๋ฐฐ์๋ฅผ ์ธ์ํ๋ ํจ์
๋ฌธ์ ์ค๋ช ์๋ฅผ ๋ฐ์ผ๋ฉด ๊ทธ ์๋ถํฐ 0 ๊น์ง ์ ์ค์ 3์ ๋ฐฐ์๋ฅผ ๋ชจ๋ ์ธ์ํ๋ผ parameter๋ int, return์ ์์, ๋จ, ๋ฐ์ ์๋ถํฐ 0๊น์ง ์ญ์์ผ๋ก ์ธ์ํ๋ค scanf๋ก ์์ ์ ์ ํ๋๋ฅผ ์ ๋ ฅ ๋ฐ์์ ํจ์์ ์ ๋ฌํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 10 ์ถ๋ ฅ ์์ 9 6 3 0 ์ ๋ต ์ฝ๋ #include <stdio.h> void Multiple_3(int num) { while (num >= 0) { if ((num % 3) == 0) printf("%d\n", num); num--; } } void main(void) { int n; scanf("%d", &n); Multiple_3(n); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex50] 1๋ถํฐ ์ ๋ ฅ ๊ฐ๊น์ง ์์๋ฅผ ๋ชจ๋ ์ธ์ํ๋ ํจ์
๋ฌธ์ ์ค๋ช 1๋ถํฐ ์ ๋ ฅ ๋ฐ์ ๊ฐ ์ฌ์ด์ ์์๋ฅผ ๋ชจ๋ ์ธ์ํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ ๋ฆฌํด์ ์๊ณ ์ ๋ ฅ ๋ฐ์ ์ ์๊น์ง ๋ชจ๋ ์์๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌํ์ฌ ์ธ์ํ๋ ํจ์ ์์(็ด ๆธ, prime number): 1๊ณผ ์๊ธฐ ์์ ์ผ๋ก๋ง ๋๋์ด์ง๋ ์ main์์ scanf๋ก ์์ ์ ์ ํ๋๋ฅผ ์ ๋ ฅ ๋ฐ์ ํจ์์ ์ ๋ฌํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 11 ์ถ๋ ฅ ์์ 2 3 5 7 11 ์ ๋ต ์ฝ๋ #include <stdio.h> void Print_Prime(int max) { int i, j, k; for (i = 2; i <= max; i++) { k = 0; for (j = 2; j < i; j++) { if ((i % j) == 0) { k = 1; break; } } if (k == 0) { printf("%d ", i); } } } void main(void) { int n; scanf("%d", &n); Print_Prime(n); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex49] ์ง์์ 3์ ๋ฐฐ์๋ฅผ ์ ์ธํ ์ซ์ ์ธ์
๋ฌธ์ ์ค๋ช for loop๋ฅผ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ ํจ์๋ฅผ ์ค๊ณํ๋ผ 1๋ถํฐ ๋์ด์จ ์ซ์๊น์ง ๊ฐ ์ค์์ ์ง์์ 3์ ๋ฐฐ์๋ฅผ ์ ์ธํ ์ซ์๋ฅผ ๋ชจ๋ ์ธ์ํ๋ค ์ธ์๋ ํ ์ค์ 9๊ฐ์ ์ซ์๋ฅผ ์ธ์ํ๋ฉฐ ์ซ์๋น ์ต๋ 3์๋ฆฌ๋ฅผ ์ฐจ์งํ๋๋ก ํ๋ค ์ซ์ ์ธ์๋ ๋ค์ ์ฝ๋๋ฅผ ํ์ฉํ๋ค. printf(โ%3d โ, i); // ์ซ์์ ์ธ์๋ ์๋ ์ฝ๋๋ฅผ ์ด์ฉํ๋ค // printf("%3d ", i); #include <stdio.h> void func(int num) { // ์ฝ๋ ๊ตฌํ } void main(void) { int n; scanf("%d", &n); func(n); } ์ ๋ ฅ ์์ 121 ์ถ๋ ฅ ์์ 1 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55 59 61 65 67 71 73 77 79 83 85 89 91 95 97 101 103 107 109 113 115 119 121 ์ ๋ต ์ฝ๋ #include <stdio.h> void func(int num) { // ์ฝ๋ ๊ตฌํ int i; int cnt = 0; for (i = 1; i <= num; i++) { if ((i % 2 != 0) && (i % 3 != 0)) { printf("%3d ", i); cnt++; } if (cnt == 9) { printf("\n"); cnt = 0; } } } void main(void) { int n; scanf("%d", &n); func(n); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex48] j == 4 ์ผ ๋ ์์ ํ ๋ฃจํ๋ฅผ ํ์ถํ๋ ค๋ฉด?
๋ฌธ์ ์ค๋ช j == 4์ผ ๋ ์์ ํ ๋ฐ๊นฅ ๋ฃจํ๊น์ง ๋น ์ ธ ๋์ค๋ ค๋ฉด? break๋ฌธ์ ๊ฐ์ฅ ๊ฐ๊น์ด์ ์๋ for loop๋ฅผ ๋น ์ ธ ๋์จ๋ค ์ฝ๋๋ฅผ ๋ณํํ์ฌ ๋ค์ค ๋ฃจํ๋ฅผ ํ์ถํ๊ธฐ ์ํ ์ฝ๋๋ฅผ ๊ตฌํํ์ฌ ๋ณด์ #include <stdio.h> void main(void) { int i, j; for (i = 0; i < 20; i++) { for (j = 0; j < 10; i++, j++) { if (j == 4) break; printf("%d %d\n", i, j); } if (i % 3) continue; } } ์ถ๋ ฅ ์์ 0 0 1 1 2 2 3 3 ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int i, j, flag = 0; for (i = 0; i < 20; i++) { for (j = 0; j < 10; i++, j++) { if (j == 4) { flag = 1; break; } printf("%d %d\n", i, j); } if (flag != 0) break; if (i % 3) continue; } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex47] ๊ตฌ๊ตฌ๋จ ์ธ์
๋ฌธ์ ์ค๋ช 2๋จ๋ถํฐ 9๋จ ๊น์ง ๊ตฌ๊ตฌ๋จ์ ์ธ์ํ๋ ํ๋ก๊ทธ๋จ์ ์ค๊ณํ๋ผ 2๋จ ๋ถํฐ 9๋จ๊น์ง ์๋ ํ์์ผ๋ก ์ธ์ํ๋ ๊ฐ ๋จ์ ๊ตฌ๋ถ๊ธฐํธ๋ฅผ ์ถ๊ฐํ๋ค ๊ฐ ๋จ์ ๊ตฌ๋ถ์ printf(โ=======================\nโ); ๋ฅผ ์ฌ์ฉํ๋ผ // ๊ฐ ๋จ์ ๊ตฌ๋ถ์ ์๋ ์ฝ๋๋ฅผ ์ด์ฉํ๋ค // printf("=======================\n"); #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ถ๋ ฅ ์์ 2๋จ 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 ======================= 3๋จ 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 ======================= 4๋จ 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 ======================= 5๋จ 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 ======================= 6๋จ 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 ======================= 7๋จ 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 ======================= 8๋จ 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 ======================= 9๋จ 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 ======================= ์ ๋ต ์ฝ๋ // ๊ฐ ๋จ์ ๊ตฌ๋ถ์ ์๋ ์ฝ๋๋ฅผ ์ด์ฉํ๋ค // printf("=======================\n"); // ์ฝ๋ ์์ฑ #include <stdio.h> void main(void) { int i, j; for (i = 2; i <= 9; i++) { printf("%d๋จ\n", i); for (j = 1; j <= 9; j++) { printf("%d * %d = %d\n", i, j, i*j); } printf("=======================\n"); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex46] ๋ณ ์ํ๊ธฐ - ํธ๋ฆฌ๋ณ
๋ฌธ์ ์ค๋ช ์ถ๋ ฅ ์์ ๋ชจ์์ ๋ณ์ ์ธ์ํ๋ ์ฝ๋๋ฅผ ์ด์ค for๋ฃจํ๋ก ๊ตฌํํ๋ผ ๋จ, printf ํ๋ฒ์ * ๋๋ ๊ณต๋ฐฑ ๋๋ ์ค ๋ฐ๊ฟ ๋ฌธ์ ํ๋์ฉ๋ง ์ธ์ํด์ผ ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ถ๋ ฅ ์์ * *** ***** ******* ********* ์ ๋ต ์ฝ๋ #include <stdio.h> void Draw_Start5(void) { int line, blank, star; for (line = 0; line < 5; line++) { for (blank = 0; blank < 5 - line - 1; blank++) { printf(" "); } for (star = 0; star < line * 2 + 1; star++) { printf("*"); } printf("\n"); } } void main(void) { Draw_Start5(); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex45] ๋ณ ์ํ๊ธฐ - ๋ฐ๋ ์ญ์ผ๊ฐ๋ณ
๋ฌธ์ ์ค๋ช ์ถ๋ ฅ ์์ ๋ชจ์์ ๋ณ์ ์ธ์ํ๋ ์ฝ๋๋ฅผ ์ด์ค for๋ฃจํ๋ก ๊ตฌํํ๋ผ ๋จ, printf ํ๋ฒ์ * ๋๋ ๊ณต๋ฐฑ ๋๋ ์ค ๋ฐ๊ฟ ๋ฌธ์ ํ๋์ฉ๋ง ์ธ์ํด์ผ ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ถ๋ ฅ ์์ ***** **** *** ** * ์ ๋ต ์ฝ๋ #include <stdio.h> void Draw_Start4(void) { int line, blank, star; for (line = 0; line < 5; line++) { for (blank = 0; blank < line; blank++) { printf(" "); } for (star = 0; star < 5 - line; star++) { printf("*"); } printf("\n"); } } void main(void) { Draw_Start4(); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex44] ๋ณ ์ํ๊ธฐ - ๋ฐ๋ ์ผ๊ฐ๋ณ
๋ฌธ์ ์ค๋ช ์ถ๋ ฅ ์์ ๋ชจ์์ ๋ณ์ ์ธ์ํ๋ ์ฝ๋๋ฅผ ์ด์ค for๋ฃจํ๋ก ๊ตฌํํ๋ผ ๋จ, printf ํ๋ฒ์ * ๋๋ ๊ณต๋ฐฑ ๋๋ ์ค ๋ฐ๊ฟ ๋ฌธ์ ํ๋์ฉ๋ง ์ธ์ํด์ผ ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ถ๋ ฅ ์์ * ** *** **** ***** ์ ๋ต ์ฝ๋ #include <stdio.h> void Draw_Start3(void) { int line, blank, star; for (line = 0; line < 5; line++) { for (blank = 0; blank < 5 - line -1; blank++) { printf(" "); } for (star = 0; star < line + 1; star++) { printf("*"); } printf("\n"); } } void main(void) { Draw_Start3(); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex43] ๋ณ ์ํ๊ธฐ - ์ญ์ผ๊ฐ๋ณ
๋ฌธ์ ์ค๋ช ์ถ๋ ฅ ์์ ๋ชจ์์ ๋ณ์ ์ธ์ํ๋ ์ฝ๋๋ฅผ ์ด์ค for๋ฃจํ๋ก ๊ตฌํํ๋ผ ๋จ, printf ํ๋ฒ์ * ๋๋ ๊ณต๋ฐฑ ๋๋ ์ค ๋ฐ๊ฟ ๋ฌธ์ ํ๋์ฉ๋ง ์ธ์ํด์ผ ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ถ๋ ฅ ์์ ***** **** *** ** * ์ ๋ต ์ฝ๋ #include <stdio.h> void Draw_Start2(void) { int line, star; for (line = 0; line < 5; line++) { for (star = 0; star < 5 - line; star++) { printf("*"); } printf("\n"); } } void main(void) { Draw_Start2(); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex42] ๋ณ ์ํ๊ธฐ - ์ผ๊ฐ๋ณ
๋ฌธ์ ์ค๋ช ์ถ๋ ฅ ์์ ๋ชจ์์ ๋ณ์ ์ธ์ํ๋ ์ฝ๋๋ฅผ ์ด์ค for๋ฃจํ๋ก ๊ตฌํํ๋ผ ๋จ, printf ํ๋ฒ์ * ๋๋ ๊ณต๋ฐฑ ๋๋ ์ค ๋ฐ๊ฟ ๋ฌธ์ ํ๋์ฉ๋ง ์ธ์ํด์ผ ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ถ๋ ฅ ์์ * ** *** **** ***** ์ ๋ต ์ฝ๋ #include <stdio.h> void Draw_Start1(void) { int line, star; for (line = 0; line < 5; line++) { for (star = 0; star <= line; star++) { printf("*"); } printf("\n"); } } void main(void) { Draw_Start1(); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex41] ์ ์์ ์๋ฆฌ์์ ํฉ ๊ตฌํ๊ธฐ
๋ฌธ์ ์ค๋ช ์ ๋ ฅ ๋ฐ์ ์ ์ N(N >= 0)์ ๊ฐ ์๋ฆฌ์์ ํฉ์ ๊ตฌํ์ฌ ์ธ์ํ๋ผ scanf๋ก ์ต๋ 10์๋ฆฌ์ ์ ์๋ฅผ ํ๋ ์ ๋ ฅ ๋ฐ์ ๊ฐ ์๋ฆฌ์์ ํฉ์ ์ธ์ํ๋ผ ๋ง์ฝ 12345๊ฐ ์ ๋ ฅ๋์๋ค๋ฉด ๊ฐ ์๋ฆฌ์ ํฉ์ธ 15๊ฐ ์ธ์๋์ด์ผ ํ๋ค ๋ง์ฝ 0์ด ์ ๋ ฅ๋์๋ค๋ฉด ๊ฐ ์๋ฆฌ์ ํฉ์ 0์ด ๋์ด์ผ ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 12345 ์ถ๋ ฅ ์์ 15 ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int N, sum = 0; scanf("%d", &N); while (N > 0) { sum += N % 10; N /= 10; } printf("%d", sum); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex40] ์ ์์ ์๋ฆฌ์ ๊ตฌํ๊ธฐ
๋ฌธ์ ์ค๋ช ์ ๋ ฅ ๋ฐ์ ์ ์ N(N > 0)์ ์๋ฆฌ์๋ฅผ ์ธ์ํ๋ ์ฝ๋๋ฅผ ์ค๊ณํ๋ผ scanf๋ก ์ต๋ 10์๋ฆฌ์ ์ ์๋ฅผ ํ๋ ์ ๋ ฅ ๋ฐ์ ์๋ฆฌ์๋ฅผ ์ธ์ํ๋ผ ๋ง์ฝ 12345๊ฐ ์ ๋ ฅ๋์๋ค๋ฉด 5์๋ฆฌ ์์ด๋ฏ๋ก 5๋ฅผ ์ธ์ํ๋ค ๋จ, 01234์ ๊ฐ์ด 0์ผ๋ก ์์ํ๋ ์ซ์๋ ์ ๋ ฅ๋์ง ์๋ ๊ฒ์ผ๋ก ๋ณธ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 12345 ์ถ๋ ฅ ์์ 5 ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int N, cnt = 0; scanf("%d", &N); while (N > 0) { N /= 10; cnt++; } printf("%d", cnt); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex39] 3,6,9 ๊ฒ์
๋ฌธ์ ์ค๋ช ๋ค์์์ ์๊ตฌํ๋ 3,6,9 ๊ฒ์ ํ๋ก๊ทธ๋จ์ ์ค๊ณํ๋ผ ์ ์ N์ scanf๋ก ์ ๋ ฅ ๋ฐ์์ 1๋ถํฐ N๊น์ง ๋ค์๊ณผ ๊ฐ์ด ์ธ์ํ๋ผ 3์ ๋ฐฐ์์ ์ซ์์ 3์ด ๋ค์ด๊ฐ๋ ๊ฒฝ์ฐ๋ ๋ชจ๋ ๊ฐ ๋์ %๋ฅผ ์ธ์ํ๋ค 3, 6, 9, 12, 13, โฆ 23 ๋ฑ์ ๋ชจ๋ ์ซ์๋์ %๋ฅผ ์ธ์ํ๋ค N์ ๋๋ฌํ๋ฉด N๋์ โSUCCESSโ๋ฅผ ์ธ์ํ๋ผ #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์ค๋ช N <= 99 ์ ๋ ฅ ์์ 12 ์ถ๋ ฅ ์์ 1 2 % 4 5 % 7 8 % 10 11 SUCCESS ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int i, N; scanf("%d", &N); for (i = 1; i < N; i++) { if (((i % 3) == 0) || ((i % 10) == 3) || ((i / 10) == 3)) { printf("%%\n"); } else { printf("%d\n", i); } } printf("SUCCESS\n"); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex38] โDโ๋ถํฐ ์ ๋ ฅ ๋ฐ์ ๊ธ์ ์ฌ์ด๋ฅผ 2๊ฐ์ฉ ๊ฑด๋ ๋๋ฉด์ ์ธ์ํ๋ผ
๋ฌธ์ ์ค๋ช โDโ๋ถํฐ ์ ๋ ฅ ๋ฐ์ ๊ธ์ ์ฌ์ด๋ฅผ 2๊ฐ์ฉ ๊ฑด๋ ๋๋ฉด์ ์ธ์ํ๋ผ #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์ค๋ช ์ ๋ ฅ ๋ฐ์ ์ํ๋ฒณ 'D' ์ด์ 'Z' ์ดํ์ ์ ๋ ฅ ์์ H ์ถ๋ ฅ ์์ D F H ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { char i, c; scanf("%c", &c); for (i = 'D'; i <= c; i += 2) { printf("%c\n", i); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex37] โAโ๋ถํฐ ์ ๋ ฅ ๋ฐ์ ์ํ๋ฒณ๊น์ง ์ํ๋ฒณ์ ์ธ์
๋ฌธ์ ์ค๋ช โAโ๋ถํฐ ์ ๋ ฅ ๋ฐ์ ๊น์ง ์ฐ์๋ ์ํ๋ฒณ์ ์ธ์ํ๋ผ #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์ค๋ช ์ ๋ ฅ ๋ฐ์ ์ํ๋ฒณ 'A' ์ด์ 'Z' ์ดํ์ ์ ๋ ฅ ์์ G ์ถ๋ ฅ ์์ A B C D E F G ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { char i, c; scanf("%c", &c); for (i = 'A'; i <= c; i++) { printf("%c\n", i); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex36] ์ซ์๋ฅผ 7 ๋ถํฐ ์ ๋ ฅ ๋ฐ์ ๊ฐ์ ๋งํผ ์ฐ์ ์ธ์ํ๋ ์ฝ๋
๋ฌธ์ ์ค๋ช ์ซ์๋ฅผ 7 ๋ถํฐ ์ ๋ ฅ ๋ฐ์ ๊ฐ์ ๋งํผ ์ฐ์๋ ์๋ฅผ ์ธ์ํ๋ผ #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 5 ์ถ๋ ฅ ์์ 7 8 9 10 11 ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int i; int n; scanf("%d", &n); for (i = 7; i < (n + 7); i++) { printf("%d\n", i); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex35] *์ ์ ๋ ฅ ๋ฐ์ ์ ๋งํผ ์ธ์ํ๋ ์ฝ๋
๋ฌธ์ ์ค๋ช ์ ๋ ฅ ๋ฐ์ ์ ๋งํผ *์ ์ธ์ํ๋ผ(๋จ, for ๋ฃจํ 1๋ฒ์ * ํ ๊ฐ ์ธ์) #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์ค๋ช ์ ๋ ฅ ๋ฐ์ ์๋ 1 ์ด์ 30 ์ดํ์ ์ ๋ ฅ ์์ 9 ์ถ๋ ฅ ์์ ********* ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int i, n; scanf("%d", &n); for (i = 0; i < n; i++) { printf("*"); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex34] 0๋ถํฐ 20๊น์ง์ ์์์ 3์ ๋ฐฐ์๋ฅผ ์ธ์ํ๋ ์ฝ๋
๋ฌธ์ ์ค๋ช 0๋ถํฐ 20๊น์ง์ ์์์ 3์ ๋ฐฐ์๋ง ์ธ์ํ๋ ํ๋ก๊ทธ๋จ์ ์ค๊ณํ๋ผ 0๊ณผ 20๋ ๋ฐ๋ณต๋ฌธ ์กฐ๊ฑด ์ฒดํฌ ๋์์ ํฌํจ๋์ด์ผ ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ถ๋ ฅ ์์ 0 3 6 9 12 15 18 ์ ๋ต ์ฝ๋ #if 0 #include <stdio.h> void main(void) { int i; for (i = 0; i <= 20; i++) { if ((i % 3) == 0) printf("%d\n", i); } } #endif #include <stdio.h> void main(void) { int i; for (i = 0; i <= 20; i += 3) { printf("%d\n", i); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex33] 0 ๋ถํฐ 20 ๊น์ง์ ์์์ ์ง์๋ฅผ ์ธ์ํ๋ ์ฝ๋
๋ฌธ์ ์ค๋ช 0๋ถํฐ 20๊น์ง์ ์์์ ์ง์๋ง ์ธ์ํ๋ ํ๋ก๊ทธ๋จ์ ์ค๊ณํ๋ผ 0๊ณผ 20๋ ์ธ์์ ํฌํจ๋์ด์ผ ํ๋ค #include <stdio.h> void main(void) { // ์ฝ๋ ์์ฑ } ์ถ๋ ฅ ์์ 0 2 4 6 8 10 12 14 16 18 20 ์ ๋ต ์ฝ๋ #if 0 #include <stdio.h> void main(void) { int i; for (i = 0; i <= 20; i++) { if ((i % 2) == 0) printf("%d\n", i); } } #endif #include <stdio.h> void main(void) { int i; for (i = 0; i <= 20; i += 2) { printf("%d\n", i); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex32] ์ผ์ดํฌ ์๋ฅด๊ธฐ
๋ฌธ์ ์ค๋ช ์๋์ ๊ฐ์ด ๋ฅ๊ทผ ์ผ์ดํฌ๋ฅผ 2๋ฒ์ ์นผ์ง๋ก 4์กฐ๊ฐ์ผ๋ก ๋๋๋ ค๊ณ ํ๋ค. ์ผ์ดํฌ์ ๋๋ ์ ์๊ณ๋ฐฉํฅ์ผ๋ก 1~100๊น์ง ์ผ์ ํ ๊ฐ๊ฒฉ์ผ๋ก ๋ฒํธ๊ฐ ๋ถ์ฌ๋์ด ์๋ค. ์นผ๋ก ์๋ฅด๋ ค๊ณ ํ๋ ๋ถ๋ถ์ 2๊ฐ์ ์ ์๋ก ํํํ๋ค. ์นผ๋ก ์๋ฅด๋ ค๋ ๋ถ๋ถ์ด 2๊ตฐ๋ฐ ์ฃผ์ด์ง ๋ ์นผ๋ก ์๋ฆฌ๋ ๋ถ๋ถ์ด ๊ต์ฐจํ๋์ง ์ ๋ฌด๋ฅผ ํ๋จํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. ์๋ ์๋ 12 53๊ณผ 99 45๋ฅผ ์๋ฅธ ์๋ฅผ ๋ํ๋ธ๋ค. #include <stdio.h> int Solve(int A, int B, int C, int D) { // ์ฌ๊ธฐ์๋ถํฐ ์์ฑ } int main(void) { int a, b, c, d, cross; // ์ ๋ ฅ๋ฐ๋ ๋ถ๋ถ scanf("%d %d", &a, &b); scanf("%d %d", &c, &d); cross = Solve(a, b, c, d); // ์ถ๋ ฅํ๋ ๋ถ๋ถ if (cross == 1) printf("cross"); else printf("not cross"); return 0; } ์ ๋ ฅ ์ค๋ช ์ฒซ ๋ฒ์งธ ์ค์๋ ์ฒซ ๋ฒ์งธ ํ์ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ ๋ ์ ์๊ฐ ์ ๋ ฅ๋๋ค. ๋ ๋ฒ์งธ ์ค์๋ ๋ ๋ฒ์งธ ํ์ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ ๋ ์ ์๊ฐ ์ ๋ ฅ๋๋ค. ์ถ๋ ฅ ์ค๋ช ์ฃผ์ด์ง ๋ ์๋ฆฐ ๋ถ๋ถ์ด ๊ต์ฐจํ๋ค๋ฉด "cross", ๊ต์ฐจํ์ง ์๋๋ค๋ฉด "not cross"๋ฅผ ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์์ 12 53 99 45 ์ถ๋ ฅ ์์ cross ๋ถ๊ฐ์ ๋ณด [์ ๋ ฅ ์์ 2] 23 77 79 83 [์ถ๋ ฅ ์์ 2] not cross ์ ๋ต ์ฝ๋ #include <stdio.h> int Solve(int A, int B, int C, int D) { if (A > B) { int tmp = A; A = B; B = tmp; } if (C > D) { int tmp = C; C = D; D = tmp; } if (((A > C) && (D > A) && (D < B)) || ((A < C) && (B > C) && (B < D))) { return 1; } return 0; } int main(void) { int a, b, c, d, cross; // ์ ๋ ฅ๋ฐ๋ ๋ถ๋ถ scanf("%d %d", &a, &b); scanf("%d %d", &c, &d); cross = Solve(a, b, c, d); // ์ถ๋ ฅํ๋ ๋ถ๋ถ if (cross == 1) printf("cross"); else printf("not cross"); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex31] ์๊ฐ์ฐจ ๊ตฌํ๊ธฐ
๋ฌธ์ ์ค๋ช HH:MM:SS(์:๋ถ:์ด)์ ํํ๋ก ํ์ํ๋ 2๊ฐ์ ์๊ฐ์ด ์ฃผ์ด์ก์ ๋, ์๊ฐ์ฐจ๋ฅผ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ค. 2๊ฐ์ ์๊ฐ์ ์ต๋ 24์๊ฐ ์ฐจ์ด๊ฐ ๋๋ค๊ณ ๊ฐ์ ํ๋ค. #include <stdio.h> int main(void) { int h1, m1, s1, h2, m2, s2; int h, m, s; // ์ ๋ ฅ๋ฐ๋ ๋ถ๋ถ scanf("%d:%d:%d", &h1, &m1, &s1); scanf("%d:%d:%d", &h2, &m2, &s2); // ์ฌ๊ธฐ์๋ถํฐ ์์ฑ // ์ถ๋ ฅํ๋ ๋ถ๋ถ printf("%02d:%02d:%02d", h, m, s); return 0; } ์ ๋ ฅ ์ค๋ช ์ ๋ ฅ์ ๋ ๊ฐ์ ์๊ฐ์ด ์ ๋ ฅ๋๋ค. ์๊ฐ์ด ์ ๋ ฅ๋๋ ํํ๋ ๋ค์๊ณผ ๊ฐ๋ค. "HH:MM:SS" HH๋ ์, MM์ ๋ถ, SS๋ ์ด๋ฅผ ๋ปํ๋ค. ๋ง์ฝ ์๋ ๋ถ, ์ด๊ฐ ํ ์๋ฆฌ ์ซ์์ผ ๊ฒฝ์ฐ ์์ 0์ ๋ถ์ด๊ฒ ๋๋ค. (0โคHHโค23, 0โคMM, SSโค59) ์์ ์ ๋ ฅ๋ ์๊ฐ๋ณด๋ค ๋ค์ ์ ๋ ฅ๋ ์๊ฐ์ด ๋ ๋ฆ์ ์๊ฐ์ด๋ผ๊ณ ๊ฐ์ ํ๋ค ์ถ๋ ฅ ์ค๋ช ์ ๋ ฅ๋ 2๊ฐ์ ์๊ฐ์ ๋ํ ์๊ฐ์ฐจ๋ฅผ HH:MM:SS์ ํํ๋ก ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์์ 20:00:00 04:00:00 ์ถ๋ ฅ ์์ 08:00:00 ์ ๋ต ์ฝ๋ #include <stdio.h> int main(void) { int h1, m1, s1, h2, m2, s2; int h, m, s; int ss, es; // ์ ๋ ฅ๋ฐ๋ ๋ถ๋ถ scanf("%d:%d:%d", &h1, &m1, &s1); scanf("%d:%d:%d", &h2, &m2, &s2); // ์ฌ๊ธฐ์๋ถํฐ ์์ฑ ss = (h1 * 60 + m1) * 60 + s1; es = (h2 * 60 + m2) * 60 + s2; if (es > ss) ss = es - ss; else ss = 3600 * 24 - ss + es; h = ss / (60 * 60); m = ss % (60 * 60) / 60; s = ss % 60; // ์ถ๋ ฅํ๋ ๋ถ๋ถ printf("%02d:%02d:%02d", h, m, s); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex30] ๋ ์์ ๊ฑฐ๋ฆฌ
๋ฌธ์ ์ค๋ช ๋ ์ ์๋ฅผ ์ ๋ ฅ ๋ฐ์, ๋ ์์ ์ฐจ์ด๋ฅผ ์ ๋๊ฐ์ผ๋ก ์ธ์ํ๋ผ. #include <stdio.h> int Solve(int A, int B) { //์ฌ๊ธฐ์ ๋ถํฐ ์์ฑ } int main(void) { int A, B; int sol; scanf("%d %d", &A, &B); sol = Solve(A, B); printf("%d\n", sol); return 0; } ์ ๋ ฅ ์ค๋ช ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํ์ฌ ๋ ์ ์๋ฅผ ์ ๋ ฅํ๋ค. ์ถ๋ ฅ ์ค๋ช ๋ ์์ ์ฐจ์ด๋ฅผ ์ ๋๊ฐ์ผ๋ก ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์์ 15 12 ์ถ๋ ฅ ์์ 3 ์ ๋ต ์ฝ๋ #include <stdio.h> int Solve(int A, int B) { //์ฌ๊ธฐ์ ๋ถํฐ ์์ฑ int diff; if (A > B) { diff = A - B; } else { diff = B - A; } return diff; } int main(void) { int A, B; int sol; scanf("%d %d", &A, &B); sol = Solve(A, B); printf("%d\n", sol); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex29] ์ฑ์ ๊ณ์ฐ ํจ์
๋ฌธ์ ์ค๋ช ๋ค์๊ณผ ๊ฐ์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์ฑ์ ์ฒ๋ฆฌ ํจ์๋ฅผ ์ค๊ณํ๋ผ ํจ์๋ช ์ ์์๋ก ์ ํ๊ณ parameter๋ int์ด๊ณ return์ char๋ก ์ง์ ํ๋ค ์ฑ์ ๊ธฐ์ค์ ๋ค์๊ณผ ๊ฐ๋ค 100~91 => A, 90~81 => B, 80~71 => C, 70~61 => D, 60์ดํ => F ์ฑ์ ์ ์ ๋ ฅํ๋ฉด ํ์ ์ ๋ฆฌํด ํ๋ค (๋ฌธ์ A,B,C,D,F ์ค ํ๋) ๋จ, ์ ๋ ฅ๋ ๊ฐ์ด 0 ~ 100์ ๋ฒ์ด๋๋ฉด ๋ฌธ์ โXโ๋ฅผ ๋ฆฌํด ํ๋ค #include <stdio.h> char func(int score) { // ์ฝ๋ ์์ฑ } void main(void) { int score; scanf("%d", &score); printf("%c\n", func(score)); } ์ ๋ ฅ ์์ 90 ์ถ๋ ฅ ์์ B ์ ๋ต ์ฝ๋ #include <stdio.h> /* char func(int s) { if ((s < 0) || (s > 100)) { return 'X'; } switch ((s - 1) / 10) { case 0: case 1: case 2: case 3: case 4: case 5: return 'F'; case 6: return 'D'; case 7: return 'C'; case 8: return 'B'; case 9: case 10: return 'A'; } } */ char func(int s) { char * r = "FFFFFFDCBAA"; if ((s < 0) || (s > 100)) { return 'X'; } return r[(s -1) / 10]; } void main(void) { int score; scanf("%d", &score); printf("%c\n", func(score)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex28] ์ค์ ๊ฐ์ ์ ์ผ ๊ฐ๊น์ด ์ ์ ๊ฐ ๊ตฌํ๊ธฐ
๋ฌธ์ ์ค๋ช ์ ๋ ฅ๋ ์ค์ ๊ฐ์ ์ ์ผ ๊ฐ๊น์ด ์ ์ ๊ฐ ๊ตฌํ๊ธฐ ์์์ผ ๋๋ ์ ํํ ๊ฐ์ฅ ๊ฐ๊น์ด ์ ์ ๊ฐ์ด ๋์ฌ ์ ์๋๋ก ์์ฑํ๋ค (์) 3.64๋ 4์ ๊ฐ๊น์ด ์ ์์ด๊ณ -3.64์ด๋ฉด -4์ ๊ฐ๊น์ด ์ ์์ด๋ค #include <stdio.h> int func(float v) { // ์ฝ๋ ์์ฑ } void main(void) { float a; scanf("%f", &a); printf("%d\n", func(a)); } ์ ๋ ฅ ์์ 3.64 ์ถ๋ ฅ ์์ 4 ์ ๋ต ์ฝ๋ #include <stdio.h> int func(float v) { // ์ฝ๋ ์์ฑ float bias = 0.5f; return (int)(v + ((v >= 0) ? (bias) : (-bias))); } void main(void) { float a; scanf("%f", &a); printf("%d\n", func(a)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex27] L, E, W ์ฐพ๊ธฐ
๋ฌธ์ ์ค๋ช ์ ๋ ฅ๋ ๊ธ์๊ฐ โLโ, โEโ, โWโ์ด๋ฉด โ*โ ๋ฆฌํด, ๊ทธ ์ธ ๊ธ์๋ ๊ทธ๋๋ก ๋ฆฌํด #include <stdio.h> char func(char c) { // ์ฝ๋ ์์ฑ } void main(void) { char c; scanf("%c", &c); printf("%c\n", func(c)); } ์ ๋ ฅ ์์ L ์ถ๋ ฅ ์์ * ์ ๋ต ์ฝ๋ #include <stdio.h> char func(char c) { // ์ฝ๋ ์์ฑ if ((c == 'L') || (c == 'E') || (c == 'W')) return '*'; return c; } void main(void) { char c; scanf("%c", &c); printf("%c\n", func(c)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex26] ๋๋ฌธ์ ์๋ฌธ์ ๋ณ๊ฒฝ
๋ฌธ์ ์ค๋ช ์ ๋ ฅ๋ ๊ธ์๋ฅผ ๋๋ฌธ์๋ ์๋ฌธ์๋ก ์๋ฌธ์๋ ๋๋ฌธ์๋ก ๋ฐ๊ฟ ๋ฆฌํด ๊ทธ ์ธ์ ๊ธ์๋ โ0โ์ ๋ฆฌํดํ๋ค. #include <stdio.h> char func(char c) { // ์ฝ๋ ์์ฑ } void main(void) { char c; scanf("%c", &c); printf("%c\n", func(c)); } ์ ๋ ฅ ์์ F ์ถ๋ ฅ ์์ f ์ ๋ต ์ฝ๋ #include <stdio.h> char func(char c) { // ์ฝ๋ ์์ฑ if ((c >= 'A') && (c <= 'Z')) return c + ('a' - 'A'); if ((c >= 'a') && (c <= 'z')) return c - ('a' - 'A'); return '0'; } void main(void) { char c; scanf("%c", &c); printf("%c\n", func(c)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex25] ์๋ฌธ์ f ~ z ์ฐพ๊ธฐ
๋ฌธ์ ์ค๋ช ์ ๋ ฅ๋ ๋ฌธ์๊ฐ ์๋ฌธ์์ด๋ฉด์ f ~ z ์ฌ์ด ๋ฌธ์๋ฉด 1 ์๋๋ฉด 0 ๋ฆฌํด ์ ๋ ฅ์ ๋ฐ๋์ ๋๋ฌธ์ ๋๋ ์๋ฌธ์ ์ํ๋ฒณ๋ง ๋ฃ์ ์ ์๋ค๊ณ ๋ณด์ฅ ํ ์ ์๋ค ๋ฐ๋ผ์ ๋๋ฌธ์๋ ์๋ฌธ์๋ ์๋๋ฉด ๊ทธ๋ฅ 0์ ๋ฆฌํด ํ๋๋ก ํ๋ค โfโ์ โzโ ํฌํจ์. ์ฆ, โfโ๋ โzโ์ด๋ฉด 1์ ๋ฆฌํดํด์ผ ํจ #include <stdio.h> int func(char c) { // ์ฝ๋ ์์ฑ } void main(void) { char c; scanf("%c", &c); printf("%d\n", func(c)); } ์ ๋ ฅ ์์ g ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int func(char c) { // ์ฝ๋ ์์ฑ return (c >= 'f') && (c <= 'z'); } void main(void) { char c; scanf("%c", &c); printf("%d\n", func(c)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex24] ๋๋ฌธ์ ๋๋ ์๋ฌธ์ ์ฐพ๊ธฐ
๋ฌธ์ ์ค๋ช ๊ธ์๊ฐ ๋๋ฌธ์ ๋๋ ์๋ฌธ์๋ฉด 1 ์๋๋ฉด 0์ ๋ฆฌํดํ๋ ํจ์ ์ค๊ณ #include <stdio.h> int f1(char c) { // ๊ตฌํ } void main(void) { char c; scanf("%c", &c); printf("%d\n", f1(c)); } ์ ๋ ฅ ์์ C ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int f1(char c) { // ๊ตฌํ return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')); } void main(void) { char c; scanf("%c", &c); printf("%d\n", f1(c)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex23] 4~10 ์ฌ์ด ์ซ์ ์ฐพ๊ธฐ
๋ฌธ์ ์ค๋ช ์ซ์๊ฐ 4 ~ 10 ์ฌ์ด ๊ฐ์ด๋ฉด 1 ์๋๋ฉด 0์ ๋ฆฌํดํ๋ ํจ์ ์ค๊ณ 4์ 10์ ๋ฏธํฌํจ์, ์ฆ, 4๋ 10์ด๋ฉด 0์ ๋ฆฌํดํด์ผ ํจ #include <stdio.h> int f1(int num) { // ์ฝ๋ ๊ตฌํ } void main(void) { int num; scanf("%d", &num); printf("%d\n", f1(num)); } ์ ๋ ฅ ์์ 5 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int f1(int num) { // ์ฝ๋ ๊ตฌํ return (num > 4) && (num < 10); } void main(void) { int num; scanf("%d", &num); printf("%d\n", f1(num)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex22] 3์ ๋ฐฐ์ ๋๋ 5์ ๋ฐฐ์ ์ฐพ๊ธฐ
๋ฌธ์ ์ค๋ช ์ซ์๊ฐ 3 ๋๋ 5์ ๋ฐฐ์์ด๋ฉด 1 ์๋๋ฉด 0์ ๋ฆฌํดํ๋ ํจ์ ์ค๊ณ #include <stdio.h> int f1(int num) { // ์ฝ๋ ๊ตฌํ } void main(void) { int num; scanf("%d", &num); printf("%d\n", f1(num)); } ์ ๋ ฅ ์์ 15 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int f1(int num) { // ์ฝ๋ ๊ตฌํ return !(num % 3) || !(num % 5); } void main(void) { int num; scanf("%d", &num); printf("%d\n", f1(num)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex21] 3์ ๋ฐฐ์ ๊ฐ์ธ์ง ํ์ธํ๋ ํจ์
๋ฌธ์ ์ค๋ช ์ ๋ ฅ ๊ฐ์ด 3์ ๋ฐฐ์๋ฉด 1, ์๋๋ฉด 0์ ๋ฆฌํดํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ #include <stdio.h> int multiple_of_3(int num) { // ์ฝ๋ ๊ตฌํ } void main(void) { int num; scanf("%d", &num); printf("%d\n", multiple_of_3(num)); } ์ ๋ ฅ ์์ 3 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int multiple_of_3(int num) { // ์ฝ๋ ๊ตฌํ return num % 3 == 0; } void main(void) { int num; scanf("%d", &num); printf("%d\n", multiple_of_3(num)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex20] 2, 3, 5์ ๋ฐฐ์ ํ๋จํ๊ธฐ
๋ฌธ์ ์ค๋ช ์ ๋ ฅ ๋ฐ์ ๊ฐ์ด 2,3,5์ ๋ฐฐ์์ธ์ง๋ฅผ ํ๋จํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ 2์ ๋ฐฐ์๋ฉด 2, 3์ ๋ฐฐ์๋ฉด 3, 5์ ๋ฐฐ์๋ฉด 5๋ฅผ 2,3,5 ๋ฐฐ์๊ฐ ์๋๋ฉด 0์ ๋ฆฌํด ๋จ, 2,3 ๊ณต๋ฐฐ์๋ฉด 2๋ฆฌํด, 3,5 ๊ณต๋ฐฐ์๋ฉด 3๋ฆฌํด, 2,5 ๊ณต๋ฐฐ์๋ฉด 2๋ฆฌํด, 2,3,5 ๊ณต๋ฐฐ์๋ฉด 2๋ฆฌํด #include <stdio.h> int compare(int num) { // ์ฝ๋ ๊ตฌํ } void main(void) { int num; scanf("%d", &num); printf("%d\n", compare(num)); } ์ ๋ ฅ ์์ 33 ์ถ๋ ฅ ์์ 3 ์ ๋ต ์ฝ๋ #include <stdio.h> int compare(int num) { // ์ฝ๋ ๊ตฌํ if (num % 2 == 0) return 2; if (num % 3 == 0) return 3; if (num % 5 == 0) return 5; return 0; } void main(void) { int num; scanf("%d", &num); printf("%d\n", compare(num)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex19] ํ์ง์ ๋ง์ถฐ๋ผ 2
๋ฌธ์ ์ค๋ช ์์ 5-3์์ ์ง์๋ฉด 0, ํ์๋ฉด 1์ ๋ฆฌํดํ๋๋ก ํจ์๋ฅผ ์ฌ์ค๊ณํ๋ผ #include <stdio.h> int Check_Odd_Even(int num) { // ์ฝ๋ ๊ตฌํ } void main(void) { int num; scanf("%d", &num); printf("%d\n", Check_Odd_Even(num)); } ์ ๋ ฅ ์์ 3 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int Check_Odd_Even(int num) { // ์ฝ๋ ๊ตฌํ return num % 2; } void main(void) { int num; scanf("%d", &num); printf("%d\n", Check_Odd_Even(num)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex18] ํ์ง์ ๋ง์ถฐ๋ผ 1
๋ฌธ์ ์ค๋ช ์ ๋ ฅ ๋ฐ์ ์์ ํ์, ์ง์ ์ฌ๋ถ๋ฅผ ๋ฆฌํดํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ ์ ์๋ฅผ ๋ฐ์์ ์ง์๋ฉด 2, ํ์๋ฉด 1์ ๋ฆฌํดํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ #include <stdio.h> int Check_Odd_Even(int num) { // ์ฝ๋ ๊ตฌํ } void main(void) { int num; scanf("%d", &num); printf("%d\n", Check_Odd_Even(num)); } ์ ๋ ฅ ์์ 3 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int Check_Odd_Even(int num) { // ์ฝ๋ ๊ตฌํ if (num % 2 == 0) return 2; else return 1; } void main(void) { int num; scanf("%d", &num); printf("%d\n", Check_Odd_Even(num)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex17] ์ง์์ ๊ฐ์
๋ฌธ์ ์ค๋ช 4์๋ฆฌ ์์ ์ ์์ ๊ฐ ์๋ฆฌ์์ ๊ฐ์ด ์ง์์ธ ๊ฐ์๋ฅผ ๊ตฌํ๋ ํจ์๋ฅผ ์ค๊ณ ํ์์ค. ์๋ฅผ๋ค์ด, 2345๋ฉด ๊ฐ ์๋ฆฌ์์ ๊ฐ์ด 2, 3, 4, 5 ์ด๊ณ ์ด ์ค์ 2, 4๊ฐ ์ง์ ์ด๋ฏ๋ก 2๊ฐ ๋ฆฌํด๋๋ฉด ๋๋ค. ์ง์๋ 2๋ก ๋๋์ด ๋จ์ด์ง๋ ์์ด๋ค. #include <stdio.h> int Solve(int A) { //์ฌ๊ธฐ์ ๋ถํฐ ์์ฑ } int main(void) { int A; int sol; scanf("%d", &A); sol = Solve(A); printf("%d", sol); return 0; } ์ ๋ ฅ ์ค๋ช 4์๋ฆฌ ์์ ์ ์๊ฐ ์ ๋ ฅ ์ถ๋ ฅ ์ค๋ช 4์๋ฆฌ ์์ ์ ์์ ๊ฐ ์๋ฆฌ์ ๊ฐ์ด ์ง์์ธ ๊ฐ์๋ฅผ ์ถ๋ ฅ ์ ๋ ฅ ์์ 2345 ์ถ๋ ฅ ์์ 2 ์ ๋ต ์ฝ๋ #include <stdio.h> int Solve(int A) { //์ฌ๊ธฐ์ ๋ถํฐ ์์ฑ int a, b, c, d; int cnt = 4; a = A / 1000; A %= 1000; b = A / 100; A %= 100; c = A / 10; d = A % 10; cnt -= a % 2 + b % 2 + c % 2 + d % 2; return cnt; } int main(void) { int A; int sol; scanf("%d", &A); sol = Solve(A); printf("%d", sol); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex16] 3๊ฐ์ ์ ์ ์ค ํ์์ ๊ฐ์๋ฅผ ๋ฆฌํดํ๋ ํจ์ ์ค๊ณ
๋ฌธ์ ์ค๋ช ์ธ ๊ฐ์ ์์ ์ ์ A, B, C๋ฅผ ์ ๋ ฅ ๋ฐ์ ํ์์ ๊ฐ์๋ฅผ ๋ฆฌํดํ๋ ํจ์๋ฅผ ์ค๊ณ ํ์์ค. ์ธ ๊ฐ ๋ชจ๋ ํ์์ด๋ฉด 3์, ๋ ๊ฐ๋ฉด 2๋ฅผ, ํ ๊ฐ๋ฉด 1์, ์์ผ๋ฉด 0์ ๋ฆฌํดํ๋ค. #include <stdio.h> int Solve(int A, int B, int C) { int cnt = 0; // ์ฌ๊ธฐ์๋ถํฐ ์์ฑ return cnt; } int main(void) { int A, B, C; int sol; scanf("%d %d %d", &A, &B, &C); sol = Solve(A, B, C); printf("%d", sol); return 0; } ์ ๋ ฅ ์ค๋ช ์ธ ๊ฐ์ ์์ ์ ์ A, B, C๊ฐ ์ ๋ ฅ๋๋ค. ์ถ๋ ฅ ์ค๋ช ์กฐ๊ฑด์ ๋ง๊ฒ 3, 2, 1, 0์ค ํ๋๋ฅผ ์ธ์ํ๋ค. ์ ๋ ฅ ์์ 7 5 9 ์ถ๋ ฅ ์์ 3 ์ ๋ต ์ฝ๋ #include <stdio.h> int Solve(int A, int B, int C) { int cnt = 0; // ์ฌ๊ธฐ์๋ถํฐ ์์ฑ cnt = A % 2 + B % 2 + C % 2; return cnt; } int main(void) { int A, B, C; int sol; scanf("%d %d %d", &A, &B, &C); sol = Solve(A, B, C); printf("%d", sol); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex15] ASCII ์ซ์ ๋ฌธ์๋ฅผ ์ ์ ์ซ์๋ก ๋ฐํํ๋ ํจ์
๋ฌธ์ ์ค๋ช ASCII ์ซ์(โ1โ)๋ฅผ ๋ฃ์ผ๋ฉด ์ ์(1)๋ก ๋ฐ๊ฟ์ฃผ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ ๋ฌธ์๋ โ0โ ~ โ9โ ์ฌ์ด๋ก ์ ๋ ฅ๋๋ฉฐ ์ฃผ์ด์ง ํ ํ๋ฆฟ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ ํจ์๋ฅผ ์ค๊ณํ๋ค #include <stdio.h> int Change_Char_to_Int(char num) { // ์ฝ๋ ์์ฑ } void main(void) { char a; scanf("%c", &a); printf("%d\n", Change_Char_to_Int(a)); } ์ ๋ ฅ ์์ 1 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int Change_Char_to_Int(char num) { // ์ฝ๋ ์์ฑ return num - '0';//return num - 0x30; } void main(void) { char a; scanf("%c", &a); printf("%d\n", Change_Char_to_Int(a)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex14] ๋๋ฌธ์๋ฅผ ์๋ฌธ์๋ก ์ ํํ๋ ํจ์ ์ค๊ณ
๋ฌธ์ ์ค๋ช ๋๋ฌธ์๋ฅผ ๋ฃ์ผ๋ฉด ์๋ฌธ์๋ก ๋ฐ๊ฟ์ฃผ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ ๋ฌธ์๋ โAโ ~ โZโ ์ฌ์ด๋ก ์ ๋ ฅ๋๋ฉฐ ์ฃผ์ด์ง ํ ํ๋ฆฟ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ ํจ์๋ฅผ ์ค๊ณํ๋ค #include <stdio.h> char Change_Case(char upper) { // ์ฝ๋ ์์ฑ } void main(void) { char a; scanf("%c" , &a ); printf("%c => %c\n", a, Change_Case(a)); } ์ ๋ ฅ ์์ A ์ถ๋ ฅ ์์ A => a ์ ๋ต ์ฝ๋ #include <stdio.h> char Change_Case(char upper) { // ์ฝ๋ ์์ฑ return upper + ('a' - 'A'); // return upper + 0x20; } void main(void) { char a; scanf("%c" , &a ); printf("%c => %c\n", a, Change_Case(a)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex13] ์ฐจ๋ 5๋ถ์ ์ฝ๋ ์์ฑ
๋ฌธ์ ์ค๋ช ์ฐจ๋๋ฒํธ(4์๋ฆฌ ์ ์)๋ฅผ ๋ฐ์์ 5๋ถ์ ์ฝ๋๋ฅผ ๋ฆฌํดํ๋ ํจ์ ์ค๊ณ ์์ ์ ์๋ง ์ ๋ ฅ๋๋ฉฐ ์ฐจ๋๋ฒํธ ๋์๋ฆฌ๋ก ํ๋จํ์ฌ 1 ~ 5 ๊น์ง๋ก ๋ถ์ฌ๋๋ค ๋์๋ฆฌ 0, 5 -> 1 ๋ฆฌํด 1, 6 -> 2 ๋ฆฌํด 2, 7 -> 3 ๋ฆฌํด 3, 8 -> 4 ๋ฆฌํด 4, 9 -> 5 ๋ฆฌํด #include <stdio.h> int make_group(int car); void main(void) { int car; scanf("%d", &car); printf("%d\n", make_group(car)); } int make_group(int car) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 1234 ์ถ๋ ฅ ์์ 5 ์ ๋ต ์ฝ๋ #include <stdio.h> int make_group(int car); void main(void) { int car; scanf("%d", &car); printf("%d\n", make_group(car)); } int make_group(int car) { // ์ฝ๋ ์์ฑ return (car % 5) + 1; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex12] float ๊ฐ์ ๊ฐ์ฅ ๊ฐ๊น์ด ์ ์ ๊ฐ์ ๋๊ฒจ์ฃผ๋ ํจ์
๋ฌธ์ ์ค๋ช ์์ ์ค์(float)๋ฅผ ์ ๋ ฅ ๋ฐ์ ๊ฐ๊น์ด ์ ์๋ฅผ ๋ฆฌํดํ๋ ํจ์ ์ค๊ณ #include <stdio.h> int find_int(float value); void main(void) { int r; float num; scanf("%f", &num); r = find_int(num); printf("%d\n", r); } int find_int(float value) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 2.3 ์ถ๋ ฅ ์์ 2 ์ ๋ต ์ฝ๋ #include <stdio.h> int find_int(float value); void main(void) { int r; float num; scanf("%f", &num); r = find_int(num); printf("%d\n", r); } int find_int(float value) { // ์ฝ๋ ์์ฑ return (int)(value + 0.5f); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex11] ๋ฐ์ง๋ฆ์ ์ ๋ ฅํ๋ฉด ์์ ๋์ด๋ฅผ ๊ตฌํ๋ ํจ์
๋ฌธ์ ์ค๋ช ๋ฐ์ง๋ฆ(float)๋ฅผ ์ ๋ ฅ ๋ฐ์์ ์์ ๋์ด ๊ตฌํ๊ธฐ ํจ์ ์ค๊ณ #include <stdio.h> float compute_circle_area(float radius); void main(void) { float r; scanf("%f", &r); printf("%f\n", compute_circle_area(r)); } float compute_circle_area(float radius) { float pi = 3.14f; // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 3.0 ์ถ๋ ฅ ์์ 28.260000 ์ ๋ต ์ฝ๋ #include <stdio.h> float compute_circle_area(float radious); void main(void) { float r; scanf("%f", &r); printf("%f\n", compute_circle_area(r)); } float compute_circle_area(float radious) { float pi = 3.14f; // ์ฝ๋ ์์ฑ radious = pi * radious * radious; return radious; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex10] ํจ์์ ๋ถ์ ์ฐ์ต
๋ฌธ์ ์ค๋ช ์๋ฌ๊ฐ ์๋๋ก ๋ค์ ํจ์๋ค์ ์ ์ธํ๋ผ #include <stdio.h> // ์ฌ์ฉํ ํจ์๋ค์ ์ ์ธ void main(void) { printf("sqr=%d\n", sqr(3)); printf("area=%d\n", area(3, 5)); printf("arc=%f\n", compute_circle_arc(4.1f)); } int sqr(int x) { return x * x; } int area(int x, int y) { return x * y; } float compute_circle_arc(float radious) { float pi = 3.141592f; radious = 2 * radious * pi; return radious; } ์ถ๋ ฅ ์์ sqr=9 area=15 arc=25.761053 ์ ๋ต ์ฝ๋ #include <stdio.h> // ์ฌ์ฉํ ํจ์๋ค์ ์ ์ธ int sqr(int x); int area(int x, int y); float compute_circle_arc(float radious); void main(void) { printf("sqr=%d\n", sqr(3)); printf("area=%d\n", area(3, 5)); printf("arc=%f\n", compute_circle_arc(4.1f)); } int sqr(int x) { return x * x; } int area(int x, int y) { return x * y; } float compute_circle_arc(float radious) { float pi = 3.141592f; radious = 2 * radious * pi; return radious; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex09] ํจ์์ ์ ์ธ
๋ฌธ์ ์ค๋ช ํ๋ก๊ทธ๋จ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์๋๋ก ํจ์๋ฅผ ์ ์ธํ๋ผ #include <stdio.h> // ์ฌ๊ธฐ์ ํจ์ ์ ์ธ void main(void) { weight(50); } void weight(int w) { printf("Weight = %d Kg\n", w); } ์ถ๋ ฅ ์์ Weight = 50 Kg ์ ๋ต ์ฝ๋ #include <stdio.h> // ์ฌ๊ธฐ์ ํจ์ ์ ์ธ void weight(int w); void main(void) { weight(50); } void weight(int w) { printf("Weight = %d Kg\n", w); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex08] 100์ ๋ฏธ๋ง์ ๋ฐ์ฌ๋ฆผ ํ๋ ์ฝ๋๋ฅผ ๊ตฌํ
๋ฌธ์ ์ค๋ช ์ ๋ ฅ๋ฐ์ 4์๋ฆฌ ์ด์ ์ ์์์ 100์ ๋ฏธ๋ง์ ๋ฐ์ฌ๋ฆผ ํ๋ ์ฝ๋๋ฅผ ๊ตฌํํ์์ค ์ฆ, 50์ ์ด์์ด๋ฉด ์ฌ๋ฆผ์ฒ๋ฆฌํ๊ณ 49์ ์ดํ์ด๋ฉด ๋ด๋ฆผ์ฒ๋ฆฌ๋ฅผ ํ๋ค ์๋ฅผ๋ค์ด, 1249์์ด๋ฉด 100์ ๋ฏธ๋ง์ด 49์์ด๋ฏ๋ก 1200์์ด ๋๋ค. 4350์์ด๋ฉด 100์ ๋ฏธ๋ง์ด 50์์ด๋ฏ๋ก 4400์์ด ๋๋ค #include <stdio.h> int main(void) { int A; scanf("%d", &A); printf("%d\n", A); return 0; } ์ ๋ ฅ ์ค๋ช 4์๋ฆฌ ์ด์์ ์ ์ ์ ๋ ฅ ์ถ๋ ฅ ์ค๋ช 100์ ๋ฏธ๋ง์ ๋ฐ์ฌ๋ฆผ ์ฒ๋ฆฌํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅ ์ ๋ ฅ ์์ 1249 ์ถ๋ ฅ ์์ 1200 ์ ๋ต ์ฝ๋ #include <stdio.h> int main(void) { int A; scanf("%d", &A); A = (A + 50) / 100 * 100; printf("%d\n", A); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex07] 100์๋ฆฌ ๊ฐ์ 0์ผ๋ก ๋ง๋ค๊ธฐ
๋ฌธ์ ์ค๋ช 3~9์๋ฆฌ ์ ์๋ฅผ ์ ๋ ฅ๋ฐ์ 100์๋ฆฌ ๊ฐ์ 0์ผ๋ก ๋ง๋๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ผ ์๋ฅผ๋ค์ด 1234๊ฐ ์ ๋ ฅ๋๋ฉด 100์๋ฆฌ 2๋ฅผ 0์ผ๋ก ๋ง๋ค์ด์ 1034๊ฐ ๋๊ฒ ํ์์ค #include <stdio.h> int main(void) { int A; scanf("%d", &A); // ์ฝ๋ ์์ฑ printf("%d\n", A); return 0; } ์ ๋ ฅ ์ค๋ช 3~9์๋ฆฌ ์ ์ ์ ๋ ฅ ์ถ๋ ฅ ์ค๋ช 100์๋ฆฌ ๊ฐ์ 0์ผ๋ก ๋ง๋ค์ด์ ์ถ๋ ฅ ์ ๋ ฅ ์์ 1234 ์ถ๋ ฅ ์์ 1034 ์ ๋ต ์ฝ๋ #include <stdio.h> int main(void) { int A; scanf("%d", &A); A = A - (A / 100 % 10 * 100); printf("%d\n", A); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex06] ์ ์ 3๊ฐ ํฉ๊ณผ ํ๊ท ๊ตฌํ๊ธฐ
๋ฌธ์ ์ค๋ช ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋ ์ ์ 3๊ฐ๋ฅผ ์ ๋ ฅ ๋ฐ์ ํฉ๊ณผ ํ๊ท ์ ์ธ์ํ๋ผ #include<stdio.h> void main(void) { int a, b, c, sum; float avg; // ์ฝ๋ ์์ฑ printf("%d, %f\n", sum, avg); } ์ ๋ ฅ ์์ 10 20 30 ์ถ๋ ฅ ์์ 60, 20.000000 ์ ๋ต ์ฝ๋ #include<stdio.h> void main(void) { int a, b, c, sum; float avg; // ์ฝ๋ ์์ฑ scanf("%d %d %d", &a, &b, &c); sum = a + b + c; avg = sum / 3.0f; printf("%d, %f\n", sum, avg); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex05] 16์ง์์ ์๋ฆฌ์ ๋ถ๋ฆฌ
๋ฌธ์ ์ค๋ช 4์๋ฆฌ 16์ง์ ์๋ฅผ ์ ๋ ฅ ๋ฐ์ ๊ฐ ์๋ฆฌ ๊ฐ์ ์ธ์ํ๋ผ #include<stdio.h> void main(void) { unsigned int x; unsigned int x4, x3, x2, x1; scanf("%x", &x); // ์ฝ๋ ์์ฑ printf("%X, %X, %X, %X", x4, x3, x2, x1); } ์ ๋ ฅ ์์ AB9D ์ถ๋ ฅ ์์ A, B, 9, D ์ ๋ต ์ฝ๋ #include<stdio.h> void main(void) { unsigned int x; unsigned int x4, x3, x2, x1; scanf("%x", &x); // ์ฝ๋ ์์ฑ x4 = (x / (16 * 16 * 16)); x3 = ((x % (16 * 16 * 16)) / (16 * 16)); x2 = ((x % (16 * 16)) / 16); x1 = (x % 16); printf("%X, %X, %X, %X", x4, x3, x2, x1); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex04] ๊ฐ๊ฒฉ ์ ์ฌ ํ๋งค
๋ฌธ์ ์ค๋ช ๋ณ์ p์ ์ ์ฅ๋ ๊ฐ์ 1000์ ๋ฏธ๋ง์ ์ ์ฌํ๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ผ #include <stdio.h> void main(void) { int p = 123456; p = printf("%d\n", p); } ์ถ๋ ฅ ์์ 123000 ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int p = 123456; p = p - (p % 1000); printf("%d\n", p); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex03] /, % ์ฐ์ฐ์์ ํ์ฉ => 10์ง์ ์๋ฆฌ์ ๋ถ๋ฆฌ
๋ฌธ์ ์ค๋ช 4์๋ฆฌ ์ ์์ ๊ฐ ์๋ฆฌ ๊ฐ์ ์ถ์ถํ์ฌ a4, a3, a2, a1์ ์ ์ฅํ๋ผ #include <stdio.h> void main(void) { int a = 2345; int a4, a3, a2, a1; a4 = a3 = a2 = a1 = printf("1000์๋ฆฌ=%d, 100์๋ฆฌ=%d, 10์๋ฆฌ=%d, 1์๋ฆฌ=%d\n", a4, a3, a2, a1); } ์ถ๋ ฅ ์์ 1000์๋ฆฌ=2, 100์๋ฆฌ=3, 10์๋ฆฌ=4, 1์๋ฆฌ=5 ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int a = 2345; int a4, a3, a2, a1; // 1์ ์๋ฆฌ๋ถํฐ ์ฝ๋ฉ a4 = (a / 1000); a3 = (a / 100) % 10; a2 = (a / 10) % 10; a1 = a % 10; printf("1000์๋ฆฌ=%d, 100์๋ฆฌ=%d, 10์๋ฆฌ=%d, 1์๋ฆฌ=%d\n", a4, a3, a2, a1); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex02] ๋ค์ํ ์ ๋ ฅ ๋ฐ ์ถ๋ ฅ ์ฐ์ต
๋ฌธ์ ์ค๋ช ๋ค์ ์ฝ๋์ ๋ค์ด๊ฐ ์ ๋ ฅ ๋ฐ ์ธ์ ์ฝ๋๋ฅผ ์ค๊ณํ๋ผ ์ ๋ ฅ ์์์ ๊ฐ์ด ์ ๋ ฅ ์ ์ถ๋ ฅ ์์์ ๊ฐ์ด ์ธ์๊ฐ ๋์ด์ผ ํ๋ค [์ฃผ์] printf ์ฌ์ฉ์ ์ถ๋ ฅ ์์์ ์์ ํ ๋์ผํ๋๋ก ๊ณต๋ฐฑ, โ,โ ๋ฑ์ ์ธ์์ ์ฃผ์ํ์ฌ์ผ ํ๋ค #include <stdio.h> void main(void) { char name[31]; int age; float height; char blood_type; char nationality[11]; // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ Hong Gil Dong 100 182.9 A KOR ์ถ๋ ฅ ์์ Hong Gil Dong, 100, 182.899994 A, KOR ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { char name[31]; int age; float height; char blood_type; char nationality[11]; // ์ฝ๋ ์์ฑ gets(name); scanf("%d %f %c %s", &age, &height, &blood_type, nationality); printf("%s, %d, %f\n", name, age, height); printf("%c, %s", blood_type, nationality); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex01] printf ํจ์ ์ฐ์ต
๋ฌธ์ ์ค๋ช printf ํจ์๋ฅผ ์ด์ฉํ์ฌ ์ถ๋ ฅ ์์์ ๊ฐ์ด ์ธ์๋๋๋ก ์ฝ๋๋ฅผ ์ค๊ณํ๋ผ #include <stdio.h> void main(void) { } ์ถ๋ ฅ ์์ Hello C World! Welcome!! ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { printf("Hello C World!\n"); printf("Welcome!!\n"); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex64] ๋ฐฐ์ด์์ ํจํด ๊ฐ์ ์ฐพ๊ธฐ (10์ )
๋ฌธ์ ์ค๋ช 20๊ธ์ ์ดํ์ ๋ฌธ์์ด์ด ์ ๋ ฅ๋์ด s1 ๋ฐฐ์ด์ ์ ์ฅ๋๊ณ 5๊ธ์ ์ดํ์ ๋ฌธ์์ด์ด ์ ๋ ฅ๋์ด s2 ๋ฐฐ์ด์ ์ ์ฅ๋๋ค. ๋ฌธ์์ด s1์์ s2์ ๊ฐ์ ํจํด์ ๋ฌธ์์ด์ด ๋ช๊ฐ๊ฐ ์กด์ฌํ๋์ง ๊ฐ์๋ฅผ ์ธ์ํ๋ผ ๋ง์ฝ s1์ด abababac ์ด๊ณ s2๊ฐ aba์ผ ๊ฒฝ์ฐ ์ด 3๋ฒ aba ํจํด์ด ์กด์ฌํ๋ฏ๋ก ๋ต์ 3์ด ๋๋ค. ์๋ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ ๋ณ์, ํจ์๋ ์์๋ก ์ถ๊ฐํ์ฌ ์ฌ์ฉํ๋ฉด ๋๋ค. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { char s1[21]; char s2[6]; scanf(" %s", s1); scanf(" %s", s2); // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ abababac aba ์ถ๋ ฅ ์์ 3 ์ ๋ต ์ฝ๋ #if 0 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { char s1[21]; char s2[6]; scanf(" %s", s1); scanf(" %s", s2); int i, j, cnt = 0; for (i = 0; s1[i] != '\0'; i++) { for (j = 0; ; j++) { if (s2[j] == '\0') { cnt++; break; } else if ((s1[j + i] != s2[j]) || (s1[j + i] == '\0')) { break; } } } printf("%d\n", cnt); } #endif /* ํจ์๋ก ์ค๊ณ */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int str_cmp(char * p, char * q) { int i; for (i = 0; ; i++) { if (q[i] == '\0') return 1; if ((p[i] != q[i]) || (p[i] == '\0')) return 0; } } void main(void) { char s1[21]; char s2[6]; scanf(" %s", s1); scanf(" %s", s2); int i, cnt = 0; for (i = 0; s1[i] != '\0'; i++) { cnt += str_cmp(&s1[i], s2); } printf("%d\n", cnt); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex63] ๊ฐ์ ์๋ ์ฉ๋ฉ ๋ชปํด (10์ )
๋ฌธ์ ์ค๋ช 10๊ฐ์ ์ ์(์์, 0, ์์ ๊ฐ๋ฅ)๊ฐ ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋์ด num ๋ฐฐ์ด์ ์ ์ฅ๋๋ค. 10๊ฐ์ ๊ฐ์ด ๋ชจ๋ ๋ค๋ฅผ ๊ฒฝ์ฐ YES, ํ๋๋ผ๋ ๊ฐ์ ๊ฐ์ด ์์ผ๋ฉด NO๋ฅผ ์ธ์ํ๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ผ. ์๋ ์ฝ๋๋ฅผ ์ด์ฉํ๋ ์ฃผ์ด์ง ์ฝ๋๋ ๋ณ๊ฒฝํ๋ฉด ์๋๋ฉฐ ํ์ํ ๋ณ์, ํจ์๋ ์์๋ก ์ถ๊ฐ ๊ฐ๋ฅํ๋ค. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { int num[10]; int i; for(i = 0; i < 10; i++) { scanf("%d", &num[i]); } // ์ฝ๋ ๊ตฌํ } ์ ๋ ฅ ์์ 1 9 2 8 7 3 66 55 44 100 ์ถ๋ ฅ ์์ YES ์ ๋ต ์ฝ๋ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { int num[10]; int i; for(i = 0; i < 10; i++) { scanf("%d", &num[i]); } int j, r = 0; for(i = 0; i < 10; i++) { for(j = i + 1; j < 10; j++) { if(num[i] == num[j]) { printf("NO\n"); return; } } } printf("YES\n"); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex62] ๋ฐฐ์ด์์ ํฐ ๊ฐ 2๊ฐ ์ฐพ๊ธฐ (10์ )
๋ฌธ์ ์ค๋ช ์ ์ 10๊ฐ๊ฐ ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ํ ์ค์ ์ ๋ ฅ๋์ด ๋ฐฐ์ด a์ ์ ์ฅ๋๋ค. ์ด ๊ฐ๋ค ์ค ๊ฐ์ฅ ํฐ ๊ฐ๊ณผ ๋ ๋ฒ์งธ๋ก ํฐ ๊ฐ์ ์ฐจ๋ก๋๋ก ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ํ ์ค์ ์ธ์ํ๋ผ ์๋ฅผ ๋ค์ด 1 23 435 20 -29 20 1234 -45 24 100 ์ด ์ ๋ ฅ๋๋ค๋ฉด ๊ฐ์ฅ ํฐ๊ฐ์ด 1234์ด๊ณ ๊ทธ ๋ค์์ด 435์ด๋ฏ๋ก 1234 435๊ฐ ์ธ์๋์ด์ผ ํ๋ค. ๋ง์ฝ ๊ฐ์ฅ ํฐ ๊ฐ๊ณผ ๊ฐ์ ๊ฐ์ด ์ฌ๋ฟ์ผ ๊ฒฝ์ฐ ๊ฐ์ฅ ํฐ ๊ฐ๊ณผ ๋ ๋ฒ์งธ๋ก ํฐ ๊ฐ์ ๊ฐ์ ๊ฐ์ด ๋๋ค. ์๋ฅผ ๋ฑ์ด 1 2 3 4 9 9 8 7 6 5 ์ผ ๊ฒฝ์ฐ 1๋ฑ, 2๋ฑ์ ๋ชจ๋ 9 ์ด๋ฏ๋ก 9 9๊ฐ ์ธ์๋์ด์ผ ํ๋ค. ๋ค์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ ํ์ํ ๋ณ์๋ ์์๋ก ์ ์ธํ์ฌ ์ฌ์ฉํ๋ค. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { int a[10]; int i; for(i = 0; i < 10; i++) { scanf("%d", &a[i]); } // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 1 23 435 20 -29 20 1234 -45 24 100 ์ถ๋ ฅ ์์ 1234 435 ์ ๋ต ์ฝ๋ #if 0 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { int a[10]; int i; for(i = 0; i < 10; i++) { scanf("%d", &a[i]); } int n; for(n = 0; n < 2; n++) { int r = 0; int t; for(i = 1; i <= 9 - n; i++) { if(a[i] > a[r]) { r = i; } } t = a[i-1]; a[i-1] = a[r]; a[r] = t; } printf("%d %d", a[9], a[8]); } #endif #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { int a[10]; int i; for(i = 0; i < 10; i++) { scanf("%d", &a[i]); } int r[2] = {0,1}; if(a[r[0]] < a[r[1]]) { r[0] = 1; r[1] = 0; } for(i = 1; i < 10; i++) { if(a[i] > a[r[1]]) { r[1] = i; if(a[i] > a[r[0]]) { r[1] = r[0]; r[0] = i; } } } printf("%d %d", a[r[0]], a[r[1]]); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex61] ์ํ๋ฒณ ์ถ๋ฆฌ๊ธฐ (10์ )
๋ฌธ์ ์ค๋ช in ๋ฐฐ์ด์ ์ต๋ 10๊ธ์(๋๋ฌธ์ ํฌํจ 11๊ธ์)์ ๋ฌธ์์ด์ด ์ ๋ ฅ๋๋ค. ์ด ๋ฌธ์์ด์์ ์ํ๋ฒณ ๋๋ฌธ์ ๋๋ ์๋ฌธ์๋ง ์์๋๋ก ๋ชจ์์ ๋ฐฐ์ด out์ ์ ์ฅํ๊ณ ๊ทธ ๋ด์ฉ์ ์ธ์ํ๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ผ. ์๋ ์ฃผ์ด์ง ์ ๋ ฅ, ์ถ๋ ฅ ํจ์๋ ์ ๋ ์์ ํ ์ ์๋ค. (์ฃผ์) %s ์ต์ ์ ๋ฌธ์์ด์ ์ธ์ํ๋ฉฐ ๋์ ๋๋ฌธ์๊ฐ ์์ด์ผ ์ธ์๋ฅผ ์ข ๋ฃํ๋ค. ๋ฐ๋ผ์ out ๋ฐฐ์ด์๋ ์ ๋ณ๋ ๊ธ์๋ค ๋ค์ ๋ฐ๋์ ๋๋ฌธ์๊ฐ ์์ด์ผ ํ๋ค. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { char in[11]; char out[11]; scanf("%s", in); // ์ฝ๋ ๊ตฌํ (ํ์์ ๋ณ์ ์์ ๋กญ๊ฒ ์ ์ธํ์ฌ ์ฌ์ฉ ๊ฐ๋ฅ) printf("%s\n", out); } ์ ๋ ฅ ์์ dkIWO23JI# ์ถ๋ ฅ ์์ dkIWOJI ์ ๋ต ์ฝ๋ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { char in[11]; char out[11]; scanf("%s", in); int i, j = 0; for(i = 0; i < sizeof(in)/sizeof(in[0]); i++) { if(in[i] == '\0') { out[j] = '\0'; break; } if(((in[i] >= 'A') && (in[i] <= 'Z')) || ((in[i] >= 'a') && (in[i] <= 'z'))) { out[j++] = in[i]; } } printf("%s\n", out); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex60] 8๋นํธ ํต์ ๋ฐ์ดํฐ์ Even Parity ์ถ๊ฐํ๊ธฐ (10์ )
๋ฌธ์ ์ค๋ช ์ซ์ 1 ๋๋ 0 ์ด ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋์ด 8๊ฐ๊ฐ data ๋ฐฐ์ด์ ์ ๋ ฅ๋๋ค. ์ ๋ ฅ๋ ๊ฐ๋ค ์ค 1์ ๊ฐ์๊ฐ ํ์๊ฐ์ด๋ฉด data ๋ฐฐ์ด์ ๋ง์ง๋ง์ 1์ 1์ ๊ฐ์๊ฐ ์ง์๊ฐ์ด๋ฉด data ๋ฐฐ์ด์ ์ ์ผ ๋ง์ง๋ง์ 0์ ์ถ๊ฐํ๋ค. ์ด๋ ๊ฒ ๋ฐ์ดํฐ์ ์ ์ฒด 1์ ๊ฐ์๊ฐ ์ง์๊ฐ ๋๊ฒ ํ๋ ๋ฐฉ์์ even parity ๋ฐฉ์์ด๋ผ ๋ถ๋ฅธ๋ค. ์๋ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ ์ ์ถ๋ ฅ ์ฝ๋๋ ์ ๋ ์์ ํ ์ ์์ผ๋ฉฐ ํ์ํ ๋ณ์๋ ์ ์ธํ์ฌ ์ฌ์ฉํ๋ฉด ๋๋ค. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { int data[9]; int i; for(i = 0; i < 8; i++) { scanf("%d", &data[i]); } // ์ฝ๋ ๊ตฌํ for(i = 0; i < 9; i++) { printf("%d ", data[i]); } } ์ ๋ ฅ ์์ 1 0 1 1 1 1 0 1 ์ถ๋ ฅ ์์ 1 0 1 1 1 1 0 1 0 ์ ๋ต ์ฝ๋ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { int data[9]; int i; for(i = 0; i < 8; i++) { scanf("%d", &data[i]); } data[8] = 0; for(i = 0; i < 8; i++) { data[8] ^= data[i]; } for(i = 0; i < 9; i++) { printf("%d ", data[i]); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex59] ๊ฐ์ ๋ฐ์ ๋ณด ํ์ ํ๊ธฐ (10์ )
๋ฌธ์ ์ค๋ช a์ b ๋ ์ฌ๋์ด ๊ฐ์, ๋ฐ์, ๋ณด ๊ฒ์์ ํ๋ ค๊ณ ํ๋ค. ๊ฐ์๋ 0, ๋ฐ์๋ 1, ๋ณด๋ 2๋ฅผ ์ ๋ ฅํ๊ฒ ๋๋ฉฐ ์น๋ถ ๊ฒฐ๊ณผ๋ a๊ฐ ์ด๊ธฐ๋ฉด a, b๊ฐ ์ด๊ธฐ๋ฉด b ๋น๊ธฐ๋ฉด =์ ์ธ์ํ๋ฉด ๋๋ค. ์ฆ, 0 0, 1 1, 2 2์ ๊ฐ์ด ๊ฐ์ ๊ฒ์ ๋ด๋ฉด ๋น๊ธฐ๋ฏ๋ก =๊ฐ ๋์์ผ ํ๊ณ 0 1 ์ด๋ ๊ฒ ๋๋ฉด a๋ ๊ฐ์ b๋ ๋ฐ์์ด๋ฏ๋ก b๊ฐ ์ด๊ฒจ์ b๊ฐ ์ธ์๋์ด์ผ ํ๋ค. ์๋ ์ฝ๋๋ฅผ ์ด์ฉํ์ฌ ์์ฑํ๋ ํ์ํ ๋ณ์ ๋ฑ์ ์์ ๋กญ๊ฒ ์ถ๊ฐ๊ฐ ๊ฐ๋ฅํ๋ค. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { int a, b; scanf("%d %d", &a, &b); // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 0 1 ์ถ๋ ฅ ์์ b ์ ๋ต ์ฝ๋ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> char r[3][3] = { {'=','b','a'},{'a','=','b'},{'b','a','='} }; void main(void) { int a, b; scanf("%d %d", &a, &b); printf("%c\n", r[a][b]); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex58] ๋ง๊ธฐ ์ ๊ธ ๊ณ์ฐ ํจ์ (10์ )
๋ฌธ์ ์ค๋ช ๋ค์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์ ๊ธ ๋ง๊ธฐ์ก์ ๊ตฌํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ (1) ์๊ธ(total), ์ผ ์ด์์ก(rate), ๊ฐ์ ์ผ์(day)๋ฅผ ์ ๋ฌ ๋ฐ๋๋ค. (2) ๊ฐ์ ์ผ(day) 1์ผ๋ง๋ค ์ ์ด์์ก(rate) ๋งํผ ์๊ธ์ ์ด์๊ฐ ๋ถ๋๋ค. (3) ๋ค๋ง, ์ต์ข ์ง๊ธ์ก์ 100์ ๋ฏธ๋ง ๊ธ์ก์ ์ ์ฌํ์ฌ ๊ฒฐ์ ํ๋ค. ์๋ฅผ ๋ค์ด total์ด 12,310์, rate๊ฐ 40์, day๊ฐ 9์ผ์ด๋ผ๋ฉด ์ด ์ด์๋ 360์์ด๋ฏ๋ก ์ด ๊ธ์ก์ 12,670์์ด๋ค. ๊ทธ๋ฌ๋ 100์ ๋ฏธ๋ง ์ ์ฌํ๊ฒ ๋๋ฏ๋ก ์ต์ข ์ง๊ธ๊ธ์ 12,600์์ด ๋๋ค. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int money(int total, int rate, int day) { // ์ฝ๋ ์์ฑ } void main(void) { int t, r, d; scanf("%d %d %d", &t, &r, &d); printf("%d\n", money(t, r, d)); } ์ ๋ ฅ ์์ 12310 40 9 ์ถ๋ ฅ ์์ 12600 ์ ๋ต ์ฝ๋ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int money(int total, int rate, int day) { total = (total + rate * day); return total - total % 100; } void main(void) { int t, r, d; scanf("%d %d %d", &t, &r, &d); printf("%d\n", money(t, r, d)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex57] ๊ฐ๊น์ด ์ ์ ๋ฐํ ํจ์ ์ค๊ณ (10์ )
๋ฌธ์ ์ค๋ช ์ ๋ ฅ๋ ์ค์ ๊ฐ์ ๊ฐ๊น์ด ์ ์๋ฅผ ๋ฆฌํดํ๋ ํจ์ round๋ฅผ ์ค๊ณํ๋ผ ๋จ, ์๋ ์ฃผ์ด์ง ํ ํ๋ฆฟ์ ์ฌ์ฉํ๋ round ํจ์ ์ด์ธ ์ฝ๋๋ ์ ๋ ์์ ํ๋ฉด ์๋จ (์ฃผ์) 3.51๋ ๊ฐ๊น์ด ์ ์๊ฐ 4์ด์ง๋ง -3.51์ ๊ฐ๊น์ด ์ ์๋ -4์ด๋ค. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int round(double x) { // ์ฝ๋ ์์ฑ } void main(void) { double d = 3.5; scanf("%lf", &d); printf("%d\n", round(d)); } ์ ๋ ฅ ์์ 32.768 ์ถ๋ ฅ ์์ 33 ์ ๋ต ์ฝ๋ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int round(double x) { if (x >= 0) return (int)(x + 0.5); return (int)(x - 0.5); } void main(void) { double d = 3.5; scanf("%lf", &d); printf("%d\n", round(d)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex56] ๋จ์ด ๊ธ์์ ๋ง์ถ๊ธฐ (10์ )
๋ฌธ์ ์ค๋ช ๊ณต๋ฐฑ์ด ์๋ ์ต๋ 20๊ธ์์ ๋จ์ด๊ฐ ๋ฐฐ์ด x์ ์ ๋ ฅ๋๋ค. ์ด ๋จ์ด์ ๊ธ์์๋ฅผ ์ธ์ํ๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ผ. ๋จ, ๋ฌธ์์ด ๋์ ๋๋ฌธ์๋ ๊ฐ์์ ํฌํจ๋์ง ์๋๋ค. ์๋ ์ฝ๋๋ฅผ ์ด์ฉํ๋ ๋ณ์ ๋ฑ์ ์์ ๋กญ๊ฒ ์ถ๊ฐ๊ฐ ๊ฐ๋ฅํ๋ค. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { char x[21]; scanf(" %s", x); // ์ฝ๋ ๊ตฌํ } ์ ๋ ฅ ์์ Hello ์ถ๋ ฅ ์์ 5 ์ ๋ต ์ฝ๋ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { char x[21]; scanf(" %s", x); int i, cnt = 0; for (i = 0; x[i] != '\0'; i++) { cnt++; } printf("%d\n", cnt); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex55] Black Jack ํจ์ (10์ )
๋ฌธ์ ์ค๋ช ์ ์ 3๊ฐ์ ํฉ์ด 21์ด๋ฉด 1์ ์๋๋ฉด 0์ ๋ฆฌํดํ๋ black_jack ํจ์๋ฅผ ์ค๊ณํ๋ผ ์ ๋ ฅ๋๋ 3๊ฐ์ ์ ์๋ 1 ์ด์ 9 ์ดํ์ ์ ์๋ง ๊ฐ๋ฅํ๋ฉฐ card ๋ฐฐ์ด์ ์ ์ฅ๋๋ค. ์ด๋ ์ฝ๋๋ฅผ ์ด์ฉํ๋ ์ฃผ์ด์ง ์ฝ๋๋ ๋ณ๊ฒฝํ ์ ์์ผ๋ฉฐ ํ์ํ ๋ณ์๋ ์ถ๊ฐํ์ฌ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int card[3]; int black_jack(void) { // ์ฝ๋ ๊ตฌํ } void main(void) { int i; for(i = 0; i < 3; i++) { scanf("%d", &card[i]); } printf("%d\n", black_jack()); } ์ ๋ ฅ ์์ 3 9 9 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int card[3]; int black_jack(void) { int i, s = 0; for(i = 0; i < 3; i++) { s += card[i]; } return (s == 21) ? 1 : 0; } void main(void) { int i; for(i = 0; i < 3; i++) { scanf("%d", &card[i]); } printf("%d\n", black_jack()); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex54] ๋ ๊ฐ์ ๋ฌธ์์ด์ ๊ตํํ๋ Swap ํจ์๋ฅผ ๊ตฌํํ์์ค
๋ฌธ์ ์ค๋ช ๋ ๊ฐ์ ๋ฌธ์์ด์ ๊ตํํ๋ Swap ํจ์๋ฅผ ๊ตฌํํ์์ค. (ํ์์ ํจ์ ์ถ๊ฐ ์ค๊ณํ์ฌ ๊ตฌํํ๋ค.) ๋ค์ ์ฝ๋๋ ์ ์ถ๋ ฅ Template์ผ๋ก ๋ณต์ฌํ์ฌ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ์ ์ํ ํจ์( Swap() )๋ฅผ ์ง์ ๊ตฌํํ์ฌ ์์ฑํ๋ค. #include < stdio.h> // ํ์์ ํจ์ ์ถ๊ฐ ์ค๊ณ (์)๋ฌธ์์ด์ ๋ณต์ฌํ๋ ํจ์ // ์ฌ๊ธฐ์ Swap()ํจ์๋ฅผ ๊ตฌํํ๋ค int main(void) { char a[110], b[110]; scanf("%s %s", a, b); Swap( a, b ); printf("%s %s\n", a, b); return 0; } ์ ๋ ฅ ์ค๋ช ๋ฌธ์์ด 2๊ฐ๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค ์ถ๋ ฅ ์ค๋ช ๊ตํํ ๋ฌธ์์ด์ ์ถ๋ ฅํ๋ค ์ ๋ ฅ ์์ Hi Hello ์ถ๋ ฅ ์์ Hello Hi ์ ๋ต ์ฝ๋ #include <stdio.h> void str_copy( char *d, const char *s) { while(*d++=*s++); } void Swap(char *ap, char *bp) { char temp[110]; str_copy(temp,ap); str_copy(ap,bp); str_copy(bp,temp); } int main(void) { char a[110], b[110]; scanf("%s %s", a, b); Swap( a, b ); printf("%s %s\n", a, b); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex53] ๋ฌธ์์ด ์ฐ๊ฒฐ
๋ฌธ์ ์ค๋ช ๋ฌธ์์ด์ ์ฐ๊ฒฐํ๋ ํจ์๋ฅผ ์์ฑํ๋ผ #include <stdio.h> void str_add(char * d, const char * s) { } void main(void) { char a[15] = "Willtek"; char b[15] = " Corp."; str_add(a, b); printf("%s\n", a); } ์ถ๋ ฅ ์์ Willtek Corp. ์ ๋ต ์ฝ๋ #include <stdio.h> void str_add(char * d, const char * s) { while (*d) d++; while (*d++ = *s++); } void main(void) { char a[15] = "Willtek"; char b[15] = " Corp."; str_add(a, b); printf("%s\n", a); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex52] ๋ฌธ์์ด ๊ธธ์ด ์ธก์
๋ฌธ์ ์ค๋ช ๋ฌธ์์ด ๊ธธ์ด๋ฅผ ์ธก์ ํ๋ ํจ์๋ฅผ ์์ฑํ๋ผ #include <stdio.h> unsigned int str_length(const char * d) { } void main(void) { char a[] = "Willtek"; printf("%d\n", sizeof(a)); printf("%d\n", str_length(a)); } ์ถ๋ ฅ ์์ 8 7 ์ ๋ต ์ฝ๋ #include <stdio.h> unsigned int str_length(const char * d) { int cnt = 0; while (*d++) cnt++; return cnt; } void main(void) { char a[] = "Willtek"; printf("%d\n", sizeof(a)); printf("%d\n", str_length(a)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex51] ์๋ฃ์ ์ํ๊ธฐ
๋ฌธ์ ์ค๋ช ๋ฒํธ๋ฅผ ๋ฃ์ผ๋ฉด ์ํ๋ ์๋ฃ๋ฅผ ๋ฆฌํดํด ์ฃผ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ scnaf๋ก ์ ๋ ฅ๋ฐ์ ์ ์ ๊ฐ(0~3)์ ์ํ๊ธฐ ํจ์์ ๋ฃ์ผ๋ฉด ํด๋นํ๋ ์๋ฃ ์ด๋ฆ์ ๋ฆฌํด ํ๋ค. 0 ~ 3 ๋ฒ์์ ๊ฐ ์ด์ธ์ ๊ฐ์ ์ ๋ ฅ๋์ง ์๋๋ค. main์ ๋ฆฌํด๋ ์๋ฃ์๋ฅผ ์ธ์ํด ์ค๋ค. main ํจ์ ๋ด๋ถ์ Vending_Machine ํจ์์ ๋ฆฌํด ํ์์ ์์ฑํ๋ผ #include <stdio.h> Vending_Machine(int num) // ํจ์ ๋ฆฌํด ํ๋กํ ํ์ ์์ฑ { static char drink[4][10] = { "cola", "milk", "coffee", "wine" }; return drink[num]; } void main(void) { // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์ค๋ช ์ ์ ๊ฐ์ผ๋ก ์๋ฃ์ ๋ฒํธ(0 ~ 3)๋ฅผ ์ ๋ ฅ ๋ฐ๋๋ค. ์ถ๋ ฅ ์ค๋ช ์ถ๋ ฅ ์์์ ๊ฐ์ ํํ๋ก ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์์ 0 ์ถ๋ ฅ ์์ cola ์ ๋ต ์ฝ๋ #include <stdio.h> char * Vending_Machine(int num) { static char drink[4][10] = { "cola", "milk", "coffee", "wine" }; return drink[num]; } void main(void) { int n; scanf("%d", &n); printf("%s\n", Vending_Machine(n)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex50] ๊ฐ์ ๋ชจ์ ์ฐพ๊ธฐ simple
๋ฌธ์ ์ค๋ช ์๋์ ๊ฐ์ด ๋ชจ๋์ข ์ด์ ๊ฐ๊ฐ์ ์นธ๋ค์ด ์น ํด์ ธ ์๋ ๊ทธ๋ฆผ์ด ์์ ๋ ๋ชจ๋์ข ์ด์์ ์ฐพ๊ณ ์ถ์ ํจํด์ ๋ชจ์์ด ๋ช ๊ฐ๊ฐ ์๋์ง๋ฅผ ๊ฒ์ฌํ๋ ค๊ณ ํ๋ค. ์ด ๋, ์ฐพ๊ณ ์ ํ๋ ๋ชจ๋์ข ์ด์ ํฌ๊ธฐ M(0โคMโค100)๊ณผ ํจํด์ ํฌ๊ธฐ P(0โคPโค100)์ ์ฃผ์ด์ง๋ค. ์ฐพ๊ณ ์ ํ๋ ํจํด์ ๋ชจ์์ ํ์๊ณผ ํฐ์์ผ๋ก ์น ํด์ง๊ณ , ํจํด์ ๊ฐ์ ๋ชจ์๋ง ์ฐพ์ผ๋ฉด ๋๋ค. ๋ํ ํ์์ผ๋ก ์น ํด์ง ์นธ์ ํจํด ๋งค์น ๊ฒ์ฌ ์ ๋ฐ๋ณต๋์ด ์ฌ์ฉ๋ ์ ์๋ค. #include <stdio.h> int main(void) { // ์ฌ๊ธฐ์๋ถํฐ ์์ฑ return 0; } ์ ๋ ฅ ์ค๋ช ์ฒซ ๋ฒ์งธ ์ค์๋ ๋ชจ๋์ข ์ด์ด์ ํฌ๊ธฐ M(0โคMโค100)์ด ์ฃผ์ด์ง๋ค. ๋ ๋ฒ์งธ ์ค๋ถํฐ M ์ค๊น์ง๋ ๋ชจ๋์ข ์ด์ ๊ทธ๋ฆฐ ๊ทธ๋ฆผ์ ์น ํ ์นธ์ 1๋ก, ์น ํ์ง ์์ ์นธ์ 0์ผ๋ก ๋ชจ๋์ข ์ด์ ์ค ๋ณ๋ก ์ ๋ ฅํ๋ค. ๋ค์ ์ค์๋ ํจํด์ ํฌ๊ธฐ P(0โคPโค100)๊ฐ ์ฃผ์ด์ง๋ค. ๋ค์ ์ค๋ถํฐ P๊ฐ์ ์ค์ ๊ฑธ์ณ ์ฐพ๊ณ ์ถ์ ํจํด์ ๋ชจ์์ด ์ฃผ์ด์ง๋ค. ๋ชจ์์ด ์๋ ๋ถ๋ถ๋ง 1 ๋ก ์ ๋ ฅํ๊ณ ๋๋จธ์ง๋ 0์ผ๋ก ์ฒ๋ฆฌํ๋ค. ์ถ๋ ฅ ์ค๋ช ์ถ๋ ฅ์ ์ฐพ๊ณ ์ถ์ ํจํด์ ๋ชจ์์ด ๋ชจ๋์ข ์ด์ ๊ทธ๋ฆฐ ๊ทธ๋ฆผ์ ๋ช ๊ฐ๊ฐ ์๋์ง ๊ทธ ๊ฐ์๋ฅผ ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์์ 10 0000000001 1110000000 0000001000 0000101000 1111111111 0000101000 0000001000 0000000000 1110000000 0000000001 3 100 111 100 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int maa[110][110], b[110][110]; int N,P; int pattern(int k, int m) { int i, j; int sol = 0; for (i = 0; i < N ; i++) { for (j = 0; j < N ; j++) { if (maa[i + k][j + m] == b[i][j]) { sol++; } } } return sol; } int main(void) { int i, j; scanf("%d", &P); for (i = 0; i < P; i++) { for (j = 0; j < P; j++) { scanf("%1d", &maa[i][j]); } } scanf("%d", &N); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { scanf("%1d", &b[i][j]); } } int Num = 0; for (i = 0; i <= P - N; i++) { for (j = 0; j <= P - N; j++) { if (pattern(i, j) == N*N) { Num++; } } } printf("%d ", Num); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex49] ๊ฐ์ฅ ํฐ ์ซ์์ ๊ฐ์ฅ ํฐ ์ซ์๊ฐ ์๋ ํ๊ณผ ์ด ๋ณํธ ์ธ์
๋ฌธ์ ์ค๋ช a[5][4] ๋ฐฐ์ด์ ๋์์ผ๋ก ์๋ ๊ฒฐ๊ณผ๋ฅผ ์ธ์ํ๋ ์ฝ๋๋ฅผ ์ค๊ณํ๋ผ ๋ฐฐ์ด์์ ๊ฐ์ฅ ํฐ ์ซ์๊ฐ ํฌํจ๋ ํ๊ณผ ์ด๋ฒํธ ๋ฐ ์ต๋๊ฐ์ ์ธ์ ๊ฐ์ ์ต๋๊ฐ์ด ์ฌ๋ฌ ์ค์ ์์ ๊ฒฝ์ฐ ๊ฐ์ฅ ๋ฎ์ ์ค ๋ฒํธ๋ฅผ ์ธ์ ํ๊ณผ ์ด์ ๋ฒํธ๋ 0๋ฒ ํ๋ถํฐ ์์ํ๋ค #include <stdio.h> int a[5][4] = { {10,2,-3,4}, {5,-6,7,-8}, {-9,10,-19,12}, {15,-8,7,-8}, {-3,10,9,17} }; void main(void) { int i, j; // ๊ฐ์ฅ ํฐ ๊ฐ์ด ์ ์ฅ๋ ํ ๋ฒํธ, ์ด๋ฒํธ, ์ต๋๊ฐ์ ์ฐพ์์ ์ธ์ํ๋ค } ์ถ๋ ฅ ์ค๋ช 4 3 17 ์ถ๋ ฅ ์์ 4 3 17 ์ ๋ต ์ฝ๋ #include <stdio.h> int a[5][4] = { {10,2,-3,4}, {5,-6,7,-8}, {-9,10,-19,12}, {15,-8,7,-8}, {-3,10,9,17} }; void main(void) { int i, j; int max = a[0][0]; int max_row = 0; int max_col = 0; for (i = 0; i < 5; i++) { for (j = 0; j < 4; j++) { if (a[i][j] > max) { max = a[i][j]; max_row = i; max_col = j; } } } printf("%d %d %d", max_row, max_col, max); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex48] 4๋ช ์ ์ฑ์ ์ ์ ๋ ฅ ๋ฐ์ 1๋ฑ์ธ ํ์์ ์ฐพ์ผ์์ค
๋ฌธ์ ์ค๋ช 1์ค์ ์ ์ 4๊ฐ์ฉ 3์ค์ ๊ฑธ์ณ ์ ๋ ฅ ๋ฐ์ ๋ฐฐ์ด์ ์ ์ฅํ๋ค. ํ๊ณผ ์ด ๋จ์์ ํฉ๊ณ๋ฅผ ์ธ์ํ๋ค. ์๋ฅผ ๋ค์ด ์๋์ ๊ฐ์ด ์ ๋ ฅ๋๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๋ค 4 3 1 7 7 8 6 2 9 5 2 6 ๊ทธ๋ฌ๋ฉด ์ถ๋ ฅ์ ์ฒซ์งธ ์ค์๋ ๊ฐ ํ์ ํฉ๊ณ๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํด์ ์ถ๋ ฅํ๊ณ ๋์งธ ์ค์๋ ๊ฐ ์ด์ ํฉ๊ณ๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํด์ ์ถ๋ ฅํ๋ค. ์ฆ, ๋ค์๊ณผ ๊ฐ์ด ์ถ๋ ฅํ๋ฉด ๋๋ค. 15 23 22 20 16 9 15 ์ ๋ ฅ ์ค๋ช 1์ค์ ์ ์ 4๊ฐ๊ฐ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋๋ฉฐ 3์ค์ ๊ฑธ์ณ ์ ๋ ฅ ๋ฐ๋๋ค. ๋จ ์ ์๋ ์์ ์ ์์ด๋ค. ์ถ๋ ฅ ์ค๋ช ์ฒซ์งธ ์ค์๋ ํ ๋จ์ ํฉ๊ณ๋ฅผ ์ธ์ํ๊ณ ๋ ๋ฒ์งธ ์ค์๋ ์ด ๋จ์ ํฉ๊ณ๋ฅผ ์ธ์ํ๋ค. ์ ๋ ฅ ์์ 4 3 1 7 7 8 6 2 9 5 2 6 ์ถ๋ ฅ ์์ 15 23 22 20 16 9 15 ์ ๋ต ์ฝ๋ #include <stdio.h> int arr[3 + 2][4 + 2]; void input(void) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { scanf("%d", &arr[i][j]); } } } void solve(void) { int d; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { d = arr[i][j]; arr[i][4] += d; arr[3][j] += d; } } } void output(void) { for (int i = 0; i < 3; i++) { printf("%d ", arr[i][4]); } printf("\n"); for (int j = 0; j < 4; j++) { printf("%d ", arr[3][j]); } } void main(void) { input(); solve(); output(); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex47] 4๋ช ์ ์ฑ์ ์ ์ ๋ ฅ ๋ฐ์ 1๋ฑ์ธ ํ์์ ์ฐพ์ผ์์ค
๋ฌธ์ ์ค๋ช 3๊ณผ๋ชฉ์ฉ 4๋ช ์ ์ ์๋ฅผ ์ ๋ ฅ ๋ฐ์ ๋ฐฐ์ด์ ์ ์ฅํ ํ ์ฑ์ ์ด 1๋ฑ์ธ ํ์์ด ๋ช ๋ฒ์งธ ํ์์ธ์ง ํด๋น ์์๋ฒํธ๋ฅผ ์ธ์ํ๋ค. ์์๋ฒํธ๋ 0๋ฒ๋ถํฐ์ด๋ค. ์ฑ์ ์ด ๊ฐ์ ๊ฒฝ์ฐ๋ ๋ฎ์ ๋ฒํธ๋ฅผ ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์ค๋ช ์ค๋น ํ ๋ช ์ฉ 3๊ณผ๋ชฉ์ ์ ์๊ฐ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋๋ฉฐ 4์ค์ ๊ฑธ์ณ ์ ๋ ฅ๋๋ค. ์ ์๋ ์์ ์ ์๋ก 0~100 ์ฌ์ด๋ค. ์ถ๋ ฅ ์ค๋ช 1๋ฑ์ธ ํ์์ด ๋ช ๋ฒ์งธ ํ์์ธ์ง ํด๋น ์์๋ฒํธ๋ฅผ ์ถ๋ ฅํ๋ค. ์์๋ฒํธ๋ 0๋ฒ๋ถํฐ์ด๋ค. ์ ๋ ฅ ์์ 80 50 40 30 80 90 60 100 70 90 50 80 ์ถ๋ ฅ ์์ 2 ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int s[4][3]; int i, j, sum, max = 0, num = 0; for (i = 0; i < 4; i++) { sum = 0; for (j = 0; j < 3; j++) { scanf(" %d", &s[i][j]); sum += s[i][j]; } if (sum > max) { max = sum; num = i; } } printf("%d\n", num); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex46] 4๋ช ์ ์ฑ์ ์ ์ ๋ ฅ ๋ฐ์ ํฉ๊ณ๋ฅผ ๊ณ์ฐํ์ฌ ์ธ์ํ์์ค
๋ฌธ์ ์ค๋ช 3๊ณผ๋ชฉ์ฉ 4๋ช ์ ์ ์๋ฅผ ์ ๋ ฅ ๋ฐ์ ๋ฐฐ์ด์ ์ ์ฅํ ํ ํฉ๊ณ๋ฅผ ์ธ์ํ๋ค. ์ ๋ ฅ ์ค๋ช ์ค๋น ํ ๋ช ์ฉ 3๊ณผ๋ชฉ์ ์ ์๊ฐ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋๋ฉฐ 4์ค์ ๊ฑธ์ณ ์ ๋ ฅ ๋ฐ๋๋ค. ์ ์๋ ์์ ์ ์๋ก 0~100์ฌ์ด์ด๋ค. ์ถ๋ ฅ ์ค๋ช ์ค๋น ํ ๋ช ์ฉ 3๊ณผ๋ชฉ์ ์ ์์ ํฉ๊ณ๋ฅผ 4์ค ์ธ์ํ๋ค. (์ธ์์ ์ ์ 1๊ฐ์ฉ %4d๋ก ํฌ๋งท์ ๋ง์ถฐ ์ธ์ํ๊ณ ์ ์์ ์ ์์ฌ์ด๋ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํ๋ค) ์ ๋ ฅ ์์ 80 50 40 30 80 90 60 100 70 90 50 80 ์ถ๋ ฅ ์์ 80 50 40 170 30 80 90 200 60 100 70 230 90 50 80 220 ์ ๋ต ์ฝ๋ #include <stdio.h> int s[4][3]; void main(void) { int i, j, sum; for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { scanf(" %d", &s[i][j]); } } for (i = 0; i < 4; i++) { for (sum = 0, j = 0; j < 3; j++) { printf("%4d ", s[i][j]); sum += s[i][j]; } printf("%4d\n", sum); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex45] ๋ฐฐ์ด์ ํ์
๋ฌธ์ ์ค๋ช a ๋ฐฐ์ด์ ํ๊ณผ ์ด์ ๋ฐ๊พธ์ด b ๋ฐฐ์ด์ ์ ์ฅํ๋ ์ฝ๋๋ฅผ ์ค๊ณํ๋ผ. a์ b ๋ฐฐ์ด์ ์๋ ๋ค์๊ณผ ๊ฐ๋ค. a ๋ฐฐ์ด 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 b๋ฐฐ์ด 16 12 8 4 15 11 7 3 14 10 6 2 13 9 5 1 ์ ๋ ฅ ์ค๋ช ๋ฐฐ์ด์ ํฌ๊ธฐ N ๋ฐ N* N ํฌ๊ธฐ์ ๋ฐฐ์ด a๋ฅผ ์ ๋ ฅ ๋ฐ๋๋ค N์ (1 <= N <= 10) ๋ฒ์์ด๋ค. ์ถ๋ ฅ ์ค๋ช a ๋ฐฐ์ด์ ๋ฌธ์ ์์ ์ ์ํ ๊ฒ๊ณผ ๊ฐ์ด ํ์ ํ์ฌ b ๋ฐฐ์ด์ ์ ์ฅํ๊ณ , b ๋ฐฐ์ด์ ๋ด์ฉ์ ์ถ๋ ฅ ํ๋ค. ์ ๋ ฅ ์์ 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ์ถ๋ ฅ ์์ 16 12 8 4 15 11 7 3 14 10 6 2 13 9 5 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int a[10][10]; int b[10][10]; int N; int main(void) { int i, j; scanf("%d", &N); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { scanf("%d", &a[i][j]); } } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { b[N - 1 - j][N - 1 - i] = a[i][j]; } } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { printf("%d ", b[i][j]); } printf("\n"); } return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex44] 1๊ฐ์ ์ ์๋ฅผ ์ถ๊ฐํ๊ณ ๋ฐฐ์ด๋ฅผ ์ธ์ํ์์ค
๋ฌธ์ ์ค๋ช ์ฒซ์ค์ ์ ๋ ฅ๋๋ N(1 <= N <= 17)๊ฐ ๋งํผ ๋ค์์ค์ ์ ๋ ฅ๋๋ ์ ์(์์, 0, ์์ ๋ชจ๋ ๊ฐ๋ฅ)๋ฅผ ์ ๋ ฅ๋ฐ์ ๋ฐฐ์ด์ ์ ์ฅํ๋ค. ๋จ, ์ ๋ ฅ๋๋ ๊ฐ์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋ ์ํ๋ก ์ฃผ์ด์ง๋ฉฐ ๊ฐ์ ๊ฐ์ด ์ค๋ณต๋์ด ์์์๋ ์๋ค. ๋ง์ง๋ง ์ค์ ์ถ๊ฐํ ๊ฐ์ ์ ์๋ก ์ ๋ ฅ ๋ฐ๋๋ค. ์ถ๊ฐ๋ ๊ฐ์ด ์ค๋ฆ์ฐจ์์ด ๋๋๋ก ๋ฐฐ์ด์ ์ถ๊ฐํ ํ ๊ฒฐ๊ณผ๋ฅผ ์ธ์ํ๋ค. ์ ๋ ฅ ์์ 10 10 20 30 40 50 60 70 80 90 100 65 ์ถ๋ ฅ ์์ 10 20 30 40 50 60 65 70 80 90 100 ์ ๋ต ์ฝ๋ #include <stdio.h> int n, m; int a[18 + 2]; void input(void) { int i; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } scanf("%d", &m); } void solve(void) { int i; for (i = (n - 1); i >= 0; i--) { a[i + 1] = a[i]; if (a[i] <= m) { a[(i + 1)] = m; break; } } if (i == -1) { a[0] = m; } } void output(void) { int i; for (i = 0; i < (n + 1); i++) { printf("%d ", a[i]); } } void main(void) { input(); solve(); output(); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex43] ๋ ์์ ๊ฑฐ๋ฆฌ
๋ฌธ์ ์ค๋ช ๋ ์ ์๋ฅผ ์ ๋ ฅ ๋ฐ์, ๋ ์์ ์ฐจ์ด๋ฅผ ์ ๋๊ฐ์ผ๋ก ์ธ์ํ๋ผ. #include <stdio.h> int main(void) { int a, b; scanf("%d %d", &a, &b); // ์ฌ๊ธฐ์๋ถํฐ ์์ฑ return 0; } ์ ๋ ฅ ์ค๋ช ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํ์ฌ ๋ ์ ์๋ฅผ ์ ๋ ฅํ๋ค. ์ถ๋ ฅ ์ค๋ช ๋ ์์ ์ฐจ์ด๋ฅผ ์ ๋๊ฐ์ผ๋ก ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์์ 15 12 ์ถ๋ ฅ ์์ 3 ์ ๋ต ์ฝ๋ #include <stdio.h> int ABS (int x) { return (x < 0) ? -x : x; } int main(void) { int a, b; scanf("%d %d", &a, &b); printf("%d\n", ABS(a-b)); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex42] ์ ์ ์ญ์ ์ธ์
๋ฌธ์ ์ค๋ช 1๊ฐ์ ์์ ์ ์ n์ ์ ๋ ฅ ๋ฐ์ n์ ์๊ฐ์ ์ญ์์ผ๋ก ์ธ์ํ๋ค ์ ๋ ฅ ์ค๋ช ์ฒซ์งธ ์ค์ ์์ ์ ์ n์ด ์ ๋ ฅ๋๋ค ์ถ๋ ฅ ์ค๋ช ์ ๋ ฅ๋ n ์ ์๊ฐ์ ์ญ์์ผ๋ก ์ถ๋ ฅํ๋ค (n์ด 12345678์ด๋ฉด, ์ถ๋ ฅ์ 87654321) ์ ๋ ฅ ์์ 12345678 ์ถ๋ ฅ ์์ 87654321 ์ ๋ต ์ฝ๋ #include <stdio.h> void Num_Reverse(int num) { int i=0; while(num) { printf("%d", num%10); num/=10; } } void main(void) { int n; scanf("%d", &n ); Num_Reverse(n); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex41] ์ฝ์ ์ถ๋ ฅ
๋ฌธ์ ์ค๋ช 1๊ฐ์ ์ ์๋ฅผ n์ ์ ๋ ฅ ๋ฐ์ 2๋ถํฐ n๊น์ง ๊ฐ ์๋ค์ ์ฝ์๋ฅผ ๋ชจ๋ ์ธ์ํ์์ค. (1<n<1000) ์ ๋ ฅ ์ค๋ช ์ฒซ์งธ ์ค์ ์์ ์ ์ n์ด ์ ๋ ฅ๋๋ค (1<n<1000) ์ถ๋ ฅ ์ค๋ช 2๋ถํฐ n๊น์ง ๊ฐ ์๋ค์ ์ฝ์๋ฅผ ๋ชจ๋ ์ธ์ํ์์ค ์ ๋ ฅ ์์ 10 ์ถ๋ ฅ ์์ 1 2 1 3 1 2 4 1 5 1 2 3 6 1 7 1 2 4 8 1 3 9 1 2 5 10 ์ ๋ต ์ฝ๋ # include <stdio.h> int main(void) { int n; scanf("%d", &n); for (int i=2; i<=n; i++) { for (int j=1; j<=i; j++) { if (i%j==0) printf("%d ", j); } printf("\n"); } return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex40] 3์ ๋ฐฐ์์ ํฉ
๋ฌธ์ ์ค๋ช 3๋ถํฐ ๊ฐ์ ์ฆ๊ฐ์ํค๋ฉฐ 3์ ๋ฐฐ์์ ํฉ์ ๊ตฌํ ๋ ํฉ์ด 1000์ด ๋์ง ์๋ 3์ ๋ฐฐ์๋ฅผ ์ธ์ํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. (์ฆ, 3+6+9+12+โฆ+N < 1000 ์ ๋ง์กฑํ๋ ๊ฐ์ฅ ํฐ N์ ์ธ์) ์ถ๋ ฅ ์ค๋ช 3+6+9+12+...+N < 1000 ์ ๋ง์กฑํ๋ ๊ฐ์ฅ ํฐ N์ ์ธ์ ์ถ๋ ฅ ์์ 75 ์ ๋ต ์ฝ๋ # include <stdio.h> int main() { int sum = 0; int i; for (i=3; ;i+=3) { sum += i; if (sum > 1000) break; } printf("%d", i-3); return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex39] ์ฃผ์ฌ์ ๋์ง๊ธฐ2
๋ฌธ์ ์ค๋ช 3๊ฐ์ ์ฃผ์ฌ์๋ฅผ ๋์ก์ ๋ ๋์ฌ ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ธ์ํ์์ค ์ถ๋ ฅ ์์ 1 1 1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 2 1 1 2 2 1 2 3 1 2 4 1 2 5 1 2 6 1 3 1 1 3 2 1 3 3 1 3 4 1 3 5 1 3 6 1 4 1 1 4 2 1 4 3 1 4 4 1 4 5 1 4 6 1 5 1 1 5 2 1 5 3 1 5 4 1 5 5 1 5 6 1 6 1 1 6 2 1 6 3 1 6 4 1 6 5 1 6 6 2 1 1 2 1 2 2 1 3 2 1 4 2 1 5 2 1 6 2 2 1 2 2 2 2 2 3 2 2 4 2 2 5 2 2 6 2 3 1 2 3 2 2 3 3 2 3 4 2 3 5 2 3 6 2 4 1 2 4 2 2 4 3 2 4 4 2 4 5 2 4 6 2 5 1 2 5 2 2 5 3 2 5 4 2 5 5 2 5 6 2 6 1 2 6 2 2 6 3 2 6 4 2 6 5 2 6 6 3 1 1 3 1 2 3 1 3 3 1 4 3 1 5 3 1 6 3 2 1 3 2 2 3 2 3 3 2 4 3 2 5 3 2 6 3 3 1 3 3 2 3 3 3 3 3 4 3 3 5 3 3 6 3 4 1 3 4 2 3 4 3 3 4 4 3 4 5 3 4 6 3 5 1 3 5 2 3 5 3 3 5 4 3 5 5 3 5 6 3 6 1 3 6 2 3 6 3 3 6 4 3 6 5 3 6 6 4 1 1 4 1 2 4 1 3 4 1 4 4 1 5 4 1 6 4 2 1 4 2 2 4 2 3 4 2 4 4 2 5 4 2 6 4 3 1 4 3 2 4 3 3 4 3 4 4 3 5 4 3 6 4 4 1 4 4 2 4 4 3 4 4 4 4 4 5 4 4 6 4 5 1 4 5 2 4 5 3 4 5 4 4 5 5 4 5 6 4 6 1 4 6 2 4 6 3 4 6 4 4 6 5 4 6 6 5 1 1 5 1 2 5 1 3 5 1 4 5 1 5 5 1 6 5 2 1 5 2 2 5 2 3 5 2 4 5 2 5 5 2 6 5 3 1 5 3 2 5 3 3 5 3 4 5 3 5 5 3 6 5 4 1 5 4 2 5 4 3 5 4 4 5 4 5 5 4 6 5 5 1 5 5 2 5 5 3 5 5 4 5 5 5 5 5 6 5 6 1 5 6 2 5 6 3 5 6 4 5 6 5 5 6 6 6 1 1 6 1 2 6 1 3 6 1 4 6 1 5 6 1 6 6 2 1 6 2 2 6 2 3 6 2 4 6 2 5 6 2 6 6 3 1 6 3 2 6 3 3 6 3 4 6 3 5 6 3 6 6 4 1 6 4 2 6 4 3 6 4 4 6 4 5 6 4 6 6 5 1 6 5 2 6 5 3 6 5 4 6 5 5 6 5 6 6 6 1 6 6 2 6 6 3 6 6 4 6 6 5 6 6 6 ์ ๋ต ์ฝ๋ #include <stdio.h> int main(void) { for (int i = 1; i <= 6; i++) { for (int j = 1; j <= 6; j++) { for (int h = 1; h <= 6; h++) { printf("%d %d %d\n", i, j, h); } } } return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex38] ๋ง์ง๋ง ์ ์ ์ฐพ๊ธฐ
๋ฌธ์ ์ค๋ช 1๊ฐ์ ์ ์ n์ ์ ๋ ฅ ๋ฐ์ 1+2+3โฆ +x ์ ํฉ๊ณ๊ฐ n์ด์์ด ๋๋ฉด ๋ง์ง๋ง ๋ํ ์ x๋ฅผ ๊ตฌํ์ฌ ์ธ์ํ๋ค. ( 1+2+3โฆ +x >= n ์ ์กฐ๊ฑด์ด๋ฉฐ n์ ์์ ์ ์์ ) ์ ๋ ฅ ์ค๋ช ์ฒซ์งธ ์ค์ ์์ ์ ์ n์ด ์ ๋ ฅ๋๋ค ์ถ๋ ฅ ์ค๋ช 1+2+3โฆ +x >= n์ ๋ง์กฑํ๋ ์ ์ผ ์์ x๋ฅผ ์ถ๋ ฅํ๋ค ์ ๋ ฅ ์์ 2500 ์ถ๋ ฅ ์์ 71 ์ ๋ต ์ฝ๋ #include <stdio.h> int Count_Sum(int num) { int i=1, sum=0; while(sum<num) { sum+=i; i++; } return i-1; } void main(void) { int n; scanf("%d", &n); printf( "%d\n", Count_Sum( n ) ); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex37] ๋ฐ๋ณต๋ฌธ์ ํ์ฉํ ๋ณ ์ํ๊ธฐ - ์ญ์ผ๊ฐ๋ณ
๋ฌธ์ ์ค๋ช ๋ฐ๋ณต๋ฌธ์ ์ด์ฉํ์ฌ ๋ค์ ๋ชจ์์ ์ธ์ํ๋ ํจ์๋ฅผ ์ค๊ณํ๊ณ ์ด๋ฆ main์์ ํธ์ถํ๋ผ. ๋จ, printf ํ ๋ฒ์ โ*โ ํ ๋ฒ๋ง ์ถ๋ ฅํ๊ณ , for๋ฃจํ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ๋ผ. ์ถ๋ ฅ ์ค๋ช '*' ๋ฌธ์๋ฅผ ์ถ๋ ฅ ์์์ ๊ฐ์ด ์ถ๋ ฅํ๋ค. ์ถ๋ ฅ ์์ ***** **** *** ** * ์ ๋ต ์ฝ๋ #include <stdio.h> void Draw_Start2(void) { int line, star; for (line = 0; line < 5; line++) { for (star = 0; star < 5 - line; star++) { printf("*"); } printf("\n"); } } void main(void) { Draw_Start2(); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex36] ๋ฐ๋ณต๋ฌธ์ ํ์ฉํ ๋ณ ์ํ๊ธฐ - ์ฌ๊ฐ๋ณ
๋ฌธ์ ์ค๋ช ๋ฐ๋ณต๋ฌธ์ ์ด์ฉํ์ฌ ๋ค์ ๋ชจ์์ ์ธ์ํ๋ ํจ์๋ฅผ ์ค๊ณํ๊ณ ์ด๋ฆ main์์ ํธ์ถํ๋ผ. ๋จ, printf ํ ๋ฒ์ โ*โ ํ ๋ฒ๋ง ์ถ๋ ฅํ๊ณ , for๋ฃจํ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ๋ผ. ์ถ๋ ฅ ์ค๋ช '*' ๋ฌธ์๋ฅผ ์ถ๋ ฅ ์์์ ๊ฐ์ด ์ถ๋ ฅํ๋ค. ์ถ๋ ฅ ์์ ***** ***** ***** ์ ๋ต ์ฝ๋ #include <stdio.h> void Draw_Start1(void) { int line, star; for (line = 0; line < 3; line++) { for (star = 0; star < 5; star++) { printf("*"); } printf("\n"); } } void main(void) { Draw_Start1(); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex35] ๋ ์ ์์ ์ค๊ฐ ๊ฐ์ ๊ตฌํ๋ ํจ์
๋ฌธ์ ์ค๋ช ์ ์ 2๊ฐ๋ฅผ ์ ๋ ฅ ๋ฐ์ผ๋ฉด ๋ ์์ ์ค๊ฐ ๊ฐ์ ๋ฆฌํดํ๋ ํจ์ ์ค๊ณ ์ ๋ ฅ ๊ฐ์ 0์ ์์ ์ ์, ์์ ์ ์ ๋ชจ๋ ๊ฐ๋ฅํ๋ค 10๊ณผ 20์ ์ค๊ฐ์ 15 ์ด๋ 1๊ณผ 6 ์ฌ์ด์ผ ๊ฒฝ์ฐ 3์ด๋ 4๋ ๋ชจ๋ ์ค๊ฐ ๊ฐ์ด ๋ ์ ์๋ค ๋ฐ๋ผ์ ์ค๊ฐ ๊ฐ์ด 2๊ฐ๊ฐ ๋๋ ๊ฐ๋ค์ ์ ๋ ฅ๋์ง ์๋๋ค ๋ํ ๋ ์๊ฐ ์๋ก ๊ฐ์ ๊ฒฝ์ฐ๋ ์ ๋ ฅ๋์ง ์๋๋ค. ์ ๋ ฅ ์์ 20 10 ์ถ๋ ฅ ์์ 15 ์ ๋ต ์ฝ๋ #include <stdio.h> int Search_Middle(int num1, int num2) { while (++num1 < --num2); return num1; } void main(void) { int min = 0, max = 0, temp; scanf("%d %d", &min, &max); if (min > max) { temp = min; min = max; max = temp; } printf("%d\n", Search_Middle(min, max)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex34] ํฉํ ๋ฆฌ์ผ(!)์ ๊ตฌํ๋ ํจ์
๋ฌธ์ ์ค๋ช ์ ๋ ฅ ๋ฐ์ ์์ ์ ์์ ํฉํ ๋ฆฌ์ผ(!)์ ๊ตฌํ๋ ํจ์ parameter, return ๋ชจ๋ int ํฉํ ๋ฆฌ์ผ: 3! = 3 * 2 * 1 ํจ์์ ๋ฆฌํดํ์ ์ด ์ unsigned long long int ์ผ๊น? ์ด ๊ฒฝ์ฐ ๋ช๊น์ง์ ํฉํ ๋ฆฌ์ผ ๊ฐ์ ๊ตฌํ ์ ์์๊น? #include <stdio.h> unsigned long long int Factorial(int num) { } void main(void) { int value; scanf("%d", &value); printf("%llu\n", Factorial(value)); } ์ ๋ ฅ ์์ 3 ์ถ๋ ฅ ์์ 6 ์ ๋ต ์ฝ๋ #include <stdio.h> unsigned long long int Factorial(int num) { int i; unsigned long long int answer = 1; for (i = 2; i <= num; i++) { answer *= i; } return answer; } void main(void) { int value; scanf("%d", &value); printf("%llu\n", Factorial(value)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex33] ๋ ์๋ฅผ ์ ๋ ฅ ๋ฐ์์ ๊ทธ ์ฌ์ด์ ์กด์ฌํ๋ ์์๋ฅผ ์ธ์ํ๋ ํ๋ก๊ทธ๋จ
๋ฌธ์ ์ค๋ช ๋ ์๋ฅผ ์ ๋ ฅ ๋ฐ์ ๊ทธ ์ฌ์ด์ ์์๋ฅผ ์ธ์ํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ main์์ ๋ ์๋ฅผ ์ ๋ ฅ ๋ฐ์ ํจ์์ ์ ๋ฌํ์ฌ ๋ ์ ์ฌ์ด์ ์์๋ฅผ ์ธ์ํ๋ค ๋ค์ ์ฌํญ์ ์ค์ํ๋๋ก ํ๋ค ์ ๋ ฅ ๊ฐ์ด ์์์ด๊ฑฐ๋ 0์ด๋ฉด ๋ค์ ์ ๋ ฅ ๋ฐ์ ๊ฒ (์ฆ, ์์ ์ ์๋ง ์ ๋ ฅ ๊ฐ์ผ๋ก ์ฌ์ฉํจ) 10 ๋ค์ 200์ด ์ฌ์๋ ์์ง๋ง 200๋ค์ 10์ด ์ฌ ์๋ ์์ผ๋ ์ด๋ค ๊ฒฝ์ฐ๋ ์ง ์ฒ๋ฆฌ๋๋๋ก ํจ ์๋ชป๋ ์ ๋ ฅ์ ๋ํ ์์ธ์ฒ๋ฆฌ๋ main์์ ํ ๊น ์์์ธ์ ํจ์์์ ํ ๊น? ์ผ๋ฐ์ ์ผ๋ก ํจ์๋ ๋จ์ํ ์ผ๋ง ํ๊ฒ ๋ง๋๋ ๊ฒ์ด ์ข์ ๊ฒฐ๊ตญ main์์ ์๋ชป๋ ์ ๋ ฅ์ ์ฒ๋ฆฌํด ์ฃผ๊ณ ์ ์์ธ ๊ฐ๋ค๋ง ํจ์๋ก ์ ๋ฌํ๋ ๊ฒ์ด ์ข์ ์์ธ์ฒ๋ฆฌ ๋ฐ ์ ๋ ฅ ์ฝ๋๋ ์๋ ์ฝ๋ ์ฐธ๊ณ ์์๋ 1์ ํฌํจ์ด ๋์ง ์์ง๋ง ๊ทธ๋ฅ ๋๊ทธ๋ฌ์ด ์ฉ์ํด ์ฃผ์ธ์ 1๋ ์์๋ก ๋ณด๊ณ ์ธ์ ํด์ฃผ์ธ์ #include <stdio.h> void Print_Prime(int min, int max) { } void main(void) { int a, b; scanf(" %d", &a); scanf(" %d", &b); if ((a <= 0) || (b <= 0)) { printf("Error!!\n"); } else { // ์ฝ๋ ๊ตฌํ Print_Prime(a, b); } } ์ ๋ ฅ ์์ 1 20 ์ถ๋ ฅ ์์ 1 2 3 5 7 11 13 17 19 ์ ๋ต ์ฝ๋ #include <stdio.h> void Print_Prime(int min, int max) { int i, j, k; for (i = min; i <= max; i++) { k = 0; for (j = 2; j < i; j++) { if ((i % j) == 0) { k = 1; break; } } if (k == 0) { printf("%d\n", i); } } } void main(void) { int i, a, b; scanf(" %d", &a); scanf(" %d", &b); if ((a <= 0) || (b <= 0)) { printf("Error!!\n"); } else { if (a > b) { i = a; a = b; b = i; } Print_Prime(a, b); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex32] ์์ ์ธ์ํ๊ธฐ
๋ฌธ์ ์ค๋ช 1๋ถํฐ ์ ๋ ฅ ๋ฐ์ ๊ฐ n(n > 1) ๊น์ง์ ์์๋ฅผ ๋ชจ๋ ์ธ์ํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ. ๋ฆฌํด์ ์๊ณ int๋ฅผ ๋ฐ์ผ๋ฉด ๊ทธ ์ฌ์ด์ ์๋ ์์๋ ๋ชจ๋ ์ธ์ํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ค. ์์(prime number): 1๊ณผ ์๊ธฐ ์์ ์ผ๋ก๋ง ๋๋์ด์ง๋ ์ 2๋ถํฐ (์๊ธฐ-1)๊น์ง ๊ฐ์ผ๋ก ๊ณ์ ๋๋์ด๊ฐ๋ฉด์ ๋๋จธ์ง 0์ธ ๊ฐ์ด ๋์ค๋ฉด ์์๊ฐ ์๋๋ค. ์ ๋ ฅ ์ค๋ช ์ซ์๋ฅผ ํ ๊ฐ ์ ๋ ฅ ๋ฐ๋๋ค. ์ถ๋ ฅ ์ค๋ช ๊ฐ์ ์ถ๋ ฅ ์์์ ๊ฐ์ด ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์์ 11 ์ถ๋ ฅ ์์ 2 3 5 7 11 ์ ๋ต ์ฝ๋ #include <stdio.h> void Print_Prime(int max) { int i, j, k; for (i = 2; i <= max; i++) { k = 0; for (j = 2; j < i; j++) { if ((i % j) == 0) { k = 1; break; } } if (k == 0) { printf("%d ", i); } } } void main(void) { int n; scanf("%d", &n); Print_Prime(n); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex31] ์์ ํ๋จ
๋ฌธ์ ์ค๋ช N๊ฐ๋งํผ ์ ์ M์ ๋ฐ๋ณต ์ ๋ ฅ ๋ฐ์ ์ ์ M์ด ์์๋ผ๋ฉด YES, ์๋๋ฉด NO๋ฅผ ์ธ์ํ๋ค. ๋จ ํจ์๋ฅผ ์ค๊ณํ์ฌ ๊ตฌํ ํ๋ค. ์ ๋ ฅ ์ค๋ช ์ฒซ์งธ ์ค์ ์ ์ N(1โฆ N โฆ 100)์ด ์ ๋ ฅ๋๊ณ ๋ค์ ์ค๋ถํฐ N๊ฐ์ ์ค์ ์ ์ M(1โฆ M โฆ 1,000,000)์ด ์ ๋ ฅ๋๋ค ์ถ๋ ฅ ์ค๋ช ์์์ด๋ฉด YES๋ฅผ ์๋๋ฉด NO๋ฅผ ์ถ๋ ฅํ๋ค ์ ๋ ฅ ์์ 4 13 59 124 1151 ์ถ๋ ฅ ์์ YES YES NO YES ์ ๋ต ์ฝ๋ int Prime_Check(int m) { int i; for(i=2;i<m;i++) { if(m%i==0) return 0; } return 1; } void main(void) { int N, M; int i; scanf("%d", &N); for(i=0;i<N;i++) { scanf("%d", &M); if( Prime_Check( M )==1 ) printf("YES\n"); else printf("NO\n"); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex30] ์ ๋ ฅ๋ฐ์ ์ ๊น์ง ์ง์์ 3์ ๋ฐฐ์๋ฅผ ์ ์ธํ ์ซ์ ์ธ์
๋ฌธ์ ์ค๋ช for loop์ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ ํจ์๋ฅผ ์ค๊ณํ๋ผ. 1๋ถํฐ ๋์ด์จ ์ซ์๊น์ง ์ฌ์ด์ ์กด์ฌํ๋ ์ง์์ 3์ ๋ฐฐ์๋ฅผ ์ ์ธํ ์ซ์๋ฅผ ์ธ์ํ๋ค. ์ธ์๋ ํ ์ค์ 9๊ฐ์ ์ซ์๋ฅผ ์ธ์ํ๋ฉฐ ์ซ์๋น ์ต๋ 3์๋ฆฌ๋ฅผ ์ฐจ์งํ๋๋ก ํ๋ค. ๋ฆฌํด์ ์๊ณ ํจ์๋ช ์ ์์๋ก ํ๋ ์ ๋ ฅ ๋ฐ์ ์ ์๋ ์ต๋๊ฐ์ 999 ๊น์ง์ด๋ค. ๊ฐ ํ๋๋ฅผ ์ธ์ํ๋ printf๋ ๋ค์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ค printf("%3d ", i); ์ ๋ ฅ ์ค๋ช ์ซ์ 1๊ฐ๋ฅผ ์ ๋ ฅ ๋ฐ๋๋ค. ์ ๋ ฅ ๋ฐ์ ์ ์๋ ์ต๋๊ฐ์ 999์ด๋ค. ์ถ๋ ฅ ์ค๋ช ๊ฐ์ ์ถ๋ ฅ ์์์ ๊ฐ์ด ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์์ 121 ์ถ๋ ฅ ์์ 1 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55 59 61 65 67 71 73 77 79 83 85 89 91 95 97 101 103 107 109 113 115 119 121 ์ ๋ต ์ฝ๋ #include <stdio.h> void func(int num) { int i; int cnt = 0; for (i = 1; i <= num; i++) { if ((i % 2 != 0) && (i % 3 != 0)) { printf("%3d ", i); cnt++; } if (cnt == 9) { printf("\n"); cnt = 0; } } } void main(void) { int n; scanf("%d", &n); func(n); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex29] 'A'๋ถํฐ ์ ๋ ฅ ๋ฐ์ ๊ธ์ ๊น์ง ์ํ๋ฒณ ์ธ์
๋ฌธ์ ์ค๋ช โAโ๋ถํฐ ์ ๋ ฅ๋ฐ์ ๊ธ์(์๋ฌธ ์ํ๋ฒณ ๋๋ฌธ์)๊น์ง ์ํ๋ฒณ์ ์ธ์ํ๋ ์ฝ๋๋ฅผ for ๋ฃจํ๋ก ๊ตฌํํ๋ผ. ์ ๋ ฅ ์์ K ์ถ๋ ฅ ์์ A B C D E F G H I J K ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { char i, c; scanf("%c", &c); for (i = 'A'; i <= c; i++) { printf("%c\n", i); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex28] ์ซ์ 7๋ถํฐ n๊ฐ ์ ์ ์ธ์
๋ฌธ์ ์ค๋ช ์ซ์๋ฅผ 7๋ถํฐ ์ ๋ ฅ๋ฐ์ ์ ์ n(n > 0)๊ฐ ๋งํผ 1์ฉ ์ฆ๊ฐํ ์ ์๋ฅผ ์ธ์ํ๋ ์ฝ๋๋ฅผ for ๋ฃจํ๋ก ๊ตฌํํ๋ผ. ์ ๋ ฅ ์์ 8 ์ถ๋ ฅ ์์ 7 8 9 10 11 12 13 14 ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int i; int n; scanf("%d", &n); for(i = 7; i < (n + 7); i++) { printf("%d\n", i); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex27] *์ ์ ๋ ฅ ๋ฐ์ ์ ๋งํผ ์ธ์ํ๋ ์ฝ๋
๋ฌธ์ ์ค๋ช *์ ์ ๋ ฅ ๋ฐ์ ์ ์ n๊ฐ ๋งํผ ์ธ์ํ๋ ์ฝ๋๋ฅผ for ๋ฃจํ๋ก ๊ตฌํํ๋ผ. ๋จ, ๋ฃจํ 1๋ฒ์ ํ ๊ฐ์ * ์ถ๋ ฅ #include <stdio.h> char Common_Calc(int num); int main(void) { //ํจ์์์ฑ return 0; } char Common_Calc(int num) { //ํจ์์์ฑ } ์ ๋ ฅ ์์ 15 ์ถ๋ ฅ ์์ *************** ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int i, n; scanf("%d", &n); for (i = 0; i < n; i++) { printf("*"); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex26] ๋์ด์จ ์ ์๊ฐ 3๊ณผ 5์ ๊ณต๋ฐฐ์์ธ์ง ํ๋ณํ๋ Common_Calc ํจ์๋ฅผ ๊ตฌํํ์์ค
๋ฌธ์ ์ค๋ช [ํจ์ ํ์] char Common_Calc( int num ); [ํจ์ ๊ธฐ๋ฅ] ๋์ด์จ ์๊ฐ 3๊ณผ 5์ ๊ณต๋ฐฐ์๋ผ๋ฉด โOโ, ์๋๋ฉด โXโ๋ฅผ ๋ฆฌํดํ๋ค. [main ํจ์] 1๊ฐ์ ์ ์๋ฅผ ์ ๋ ฅ ๋ฐ์ Common_Calc ํจ์๋ฅผ ํธ์ถํ์ฌ ๋ฆฌํด ๋ ๋ฌธ์๋ฅผ ์ธ์ํ๋ค. #include <stdio.h> char Common_Calc(int num); int main(void) { //ํจ์์์ฑ return 0; } char Common_Calc(int num) { //ํจ์์์ฑ } ์ ๋ ฅ ์ค๋ช ์ฒซ ์ค์ ์ ์ a๊ฐ ์ ๋ ฅ๋๋ค. (a๋ int ์ ์ ๋ฒ์ ์ด๋ด) ์ถ๋ ฅ ์ค๋ช ์ ๋ ฅ๋ ์ ์๊ฐ 3๊ณผ 5์ ๊ณต๋ฐฐ์์ด๋ฉด โOโ(์๋ฌธ์ ๋๋ฌธ์)๋ฅผ ์๋๋ฉด โXโ (์๋ฌธ์ ๋๋ฌธ์)๋ฅผ ์ธ์ํ๋ค. ์ ๋ ฅ ์์ 15 ์ถ๋ ฅ ์์ O ์ ๋ต ์ฝ๋ #include <stdio.h> char Common_Calc(int num); int main(void) { int input; scanf("%d", &input); printf("%c", Common_Calc(input)); return 0; } char Common_Calc(int num) { //if(!(num%3) && !(num%5)) return 'O'; if(!(num%15)) return 'O'; return 'X'; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex25] ๋์ด์จ ์ ์์ ์ ๋๊ฐ์ ๋ฆฌํด ํ๋ Abs_Calc ํจ์๋ฅผ ๊ตฌํํ์์ค
๋ฌธ์ ์ค๋ช [ํจ์ ํ์] int Abs_Calc(int num); [ํจ์ ๊ธฐ๋ฅ] ๋์ด์จ ์ ์์ ์ ๋๊ฐ์ ๋ฆฌํดํ๋ค. [main ํจ์] 1๊ฐ์ ์ ์๋ฅผ ์ ๋ ฅ ๋ฐ์ Abs_Calc ํจ์๋ฅผ ํธ์ถํ์ฌ ๋ฆฌํด ๋ ์ ๋๊ฐ์ ์ธ์ํ๋ค #include <stdio.h> int Abs_Calc(int num); int main(void) { //ํจ์์์ฑ return 0; } int Abs_Calc(int num) { //ํจ์์์ฑ } ์ ๋ ฅ ์ค๋ช ์ฒซ ์ค์ ์ ์ a๊ฐ ์ ๋ ฅ๋๋ค. (a๋ int ์ ์ ๋ฒ์ ์ด๋ด) ์ถ๋ ฅ ์ค๋ช ์ ๋ ฅ๋ ์ ์์ ์ ๋๊ฐ์ ์ธ์ํ๋ค. ์ ๋ ฅ ์์ -5 ์ถ๋ ฅ ์์ 5 ์ ๋ต ์ฝ๋ #include <stdio.h> int Abs_Calc(int num); int main(void) { int x; scanf("%d", &x); printf("%d", Abs_Calc(x)); return 0; } int Abs_Calc(int num) { if(num>=0) return num; return -num; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex24] ๋ ๊ฐ์ ์ ์ ์ค ํฐ ์๋ฅผ ํ๋ณํ๋ Max_Calc ํจ์๋ฅผ ๊ตฌํํ์์ค
๋ฌธ์ ์ค๋ช [ํจ์ ํ์] int Max_Calc(int a, int b); [ํจ์ ๊ธฐ๋ฅ] ๋์ด์จ ๋ ์ ์ ์ค ํฐ ์๋ฅผ ํ๋ณํ์ฌ ํฐ ์๋ฅผ ๋ฆฌํดํ๋ค. ๊ฐ์ ๊ฐ์ผ ๊ฒฝ์ฐ ๊ทธ ๊ฐ์ ๋ฆฌํดํ๋ค [main ํจ์] 2๊ฐ์ ์ ์๋ฅผ ์ ๋ ฅ๋ฐ์ Max_Calcํจ์๋ฅผ ํธ์ถํ์ฌ ๋ฆฌํด ๋ ํฐ ์๋ฅผ ์ธ์ํ๋ค. #include <stdio.h> int Max_Calc(int a, int b); int main(void) { //ํจ์์์ฑ return 0; } int Max_Calc(int a, int b) { //ํจ์์์ฑ } ์ ๋ ฅ ์ค๋ช ์ฒซ ์ค์ ๋ ๊ฐ์ ์ ์ a, b๊ฐ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋์ด ์ ๋ ฅ๋๋ค. (a, b ๋ชจ๋ int ์ ์ ๋ฒ์ ์ด๋ด) ์ถ๋ ฅ ์ค๋ช ์ ๋ ฅ๋ ๋ ์์ค ํฐ ์๋ฅผ ์ธ์ํ๋ค. ์ ๋ ฅ ์์ 5 10 ์ถ๋ ฅ ์์ 10 ์ ๋ต ์ฝ๋ #include <stdio.h> int Max_Calc(int a, int b) { if(a>=b) return a; return b; } void main(void) { int a,b; scanf("%d %d",&a,&b); printf("%d\n",Max_Calc(a,b)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex23] ์ฑ์ ๊ณ์ฐ ํจ์
๋ฌธ์ ์ค๋ช ๋ค์ ์กฐ๊ฑด์ ๋ง๋ ์ฑ์ ์ฒ๋ฆฌ ํจ์๋ฅผ ์ค๊ณํ์์ค. parameter๋ int์ด๊ณ ํจ์๋ช ์ func, return ํ์ ์ char๋ก ํ๋ค. ์ฑ์ ๊ธฐ์ค์ 90~100(โAโ), 80~89(โBโ), 70~79(โCโ), 60~69(โDโ), 60๋ฏธ๋ง(โFโ)์ด๋ค. ์ฑ์ ์ ์ ๋ ฅํ๋ฉด ํ์ (๋ฌธ์ A, B, C, D, F์ค ํ๋)์ ๋ฆฌํดํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ค. ๋จ, ์ง์ ๋ ๋ฒ์์ ๊ฐ์ด ์๋๋ฉด(์์๋ 100๋ณด๋ค ํฐ ๊ฐ) ๋ฐ๋์ ๋ฌธ์ โXโ๋ฅผ ๋ฆฌํดํ์ฌ ์ถ๋ ฅํ๋ค. #include <stdio.h> // ํจ์ func ์ค๊ณ void main(void) { int score; scanf("%d", &score); printf("%c\n", func(score)); } ์ ๋ ฅ ์ค๋ช ์ฑ์ ์ ์์ธ ์ ์ ํ๋๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. (์์, ์์, 0 ๋ชจ๋ ๊ฐ๋ฅํ ์ ์) ์ถ๋ ฅ ์ค๋ช ์ ๋ ฅ ๊ฐ์ ๋ฐ๋ฅธ ์ฑ์ (A, B, C, D, F์ค ํ๋)์ ์ถ๋ ฅํ๊ณ 0~100๋ฒ์์ ์๊ฐ ์๋๋ฉด X๋ฅผ ์ถ๋ ฅํ๋ค. ์ ๋ ฅ ์์ 92 ์ถ๋ ฅ ์์ A ์ ๋ต ์ฝ๋ #include <stdio.h> char func(int s) { if ((s < 0) || (s > 100)) return 'X'; switch (s / 10) { case 0 : case 1 : case 2 : case 3 : case 4 : case 5 : return 'F'; case 6 : return 'D'; case 7 : return 'C'; case 8 : return 'B'; case 9 : case 10: return 'A'; } } void main(void) { int score; scanf("%d", &score); printf("%c\n", func(score)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex22] 4 ~ 10 ์ฌ์ด ๊ฐ์ ์ฐพ๋ ํจ์
๋ฌธ์ ์ค๋ช ์ซ์๊ฐ 4 ~ 10 ์ฌ์ด ๊ฐ์ด๋ฉด 1 ์๋๋ฉด 0์ ๋ฆฌํดํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ. #include <stdio.h> int f1(int num) { } void main(void) { int num; scanf("%d", &num); printf("%d\n", f1(num)); } ์ ๋ ฅ ์์ 4 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int f1(int num) { return (num >= 4) && (num <= 10); } void main(void) { int num; scanf("%d", &num); printf("%d\n", f1(num)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex21] 3์ ๋ฐฐ์ ๋๋ 5์ ๋ฐฐ์ ์ฐพ๋ ํจ์
๋ฌธ์ ์ค๋ช ์ซ์๊ฐ 3 ๋๋ 5์ ๋ฐฐ์์ด๋ฉด 1 ์๋๋ฉด 0์ ๋ฆฌํดํ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ. #include <stdio.h> int f1(int num) { } void main(void) { int num; scanf("%d", &num); printf("%d\n", f1(num)); } ์ ๋ ฅ ์์ 6 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int f1(int num) { return !(num % 3) || !(num % 5); } void main(void) { int num; scanf("%d", &num); printf("%d\n", f1(num)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex20] 2,3,5์ ๋ฐฐ์ ํ๋จํ๊ธฐ
๋ฌธ์ ์ค๋ช 2์ ๋ฐฐ์๋ฉด 2, 3์ ๋ฐฐ์๋ฉด 3, 5์ ๋ฐฐ์๋ฉด 5๋ฅผ 2,3,5์ ๋ฐฐ์๊ฐ ์๋๋ฉด 0์ ๋ฆฌํดํ๋ ํจ์๋ฅผ ์์ฑํ๋ผ. ๋จ, ๊ณตํต๋ฐฐ์๋ ์ ๋ ฅ๋์ง ์๋ ๊ฒ์ผ๋ก ๋ณธ๋ค #include <stdio.h> int compare(int num) { } void main(void) { int num; scanf("%d", &num); printf("%d\n", compare(num)); } ์ ๋ ฅ ์์ 33 ์ถ๋ ฅ ์์ 3 ์ ๋ต ์ฝ๋ #include <stdio.h> #if 1 int compare(int num) { if(num % 2 == 0) return 2; if(num % 3 == 0) return 3; if(num % 5 == 0) return 5; return 0; } #endif #if 0 int compare(int num) { int r = 0; if (num % 2 == 0) r = 2; else if (num % 3 == 0) r = 3; else if (num % 5 == 0) r = 5; return r; } #endif #if 0 int compare(int num) { int r = 0; if ((num % 2) == 0) r = 2; else if ((num % 3) == 0) r = 3; else if ((num % 5) == 0) r = 5; return r; } #endif void main(void) { int num; scanf("%d", &num); printf("%d\n", compare(num)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex19] ํ์ง์ ๋ง์ถฐ๋ผ - ๋ณ๊ฒฝ
๋ฌธ์ ์ค๋ช ์์ ์ ์๋ฅผ ๋ฃ์ผ๋ฉด ์ง์์ธ์ง ํ์ ์ธ์ง๋ฅผ ๋ง์ถ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ ์ด ํจ์๋ ๋ฆฌํด์ผ๋ก ์ง์์ด๋ฉด 0, ํ์์ด๋ฉด 1์ ๋ฆฌํดํ๋ค #include <stdio.h> int Check_Odd_Even(int num) { } void main(void) { int num; scanf("%d", &num); printf("%d\n", Check_Odd_Even(num)); } ์ ๋ ฅ ์์ 3 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> int Check_Odd_Even(int num) { return num % 2; } void main(void) { int num; scanf("%d", &num); printf("%d\n", Check_Odd_Even(num)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex18] ํ์ง์ ๋ง์ถฐ๋ผ
๋ฌธ์ ์ค๋ช ์์ ์ ์๋ฅผ ๋ฃ์ผ๋ฉด ์ง์์ธ์ง ํ์ ์ธ์ง๋ฅผ ๋ง์ถ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ ์ด ํจ์๋ ๋ฆฌํด์ผ๋ก ์ง์์ด๋ฉด 2, ํ์์ด๋ฉด 1์ ๋ฆฌํดํ๋ค #include <stdio.h> int Check_Odd_Even(int num) { } void main(void) { int num; scanf("%d", &num); printf("%d\n", Check_Odd_Even(num)); } ์ ๋ ฅ ์์ 3 ์ถ๋ ฅ ์์ 1 ์ ๋ต ์ฝ๋ #include <stdio.h> #if 1 int Check_Odd_Even(int num) { if (num % 2 == 0) return 2; else return 1; } #endif #if 0 int Check_Odd_Even(int num) { if (num % 2) return 1; return 2; } #endif #if 0 int Check_Odd_Even(int num) { int r; if (num % 2) r = 1; else r = 2; return r; } #endif #if 0 int Check_Odd_Even(int num) { int r = 2; if (num % 2) r = 1; return r; } #endif #if 0 int Check_Odd_Even(int num) { return 2 - num % 2; } #endif void main(void) { int num; scanf("%d", &num); printf("%d\n", Check_Odd_Even(num)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex17] ASCII ์ซ์ ๋ฌธ์๋ฅผ ์ ์ ์ซ์๋ก ๋ฐํํ๋ ํจ์
๋ฌธ์ ์ค๋ช ASCII ์ซ์(โ1โ)๋ฅผ ๋ฃ์ผ๋ฉด ์ ์(1)๋ก ๋ฐ๊ฟ์ฃผ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ. ๋ฌธ์๋ โ0โ ~ โ9โ ์ฌ์ด๋ฅผ ์ ๋ ฅํ๋ ๊ฒ์ผ๋ก ํ๋ค. #include <stdio.h> int Change_Char_to_Int(char num) { } void main(void) { char a; scanf("%c", &a); printf("%d\n", Change_Char_to_Int(a)); } ์ ๋ ฅ ์์ 3 ์ถ๋ ฅ ์์ 3 ์ ๋ต ์ฝ๋ #include <stdio.h> int Change_Char_to_Int(char num) { return num - '0'; } void main(void) { char a; scanf("%c", &a); printf("%d\n", Change_Char_to_Int(a)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex16] ๋๋ฌธ์๋ฅผ ์๋ฌธ์๋ก ๋ฐ๊พธ๋ ํจ์ ์์ฑ
๋ฌธ์ ์ค๋ช ๋๋ฌธ์๋ฅผ ๋ฃ์ผ๋ฉด ์๋ฌธ์๋ก ๋ฐ๊ฟ์ฃผ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ. ๋ฌธ์๋ โAโ ~ โZโ ์ฌ์ด๋ฅผ ์ ๋ ฅํ๋ ๊ฒ์ผ๋ก ํ๋ค. #include <stdio.h> char Change_Case(char upper) { } void main(void) { char a; scanf("%c" , &a ); printf("%c => %c\n", a, Change_Case(a)); } ์ ๋ ฅ ์์ A ์ถ๋ ฅ ์์ A => a ์ ๋ต ์ฝ๋ #include <stdio.h> char Change_Case(char upper) { return upper + ('a' - 'A'); } void main(void) { char a; scanf("%c", &a); printf("%c => %c\n", a, Change_Case(a)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex15] float ๊ฐ์ ๋ฃ์ผ๋ฉด ๊ฐ์ฅ ๊ฐ๊น์ด ์ ์ ๊ฐ์ ์ฐพ์์ ๋๊ฒจ์ฃผ๋ ํจ์ ์์ฑ
๋ฌธ์ ์ค๋ช float ๊ฐ์ ๋ฃ์ผ๋ฉด ๊ฐ์ฅ ๊ฐ๊น์ด ์ ์ ๊ฐ์ ์ฐพ์์ ๋๊ฒจ์ฃผ๋ ํจ์๋ฅผ ์ค๊ณํ๋ผ 3.56๋ 4, 3.12๋ 3์ผ๋ก ๋ฐ์ฌ๋ฆผํ์ฌ ๊ฐ๊น์ด ์ ์๋ฅผ ๋ฆฌํดํ๋ ์์ ๊ฐ์ ์ ๋ ฅํ์ง ์๋ ๊ฒ์ผ๋ก ํ๋ค #include <stdio.h> int find_int(float value); void main(void) { float num; scanf("%f", &num); printf("%d\n", find_int(num)); } int find_int(float value) { } ์ ๋ ฅ ์์ 3.56 ์ถ๋ ฅ ์์ 4 ์ ๋ต ์ฝ๋ #include <stdio.h> int find_int(float value); void main(void) { float num; scanf("%f", &num); printf("%d\n", find_int(num)); } int find_int(float value) { float bias = 0.5f; value += bias; return (int)value; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex14] 10์ง์ ์๋ฆฌ์ ๋ถ๋ฆฌํ๊ธฐ
๋ฌธ์ ์ค๋ช 4์๋ฆฌ์ ์ซ์๋ฅผ ์ ๋ ฅ๋ฐ์ 1000์๋ฆฌ 100์๋ฆฌ 10์๋ฆฌ 1์ ์๋ฆฌ ๊ฐ์ ๊ฐ๊ฐ ์ธ์ํ๋ผ ๋จ, ์ ๋ ฅ์ 1000 ~ 9999 ์ฌ์ด์ ๊ฐ์ด๋ฉฐ ์ด์ธ์ ์ซ์๋ ์ ๋ ฅ๋์ง ์๋๋ค ์ ๋ ฅ ์ค๋ช 4์๋ฆฌ ์ซ์ ํ ๊ฐ๋ฅผ ์ ๋ ฅ ๋ฐ๋๋ค. ์ซ์ N์ 1000~9999์ฌ์ด์ ๊ฐ์ด๋ค. ์ ๋ ฅ ์์ 1234 ์ถ๋ ฅ ์์ 1000์๋ฆฌ=1, 100์๋ฆฌ=2, 10์๋ฆฌ=3, 1์๋ฆฌ=4 ์ ๋ต ์ฝ๋ #include <stdio.h> int main() { int N, a, b, c, d; scanf("%d", &N); if ((N >= 1000) && (N <= 9999)) { a = N / 1000; b = (N / 100) % 10; c = (N / 10) % 10; d = N % 10; printf("1000์๋ฆฌ=%d, 100์๋ฆฌ=%d, 10์๋ฆฌ=%d, 1์๋ฆฌ=%d\n", a, b, c, d); } return 0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex13] ๋ฐ์ง๋ฆ์ ์ ๋ ฅํ๋ฉด ์์ ๋์ด๋ฅผ ๊ตฌํ๋ ํจ์ ์์ฑ
๋ฌธ์ ์ค๋ช ๋ค์ compute_circle_area ํจ์๋ฅผ ์์ฑํ๋ผ. pi๋ 3.14๋ก ์ค์ ํ๋ค. #include <stdio.h> float compute_circle_area(float radious); void main(void) { float r; scanf("%f", &r); printf("%f\n", compute_circle_area(r)); } float compute_circle_area(float radious) { float pi = 3.14f; // ์ฝ๋ ์์ฑ } ์ ๋ ฅ ์์ 3.2 ์ถ๋ ฅ ์์ 32.153603 ์ ๋ต ์ฝ๋ #include <stdio.h> float compute_circle_area(float radious); void main(void) { float r; scanf("%f", &r); printf("%f\n", compute_circle_area(r)); } float compute_circle_area(float radious) { float pi = 3.14f; radious = radious * radious * pi; return radious; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex12] /, % ์ฐ์ฐ์์ ํ์ฉ => 10์ง์ ์๋ฆฌ์ ๋ถ๋ฆฌ
๋ฌธ์ ์ค๋ช 4์๋ฆฌ ์ ์์ ๊ฐ ์๋ฆฌ ๊ฐ์ ์ถ์ถํ๋ ๋ค์ ์ฝ๋๋ฅผ ์์ฑํ๋ผ #include <stdio.h> void main(void) { int a = 2345; int a4, a3, a2, a1; a4 = a3 = a2 = a1 = printf("1000์๋ฆฌ=%d, 100์๋ฆฌ=%d, 10์๋ฆฌ=%d, 1์๋ฆฌ=%d\n", a4, a3, a2, a1); } ์ถ๋ ฅ ์์ 1000์๋ฆฌ=2, 100์๋ฆฌ=3, 10์๋ฆฌ=4, 1์๋ฆฌ=5 ์ ๋ต ์ฝ๋ #include <stdio.h> void main(void) { int a = 2345; int a4, a3, a2, a1; a4 = a / 1000 % 10; a3 = a / 100 % 10; a2 = a / 10 % 10; a1 = a / 1 % 10; printf("1000์๋ฆฌ=%d, 100์๋ฆฌ=%d, 10์๋ฆฌ=%d, 1์๋ฆฌ=%d\n", a4, a3, a2, a1); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex11] int ๋ณ์๋ก ํจ์ ์คํํ๊ธฐ
๋ฌธ์ ์ค๋ช #include <stdio.h> int func(int a, int b) { return a+b; } void main(void) { int a = (int)func; printf("%d\n", func(3,4)); printf("%d\n", ); } ์ ๋ต ์ฝ๋ #include <stdio.h> int func(int a, int b) { return a+b; } typedef int(*FP)(int, int); void main(void) { int a = (int)func; printf("%d\n", func(3,4) ); printf("%d\n", ((FP)a)(3,4)); // printf("%d\n", ((int (*)(int, int))a)(3, 4)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex10] Type casting ์ฐ์ต 3
๋ฌธ์ ์ค๋ช #include <stdio.h> void func(void *p) { printf("%s\n", ); } void main(void) { char * p = "Willtek"; func(&p); } ์ ๋ต ์ฝ๋ #include <stdio.h> void func(void *p) { printf("%s\n", (*(char**)p)); } void main(void) { char * p = "Willtek"; func(&p); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex09] Type casting ์ฐ์ต 2
๋ฌธ์ ์ค๋ช #include<stdio.h> void func(void * p) { int i; for(i=0; i<3; i++) { printf("%f\n", ); } } void main(void) { double d[3] = {3.14, 5.125, -7.42}; void *p = d; func(&p); } ์ ๋ต ์ฝ๋ #include<stdio.h> void func(void * p) { int i; for(i=0; i<3; i++) { printf("%f\n", (*(double**)p)[i]); } } void main(void) { double d[3] = {3.14, 5.125, -7.42}; void *p = d; func(&p); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex08] Type casting ์ฐ์ต 1 (์ค๋ฅ ๋ฌด์)
๋ฌธ์ ์ค๋ช #include <stdio.h> void func(int x) { printf("%f\n", ); printf("%f\n", ); printf("%f\n", ); } void main(void) { double d[3] = {3.14, 5.125, -7.42}; func((int)d); } ์ ๋ต ์ฝ๋ #include <stdio.h> void func(int x) { /* printf("%f\n", ((double*)x)[0]); printf("%f\n", ((double*)x)[1]); printf("%f\n", ((double*)x)[2]);*/ for(i=0; i<3; i++) { printf("%f\n", ((double*)x)[i]); } } void main(void) { double d[3] = {3.14, 5.125, -7.42}; func((int)d); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex07] ํจ์ Lookup table (์ฝ๋ ์์ )
๋ฌธ์ ์ค๋ช ๊ต์ฌ์ ์ฝ๋๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๋ฉด ์๋จ(์ฑ์ ์ ์ํ์ฌ get_key ํจ์ ๋ณ๊ฒฝ๋จ) ์๋ ์ฝ๋ ๋ณต์ฌํ์ฌ fa ๋ฐฐ์ด๋ง ์ ์ธํ ๊ฒ #include <stdio.h> #include <stdlib.h> int add(int a, int b) { return a+b; } int sub(int a, int b) { return a-b; } int mul(int a, int b) { return a*b; } int get_key(void) { static int r = 0; int ret = r; r = (r + 1) % 3; return ret; } fa[3] = {add, sub, mul}; int op(int a, int b) { return fa[get_key()](a,b); } void main(void) { printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); } ์ถ๋ ฅ ์์ 7 -1 12 7 -1 12 7 ์ ๋ต ์ฝ๋ #include <stdio.h> #include <stdlib.h> int add(int a, int b) { return a+b; } int sub(int a, int b) { return a-b; } int mul(int a, int b) { return a*b; } int get_key(void) { static int r = 0; int ret = r; r = (r + 1) % 3; return ret; } int (*fa[3])(int a, int b) = {add, sub, mul}; int op(int a, int b) { return fa[get_key()](a,b); } void main(void) { printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); printf("%d\n", op(3, 4)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex06] ํจํจ์๋ฅผ ํจ์์ ์ ๋ฌํ์
๋ฌธ์ ์ค๋ช #include <stdio.h> int add(int a, int b) { return a+b; } int sub(int a, int b) { return a-b; } void func( ) { printf("%d\n", p(3,4)); } void main(void) { func(add); func(sub); } ์ถ๋ ฅ ์์ 7 -1 ์ ๋ต ์ฝ๋ #include <stdio.h> int add(int a, int b) { return a+b; } int sub(int a, int b) { return a-b; } void func(int (*p)(int a, int b)) { printf("%d\n", p(3,4)); } void main(void) { func(add); func(sub); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex05] ํจ์ ๋ฑ๊ฐํฌ์ธํฐ์ ์คํ
๋ฌธ์ ์ค๋ช #include <stdio.h> int add(int a, int b) { return a+b; } void f1(void) { printf("func\n"); } int * f2(void) { static int a[4] = {1,2,3,4}; return a; } void main(void) { // p, q, r ์ ์ธ // p, q, r์ ๋์ ํจ์ ๋์ printf("%d\n", add(3,4)); f1(); printf("%d\n", f2()[2]); // ์์ ๋์ผํ ๊ฒฐ๊ณผ๊ฐ ๋์ค๋๋ก p, q, r๋ก ์คํ } ์ถ๋ ฅ ์์ 7 func 3 7 func 3 ์ ๋ต ์ฝ๋ #include <stdio.h> int add(int a, int b) { return a+b; } void f1(void) { printf("func\n"); } int * f2(void) { static int a[4] = {1,2,3,4}; return a; } void main(void) { // p, q, r ์ ์ธ int (*p)(int a, int b); void (*q)(void); int * (*r)(void); // p, q, r์ ๋์ ํจ์ ๋์ p = add; q = f1; r = f2; printf("%d\n", add(3,4)); f1(); printf("%d\n", f2()[2]); // ์์ ๋์ผํ ๊ฒฐ๊ณผ๊ฐ ๋์ค๋๋ก p, q, r๋ก ์คํ printf("%d\n", p(3,4)); q(); printf("%d\n", r()[2]); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex04] 2์ฐจ์ ๋ฐฐ์ด์ ๋ฆฌํด
๋ฌธ์ ์ค๋ช #include <stdio.h> func(void) { static int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}; return a; } void main(void) { printf("%d\n", func() ); } ์ถ๋ ฅ ์์ 7 ์ ๋ต ์ฝ๋ #include <stdio.h> int (*func(void))[4] { static int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}; return a; } void main(void) { printf("%d\n", func()[1][2]); } /* typedef */ #if 0 #include <stdio.h> typedef int(*ARRP)[4]; ARRP func(void) { static int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}; return a; } void main(void) { printf("%d\n", func()[1][2] ); } #endif ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex03] 2์ฐจ์ ๋ฐฐ์ด์ ์ ๋ฌ
๋ฌธ์ ์ค๋ช #include <stdio.h> void draw_pixel(int y, int x, int value, p ) { p[y][x] = value; } void main(void) { int a[2][3] = {1,2,3,4,5,6}; printf("%d\n", a[1][2]); draw_pixel(1, 2, 10, a); printf("%d\n", a[1][2]); } ์ถ๋ ฅ ์์ 6 10 ์ ๋ต ์ฝ๋ #include <stdio.h> void draw_pixel(int y, int x, int value, int (*p)[3]) { p[y][x] = value; } void main(void) { int a[2][3] = {1,2,3,4,5,6}; printf("%d\n", a[1][2]); draw_pixel(1, 2, 10, a); printf("%d\n", a[1][2]); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex02] ๊ตฌ์กฐ์ฒด ์ฃผ์์ ํจ์ ์ ๋ฌ
๋ฌธ์ ์ค๋ช #include <stdio.h> struct math { int id; char name[20]; int score; }; void cheat(struct math * test); void main(void) { struct math final={1, "Kim", 50}; cheat(&final); printf("%d\n", final.score); } // ํจ์์์ score๋ฅผ 100์ผ๋ก ์์ ํ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ผ // ๋จ, -> ์ฐ์ฐ์๋ ์ฌ์ฉํ ์ ์๋ค void cheat(struct math * test) { = 100; } ์ถ๋ ฅ ์ค๋ช 50์ด 100์ผ๋ก ์์ ๋์ด ์ธ์ ์ถ๋ ฅ ์์ 100 ์ ๋ต ์ฝ๋ #include <stdio.h> struct math { int id; char name[20]; int score; }; void cheat(struct math * test); void main(void) { struct math final={1, "Kim", 50}; cheat(&final); final.score = 100; printf("%d\n", final.score); } void cheat(struct math * test) // test = &final => *test = final { (*test).score= 100; test[0].score = 100; test->score = 100; // ๊ตฌ์กฐ์ฒด(๊ณต์ฉ์ฒด) ์ฃผ์ } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex02] ํฌ์ธํฐ ๋ฐฐ์ด
๋ฌธ์ ์ค๋ช ๋ฐฐ์ด a๋ฅผ ์ด์ฉํ์ฌ x[2]๋ฅผ 30์ผ๋ก ๋ง๋๋ ์ฝ๋๋ฅผ ๋ง๋์์ค ๋ฐฐ์ด a๋ฅผ ์ด์ฉํ์ฌ x[2]๋ฅผ 30์ผ๋ก ๋ง๋๋ ์ฝ๋๋ฅผ ๋ง๋์์ค #include <stdio.h> int x[4] = {1,2,3,4}; void main(void) { int *a[4] = {x+3, x+2, x+1, x}; printf("%d\n", x[2]); = 30; printf("%d\n", x[2]); } ์ถ๋ ฅ ์์ 3 30 ์ ๋ต ์ฝ๋ #include <stdio.h> int x[4] = {1,2,3,4}; void main(void) { int *a[4] = {x+3, x+2, x+1, x}; printf("%d\n", x[2]); //a[0][-1] = 30; //a[1][0] = 30; //a[2][1] = 30; a[3][2] = 30; printf("%d\n", x[2]); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex46] #8 ์ธํฐ๋ฝํธ ๊ธฐ๋ฐ ๋ฉ๋ก๋ ์ฌ์ ๋ฐ ์ค์๊ฐ PWM LED ์ ์ด ์ค์ต
๋ฌธ์ ์ค๋ช ํ์ด๋จธ ์ธํฐ๋ฝํธ๋ฅผ ํ์ฉํ์ฌ ๋ฉ๋ก๋(song1)๋ฅผ ์์ฐจ ์ฌ์ํ๊ณ , UART ์์ ์ธํฐ๋ฝํธ๋ฅผ ํตํด LED ๋ฐ๊ธฐ๋ฅผ ์ค์๊ฐ์ผ๋ก ์ ์ดํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ. TIM2 ์ธํฐ๋ฝํธ๋ฅผ ํตํด ์ํ ํ ๊ฐ์ ์ฐ์ฃผ๊ฐ ๋๋๋ฉด ๋ค์ ์ํ๋ฅผ ์๋ ์ฌ์ TIM3์ ์ฃผํ์ ๋ฐ์์ฉ ํ์ด๋จธ๋ก ๋ถ์ ์ ์ถ๋ ฅ์ ์ฌ์ฉ๋จ USART1 ์ธํฐ๋ฝํธ๋ก ์์ ํ ์ซ์(1~9)๋ PWM ๋ํฐ๋น๋ก ์ค์ ๋์ด LED ๋ฐ๊ธฐ๋ฅผ ์กฐ์ ํ๊ณ , 0์ PWM ์ถ๋ ฅ์ ์ค๋จํจ ๋ ์ธํฐ๋ฝํธ๋ ๋ ๋ฆฝ์ ์ผ๋ก ๋์ํ๋ฉฐ, ๋ฉ๋ก๋ ์ฌ์๊ณผ LED ์ ์ด๋ฅผ ๋์์ ์ํํจ ์ ๋ต ์ฝ๋ //stm32f10x_it.c volatile int Uart1_Rx_In = 0; volatile int Uart1_Rx_Data = 0; void USART1_IRQHandler(void) { Uart1_Rx_Data = (unsigned char)USART1->DR; Uart1_Send_Byte(Uart1_Rx_Data); Uart1_Rx_In = 1; NVIC_ClearPendingIRQ(37); } volatile int note_end = 0; void TIM2_IRQHandler(void) { Macro_Clear_Bit(TIM2->SR, 0); NVIC_ClearPendingIRQ(28); note_end = 1; } //main.c #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); SCB->VTOR = 0x08003000; SCB->SHCSR = 0; } #define BASE (500) //msec enum key{C1, C1_, D1, D1_, E1, F1, F1_, G1, G1_, A1, A1_, B1, C2, C2_, D2, D2_, E2, F2, F2_, G2, G2_, A2, A2_, B2}; enum note{N16=BASE/4, N8=BASE/2, N4=BASE, N2=BASE*2, N1=BASE*4}; const int song1[][2] = { {G1,N4},{G1,N4},{E1,N8},{F1,N8},{G1,N4},{A1,N4},{A1,N4},{G1,N2},{G1,N4},{C2,N4},{E2,N4},{D2,N8},{C2,N8},{D2,N2} }; static void Buzzer_Beep(unsigned char tone, int duration) { const static unsigned short tone_value[] = {261,277,293,311,329,349,369,391,415,440,466,493,523,554,587,622,659,698,739,783,830,880,932,987}; TIM3_Out_Freq_Generation(tone_value[tone]); TIM2_Delay(duration); } extern volatile int Uart1_Rx_In; extern volatile int Uart1_Rx_Data; extern volatile int note_end; void Main(void) { Sys_Init(); TIM4_Out_Init(); TIM3_Out_Init(); Uart1_RX_Interrupt_Enable(1); static int timer_run = 0; volatile int i = 0; Buzzer_Beep(song1[i][0], song1[i][1]); for(;;) { if (Uart1_Rx_In) { Uart1_Rx_Data = Uart1_Rx_Data - '0'; if (Uart1_Rx_Data == 0) { TIM4_Out_Stop(); timer_run = 0; } else { if (timer_run == 0) { TIM4_Out_PWM_Generation(1000, Uart1_Rx_Data); timer_run = 1; } else { TIM4_Change_Duty(Uart1_Rx_Data); } } Uart1_Rx_In = 0; } if (note_end) { TIM3_Out_Stop(); i++; if (i >= sizeof(song1)/sizeof(song1[0])) { i = 0; } Buzzer_Beep(song1[i][0], song1[i][1]); note_end = 0; } } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex45] #7 ๋ฉ๋ก๋ ๋ฐ๋ณต ์ฌ์ ๋ฐ UART ๊ธฐ๋ฐ LED PWM ์ ์ด ์ค์ต
๋ฌธ์ ์ค๋ช ๋ถ์ ๋ฅผ ํตํด ๋ฏธ๋ฆฌ ์ ์๋ ๋ฉ๋ก๋(song1)๋ฅผ ๋ฐ๋ณต ์ฌ์ํ๊ณ , UART ์์ ์ธํฐ๋ฝํธ๋ฅผ ํตํด LED ๋ฐ๊ธฐ๋ฅผ ์ค์๊ฐ์ผ๋ก ์ ์ดํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ. main ๋ฃจํ์์๋ TIM3๊ณผ TIM2๋ฅผ ์ด์ฉํ์ฌ ์๊ณ ์ฃผํ์ ๋ฐ ์ง์์๊ฐ์ ๋ฐ๋ผ ๋ฉ๋ก๋๋ฅผ ์ฌ์ USART1 ์ธํฐ๋ฝํธ์์ UART๋ก ์์ ๋ ๊ฐ(1~9)์ TIM4์ PWM ๋ํฐ๋น๋ก ์ ์ฉ๋์ด LED ๋ฐ๊ธฐ๋ฅผ ์กฐ์ UART๋ก 0์ด ์ ๋ ฅ๋๋ฉด PWM ์ถ๋ ฅ์ ์ค๋จํ์ฌ LED๋ฅผ ๋ ๋ฉ๋ก๋ ์ฌ์๊ณผ LED ์ ์ด๋ ๋ ๋ฆฝ์ ์ผ๋ก ๋์ ์ ๋ต ์ฝ๋ //stm32f10x_it.c void USART1_IRQHandler(void) { unsigned char c = (unsigned char)USART1->DR; Uart1_Send_Byte(c); TIM4_Out_Init(); static int timer_run = 0; int n = c - '0'; if (n == 0) { TIM4_Out_Stop(); timer_run = 0; } else { if (timer_run == 0) { TIM4_Out_PWM_Generation(1000, n); timer_run = 1; } else { TIM4_Change_Duty(n); } } NVIC_ClearPendingIRQ(37); } //main.c #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); SCB->VTOR = 0x08003000; SCB->SHCSR = 0; } #define BASE (500) //msec static void Buzzer_Beep(unsigned char tone, int duration) { const static unsigned short tone_value[] = {261,277,293,311,329,349,369,391,415,440,466,493,523,554,587,622,659,698,739,783,830,880,932,987}; TIM3_Out_Freq_Generation(tone_value[tone]); TIM2_Delay(duration); TIM3_Out_Stop(); } void Main(void) { Sys_Init(); // NVIC USART1 Pending clear NVIC_ClearPendingIRQ(37); // USART1 RX interrupt enable Macro_Set_Bit(USART1->CR1, 5); // NVIC USART1 interrupt enable NVIC_EnableIRQ(37); int i; enum key{C1, C1_, D1, D1_, E1, F1, F1_, G1, G1_, A1, A1_, B1, C2, C2_, D2, D2_, E2, F2, F2_, G2, G2_, A2, A2_, B2}; enum note{N16=BASE/4, N8=BASE/2, N4=BASE, N2=BASE*2, N1=BASE*4}; const int song1[][2] = { {G1,N4},{G1,N4},{E1,N8},{F1,N8},{G1,N4},{A1,N4},{A1,N4},{G1,N2},{G1,N4},{C2,N4},{E2,N4},{D2,N8},{C2,N8},{D2,N2} }; //const char * note_name[] = {"C1", "C1#", "D1", "D1#", "E1", "F1", "F1#", "G1", "G1#", "A1", "A1#", "B1", "C2", "C2#", "D2", "D2#", "E2", "F2", "F2#", "G2", "G2#", "A2", "A2#", "B2"}; TIM3_Out_Init(); Uart1_Printf("\nSong Play\n"); for(;;) { for(i=0; i<(sizeof(song1)/sizeof(song1[0])); i++) { //Uart1_Printf("%s ", note_name[song1[i][0]]); Buzzer_Beep(song1[i][0], song1[i][1]); } } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex44] #6 PWM ๊ธฐ๋ฐ LED ๋ฐ๊ธฐ ์ ์ด ๋ฐ ๋ฉ๋ก๋ ์ฐ์ฃผ ํตํฉ ์ค์ต
๋ฌธ์ ์ค๋ช PWM์ ์ด์ฉํด LED ๋ฐ๊ธฐ๋ฅผ ์ ์ดํ๊ณ , ๋์์ ๋ฏธ๋ฆฌ ์ ์๋ ์๊ณ ๋ฐฐ์ด(song1)์ ๋ฐ๋ผ ๋ถ์ ๋ก ์์ ์ ์ฐ์ฃผํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ. TIM4๋ PWM ์ถ๋ ฅ์ ์์ฑํ๋ฉฐ, UART๋ก ์ ๋ ฅ๋ ๊ฐ(1~9)์ ๋ฐ๋ผ ๋ํฐ๋น๋ฅผ ์กฐ์ ํด LED ๋ฐ๊ธฐ๋ฅผ ๋ณ๊ฒฝ ์ ๋ ฅ๊ฐ 0์ด ๋ค์ด์ค๋ฉด PWM ์ถ๋ ฅ์ ์ค๋จํ์ฌ LED๋ฅผ ๋ TIM3๊ณผ TIM2๋ฅผ ์ด์ฉํด ์๊ณ ์ฃผํ์์ ์ํ ๊ธธ์ด์ ๋ฐ๋ผ ๋ฉ๋ก๋๋ฅผ ๋ฐ๋ณต ์ฌ์ ์ฐ์ฃผ๋๋ ์์ UART๋ฅผ ํตํด ์๊ณ ์ด๋ฆ์ผ๋ก ์ถ๋ ฅ๋จ ์ ๋ต ์ฝ๋ #include "device_driver.h" #define BASE (500) static void Buzzer_Beep(unsigned char tone, int duration) { const static unsigned short tone_value[] = { 261,277,293,311,329,349,369,391,415,440,466,493, 523,554,587,622,659,698,739,783,830,880,932,987 }; TIM3_Out_Freq_Generation(tone_value[tone]); TIM2_Delay(duration - 20); TIM3_Out_Stop(); TIM2_Delay(20); } static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); } void Main(void) { Sys_Init(); TIM4_Out_Init(); TIM4_Out_Freq_Generation(1000, 0); TIM3_Out_Init(); Uart1_Printf("HomeWork (Verilog 6)\n"); char x; int i = 0; int state = 0; enum key { C1, C1_, D1, D1_, E1, F1, F1_, G1, G1_, A1, A1_, B1, C2, C2_, D2, D2_, E2, F2, F2_, G2, G2_, A2, A2_, B2 }; enum note { N16 = BASE/4, N8 = BASE/2, N4 = BASE, N2 = BASE*2, N1 = BASE*4 }; const int song1[][2] = { {G1,N4},{G1,N4},{E1,N8},{F1,N8},{G1,N4}, {A1,N4},{A1,N4},{G1,N2},{G1,N4},{C2,N4}, {E2,N4},{D2,N8},{C2,N8},{D2,N2} }; const char * note_name[] = { "C1", "C1#", "D1", "D1#", "E1", "F1", "F1#", "G1", "G1#", "A1", "A1#", "B1", "C2", "C2#", "D2", "D2#", "E2", "F2", "F2#", "G2", "G2#", "A2", "A2#", "B2" }; for(;;) { if(i == sizeof(song1)/ sizeof(song1[0])){ i = 0; } if(state == 0) { x = Uart1_Get_Pressed(); if (x != 0) { Uart1_Printf("Input: %d\n", x - '0'); if (x == '0') { TIM4->CCR3 = TIM4->ARR; } else { TIM4_Change_Duty_Rate(x - '0'); } } } Uart1_Printf("%s ", note_name[song1[i][0]]); Buzzer_Beep(song1[i][0], song1[i][1]); i++; } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex43] #5 UART ์ ๋ ฅ ๊ธฐ๋ฐ PWM LED ๋ฐ๊ธฐ ์ ์ด ์ค์ต
๋ฌธ์ ์ค๋ช TIM4 ํ์ด๋จธ๋ฅผ ์ด์ฉํ์ฌ PWM ์ ํธ๋ฅผ ์์ฑํ๊ณ , UART๋ก ์ ๋ ฅ๋ ์ซ์(0~9)์ ๋ฐ๋ผ LED ๋ฐ๊ธฐ๋ฅผ ์ ์ดํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ. TIM4 ์ฑ๋์ ์ฌ์ฉํ์ฌ PWM ํํ ์ถ๋ ฅ ์ ๋ ฅ๊ฐ 1~9๋ ๋ํฐ๋น๋ก ์ ์ฉ๋์ด LED ๋ฐ๊ธฐ๋ฅผ ๋ณ๊ฒฝ ์ ๋ ฅ๊ฐ 0์ PWM ์ถ๋ ฅ์ ์ ์ง์์ผ LED๋ฅผ ๋ UART ์ ๋ ฅ์ ํตํด ์ฌ์ฉ์์ ์ํธ์์ฉํ๋ฉฐ, ํ์ฌ ์ ๋ ฅ๊ฐ์ UART๋ก ๋ค์ ์ถ๋ ฅํจ ์ ๋ต ์ฝ๋ // timer.c #define TIM4_FREQ (1000000) // 1MHz #define TIM4_TICK (1000000/TIM4_FREQ) // usec #define TIME4_PLS_OF_1ms (1000/TIM4_TICK) void TIM4_Out_Init(void) { Macro_Set_Bit(RCC->APB1ENR, 2); // tim4 Macro_Set_Bit(RCC->APB2ENR, 3); // b port Macro_Write_Block(GPIOB->CRH, 0xF, 0xE, 0); //1110 open drain Macro_Write_Block(TIM4->CCMR2, 0xFF, 0x68, 0); TIM4->CCER = (0<<9)|(1<<8); } void TIM4_Out_PWM_Generation(unsigned int freq, unsigned int duty) { // Down Counter, Repeat Mode TIM4->CR1 = (1<<4)|(0<<3); // Timer ์ฃผํ์๊ฐ TIM4_FREQ๊ฐ ๋๋๋ก PSC ์ค์ TIM4->PSC = (unsigned int)((TIMXCLK / (double)TIM4_FREQ) + 0.5) - 1; // 1KHz // ์์ฒญํ ์ฃผํ์๊ฐ ๋๋๋ก ARR ์ค์ TIM4->ARR = (int)((double)TIM4_FREQ/freq + 0.5) - 1; // Duty Rate 50%๊ฐ ๋๋๋ก CCR3 ์ค์ TIM4->CCR3 = (int)(TIM4->ARR * (duty / 10.)); // Manual Update(UG ๋ฐ์) Macro_Set_Bit(TIM4->EGR, 0); // Timer Start Macro_Set_Bit(TIM4->CR1, 0); } void TIM4_Out_Stop(void) { Macro_Clear_Bit(TIM4->CR1, 0); } void TIM4_Change_Duty(unsigned int duty) { TIM4->CCR3 = (int)(TIM4->ARR * (duty / 10.)); } // main.c #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); } void Main(void) { Sys_Init(); TIM4_Out_Init(); char x; int timer_run = 0; TIM4_Out_PWM_Generation(1000, 0); for(;;) { x = Uart1_Get_Pressed(); if (x != 0) { int n = x - '0'; Uart1_Printf("%d", n); if (n == 0) { TIM4_Out_Stop(); timer_run = 0; } else { if (timer_run == 0) { TIM4_Out_PWM_Generation(1000, n); timer_run = 1; } else { TIM4_Change_Duty(n); } } } } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex42] #4 ๋์ผ ํ์ด๋จธ ๊ธฐ๋ฐ LED ๊น๋นก์ ์ ์ด ๋ฐ UART ์ฃผ๊ธฐ ์ถ๋ ฅ ์ค์ต
๋ฌธ์ ์ค๋ช UART๋ก ์ ๋ ฅ๋ ์ซ์(1~9)๋ฅผ ์ด์ฉํ์ฌ LED ๊น๋นก์ ์๋๋ฅผ ์กฐ์ ํ๊ณ , ์ผ์ ์ฃผ๊ธฐ๋ก UART๋ฅผ ํตํด ์ (.)์ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ. ์ซ์ 1~9 ์ ๋ ฅ ์ TIM4 ํ์ด๋จธ์ ์ฃผ๊ธฐ๋ฅผ ์กฐ์ ํ์ฌ LED ๊น๋นก์ ์๋ ๋ณ๊ฒฝ ์ซ์ 0 ์ ๋ ฅ ์ LED ๊น๋นก์์ ๋ฉ์ถค TIM2๋ฅผ ์ด์ฉํด ์ผ์ ์ฃผ๊ธฐ๋ก UART์ ์ (.) ์ถ๋ ฅ ๋ ํ์ด๋จธ๋ ๋ ๋ฆฝ์ ์ผ๋ก ๋์ํ๋ฉฐ, ๊น๋นก์๊ณผ ์ถ๋ ฅ์ด ๋์์ ์ด๋ฃจ์ด์ง ์ ๋ต ์ฝ๋ void Main(void) { Sys_Init(); char x; int n; int timer_run = 0; int led = 0; TIM4_Repeat(10); TIM2_Repeat(100); for(;;) { x = Uart1_Get_Pressed(); if(x != 0) { n = x - '0'; Uart1_Printf("%d", n); // 0? timer_run = 0; if (n==0) { timer_run = 0; } // 1~9? TIM4_Change_Value(n * 10); timer_run = 1 else { TIM4_Change_Value(n * 10); timer_run = 1; } } if(TIM4_Check_Timeout() && timer_run == 1) { //LED๋ฐ์ LED_Display(led ^= 1); } if(TIM2_Check_Timeout()) { Uart1_Printf("."); } } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex41] #3 UART ์ซ์ ์ ๋ ฅ ๋ฐ ๋ฌธ์์ด โ ์ ์ ๋ณํ ํจ์ ์ค์ต
๋ฌธ์ ์ค๋ช UART๋ฅผ ํตํด ์ต๋ 4์๋ฆฌ๊น์ง์ ์ซ์ ๋ฌธ์์ด์ ์ ๋ ฅ๋ฐ๊ณ , ์ด๋ฅผ ์ ์๋ก ๋ณํํ์ฌ ๋ฐํํ๋ ํจ์๋ฅผ ์์ฑํ๋ผ. ์ ๋ ฅ ๋ฌธ์๋ ํ ๊ธ์์ฉ UART ์์ ๋ ์ง์คํฐ์์ ์ฝ์ ์์ ๋ ๋ฌธ์๋ ์ฆ์ ์์ฝ(๋๋๋ ค ์ถ๋ ฅ) ์ฒ๋ฆฌ ์ ๋ ฅ์ด \r (Enter)์ผ ๊ฒฝ์ฐ ์ฆ์ ์ ๋ ฅ ์ข ๋ฃ ํ ์ ์ ๋ฐํ ์ซ์ ์ด์ธ ๋ฌธ์๋ ์ฒ๋ฆฌํ์ง ์์ผ๋ฉฐ, ์ต๋ 4์๋ฆฌ๊น์ง๋ง ์ ๋ ฅ ๊ฐ๋ฅ ์ ๋ต ์ฝ๋ int Uart_Get_Int_Num(void) { char s[10]; int i; int num = 0; for (i=0;i<4;i++) { while (!Macro_Check_Bit_Set(USART1->SR, 5)); s[i] = USART1->DR; while (!Macro_Check_Bit_Set(USART1->SR, 7)); USART1->DR = s[i]; if (s[i] == '\r') { return num; } num = num * 10 + (s[i] - '0'); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex40] #2 ์ธํฐ๋ฝ ๋ฐฉ์์ ๋ฒํผ ์ ๋ ฅ ๊ธฐ๋ฐ LED ํ ๊ธ ์ค์ต
๋ฌธ์ ์ค๋ช PA3 ํ์ ์ฐ๊ฒฐ๋ ๋ฒํผ ์ ๋ ฅ์ ๊ฐ์งํ์ฌ, ๋ฒํผ์ด ๋๋ฆด ๋๋ง๋ค PA2์ ์ฐ๊ฒฐ๋ LED์ ์ํ๋ฅผ ๋ฐ์ ์ํค๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ. ๋ฒํผ ์ ๋ ฅ์ ํ์ ์ค์ ๋ PA3 ํ์ ํตํด ์ฝ์ LED๋ PA2 ํ์ ์ฐ๊ฒฐ๋์ด ์์ผ๋ฉฐ, ๋ฒํผ์ด ๋๋ฆด ๋๋ง๋ค ํ ๊ธ๋จ ๋๋ฐ์ด์ฑ ํจ๊ณผ๋ฅผ ์ํด ์ธํฐ๋ฝ(interlock) ๋ฐฉ์ ์ฌ์ฉ ์ ๋ต ์ฝ๋ #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); } void Main(void) { Sys_Init(); Uart_Printf("PA3 Switch Input Toggling for LED on PA2\n"); Macro_Write_Block(GPIOA->CRL, 0xF, 0x8, 12); Macro_Write_Block(GPIOA->CRL, 0xF, 0x6, 8); Macro_Set_Bit(GPIOA->ODR, 3); int interlock = 0; for (;;) { if ((interlock == 0) && Macro_Check_Bit_Clear(GPIOA->IDR, 3)) { Macro_Invert_Bit(GPIOA->ODR, 2); interlock = 1; } else if ((interlock == 1) && Macro_Check_Bit_Set(GPIOA->IDR, 3)) { interlock = 0; } } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex39] #1 CMSIS ์ง์ ์ ๊ทผ์ ํตํ GPIOA LED ์ ๋ฉธ ์ค์ต
๋ฌธ์ ์ค๋ช CMSIS ๊ธฐ๋ฐ ๋ ์ง์คํฐ ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉํ์ฌ GPIOA ํ์ ์ง์ ์ ์ดํ๋ LED ์ ๋ฉธ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ. RCC ๋ ์ง์คํฐ๋ฅผ ์ด์ฉํด ํฌํธ A ํด๋ญ์ ํ์ฑํ GPIOA์ CRL, ODR ๋ ์ง์คํฐ๋ฅผ ์ง์ ์ค์ ํ์ฌ LED ON/OFF ๊ตฌํ LED ์ํ๋ฅผ ์ผ์ ์๊ฐ ๊ฐ๊ฒฉ์ผ๋ก ํ ๊ธํ์ฌ ๊น๋นก์ด๋๋ก ํจ ์ ๋ต ์ฝ๋ #include "device_driver.h" void Main(void) { volatile int i; Uart_Init(115200); Uart_Printf("CMSIS Based Register Define\n"); // ์ด ๋ถ๋ถ์ ์์ ํ์ง ๋ง ๊ฒ RCC->APB2ENR |= (1<<2); // LED Pin์ ์ถ๋ ฅ์ผ๋ก ์ค์ GPIOA->CRL = 0x600; for(;;) { // LED ๋ชจ๋ ON GPIOA->ODR = 0x0; for(i=0; i<0x40000; i++); // LED ๋ชจ๋ OFF GPIOA->ODR = 0x4; for(i=0; i<0x40000; i++); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex38] SYSTEM_SERVICE
๋ฌธ์ ์ค๋ช .global _SVC_Handler .type _SVC_Handler, %function _SVC_Handler: .extern SVC_FUNC @ SVC ํธ๋ค๋ฌ ์ค๊ณ ์ ๋ต ์ฝ๋ .global _SVC_Handler .type _SVC_Handler, %function _SVC_Handler: .extern SVC_FUNC PUSH {r4, lr} MRS r4, PSP LDR lr, [r4, #0x18] LDR lr, [lr, #-2] AND lr, lr, #0xFF LDR r12, =SVC_FUNC LDR r12, [r12, lr, lsl #2] BLX r12 STR r0, [r4] STR r1, [r4, #4] POP {r4, pc} ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex37] FAULT_HANDLER
๋ฌธ์ ์ค๋ช .syntax unified .thumb .text .extern MemManage_Handler .extern BusFault_Handler .extern UsageFault_Handler .global _HardFault_Handler .type _HardFault_Handler, %function _HardFault_Handler: @ ์ฝ๋ ์์ฑ .global _MemManage_Handler .type _MemManage_Handler, %function _MemManage_Handler: @ ์ฝ๋ ์์ฑ .global _BusFault_Handler .type _BusFault_Handler, %function _BusFault_Handler: @ ์ฝ๋ ์์ฑ .global _UsageFault_Handler .type _UsageFault_Handler, %function _UsageFault_Handler: @ ์ฝ๋ ์์ฑ .end ์ ๋ต ์ฝ๋ .syntax unified .thumb .text .extern HardFault_Handler .extern MemManage_Handler .extern BusFault_Handler .extern UsageFault_Handler .global _HardFault_Handler .type _HardFault_Handler, %function _HardFault_Handler: MOV R0, SP MOV R1, LR MRS R2, PSP B HardFault_Handler .global _MemManage_Handler .type _MemManage_Handler, %function _MemManage_Handler: MOV R0, SP MOV R1, LR MRS R2, PSP B MemManage_Handler .global _BusFault_Handler .type _BusFault_Handler, %function _BusFault_Handler: MOV R0, SP MOV R1, LR MRS R2, PSP B BusFault_Handler .global _UsageFault_Handler .type _UsageFault_Handler, %function _UsageFault_Handler: MOV R0, SP MOV R1, LR MRS R2, PSP B UsageFault_Handler .end ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex36] BASIC_HANDLER
๋ฌธ์ ์ค๋ช void Invalid_ISR(void) { /* ์ฝ๋ ์ค๊ณ */ Uart1_Printf("Invalid_Exception: %d!\n", ); Uart1_Printf("Invalid_ISR: %d!\n", ); for(;;); } void MemManage_Handler(void) { Uart1_Printf("Memory Management Fault\n"); /* ์ฝ๋ ์ค๊ณ */ for(;;); } void BusFault_Handler(void) { Uart1_Printf("Bus Fault\n"); /* ์ฝ๋ ์ค๊ณ */ for(;;); } void UsageFault_Handler(void) { Uart1_Printf("Usage Fault\n"); /* ์ฝ๋ ์ค๊ณ */ for(;;); } void HardFault_Handler(void) { Uart1_Printf("Hard Fault\n"); /* ์ฝ๋ ์ค๊ณ */ for(;;); } ์ ๋ต ์ฝ๋ void Invalid_ISR(void) { Uart1_Printf("Invalid_Exception: %d!\n", Macro_Extract_Area(SCB->ICSR, 0x1ff, 0)); Uart1_Printf("Invalid_ISR: %d!\n", Macro_Extract_Area(SCB->ICSR, 0x1ff, 0) - 16); for(;;); } void MemManage_Handler(void) { Uart1_Printf("Memory Management Fault\n"); Uart1_Printf("SHCSR: 0x%.8X\n", SCB->SHCSR); Uart1_Printf("ICSR: 0x%.8X\n", SCB->ICSR); Uart1_Printf("CFSR: 0x%.8X\n", SCB->CFSR); Uart1_Printf("MMFAR: 0x%.8X\n", SCB->MMFAR); for(;;); } void BusFault_Handler(void) { Uart1_Printf("Bus Fault\n"); Uart1_Printf("SHCSR: 0x%.8X\n", SCB->SHCSR); Uart1_Printf("ICSR: 0x%.8X\n", SCB->ICSR); Uart1_Printf("CFSR: 0x%.8X\n", SCB->CFSR); Uart1_Printf("BFAR: 0x%.8X\n", SCB->BFAR); for(;;); } void UsageFault_Handler(void) { Uart1_Printf("Usage Fault\n"); Uart1_Printf("SHCSR: 0x%.8X\n", SCB->SHCSR); Uart1_Printf("ICSR: 0x%.8X\n", SCB->ICSR); Uart1_Printf("CFSR: 0x%.8X\n", SCB->CFSR); for(;;); } void HardFault_Handler(void) { Uart1_Printf("Hard Fault\n"); Uart1_Printf("SHCSR: 0x%.8X\n", SCB->SHCSR); Uart1_Printf("ICSR: 0x%.8X\n", SCB->ICSR); Uart1_Printf("HFSR: 0x%.8X\n", SCB->HFSR); Uart1_Printf("CFSR: 0x%.8X\n", SCB->CFSR); Uart1_Printf("BFAR: 0x%.8X\n", SCB->BFAR); Uart1_Printf("MMFAR: 0x%.8X\n", SCB->MMFAR); for(;;); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex35] AAPCS_C์ธก ์ ์ญ๋ณ์ ๊ณต์ ํจ์ 2
๋ฌธ์ ์ค๋ช @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ void Asm_Var_Access(void); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .extern a .extern p .extern x .global Asm_Var_Access .type Asm_Var_Access, %function Asm_Var_Access: ์ ๋ต ์ฝ๋ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ void Asm_Var_Access(void); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .extern a .extern p .extern x .global Asm_Var_Access .type Asm_Var_Access, %function Asm_Var_Access: ldr r0, =a ldr r1, [r0] add r1, r1, #1 str r1, [r0] ldr r0, =p ldr r0, [r0] ldr r1, [r0] add r1, r1, #1 str r1, [r0] mov r2, #4 ldr r0, =x 1: ldr r1, [r0] add r1, r1, #1 str r1, [r0], #4 subs r2, r2, #1 bgt 1b bx lr ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex34] AAPCS_C์ธก ์ ์ญ๋ณ์ ๊ณต์ ํจ์ 1
๋ฌธ์ ์ค๋ช @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ void Asm_Var_Signed_Char(void); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .extern sc .global Asm_Var_Signed_Char .type Asm_Var_Signed_Char, %function Asm_Var_Signed_Char: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ void Asm_Var_Unsigned_Short(void); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .extern us .global Asm_Var_Unsigned_Short .type Asm_Var_Unsigned_Short, %function Asm_Var_Unsigned_Short: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ void Asm_Var_Signed_Short(void); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .extern ss .global Asm_Var_Signed_Short .type Asm_Var_Signed_Short, %function Asm_Var_Signed_Short: ์ ๋ต ์ฝ๋ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ void Asm_Var_Signed_Char(void); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .extern sc .global Asm_Var_Signed_Char .type Asm_Var_Signed_Char, %function Asm_Var_Signed_Char: ldr r1, =sc ldrsb r0, [r1] add r0, r0, #1 strb r0, [r1] bx lr @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ void Asm_Var_Unsigned_Short(void); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .extern us .global Asm_Var_Unsigned_Short .type Asm_Var_Unsigned_Short, %function Asm_Var_Unsigned_Short: ldr r1, =us ldrh r0, [r1] add r0, r0, #1 strh r0, [r1] bx lr @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ void Asm_Var_Signed_Short(void); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .extern ss .global Asm_Var_Signed_Short .type Asm_Var_Signed_Short, %function Asm_Var_Signed_Short: ldr r1, =ss ldrsh r0, [r1] add r0, r0, #1 strh r0, [r1] bx lr ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex33] AAPCS_Asm_Add_Sqr ํจ์
๋ฌธ์ ์ค๋ช @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ int Asm_Add_Sqr(int a, int b); @ Sqr์ ํธ์ถํ๋ฉด r0-r3,r12๋ ๋ณํ ๊ฐ๋ฅ์ฑ์ด ์์ @ ํ๊ดด ์ฐ๋ ค๊ฐ ์๋ ๋ณ์๋ ๋ํผ์ํด @ leaf function์ด ์๋๋ฏ๋ก lr์ ์ ์ฅํ๋ค @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .extern Sqr .global Asm_Add_Sqr .type Asm_Add_Sqr, %function Asm_Add_Sqr: @ int Asm_Add_Sqr(int a, int b); @ { @ return (Sqr(a)+Sqr(b)); @ } @ C์ ํจ์์ธ Sqr()์ ์ด์ฉ ์ ๋ต ์ฝ๋ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ int Asm_Add_Sqr(int a, int b); @ Sqr์ ํธ์ถํ๋ฉด r0-r3,r12๋ ๋ณํ ๊ฐ๋ฅ์ฑ์ด ์์ @ ํ๊ดด ์ฐ๋ ค๊ฐ ์๋ ๋ณ์๋ ๋ํผ์ํด @ leaf function์ด ์๋๋ฏ๋ก lr์ ์ ์ฅํ๋ค @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ .extern Sqr .global Asm_Add_Sqr .type Asm_Add_Sqr, %function Asm_Add_Sqr: @ int Asm_Add_Sqr(int a, int b); @ { @ return (Sqr(a)+Sqr(b)); @ } @ C์ ํจ์์ธ Sqr()์ ์ด์ฉ push {r4-r6, lr} mov r4, r1 bl Sqr mov r5, r0 mov r0, r4 bl Sqr add r0, r0, r5 pop {r4-r6, pc} ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex32] BOOT_CODE
๋ฌธ์ ์ค๋ช .syntax unified .thumb .text .word 0x20005000 .word __start .global __start .type __start, %function __start: .extern __RO_LIMIT__ .extern __RW_base__ .extern __ZI_base__ .extern __ZI_LIMIT__ ldr r0, =__RO_LIMIT__ ldr r1, =__RW_base__ ldr r3, =__ZI_base__ cmp r0, r1 beq 2f 1: @ RW ๋ณต์ฌ ์ฝ๋ ์์ฑ 2: ldr r1, =__ZI_LIMIT__ mov r2, #0x0 3: @ BSS ์ด๊ธฐํ ์ฝ๋ ์์ฑ .extern Main bl Main b . .end ์ ๋ต ์ฝ๋ .syntax unified .thumb .text .word 0x20005000 .word __start .global __start .type __start, %function __start: .extern __RO_LIMIT__ .extern __RW_BASE__ .extern __ZI_BASE__ .extern __ZI_LIMIT__ ldr r0, =__RO_LIMIT__ ldr r1, =__RW_BASE__ ldr r3, =__ZI_BASE__ cmp r0, r1 beq 2f 1: cmp r1, r3 ittt lo ldrlo r2, [r0], #4 strlo r2, [r1], #4 blo 1b 2: ldr r1, =__ZI_LIMIT__ mov r2, #0x0 3: cmp r3, r1 itt lo strlo r2, [r3], #4 blo 3b .extern Main bl Main b . .end ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex31] ASM_TOGGLING - 5ํ
๋ฌธ์ ์ค๋ช .syntax unified .thumb .text .word 0x20005000 .word __start .global __start .type __start, %function __start: .equ GPIOB_CRH, 0x40010C04 .equ GPIOB_ODR, 0x40010C0C .equ APB2ENR, 0x40021018 @ ์ด๋ถ๋ถ์ ์์ ํ์ง ๋ง ๊ฒ @ LDR r0, =APB2ENR LDR r1, =0x8 STR r1, [r0] @ ์ด๊ธฐ LED ๋ชจ๋ OFF @ LDR r0, =GPIOB_CRH LDR r1, [r0] BIC r1, r1, #0xFF<<0 ORR r1, r1, #0x66<<0 STR r1, [r0] LDR r0, =GPIOB_ODR LDR r1, [r0] ORR r1, r1, #0x3<<8 STR r1, [r0] @ ์ฌ๊ธฐ๋ถํฐ ์ฝ๋ ์์ฑ @ b . .end ์ ๋ต ์ฝ๋ .syntax unified .thumb .text .word 0x20005000 .word __start .global __start .type __start, %function __start: .equ GPIOB_CRH, 0x40010C04 .equ GPIOB_ODR, 0x40010C0C .equ APB2ENR, 0x40021018 LDR r0, =APB2ENR LDR r1, =0x8 STR r1, [r0] LDR r0, =GPIOB_CRH LDR r1, [r0] BIC r1, r1, #0xFF<<0 ORR r1, r1, #0x66<<0 STR r1, [r0] LDR r0, =GPIOB_ODR LDR r1, [r0] ORR r1, r1, #0x3<<8 STR r1, [r0] MOV r4, #10 1: LDR r3, =0xFFFFF 2: SUBS r3, r3, #1 BHI 2b LDR r1, [r0] EOR r1, r1, #0x3<<8 STR r1, [r0] SUBS r4, r4, #1 BHI 1b b . .end ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex30] ASM_TOGGLING
๋ฌธ์ ์ค๋ช .syntax unified .thumb .text .word 0x20005000 .word __start .global __start .type __start, %function __start: .equ GPIOB_CRH, 0x40010C04 .equ GPIOB_ODR, 0x40010C0C .equ APB2ENR, 0x40021018 @ ์ด๋ถ๋ถ์ ์์ ํ์ง ๋ง ๊ฒ @ LDR r0, =APB2ENR LDR r1, =0x8 STR r1, [r0] @ ์ด๊ธฐ LED ๋ชจ๋ OFF @ LDR r0, =GPIOB_CRH LDR r1, [r0] BIC r1, r1, #0xFF<<0 ORR r1, r1, #0x66<<0 STR r1, [r0] LDR r0, =GPIOB_ODR LDR r1, [r0] ORR r1, r1, #0x3<<8 STR r1, [r0] @ ์ฌ๊ธฐ๋ถํฐ ์ฝ๋ ์์ฑ @ b . .end ์ ๋ต ์ฝ๋ .syntax unified .thumb .text .word 0x20005000 .word __start .global __start .type __start, %function __start: .equ GPIOB_CRH, 0x40010C04 .equ GPIOB_ODR, 0x40010C0C .equ APB2ENR, 0x40021018 LDR r0, =APB2ENR LDR r1, =0x8 STR r1, [r0] LDR r0, =GPIOB_CRH LDR r1, [r0] BIC r1, r1, #0xFF<<0 ORR r1, r1, #0x66<<0 STR r1, [r0] LDR r0, =GPIOB_ODR LDR r1, [r0] ORR r1, r1, #0x3<<8 STR r1, [r0] 1: LDR r3, =0xFFFFF 2: SUBS r3, r3, #1 BHI 2b LDR r1, [r0] EOR r1, r1, #0x3<<8 STR r1, [r0] B 1b .end ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex29] ASM_BIT_OP
๋ฌธ์ ์ค๋ช .syntax unified .thumb .text .word 0x20005000 .word __start .global __start .type __start, %function __start: .equ GPIOB_CRH, 0x40010C04 .equ GPIOB_ODR, 0x40010C0C .equ APB2ENR, 0x40021018 @ ์ด๋ถ๋ถ์ ์์ ํ์ง ๋ง ๊ฒ @ LDR r0, =APB2ENR LDR r1, =0x8 STR r1, [r0] @ ์ฌ๊ธฐ๋ถํฐ ์ฝ๋ ์์ฑ @ b . .end ์ ๋ต ์ฝ๋ .syntax unified .thumb .text .word 0x20005000 .word __start .global __start .type __start, %function __start: .equ GPIOB_CRH, 0x40010C04 .equ GPIOB_ODR, 0x40010C0C .equ APB2ENR, 0x40021018 LDR r0, =APB2ENR LDR r1, =0x8 STR r1, [r0] LDR r0, =GPIOB_CRH LDR r1, [r0] BIC r1, r1, #0xFF<<0 ORR r1, r1, #0x66<<0 STR r1, [r0] LDR r0, =GPIOB_ODR LDR r1, [r0] BIC r1, r1, #0x3<<8 ORR r1, r1, #0x2<<8 STR r1, [r0] b . .end ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex28] ASM_LED_ON
๋ฌธ์ ์ค๋ช .syntax unified .thumb .text .word 0x20005000 .word __start .global __start .type __start, %function __start: .equ GPIOB_CRH, 0x40010C04 .equ GPIOB_ODR, 0x40010C0C .equ APB2ENR, 0x40021018 @ ์ด๋ถ๋ถ์ ์์ ํ์ง ๋ง ๊ฒ @ LDR r0, =APB2ENR LDR r1, =0x8 STR r1, [r0] @ ์ฌ๊ธฐ๋ถํฐ ์ฝ๋ ์์ฑ @ b . .end ์ ๋ต ์ฝ๋ .syntax unified .thumb .text .word 0x20005000 .word __start .global __start .type __start, %function __start: .equ GPIOB_CRH, 0x40010C04 .equ GPIOB_ODR, 0x40010C0C .equ APB2ENR, 0x40021018 LDR r0, =APB2ENR LDR r1, =0x8 STR r1, [r0] LDR r0, =GPIOB_CRH LDR r1, =0x66 STR r1, [r0] LDR r0, =GPIOB_ODR LDR r1, =0x0100 STR r1, [r0] b . .end ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex27] ๋ก๋ด ์ด๋ ์๊ฐ ์ธก์
๋ฌธ์ ์ค๋ช ๋ค์ ์๊ตฌํ๋ ๊ธฐ๋ฅ์ ๋ง์กฑํ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ผ [์๊ตฌ ์ฌํญ] ๋ก๋ด์ด ์ด๋์ ์์ํ ๋ KEY0๊ฐ ๋๋ฆฌ๊ณ ๋ชฉ์ ์ง์ ๋๋ฌํ๋ฉด KEY1์ด ๋๋ฆฐ๋ค๊ณ ๊ฐ์ ํ์ KEY0๋ฅผ ๋๋ฅด๊ณ KEY1์ ๋๋ฅด๋ฉด ๊ทธ ๊ฐ๊ฒฉ ์๊ฐ์ UART๋ก ์ธ์ํ๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ผ ๋ค๋ง, KEY0๋ฅผ ๋๋ฅธ ์ํ์์ ๋ค์ KEY0๋ฅผ ๋๋ฅด๋๊ฑด ๋ฌด์ํ๋ค. ์ฆ KEY0๋ฅผ ๋๋ฆฐ ๋ค ๊ทธ ๋๋ถํฐ KEY0๊ฐ ์ฌ๋ฌ๋ฒ ๋๋ ค๋ KEY1์ด ๋๋ฆด๋ ๊น์ง ์๊ฐ๋ง ์ธก์ ๋๋ฉด ๋๋ค ์๊ฐ์ 100msec ๋จ์(์ฆ, 0.1์ด ๋จ์๋ก ์ธก์ ํ๋ฉฐ ๋ฐ์ฌ๋ฆผํ๊ฑฐ๋ ๋ฒ๋ฆผํ๋ ๊ฒ์ ๋ฌด๋ฐฉํ๋ค)๋ก ์ธก์ ํ๋ค. (์๋ฅผ ๋ค์ด ์ค์ ์๊ฐ์ด 3.45์ด ์ผ ๊ฒฝ์ฐ ์ธ์ ๊ฒฐ๊ณผ๊ฐ 3.4 ๋๋ 3.5 ์ด๋ค ๊ฐ์ด๋ ๋ค ์๊ด์๋ค) ์ต๋ ์ธก์ ๊ฐ๋ฅ ์๊ฐ์ ๊ฑฐ์ ๋ฌด์ ํ์ผ๋ก ๋ณธ๋ค ๋ก์ง์๋ ๋ผ์ด์ ธ๋ฅผ KEY0์ KEY1์ ์ฐ๊ฒฐํ์ฌ ์ค์์น ๋๋ฆฐ ์ฌ์ด ์๊ฐ์ ํ์ธํ ์ ์๋๋ก ์ฐ๊ฒฐํ๋ค. [์ ์ถ ์๋ น] ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ฐ๋์ ๋์ ํ์ธ์ ๋ฐ์ํ ๊ฐ์ฌ ์ง์์ ์ํ์ฌ ์ฝ๋๋ฅผ ์ ๋ก๋ํด์ผ ํจ ์์๋ก ์ฝ๋ ์ ์ถ์ ํด๋น ๋ฌธํญ 0์ ์ฒ๋ฆฌ๋๋ฏ๋ก ์ฃผ์ ๋ฐ๋ ๋ค๋ฅธ ํ์ผ์ ์๋ ์์ ๋ ์ ๊ณต ๋๋ ์์ฑํ ์ฝ๋๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ๋ ๋ณต์ฌํ์ฌ ์ ์ถํ ํ์ ์์ (์) LED_Display ํจ์ ๋ฑ ์์ฑ ๋๋ ๊ธฐ์กด ์ฝ๋ ๋ณ๊ฒฝํ ํ์ผ์ ๋ด์ฉ(์ด ๋ฌธ์ ์ ๊ด๋ จํ ์ฝ๋ ๋ถ๋ถ๋ง)์ ๋ชจ๋ ๋ณต์ฌํ์ฌ ์ ์ถํด์ผ ํ๋ค. ์ ์ถ์์๋ main.c ๋ด์ฉ์ ์ ์ผ ์์ ๋๊ณ ๊ทธ ์๋์ ๋ค๋ฅธ ํ์ผ์ ์ฝ๋๋ฅผ ์ ์ถํ๋ main.c ์ด์ธ ํ์ผ์ ๋ฐ๋์ ํ์ผ๋ช ์ ์ฝ๋ ์์ ์ ๊ณ ๋ณต์ฌํ์ฌ ์ ์ถํ ๊ฒ ์ ๋ต ์ฝ๋ #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); SCB->VTOR = 0x08003000; SCB->SHCSR = 0; } extern volatile int Key_Value; extern volatile int TIM4_Expired; # if 1 void Main(void) { Sys_Init(); Key_ISR_Enable(1); int state = 0; double t = 0; for(;;) { if ((state == 0) && (Key_Value == 1)) { TIM4_Repeat_Interrupt_Enable(1, 100); state = 1; } if ((state == 1) && TIM4_Expired) { t += 0.1; TIM4_Expired = 0; } if ((state == 1) && (Key_Value == 2)) { TIM4_Stop(); Uart1_Printf("%.1f\n", t); state = 0; Key_Value = 0; break; } } } # endif ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex26] ํ์์
๋ฌธ์ ์ค๋ช ๋ค์ ์๊ตฌํ๋ ๊ธฐ๋ฅ์ ๋ง์กฑํ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ผ [์๊ตฌ ์ฌํญ] 3์ด ๋์์ KEY0๊ฐ ๋๋ฆฐ ํ์๋ฅผ ๊ตฌํ์ฌ UART๋ก ์ธ์ํ๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ผ. Chattering ๋ฌธ์ ๋ ๊ณ ๋ คํ ํ์๊ฐ ์์ผ๋ฉฐ ํ๋ก๊ทธ๋จ์ด ์์๋ ํ ์ฒ์ KEY0๊ฐ ๋๋ฆฐ ์์ ๋ถํฐ 3์ด ์ธก์ ์ ์์ํ๋ฉฐ 3์ด๊ฐ ์ข ๋ฃ๋๋ฉด ๊ทธ๋๊น์ง ๋๋ฆฐ KEY0 ํ์๋ฅผ ์ธ์ํ๋ค. ๋จ, KEY0๊ฐ ํ๋ฒ ๋๋ฆฌ๋ฉด ํ๋ฒ๋ง Count ๋์ด์ผ ํ๋ค. ์ฆ, ์ค๋ ๋๋ฆฌ๊ณ ์์ด๋ ํ๋ฒ์ผ๋ก ์ธ์ ๋์ด์ผ ํ๋ค. ์ธํฐ๋ฝํธ ์ฌ์ฉ ์ฌ๋ถ๋ ๋ฌด๋ฐฉํ๋ฉฐ ๋์ ๊ฒ์ฌ๋ฅผ ๋ฐ์ ํ ์ฝ๋๋ฅผ Bash ์ธ์ด๋ก ์ ์ถํ๋ค. [์ ์ถ ์๋ น] ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ฐ๋์ ๋์ ํ์ธ์ ๋ฐ์ํ ๊ฐ์ฌ ์ง์์ ์ํ์ฌ ์ฝ๋๋ฅผ ์ ๋ก๋ํด์ผ ํจ ์์๋ก ์ฝ๋ ์ ์ถ์ ํด๋น ๋ฌธํญ 0์ ์ฒ๋ฆฌ๋๋ฏ๋ก ์ฃผ์ ๋ฐ๋ ๋ค๋ฅธ ํ์ผ์ ์๋ ์์ ๋ ์ ๊ณต ๋๋ ์์ฑํ ์ฝ๋๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ๋ ๋ณต์ฌํ์ฌ ์ ์ถํ ํ์ ์์ (์) LED_Display ํจ์ ๋ฑ ์์ฑ ๋๋ ๊ธฐ์กด ์ฝ๋ ๋ณ๊ฒฝํ ํ์ผ์ ๋ด์ฉ(์ด ๋ฌธ์ ์ ๊ด๋ จํ ์ฝ๋ ๋ถ๋ถ๋ง)์ ๋ชจ๋ ๋ณต์ฌํ์ฌ ์ ์ถํด์ผ ํ๋ค. ์ ์ถ์์๋ main.c ๋ด์ฉ์ ์ ์ผ ์์ ๋๊ณ ๊ทธ ์๋์ ๋ค๋ฅธ ํ์ผ์ ์ฝ๋๋ฅผ ์ ์ถํ๋ main.c ์ด์ธ ํ์ผ์ ๋ฐ๋์ ํ์ผ๋ช ์ ์ฝ๋ ์์ ์ ๊ณ ๋ณต์ฌํ์ฌ ์ ์ถํ ๊ฒ ์ ๋ต ์ฝ๋ // main.c #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); SCB->VTOR = 0x08003000; SCB->SHCSR = 0; } extern volatile int Key_Value; extern volatile int TIM4_Expired; void Main(void) { Sys_Init(); Key_ISR_Enable(1); int cnt = 0; int state = 0; int pls_state = 0; for(;;) { if (Key_Value) { if (state == 0) { TIM4_Repeat_Interrupt_Enable(1, 1000); state = 1; cnt = 1; } else { cnt++; } Key_Value = 0; } if (TIM4_Expired) { pls_state++; if (pls_state >= 3) { TIM4_Stop(); state = 0; Uart1_Printf("%d\n", cnt); pls_state = 0; } TIM4_Expired = 0; } } } // stm32f10x_it.c volatile int Key_Value = 0; void EXTI9_5_IRQHandler(void) { Key_Value = Macro_Extract_Area(EXTI->PR, 0x1, 6); EXTI->PR = 0x1<<6; NVIC_ClearPendingIRQ(23); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex25] 3๋จ ๋ฐ๊ธฐ ์กฐ์ ๋จํ
๋ฌธ์ ์ค๋ช ๋ค์ ์๊ตฌํ๋ ๊ธฐ๋ฅ์ ๋ง์กฑํ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ผ [์๊ตฌ ์ฌํญ] ์ฝ๋๊ฐ ์คํ๋๋ฉด PA3์ duty 50%, 1khz ํ์ค๊ฐ ๊ณ์ ์ถ๋ ฅ๋๋ค. KEY0๋ฅผ ๋๋ฅด๋ฉด duty 20%๋ก ๋๊ณ KEY1์ ๋๋ฅด๋ฉด duty 80%๋ก ๋๋ค. ๋จ, duty 20% ์ํ์์ ๋ค์ KEY0๋ฅผ ๋๋ฅด๋ฉด duty๋ 50%๋ก ๋ณต๊ทํด์ผํ๋ค. ๋ง์ฐฌ๊ฐ์ง๋ก duty 80% ์ํ์์ ๋ค์ KEY1์ ๋๋ฅด๋ฉด duty 50%๋ก ๋ณต๊ทํด์ผํ๋ค. ๋ก์ง์๋ ๋ผ์ด์ ์ PA3์ ์ฐ๊ฒฐํ์ฌ ํํ์ ํ์ธํ๋ค. (์ฃผ์) ํ์ํ ์ฃผ๋ณ์ฅ์น ํด๋ก์ ์์์ Enableํด์ผํ๋ค. [์ ์ถ ์๋ น] ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ฐ๋์ ๋์ ํ์ธ์ ๋ฐ์ํ ๊ฐ์ฌ ์ง์์ ์ํ์ฌ ์ฝ๋๋ฅผ ์ ๋ก๋ํด์ผ ํจ ์์๋ก ์ฝ๋ ์ ์ถ์ ํด๋น ๋ฌธํญ 0์ ์ฒ๋ฆฌ๋๋ฏ๋ก ์ฃผ์ ๋ฐ๋ ๋ค๋ฅธ ํ์ผ์ ์๋ ์์ ๋ ์ ๊ณต ๋๋ ์์ฑํ ์ฝ๋๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ๋ ๋ณต์ฌํ์ฌ ์ ์ถํ ํ์ ์์ (์) LED_Display ํจ์ ๋ฑ ์์ฑ ๋๋ ๊ธฐ์กด ์ฝ๋ ๋ณ๊ฒฝํ ํ์ผ์ ๋ด์ฉ(์ด ๋ฌธ์ ์ ๊ด๋ จํ ์ฝ๋ ๋ถ๋ถ๋ง)์ ๋ชจ๋ ๋ณต์ฌํ์ฌ ์ ์ถํด์ผ ํ๋ค. ์ ์ถ์์๋ main.c ๋ด์ฉ์ ์ ์ผ ์์ ๋๊ณ ๊ทธ ์๋์ ๋ค๋ฅธ ํ์ผ์ ์ฝ๋๋ฅผ ์ ์ถํ๋ main.c ์ด์ธ ํ์ผ์ ๋ฐ๋์ ํ์ผ๋ช ์ ์ฝ๋ ์์ ์ ๊ณ ๋ณต์ฌํ์ฌ ์ ์ถํ ๊ฒ ์ ๋ต ์ฝ๋ #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); SCB->VTOR = 0x08003000; SCB->SHCSR = 0; } extern volatile int Key_Value; void Main(void) { Sys_Init(); TIM2_Out_Init(); Key_ISR_Enable(1); TIM2_Out_PWM_Generation(1000, 5); int state_key0 = 0; int state_key1 = 0; for(;;) { if (Key_Value) { if (Key_Value == 1) { if (state_key0 == 0) { TIM2_Change_Duty(2); state_key0 = 1; } else { TIM2_Change_Duty(5); state_key0 = 0; } state_key1 = 0; } if (Key_Value == 2) { if (state_key1 == 0) { TIM2_Change_Duty(8); state_key1 = 1; } else { TIM2_Change_Duty(5); state_key1 = 0; } state_key0 = 0; } Key_Value = 0; } } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex24] ๋์งํธ ํผ์๋ ธ
๋ฌธ์ ์ค๋ช ๋ค์ ์๊ตฌํ๋ ๊ธฐ๋ฅ์ ๋ง์กฑํ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ผ [์๊ตฌ ์ฌํญ] PC ํฐ๋ฏธ๋ ํ๋ก๊ทธ๋จ์์ ํค๋ณด๋๋ก 1,2,3,4,5,6,7,8 ์ ๋๋ฅด๋ฉด ๋ ๋ถํฐ ๋์ ๋๊น์ง ์์ 4๋ถ ์ํ๋ก ์ฐ์ฃผํ๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ผ 1์ ๋ 2๋ ๋ 3์ ๋ฏธ โฆ 8์ ๋์ ๋๊ฐ 4๋ถ ์ํ ๊ธธ์ด(500msec) ์ฐ์ฃผ๋์ด์ผ ํ๋ค. ํ๋์ ์์ด ์ฐ์ฃผ๋๋ ๋์ ํค๋ณด๋ ์ ๋ ฅ์ด ์๋ ๊ฒฝ์ฐ ๋ฌด์ํ๋ฉด ๋๋ค. ์ธํฐ๋ฝํธ ์ฌ์ฉ ์ฌ๋ถ๋ ๋ฌด๊ดํ๋ฉฐ ์ฌ๋ฌ ํ์ผ์ ์ฝ๋๋ฅผ ์์ฑํ ๊ฒฝ์ฐ ์์ฑํ ์ฝ๋ ๋ชจ๋ ๋ณต์ฌํ์ฌ Bash ์ธ์ด๋ก ์ ์ถํด์ผ ํ๋ค. [์ ์ถ ์๋ น] ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ฐ๋์ ๋์ ํ์ธ์ ๋ฐ์ํ ๊ฐ์ฌ ์ง์์ ์ํ์ฌ ์ฝ๋๋ฅผ ์ ๋ก๋ํด์ผ ํจ ์์๋ก ์ฝ๋ ์ ์ถ์ ํด๋น ๋ฌธํญ 0์ ์ฒ๋ฆฌ๋๋ฏ๋ก ์ฃผ์ ๋ฐ๋ ๋ค๋ฅธ ํ์ผ์ ์๋ ์์ ๋ ์ ๊ณต ๋๋ ์์ฑํ ์ฝ๋๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ๋ ๋ณต์ฌํ์ฌ ์ ์ถํ ํ์ ์์ (์) LED_Display ํจ์ ๋ฑ ์์ฑ ๋๋ ๊ธฐ์กด ์ฝ๋ ๋ณ๊ฒฝํ ํ์ผ์ ๋ด์ฉ(์ด ๋ฌธ์ ์ ๊ด๋ จํ ์ฝ๋ ๋ถ๋ถ๋ง)์ ๋ชจ๋ ๋ณต์ฌํ์ฌ ์ ์ถํด์ผ ํ๋ค. ์ ์ถ์์๋ main.c ๋ด์ฉ์ ์ ์ผ ์์ ๋๊ณ ๊ทธ ์๋์ ๋ค๋ฅธ ํ์ผ์ ์ฝ๋๋ฅผ ์ ์ถํ๋ main.c ์ด์ธ ํ์ผ์ ๋ฐ๋์ ํ์ผ๋ช ์ ์ฝ๋ ์์ ์ ๊ณ ๋ณต์ฌํ์ฌ ์ ์ถํ ๊ฒ ์ ๋ต ์ฝ๋ # if 1 // main.c #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); } #define BASE (500) // 4๋ถ ์ํ = 500ms static void Play_Tone(unsigned short freq) { TIM3_Out_Freq_Generation(freq); // PWM ์ถ๋ ฅ ์์ TIM2_Delay(BASE); // 500ms ์ ์ง TIM3_Out_Stop(); } volatile char dummy; void Flush_UART_Buffer(void) { while (Macro_Check_Bit_Set(USART1->SR, 5)) { dummy = USART1->DR; } } void Main(void) { const unsigned short tone_value[] = {261, 293, 329, 349, 392, 440, 493, 523}; char input; Sys_Init(); TIM3_Out_Init(); Uart1_Printf("[8323] C9\n"); while (1) { input = Uart1_Get_Char(); // ์ ๋ ฅ ๋๊ธฐ if (input >= '1' && input <= '8') { int index = input - '1'; Play_Tone(tone_value[index]); Flush_UART_Buffer(); } } } # endif # if 1 // timer.c #define TIM3_FREQ 8000000 #define TIM3_TICK (1000000 / TIM3_FREQ) #define TIME3_PLS_OF_1ms (1000 / TIM3_TICK) #define TIM2_TICK 20 #define TIM2_FREQ (1000000 / TIM2_TICK) #define TIME2_PLS_OF_1ms (1000 / TIM2_TICK) void TIM3_Out_Init(void) { Macro_Set_Bit(RCC->APB1ENR, 1); // TIM3 enable Macro_Set_Bit(RCC->APB2ENR, 3); // GPIOB enable Macro_Write_Block(GPIOB->CRL, 0xf, 0xb, 0); // PB0: Alternate function output Macro_Write_Block(TIM3->CCMR2, 0x7, 0x6, 4); // PWM mode 1 TIM3->CCER = (0 << 9) | (1 << 8); // Output enable } void TIM3_Out_Freq_Generation(unsigned short freq) { TIM3->PSC = (unsigned int)((TIMXCLK / (double)TIM3_FREQ) + 0.5) - 1; TIM3->ARR = (unsigned int)((double)TIM3_FREQ / freq) - 1; TIM3->CCR3 = TIM3->ARR / 2; Macro_Set_Bit(TIM3->EGR, 0); TIM3->CR1 = (1 << 4) | (0 << 3) | (0 << 1) | (1 << 0); } void TIM3_Out_Stop(void) { Macro_Clear_Bit(TIM3->CR1, 0); Macro_Clear_Bit(TIM3->DIER, 0); } void TIM2_Delay(int time_ms) { Macro_Set_Bit(RCC->APB1ENR, 0); TIM2->PSC = (unsigned int)((TIMXCLK / (double)TIM2_FREQ) + 0.5) - 1; TIM2->ARR = TIME2_PLS_OF_1ms * time_ms; Macro_Set_Bit(TIM2->EGR, 0); Macro_Clear_Bit(TIM2->SR, 0); Macro_Set_Bit(TIM2->DIER, 0); Macro_Set_Bit(TIM2->CR1, 0); while (Macro_Check_Bit_Clear(TIM2->SR, 0)); Macro_Clear_Bit(TIM2->CR1, 0); Macro_Clear_Bit(TIM2->DIER, 0); } # endif ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex23] UART Echo-Back ํ๋ฉด์ LED ๊น๋ฐ์ด๊ธฐ
๋ฌธ์ ์ค๋ช ๋ค์ ์๊ตฌํ๋ ๊ธฐ๋ฅ์ ๋ง์กฑํ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ผ [์๊ตฌ ์ฌํญ] UART1์ ํตํ์ฌ PC๋ก๋ถํฐ ์ ๋ ฅ ๋ฐ์ ๊ธ์๋ฅผ ๋ค์ PC๋ก ์ ์กํ๋ค. ํ์ด๋จธ๋ฅผ ์ด์ฉํ์ฌ LED0๋ฅผ 1์ด ON, 1์ด OFF๋ฅผ ๊ณ์ ๋ฐ๋ณตํ๋๋ก ํ๋ค. ํด๋ง ๋ฐ ์ธํฐ๋ฝํธ ์ฌ์ฉ์ฌ๋ถ๋ ๋ฌด๊ดํ๋ค. ๋จ, UART์ ์ ์ถ๋ ฅ ๋์์ LED ๋์๊ณผ ๋ฌด๊ดํ๊ฒ ์ง์ฐ์์ด ๋์๋์ด์ผ ํ๋ค. [์ ์ถ ์๋ น] ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ฐ๋์ ๋์ ํ์ธ์ ๋ฐ์ํ ๊ฐ์ฌ ์ง์์ ์ํ์ฌ ์ฝ๋๋ฅผ ์ ๋ก๋ํด์ผ ํจ ์์๋ก ์ฝ๋ ์ ์ถ์ ํด๋น ๋ฌธํญ 0์ ์ฒ๋ฆฌ๋๋ฏ๋ก ์ฃผ์ ๋ฐ๋ ๋ค๋ฅธ ํ์ผ์ ์๋ ์์ ๋ ์ ๊ณต ๋๋ ์์ฑํ ์ฝ๋๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ๋ ๋ณต์ฌํ์ฌ ์ ์ถํ ํ์ ์์ (์) LED_Display ํจ์ ๋ฑ ์์ฑ ๋๋ ๊ธฐ์กด ์ฝ๋ ๋ณ๊ฒฝํ ํ์ผ์ ๋ด์ฉ(์ด ๋ฌธ์ ์ ๊ด๋ จํ ์ฝ๋ ๋ถ๋ถ๋ง)์ ๋ชจ๋ ๋ณต์ฌํ์ฌ ์ ์ถํด์ผ ํ๋ค. ์ ์ถ์์๋ main.c ๋ด์ฉ์ ์ ์ผ ์์ ๋๊ณ ๊ทธ ์๋์ ๋ค๋ฅธ ํ์ผ์ ์ฝ๋๋ฅผ ์ ์ถํ๋ main.c ์ด์ธ ํ์ผ์ ๋ฐ๋์ ํ์ผ๋ช ์ ์ฝ๋ ์์ ์ ๊ณ ๋ณต์ฌํ์ฌ ์ ์ถํ ๊ฒ ์ ๋ต ์ฝ๋ // main.c #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); SCB->VTOR = 0x08003000; SCB->SHCSR = 0; } void Main(void) { Sys_Init(); Uart1_Printf("UART Echo Mode\n"); Macro_Set_Bit(USART1->CR1, 5); // UART1 ์ธํฐ๋ฝํธ ํ์ฉ NVIC_ClearPendingIRQ(USART1_IRQn); NVIC_EnableIRQ(USART1_IRQn); while (1) { LED_Display(1); TIM2_Delay(1000); LED_Display(0); TIM2_Delay(1000); } } // stm32f10x_it.c void USART1_IRQHandler(void) { if (USART1->SR & (1 << 5)) { char ch = USART1->DR; while (!(USART1->SR & (1 << 7))); USART1->DR = ch; } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex22] ๋นํธ ๊ณ์ฐ AI
๋ฌธ์ ์ค๋ช ๋ค์ ์ฃผ์ด์ง ์ฝ๋๋ฅผ ์ด์ฉํ์ฌ ์๊ตฌํ๋ ๊ธฐ๋ฅ์ ์ํํ๋ func ํจ์๋ฅผ ์ค๊ณํ๋ผ. ๋จ, func ํจ์ ์ด์ธ ๋ค๋ฅธ ์ฝ๋๋ ์ ๋ ์์ ํ๋ฉด ์๋๋ค. [์๊ตฌ ์ฌํญ] ํจ์ func๋ ์ ์ x๋ฅผ ์ ๋ ฅ ๋ฐ๊ณ ์ ์๋ฅผ ๋ฆฌํดํ๋ค. x์ 0๋ฒ ๋นํธ๋ฅผ 0์ผ๋ก ๋ง๋ ๋ค. x์ 4,5,6,7๋ฒ ๋นํธ๋ฅผ 1๋ก ๋ง๋ ๋ค. x์ 23๋ฒ ๋นํธ๋ฅผ ๋ฐ์ ์ํจ๋ค. ๋ค๋ฅธ ๋นํธ๋ ์๋ ๊ฐ์ ์ ์งํด์ผํ๋ค. ๋ณ๊ฒฝ๋ x๊ฐ์ ๋ฆฌํดํ๋ค. ๋จ, ์ฝ๋๊ฐ ์ ์์ ์ด๋ผ๋ฉด ์๋์ ๊ฐ์ ๊ฐ์ด ์ธ์ ๋์ด์ผ ํ๋ค. #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); Uart_Init(115200); } unsigned int func(unsigned int x) { // ์ฝ๋ ๊ตฌํ return x; } void Main(void) { Sys_Init(); Uart_Printf("0x%.8X\n", func(0xFFFFFFFFu)); Uart_Printf("0x%.8X\n", func(0x00000000u)); Uart_Printf("0x%.8X\n", func(0x55555555u)); Uart_Printf("0x%.8X\n", func(0xCCCCCCCCu)); Uart_Printf("0x%.8X\n", func(0xAAAAAAAAu)); Uart_Printf("0x%.8X\n", func(0x33333333u)); } [์ถ๋ ฅ๊ฐ] 0xFF7FFFFE 0x008000F0 0x55D555F4 0xCC4CCCFC 0xAA2AAAFA 0x33B333F2 [์ ์ถ ์๋ น] ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ฐ๋์ ๋์ ํ์ธ์ ๋ฐ์ํ ๊ฐ์ฌ ์ง์์ ์ํ์ฌ ์ฝ๋๋ฅผ ์ ๋ก๋ํด์ผํจ ์์๋ก ์ฝ๋ ์ ์ถ์ ํด๋น ๋ฌธํญ 0์ ์ฒ๋ฆฌ๋๋ฏ๋ก ์ฃผ์ ๋ฐ๋ ์ ์ฝ๋๋ฅผ main.c์ ๋ณต์ฌํ์ฌ ์์ฑํ ๊ฒ ๋ค๋ฅธ ํ์ผ์ ์๋ ์์ ๋ ์ฌ์ฉํ ์ฝ๋ ๋ฑ์ ํธ์ถํ์ฌ ์ฌ์ฉํ ๊ฒฝ์ฐ ๋ฐ๋์ ์ด๋ํ์ฌ ์ฌ์ฉํ ๊ฒ ์ฝ๋๋ main.c์ ์ ์ฒด๋ฅผ ๋ณต์ฌํ์ฌ ์ ์ถํ ๊ฒ ์ ๋ต ์ฝ๋ #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); Uart_Init(115200); } unsigned int func(unsigned int x) { Macro_Clear_Bit(x, 0); Macro_Write_Block(x, 0xf, 0xf, 4); Macro_Invert_Bit(x, 23); return x; } void Main(void) { Sys_Init(); Uart_Printf("0x%.8X\n", func(0xFFFFFFFFu)); Uart_Printf("0x%.8X\n", func(0x00000000u)); Uart_Printf("0x%.8X\n", func(0x55555555u)); Uart_Printf("0x%.8X\n", func(0xCCCCCCCCu)); Uart_Printf("0x%.8X\n", func(0xAAAAAAAAu)); Uart_Printf("0x%.8X\n", func(0x33333333u)); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex21] ํ๊ฐ๋ฌธ์ 2
๋ฌธ์ ์ค๋ช ๋ค์ ์๊ตฌํ๋ ๊ธฐ๋ฅ์ ๋ง์กฑํ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ผ [์๊ตฌ ์ฌํญ] ๋ณด๋์ KEY0๋ฅผ ๋๋ฅด๋ฉด B Port 0๋ฒ ํ(PB0)์ 1๋ก KEY0๋ฅผ ๋ผ๋ฉด PPB0๋ฅผ 0์ผ๋ก ์ถ๋ ฅํ๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ผ ๋ก์ง์๋ ๋ผ์ด์ ธ ์์์ ์ฑ๋์ KEY0 ์ ํธ์ PB0๋ฅผ ์ฐ๊ฒฐํ๋ค KEY0 ์ ํธ๋ฅผ Falling Edge Trigger๋ก ์ค์ ํ ํ KEY0์ PB0 ํํ์ ํ์ธํ ์ ์๋๋ก ํด์ผ ํ๋ค. (์ฃผ์) B ํฌํธ์ ํด๋ก์ด ํ์ฑํ(์ ์ ์ธ๊ฐ)๋์ด์ผ ํ๋ฏ๋ก ๋ค์ ์ฝ๋๋ฅผ ๊ผญ ์์๋ถ๋ถ์ ์ถ๊ฐํ๋ค. RCC_APB2ENR |= (1<<3); [์ ์ถ ์๋ น] ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ฐ๋์ ๋์ ํ์ธ์ ๋ฐ์ํ ๊ฐ์ฌ ์ง์์ ์ํ์ฌ ์ฝ๋๋ฅผ ์ ๋ก๋ํด์ผํจ ์์๋ก ์ฝ๋ ์ ์ถ์ ํด๋น ๋ฌธํญ 0์ ์ฒ๋ฆฌ๋๋ฏ๋ก ์ฃผ์ ๋ฐ๋ ๋ชจ๋ ์ฝ๋๋ main.c ํ๋์ ํ์ผ์๋ง ์์ฑํ ๊ฒ ๋ค๋ฅธ ํ์ผ์ ์๋ ์์ ๋ ์ ๊ณต ๋๋ ์์ฑํ ์ฝ๋๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ๋ ๋ณต์ฌํ์ฌ ์ ์ถํ ํ์ ์์ (์) LED_Display ํจ์ ๋ฑ ์ฝ๋๋ ์์ฑํ main.c์ ์ ์ฒด๋ฅผ ๋ณต์ฌํ์ฌ ์ ์ถํ ๊ฒ (๋ค๋ง ์์ฑ ์ฝ๋์ ๋ฌด๊ดํ ๊ธฐ์กด ์ฝ๋๋ ์ ๊ฑฐํ ๊ฒ) ์ ๋ต ์ฝ๋ #include "device_driver.h" void Key_Init(void) { Macro_Write_Block(GPIOB->CRL, 0xf, 0x8, 24); Macro_Set_Bit(GPIOB->ODR, 6); } void LED_Init(void) { Macro_Write_Block(GPIOB->CRL, 0xf, 0x2, 0); Macro_Clear_Bit(GPIOB->ODR, 0); } #define N 10000 int Key_Get_Pressed(void) { int cnt = 0; int before = Macro_Extract_Area(GPIOB->IDR, 0x1, 6); while (1) { int after = Macro_Extract_Area(GPIOB->IDR, 0x1, 6); if (after == before) { cnt++; if (cnt >= N) return after == 0 ? 1 : 0; } else { cnt = 0; before = after; } } } static void Sys_Init(void) { Key_Init(); LED_Init(); Uart_Init(115200); } void Main(void) { Macro_Set_Bit(RCC->APB2ENR, 3); Sys_Init(); int interlock = 0; for (;;) { if ((interlock == 0) && (Key_Get_Pressed() == 1)) { Macro_Set_Bit(GPIOB->ODR, 0); interlock = 1; } else if ((interlock == 1) && (Key_Get_Pressed() == 0)) { Macro_Clear_Bit(GPIOB->ODR, 0); interlock = 0; } } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex20] ํ๊ฐ๋ฌธ์ 1
๋ฌธ์ ์ค๋ช ๋ค์ ์๊ตฌํ๋ ๊ธฐ๋ฅ์ ๋ง์กฑํ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ผ [์๊ตฌ ์ฌํญ] LCD ๋ณด๋์ ๋ฐฉํฅํค(JOG)์ DOWN Key๋ PB5๋ฒ์ ์ฐ๊ฒฐ๋์ด ์๋ค. ๋จ, ์ด KEY๋ Pullup ์ ํญ์ ์ฐ๊ฒฐํ์ง ์์ ์ํ๋ผ ๋๋ฅด๋ฉด GND์ ์ฐ๊ฒฐ๋์ด 0์ด์ง๋ง ๋ผ๋ฉด Floating ์ํ๊ฐ ๋๋ค. ์ด JOG์ DWON Key๋ฅผ ๋๋ฅด๋ฉด LED0์ด ON๋๊ณ ๋ผ๋ฉด OFF๊ฐ ๋๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ผ (์ฃผ์) ์ธ๋ถ์ Pullup ์ ํญ์ด ์์ผ๋ฏ๋ก ๋ง์ด์ปด ๋ด๋ถ์ Pullup ์ ํญ์ ์ฌ์ฉํ๋๋ก ์ค์ ํ์ฌ์ผ ํ๋ค. ๋ด๋ถ Pullup ์ฌ์ฉ์ ๊ต์ฌ 221 ํ์ด์ง ํ๋ฅผ ์ฐธ๊ณ ํ๋ผ (Input Pullup, Pulldown ๋ชจ๋ 1000์ผ๋ก ์ค์ ํ๋ ODR ๋ ์ง์คํฐ์ ํด๋น ๋นํธ๋ฅผ 0์ผ๋ก ํ๋ฉด Pull-down, 1๋กํ๋ฉด Pull-up ์ฌ์ฉ์ด enable ๋๋ค.) (์ฃผ์) B ํฌํธ์ ํด๋ก์ด ํ์ฑํ(์ ์ ์ธ๊ฐ)๋์ด์ผ ํ๋ฏ๋ก ๋ค์ ์ฝ๋๋ฅผ ๊ผญ ์์๋ถ๋ถ์ ์ถ๊ฐํ๋ค. RCC_APB2ENR |= (1<<3); [์ ์ถ ์๋ น] ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ฐ๋์ ๋์ ํ์ธ์ ๋ฐ์ํ ๊ฐ์ฌ ์ง์์ ์ํ์ฌ ์ฝ๋๋ฅผ ์ ๋ก๋ํด์ผ ํจ ์์๋ก ์ฝ๋ ์ ์ถ์ ํด๋น ๋ฌธํญ 0์ ์ฒ๋ฆฌ๋๋ฏ๋ก ์ฃผ์ ๋ฐ๋ ๋ชจ๋ ์ฝ๋๋ main.c ํ๋์ ํ์ผ์๋ง ์์ฑํ ๊ฒ ๋ค๋ฅธ ํ์ผ์ ์๋ ์์ ๋ ์ ๊ณต ๋๋ ์์ฑํ ์ฝ๋๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ๋ ๋ณต์ฌํ์ฌ ์ ์ถํ ํ์ ์์ (์) LED_Display ํจ์ ๋ฑ ์ฝ๋๋ ์์ฑํ main.c์ ์ ์ฒด๋ฅผ ๋ณต์ฌํ์ฌ ์ ์ถํ ๊ฒ (๋ค๋ง ์์ฑ ์ฝ๋์ ๋ฌด๊ดํ ๊ธฐ์กด ์ฝ๋๋ ์ ๊ฑฐํ ๊ฒ) ์ ๋ต ์ฝ๋ #include "device_driver.h" void Main(void) { Macro_Set_Bit(RCC->APB2ENR, 3); Macro_Write_Block(GPIOB->CRL, 0xf, 0x8, 20); Macro_Set_Bit(GPIOB->ODR, 5); Macro_Write_Block(GPIOB->CRH, 0xf, 0x6, 0); Macro_Set_Bit(GPIOB->ODR, 8); while (1) { if (Macro_Check_Bit_Clear(GPIOB->IDR, 5)) Macro_Clear_Bit(GPIOB->ODR, 8); else Macro_Set_Bit(GPIOB->ODR, 8); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex19] TIMER_EVENT_LAB
๋ฌธ์ ์ค๋ช // timer.c void TIM4_Repeat_Interrupt_Enable(int en, int time) { if(en) { Macro_Set_Bit(RCC->APB1ENR, 2); TIM4->CR1 = (1<<4)|(0<<3); TIM4->PSC = (unsigned int)(TIMXCLK/(double)TIM4_FREQ + 0.5)-1; TIM4->ARR = TIME4_PLS_OF_1ms * time; Macro_Set_Bit(TIM4->EGR,0); // TIM4->SR ๋ ์ง์คํฐ์์ Timer Pending Clear // NVIC์์ 30๋ฒ ์ธํฐ๋ฝํธ Pending Clear => NVIC์ฉ Macro ์ฌ์ฉ // TIM4->DIER ๋ ์ง์คํฐ์์ Timer ์ธํฐ๋ฝํธ ํ์ฉ // NVIC์์ 30๋ฒ ์ธํฐ๋ฝํธ๋ฅผ ํ์ฉ์ผ๋ก ์ค์ => NVIC์ฉ Macro ์ฌ์ฉ // TIM4 Start } else { NVIC_DisableIRQ(30); Macro_Clear_Bit(TIM4->CR1, 0); Macro_Clear_Bit(TIM4->DIER, 0); } } ์ ๋ต ์ฝ๋ void TIM4_Repeat_Interrupt_Enable(int en, int time) { if(en) { Macro_Set_Bit(RCC->APB1ENR, 2); TIM4->CR1 = (1<<4)|(0<<3); TIM4->PSC = (unsigned int)(TIMXCLK/(double)TIM4_FREQ + 0.5)-1; TIM4->ARR = TIME4_PLS_OF_1ms * time; Macro_Set_Bit(TIM4->EGR,0); Macro_Clear_Bit(TIM4->SR, 0); NVIC_ClearPendingIRQ(30); Macro_Set_Bit(TIM4->DIER, 0); NVIC_EnableIRQ(30); Macro_Set_Bit(TIM4->CR1, 0); } else { NVIC_DisableIRQ(30); Macro_Clear_Bit(TIM4->CR1, 0); Macro_Clear_Bit(TIM4->DIER, 0); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex18] EXTI_IRQ_LAB
๋ฌธ์ ์ค๋ช // main.c #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); SCB->VTOR = 0x08003000; SCB->SHCSR = 0; } void Main(void) { Sys_Init(); Uart1_Printf("EXTI Test\n"); // AFIO, Port-B Clock Enable // PB[7:6]์ ์ ๋ ฅ์ผ๋ก ์ ์ธ // PB[7:6]์ EXTI ์์ค๋ก ์ค์ ํ๊ณ Falling edge ์ ํ, EXTI[7:6] ์ธํฐ๋ฝํธ ํ์ฉ // EXTI[7:6] Pending Clear ๋ฐ NVIC์ ์ธํฐ๋ฝํธ Pending clear // EXTI9_5 ์ธํฐ๋ฝํธ ํ์ฉ for(;;) { LED_Display(1); TIM2_Delay(500); LED_Display(2); TIM2_Delay(500); } } // stm32f10x_it.c void EXTI9_5_IRQHandler(void) { // EXTI Pending์ ํ์ธํ์ฌ ๋๋ฆฐํค์ ๋ฒํธ๋ฅผ UART๋ก ์ถ๋ ฅํ๋ค // EXTI[7:6] pending ๋ฐ IRQ(EXTI9_5)์ pending clear } ์ ๋ต ์ฝ๋ /* stm32f10x_it.c */ void EXTI9_5_IRQHandler(void) { int x = Macro_Extract_Area(EXTI->PR, 0x3, 6); Uart1_Printf("%d\n", x); EXTI->PR = 0x3 << 6; NVIC_ClearPendingIRQ(23); } /* main.c */ void Main(void) { Sys_Init(); Uart1_Printf("EXTI Test\n"); Macro_Set_Bit(RCC->APB2ENR, 3); Macro_Set_Bit(RCC->APB2ENR, 0); Macro_Write_Block(GPIOB->CRL, 0xFF, 0x44, 24); Macro_Write_Block(AFIO->EXTICR[1], 0xFF, 0x11, 8); Macro_Set_Area(EXTI->FTSR, 0x3, 6); EXTI->PR = 0x3 << 6; Macro_Set_Area(EXTI->IMR, 0x3, 6); NVIC_ClearPendingIRQ(23); NVIC_EnableIRQ(23); for(;;) { LED_Display(1); TIM2_Delay(500); LED_Display(2); TIM2_Delay(500); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex17] TIMER_OUTPUT_LAB
๋ฌธ์ ์ค๋ช // timer.c #define TIM3_FREQ (8000000) // Hz #define TIM3_TICK (1000000/TIM3_FREQ) // usec #define TIME3_PLS_OF_1ms (1000/TIM3_TICK) void TIM3_Out_Init(void) { Macro_Set_Bit(RCC->APB1ENR, 1); Macro_Set_Bit(RCC->APB2ENR, 3); Macro_Write_Block(GPIOB->CRL,0xf,0xb,0); Macro_Write_Block(TIM3->CCMR2,0x7,0x6,4); TIM3->CCER = (0<<9)|(1<<8); } void TIM3_Out_Freq_Generation(unsigned short freq) { // Timer ์ฃผํ์๊ฐ TIM3_FREQ๊ฐ ๋๋๋ก PSC ์ค์ // ์์ฒญํ ์ฃผํ์๊ฐ ๋๋๋ก ARR ์ค์ // Duty Rate 50%๊ฐ ๋๋๋ก CCR3 ์ค์ // Manual Update(UG ๋ฐ์) // Down Counter, Repeat Mode, Timer Start } void TIM3_Out_Stop(void) { Macro_Clear_Bit(TIM3->CR1, 0); Macro_Clear_Bit(TIM3->DIER, 0); } ์ ๋ต ์ฝ๋ #define TIM3_FREQ (8000000) // Hz #define TIM3_TICK (1000000/TIM3_FREQ) // usec #define TIME3_PLS_OF_1ms (1000/TIM3_TICK) void TIM3_Out_Init(void) { Macro_Set_Bit(RCC->APB1ENR, 1); Macro_Set_Bit(RCC->APB2ENR, 3); Macro_Write_Block(GPIOB->CRL,0xf,0xb,0); Macro_Write_Block(TIM3->CCMR2,0x7,0x6,4); TIM3->CCER = (0<<9)|(1<<8); } void TIM3_Out_Freq_Generation(unsigned short freq) { TIM3->PSC = (unsigned int)(TIMXCLK/(double)TIM3_FREQ + 0.5)-1; TIM3->ARR = (double)TIM3_FREQ/freq-1; TIM3->CCR3 = TIM3->ARR/2; Macro_Set_Bit(TIM3->EGR,0); TIM3->CR1 = (1<<4)|(0<<3)|(0<<1)|(1<<0); } void TIM3_Out_Stop(void) { Macro_Clear_Bit(TIM3->CR1, 0); Macro_Clear_Bit(TIM3->DIER, 0); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex16] TIMER_DRIVER_LAB #3 TIM4 repeat timeout test
๋ฌธ์ ์ค๋ช // timer.c void TIM4_Repeat(int time) { Macro_Set_Bit(RCC->APB1ENR, 2); // TIM4 CR1: ARPE=0, down counter, repeat mode // PSC(50KHz), ARR(reload์ ๊ฐ) ์ค์ // UG ์ด๋ฒคํธ ๋ฐ์ // Update Interrupt Pending Clear // Update Interrupt Enable // TIM4 start } int TIM4_Check_Timeout(void) { // ํ์ด๋จธ๊ฐ timeout ์ด๋ฉด 1 ๋ฆฌํด, ์๋๋ฉด 0 ๋ฆฌํด } void TIM4_Stop(void) { Macro_Clear_Bit(TIM4->CR1, 0); Macro_Clear_Bit(TIM4->DIER, 0); Macro_Clear_Bit(RCC->APB1ENR, 2); } ์ ๋ต ์ฝ๋ #include "device_driver.h" #define TIM2_TICK (20) // usec #define TIM2_FREQ (1000000/TIM2_TICK) // Hz #define TIME2_PLS_OF_1ms (1000/TIM2_TICK) #define TIM2_MAX (0xffffu) #define TIM4_TICK (20) // usec #define TIM4_FREQ (1000000/TIM4_TICK) // Hz #define TIME4_PLS_OF_1ms (1000/TIM4_TICK) #define TIM4_MAX (0xffffu) void TIM2_Stopwatch_Start(void) { Macro_Set_Bit(RCC->APB1ENR, 0); TIM2->CR1 = (1<<4)|(1<<3); TIM2->PSC = (unsigned int)(TIMXCLK/50000.0 + 0.5)-1; TIM2->ARR = TIM2_MAX; Macro_Set_Bit(TIM2->EGR,0); Macro_Set_Bit(TIM2->CR1, 0); } unsigned int TIM2_Stopwatch_Stop(void) { unsigned int time; Macro_Clear_Bit(TIM2->CR1, 0); time = (TIM2_MAX - TIM2->CNT) * TIM2_TICK; return time; } /* Delay Time Max = 65536 * 20use = 1.3sec */ #if 0 void TIM2_Delay(int time) { Macro_Set_Bit(RCC->APB1ENR, 0); TIM2->CR1 = (1<<4)|(1<<3); TIM2->PSC = (unsigned int)(TIMXCLK/(double)TIM2_FREQ + 0.5)-1; TIM2->ARR = TIME2_PLS_OF_1ms * time; Macro_Set_Bit(TIM2->EGR,0); Macro_Clear_Bit(TIM2->SR, 0); Macro_Set_Bit(TIM2->DIER, 0); Macro_Set_Bit(TIM2->CR1, 0); while(Macro_Check_Bit_Clear(TIM2->SR, 0)); Macro_Clear_Bit(TIM2->CR1, 0); Macro_Clear_Bit(TIM2->DIER, 0); } #else /* Delay Time Extended */ void TIM2_Delay(int time) { int i; unsigned int t = TIME2_PLS_OF_1ms * time; Macro_Set_Bit(RCC->APB1ENR, 0); TIM2->PSC = (unsigned int)(TIMXCLK/(double)TIM2_FREQ + 0.5)-1; TIM2->CR1 = (1<<4)|(1<<3); TIM2->ARR = 0xffff; Macro_Set_Bit(TIM2->EGR,0); Macro_Set_Bit(TIM2->DIER, 0); for(i=0; i<(t/0xffffu); i++) { Macro_Set_Bit(TIM2->EGR,0); Macro_Clear_Bit(TIM2->SR, 0); Macro_Set_Bit(TIM2->CR1, 0); while(Macro_Check_Bit_Clear(TIM2->SR, 0)); } TIM2->ARR = t % 0xffffu; Macro_Set_Bit(TIM2->EGR,0); Macro_Clear_Bit(TIM2->SR, 0); Macro_Set_Bit(TIM2->CR1, 0); while (Macro_Check_Bit_Clear(TIM2->SR, 0)); Macro_Clear_Bit(TIM2->CR1, 0); Macro_Clear_Bit(TIM2->DIER, 0); } #endif void TIM4_Repeat(int time) { Macro_Set_Bit(RCC->APB1ENR, 2); TIM4->CR1 = (1<<4)|(0<<3); TIM4->PSC = (unsigned int)(TIMXCLK/(double)TIM4_FREQ + 0.5)-1; TIM4->ARR = TIME4_PLS_OF_1ms * time - 1; Macro_Set_Bit(TIM4->EGR,0); Macro_Clear_Bit(TIM4->SR, 0); Macro_Set_Bit(TIM4->DIER, 0); Macro_Set_Bit(TIM4->CR1, 0); } int TIM4_Check_Timeout(void) { if(Macro_Check_Bit_Set(TIM4->SR, 0)) { Macro_Clear_Bit(TIM4->SR, 0); return 1; } else { return 0; } } void TIM4_Stop(void) { Macro_Clear_Bit(TIM4->CR1, 0); Macro_Clear_Bit(TIM4->DIER, 0); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex15] TIMER_DRIVER_LAB #2 TIM2 delay test
๋ฌธ์ ์ค๋ช // timer.c void TIM2_Delay(int time) { Macro_Set_Bit(RCC->APB1ENR, 0); // TIM2 CR1 ์ค์ : down count, one pulse // PSC ์ด๊ธฐ๊ฐ ์ค์ => 20usec tick์ด ๋๋๋ก ์ค๊ณ (50KHz) // ARR ์ด๊ธฐ๊ฐ ์ค์ => ์์ฒญํ time msec์ ํด๋นํ๋ ์ด๊ธฐ๊ฐ ์ค์ // UG ์ด๋ฒคํธ ๋ฐ์ // Update Interrupt Pending Clear // Update Interrupt Enable // TIM2 start // Wait timeout // Stop and Power off Macro_Clear_Bit(TIM2->CR1, 0); Macro_Clear_Bit(TIM2->DIER, 0); } ์ ๋ต ์ฝ๋ #include "device_driver.h" #define TIM2_TICK (20) // usec #define TIM2_FREQ (1000000/TIM2_TICK) // Hz #define TIME2_PLS_OF_1ms (1000/TIM2_TICK) #define TIM2_MAX (0xffffu) #define TIM4_TICK (20) // usec #define TIM4_FREQ (1000000/TIM4_TICK) // Hz #define TIME4_PLS_OF_1ms (1000/TIM4_TICK) #define TIM4_MAX (0xffffu) void TIM2_Stopwatch_Start(void) { Macro_Set_Bit(RCC->APB1ENR, 0); TIM2->CR1 = (1<<4)|(1<<3); TIM2->PSC = (unsigned int)(TIMXCLK/50000.0 + 0.5)-1; TIM2->ARR = TIM2_MAX; Macro_Set_Bit(TIM2->EGR,0); Macro_Set_Bit(TIM2->CR1, 0); } unsigned int TIM2_Stopwatch_Stop(void) { unsigned int time; Macro_Clear_Bit(TIM2->CR1, 0); time = (TIM2_MAX - TIM2->CNT) * TIM2_TICK; return time; } #if 1 /* Delay Time Max = 65536 * 20use = 1.3sec */ void TIM2_Delay(int time) { Macro_Set_Bit(RCC->APB1ENR, 0); TIM2->CR1 = (1<<4)|(1<<3); TIM2->PSC = (unsigned int)(TIMXCLK/(double)TIM2_FREQ + 0.5)-1; TIM2->ARR = TIME2_PLS_OF_1ms * time; Macro_Set_Bit(TIM2->EGR,0); Macro_Clear_Bit(TIM2->SR, 0); Macro_Set_Bit(TIM2->DIER, 0); Macro_Set_Bit(TIM2->CR1, 0); while(Macro_Check_Bit_Clear(TIM2->SR, 0)); Macro_Clear_Bit(TIM2->CR1, 0); Macro_Clear_Bit(TIM2->DIER, 0); } #endif #if 0 /* Delay Time Extended */ void TIM2_Delay(int time) { int i; unsigned int t = TIME2_PLS_OF_1ms * time; Macro_Set_Bit(RCC->APB1ENR, 0); TIM2->PSC = (unsigned int)(TIMXCLK/(double)TIM2_FREQ + 0.5)-1; TIM2->CR1 = (1<<4)|(1<<3); TIM2->ARR = 0xffff; Macro_Set_Bit(TIM2->EGR,0); Macro_Set_Bit(TIM2->DIER, 0); for(i=0; i<(t/0xffffu); i++) { Macro_Set_Bit(TIM2->EGR,0); Macro_Clear_Bit(TIM2->SR, 0); Macro_Set_Bit(TIM2->CR1, 0); while(Macro_Check_Bit_Clear(TIM2->SR, 0)); } TIM2->ARR = t % 0xffffu; Macro_Set_Bit(TIM2->EGR,0); Macro_Clear_Bit(TIM2->SR, 0); Macro_Set_Bit(TIM2->CR1, 0); while (Macro_Check_Bit_Clear(TIM2->SR, 0)); Macro_Clear_Bit(TIM2->CR1, 0); Macro_Clear_Bit(TIM2->DIER, 0); } #endif ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex14] TIMER_DRIVER_LAB #1 TIM2 stopwatch test
๋ฌธ์ ์ค๋ช // timer.c void TIM2_Stopwatch_Start(void) { Macro_Set_Bit(RCC->APB1ENR, 0); // TIM2 CR1 ์ค์ : down count, one pulse // PSC ์ด๊ธฐ๊ฐ ์ค์ => 20usec tick์ด ๋๋๋ก ์ค๊ณ (50KHz) // ARR ์ด๊ธฐ๊ฐ ์ค์ => ์ต๋๊ฐ 0xFFFF ์ค์ // UG ์ด๋ฒคํธ ๋ฐ์ // TIM2 start } unsigned int TIM2_Stopwatch_Stop(void) { unsigned int time; // TIM2 stop // CNT ์ด๊ธฐ ์ค์ ๊ฐ (0xffff)์ ํ์ฌ CNT์ ํ์ค์ ์ฐจ์ด๋ฅผ ๊ตฌํ๊ณ // ๊ทธ ํ์ค์ ํ๋๊ฐ 20usec์ด๋ฏ๋ก 20์ ๊ณฑํ๊ฐ์ time์ ์ ์ฅ // ๊ณ์ฐ๋ time ๊ฐ์ ๋ฆฌํด(๋จ์๋ usec) } ์ ๋ต ์ฝ๋ #define TIM2_TICK 20 // usec #define TIM2_FREQ (1000000./TIM2_TICK) // Hz #define TIM2_CNT_MAX 0xFFFF void TIM2_Stopwatch_Start(void) { Macro_Set_Bit(RCC->APB1ENR, 0); // TIM2 CR1 ์ค์ : down count, one pulse TIM2->CR1 = (0x3<<3); // PSC ์ด๊ธฐ๊ฐ ์ค์ => 20usec tick์ด ๋๋๋ก ์ค๊ณ (50KHz) TIM2->PSC = (int)((TIMXCLK / TIM2_FREQ) + 0.5) - 1; // ARR ์ด๊ธฐ๊ฐ ์ค์ => ์ต๋๊ฐ 0xFFFF ์ค์ TIM2->ARR = TIM2_CNT_MAX; // UG ์ด๋ฒคํธ ๋ฐ์ Macro_Set_Bit(TIM2->EGR, 0); // TIM2 start Macro_Set_Bit(TIM2->CR1, 0); } unsigned int TIM2_Stopwatch_Stop(void) { unsigned int time; // TIM2 stop Macro_Clear_Bit(TIM2->CR1, 0); // CNT ์ด๊ธฐ ์ค์ ๊ฐ (0xffff)์ ํ์ฌ CNT์ ํ์ค์ ์ฐจ์ด๋ฅผ ๊ตฌํ๊ณ // ๊ทธ ํ์ค์ ํ๋๊ฐ 20usec์ด๋ฏ๋ก 20์ ๊ณฑํ๊ฐ์ time์ ์ ์ฅ time = (TIM2->ARR - TIM2->CNT) * TIM2_TICK; // ๊ณ์ฐ๋ time ๊ฐ์ ๋ฆฌํด(๋จ์๋ usec) return time; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex13] SYSTICK_TIMER_LAB
๋ฌธ์ ์ค๋ช // systick.c #include "device_driver.h" void SysTick_Run(unsigned int msec) { // Timer ์ค์ : ์ธํฐ๋ฝํธ ๋ฐ์ ์ํจ, clock source๋ HCLK/8, Timer ์ ์ง // ์ฃผ์ด์ง msec ๊ฐ ๋งํผ์ msec๋ฅผ countํ๋ ์ด๊ธฐ๊ฐ ์ค์ (LOAD) // VAL ๋ ์ง์คํฐ ๊ฐ ์ด๊ธฐํ(0) ๋ฐ COUNTFLAG Clear // Timer Start (์์์ด ๋๋ฉด ์๋์ผ๋ก LOAD์ ๊ฐ์ VAL๋ก ๊ฐ์ ธ๊ฐ๋ค) } int SysTick_Check_Timeout(void) { // Timer์ Timeout์ด ๋ฐ์ํ๋ฉด ์ฐธ(1)๋ฆฌํด, ์๋๋ฉด ๊ฑฐ์ง(0) ๋ฆฌํด } unsigned int SysTick_Get_Time(void) { // Timer์ ํ์ฌ count ๊ฐ ๋ฆฌํด } unsigned int SysTick_Get_Load_Time(void) { // Timer์ ์ค์ ๋ ์ด๊ธฐ๊ฐ์ ๋ฆฌํด } void SysTick_Stop(void) { // Timer Stop } ์ ๋ต ์ฝ๋ #include "device_driver.h" void SysTick_Run(unsigned int msec) { SysTick->CTRL = (0<<2)+(0<<1)+(0<<0); SysTick->LOAD = (unsigned int)((HCLK/(8.*1000.))*msec+0.5); SysTick->VAL = 0; Macro_Set_Bit(SysTick->CTRL, 0); } int SysTick_Check_Timeout(void) { return ((SysTick->CTRL >> 16) & 0x1); } unsigned int SysTick_Get_Time(void) { return SysTick->VAL; } unsigned int SysTick_Get_Load_Time(void) { return SysTick->LOAD; } void SysTick_Stop(void) { SysTick->CTRL = 0<<0; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex12] UART_DRIVER_LAB
๋ฌธ์ ์ค๋ช // uart.c #include "device_driver.h" #include <stdio.h> #include <stdarg.h> #include <string.h> #include <stdlib.h> #include <ctype.h> void Uart1_Init(int baud) { double div; unsigned int mant; unsigned int frac; Macro_Set_Bit(RCC->APB2ENR, 2); Macro_Set_Bit(RCC->APB2ENR, 14); Macro_Write_Block(GPIOA->CRH, 0xff, 0x8a, 4); Macro_Set_Bit(GPIOA->ODR, 10); div = PCLK2/(16. * baud); mant = (int)div; frac = (int)((div - mant) * 16. + 0.5); mant += frac >> 4; frac &= 0xf; USART1->BRR = (mant<<4)+(frac<<0); USART1->CR1 = (1<<13)|(0<<12)|(0<<10)|(1<<3)|(1<<2); USART1->CR2 = 0<<12; USART1->CR3 = 0; } void Uart1_Send_Byte(char data) { if(data=='\n') { while(Macro_Check_Bit_Clear(USART1->SR, 7)); USART1->DR = 0x0d; } while(Macro_Check_Bit_Clear(USART1->SR, 7)); USART1->DR = data; } void Uart1_Send_String(char *pt) { while(*pt!=0) { Uart1_Send_Byte(*pt++); } } void Uart1_Printf(char *fmt,...) { va_list ap; char string[256]; va_start(ap,fmt); vsprintf(string,fmt,ap); Uart1_Send_String(string); va_end(ap); } char Uart1_Get_Pressed(void) { // ๊ธ์๊ฐ ์ ๋ ฅ์ด ๋์์ผ๋ฉด ์ ๋ ฅ๋ ๊ธ์๋ฅผ ๋ฆฌํด // ๊ธ์ ์ ๋ ฅ์ด ์์ผ๋ฉด 0 ๋ฆฌํด } char Uart1_Get_Char(void) { // ๊ธ์ ์ ๋ ฅ์ด ์์ผ๋ฉด ๋ฌดํ ๋๊ธฐ, ๊ธ์๊ฐ ๋ค์ด์ค๋ฉด ๋ฐ์ ๊ธ์ ๋ฆฌํด } ์ ๋ต ์ฝ๋ #include "device_driver.h" #include <stdio.h> #include <stdarg.h> #include <string.h> #include <stdlib.h> #include <ctype.h> void Uart1_Init(int baud) { double div; unsigned int mant; unsigned int frac; Macro_Set_Bit(RCC->APB2ENR, 2); Macro_Set_Bit(RCC->APB2ENR, 14); Macro_Write_Block(GPIOA->CRH, 0xff, 0x8a, 4); Macro_Set_Bit(GPIOA->ODR, 10); div = PCLK2/(16. * baud); mant = (int)div; frac = (int)((div - mant) * 16. + 0.5); mant += frac >> 4; frac &= 0xf; USART1->BRR = (mant<<4)+(frac<<0); USART1->CR1 = (1<<13)|(0<<12)|(0<<10)|(1<<3)|(1<<2); USART1->CR2 = 0<<12; USART1->CR3 = 0; } void Uart1_Send_Byte(char data) { if(data=='\n') { while(Macro_Check_Bit_Clear(USART1->SR, 7)); USART1->DR = 0x0d; } while(Macro_Check_Bit_Clear(USART1->SR, 7)); USART1->DR = data; } void Uart1_Send_String(char *pt) { while(*pt!=0) { Uart1_Send_Byte(*pt++); } } void Uart1_Printf(char *fmt,...) { va_list ap; char string[256]; va_start(ap,fmt); vsprintf(string,fmt,ap); Uart1_Send_String(string); va_end(ap); } char Uart1_Get_Pressed(void) { if(Macro_Check_Bit_Set(USART1->SR, 5)) { return (char)USART1->DR; } else { return (char)0; } } char Uart1_Get_Char(void) { char rx; while((rx = Uart1_Get_Pressed()) == 0); return rx; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex11] UART_ECHOBACK_LAB
๋ฌธ์ ์ค๋ช // main.c #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); } void Main(void) { Sys_Init(); Uart_Printf("UART Echo-Back Test\n"); for(;;) { // ๋ฐ์ ๊ธ์๋ฅผ ๋ค์ UART๋ก ์ถ๋ ฅํ๋ค } } ์ ๋ต ์ฝ๋ #include "device_driver.h" static void Sys_Init(void) { Clock_Init(); LED_Init(); Uart_Init(115200); Key_Poll_Init(); } void Main(void) { Sys_Init(); Uart_Printf("UART Echo-Back Test\n"); for(;;) { unsigned char x; while(!Macro_Check_Bit_Set(USART1->SR, 5)); x = USART1->DR; while(!Macro_Check_Bit_Set(USART1->SR, 7)); USART1->DR = x; } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex10] KEY_CHATTERING_LAB
๋ฌธ์ ์ค๋ช // key.c #include "device_driver.h" void Key_Poll_Init(void) { Macro_Set_Bit(RCC->APB2ENR, 3); Macro_Write_Block(GPIOB->CRL, 0xff, 0x44, 24); } /* 0: ๋๋ฒ๊น ์ฉ ์ค์ */ /* 1: ์ ์ ๋์์ฉ ์ค์ */ #if 0 #define N 20000 #else #define N 3000000 #endif int Key_Get_Pressed(void) { /* N ๋งํผ ๊ฐ์ ๊ฐ์ด ์ฝํ์ผ Key ๊ฐ์ผ๋ก ํ์ */ } void Key_Wait_Key_Released(void) { while(Key_Get_Pressed()); } int Key_Wait_Key_Pressed(void) { int k; while((k = Key_Get_Pressed()) == 0); return k; } ์ ๋ต ์ฝ๋ #include "device_driver.h" void Key_Poll_Init(void) { Macro_Set_Bit(RCC->APB2ENR, 3); Macro_Write_Block(GPIOB->CRL, 0xff, 0x44, 24); } /* 0: ๋๋ฒ๊น ์ฉ ์ค์ */ /* 1: ์ ์ ๋์์ฉ ์ค์ */ #if 0 #define N 20000 #else #define N 3000000 #endif static int Key_Check_Input(void) { return Macro_Extract_Area(~GPIOB->IDR, 0x3, 6); } int Key_Get_Pressed(void) { unsigned int i, k; for(;;) { k = Key_Check_Input(); for(i=0; i<N; i++) { if(k != Key_Check_Input()) { break; } } if(i == N) return k;; } } void Key_Wait_Key_Released(void) { while(Key_Get_Pressed()); } int Key_Wait_Key_Pressed(void) { int k; while((k = Key_Get_Pressed()) == 0); return k; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex09] KEY_DRIVER_LAB
๋ฌธ์ ์ค๋ช // key.c #include "device_driver.h" void Key_Poll_Init(void) { Macro_Set_Bit(RCC->APB2ENR, 3); Macro_Write_Block(GPIOB->CRL, 0xff, 0x44, 24); } int Key_Get_Pressed(void) { } void Key_Wait_Key_Released(void) { } int Key_Wait_Key_Pressed(void) { } ์ ๋ต ์ฝ๋ #define KEY0_PUSH() (Macro_Check_Bit_Clear(GPIOB->IDR, 6)) #define KEY0_REL() (Macro_Check_Bit_Set(GPIOB->IDR, 6)) #define KEY1_PUSH() (Macro_Check_Bit_Clear(GPIOB->IDR, 7)) #define KEY1_REL() (Macro_Check_Bit_Set(GPIOB->IDR, 7)) #define KEY_VALUE() (Macro_Extract_Area(~GPIOB->IDR, 0x3, 6)) int Key_Wait_Key_Pressed(void) { #if 0 for(;;) { int key = KEY_VALUE(); if( key != 0 ) return; } #endif while( !((key = KEY_VALUE()) != 0)) ); return key; } void Key_Wait_Key_Released(void) { #if 0 for(;;) { if( KEY_VALUE() == 0 ) return; } #endif while( !(KEY_VALUE() == 0) ); } int Key_Get_Pressed(void) { #if 0 if( KEY0_REL() && KEY1_REL() ) return 0; if( KEY0_PUSH() && KEY1_REL() ) return 1; if( KEY0_REL() && KEY1_PUSH() ) return 2; if( KEY0_PUSH() && KEY1_PUSH() ) return 3; #endif return KEY_VALUE(); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex08] KEY_IN_LAB #2 Key์ ์ํ LED Toggling
๋ฌธ์ ์ค๋ช // main.c #2 void Main(void) { Sys_Init(); Uart_Printf("KEY Input Toggling #1\n"); // KEY[1:0], GPB[7:6]์ GPIO ์ ๋ ฅ์ผ๋ก ์ ์ธ for(;;) { // KEY0๊ฐ ๋๋ฆด๋๋ง๋ค LED0์ ๊ฐ์ Toggling } } ์ ๋ต ์ฝ๋ /* Key์ ์ํ LED Toggling */ #if 1 void Main(void) { Sys_Init(); Uart_Printf("KEY Input Toggling #1\n"); Macro_Write_Block(GPIOB->CRL, 0xff, 0x44, 24); for(;;) { if(Macro_Check_Bit_Clear(GPIOB->IDR, 6)) { Macro_Invert_Bit(GPIOB->ODR, 8); } } } #endif /* Key Released ์ํ ๋๊ธฐ์ ์ํ LED Toggling */ #if 0 void Main(void) { Sys_Init(); Uart_Printf("KEY Input Toggling #2\n"); Macro_Write_Block(GPIOB->CRL, 0xff, 0x44, 24); for(;;) { if(Macro_Check_Bit_Clear(GPIOB->IDR, 6)) { Macro_Invert_Bit(GPIOB->ODR, 8); while(!Macro_Check_Bit_Set(GPIOB->IDR, 6)); } } } #endif /* Inter-Lock์ ์ ์ฉํ Key์ ์ํ LED Toggling */ #if 0 void Main(void) { int interlock = 1; Sys_Init(); Uart_Printf("KEY Input Toggling #3\n"); Macro_Write_Block(GPIOB->CRL, 0xff, 0x44, 24); for(;;) { if((interlock != 0) && Macro_Check_Bit_Clear(GPIOB->IDR, 6)) { Macro_Invert_Bit(GPIOB->ODR, 8); interlock = 0; } else if((interlock == 0) && Macro_Check_Bit_Set(GPIOB->IDR, 6)) { interlock = 1; } } } #endif ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex07] KEY_IN_LAB #1 Key ์ธ์
๋ฌธ์ ์ค๋ช // main.c #1 void Main(void) { Sys_Init(); Uart_Printf("KEY Input Test #1\n"); // KEY[1:0], GPB[7:6]์ GPIO ์ ๋ ฅ์ผ๋ก ์ ์ธ for(;;) { // KEY0์ด ๋๋ ธ์ผ๋ฉด LED0๋ฅผ ON, ์ ๋๋ ธ์ผ๋ฉด OFF // KEY1์ด ๋๋ ธ์ผ๋ฉด LED1๋ฅผ ON, ์ ๋๋ ธ์ผ๋ฉด OFF } } ์ ๋ต ์ฝ๋ /* Key ์ธ์ #1 */ #if 1 void Main(void) { int value = 0; Sys_Init(); Uart_Printf("KEY Input Test #1\n"); Macro_Write_Block(GPIOB->CRL, 0xff, 0x44, 24); for(;;) { if(Macro_Check_Bit_Clear(GPIOB->IDR, 6)) Macro_Set_Bit(value, 0); else Macro_Clear_Bit(value, 0); if(Macro_Check_Bit_Clear(GPIOB->IDR, 7)) Macro_Set_Bit(value, 1); else Macro_Clear_Bit(value, 1); LED_Display(value); } } #endif /* Extract Macro๋ฅผ ์ด์ฉํ Key ์ธ์ #2 */ #if 0 void Main(void) { Sys_Init(); Uart_Printf("KEY Input Test #2\n"); Macro_Write_Block(GPIOB->CRL, 0xff, 0x44, 24); for(;;) { Macro_Write_Block(GPIOB->ODR,0x3,Macro_Extract_Area(GPIOB->IDR,0x3,6),8); } } #endif ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex06] LED_DRIVER_LAB
๋ฌธ์ ์ค๋ช // led.c #include "device_driver.h" void LED_Init(void) { /* ๋ค์ ์ฝ๋๋ ์์ ํ์ง ๋ง์์ค */ Macro_Set_Bit(RCC->APB2ENR, 3); } void LED_Display(unsigned int num) { } void LED_All_On(void) { } void LED_All_Off(void) { } ์ ๋ต ์ฝ๋ #include "device_driver.h" void LED_Init(void) { Macro_Set_Bit(RCC->APB2ENR, 3); Macro_Write_Block(GPIOB->CRH, 0xff, 0x66, 0); Macro_Set_Area(GPIOB->ODR, 0x3, 8); } void LED_Display(unsigned int num) { Macro_Write_Block(GPIOB->ODR, 0x3, (~num & 3), 8); } void LED_All_On(void) { Macro_Clear_Area(GPIOB->ODR, 0x3, 8); } void LED_All_Off(void) { Macro_Set_Area(GPIOB->ODR, 0x3, 8); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex05] BIT_OP_LAB #3 ๋นํธ ์ฐ์ฐ Macro ํ์ฉ์ ์ํ LED Toggling
๋ฌธ์ ์ค๋ช // main.c #3 void Main(void) { volatile int i; /* ์ด ๋ถ๋ถ์ ์์ ํ์ง ๋ง ๊ฒ */ Macro_Set_Bit(RCC->APB2ENR, 3); /* ๋งคํฌ๋ก๋ฅผ ์ด์ฉํ์ฌ ์ด๊ธฐ์ LED ๋ชจ๋ OFF */ for(;;) { /* LED ๋ฐ์ ๋ฐ Delay, Delay๋ 0x80000์ผ๋ก ์ค์ */ } } ์ ๋ต ์ฝ๋ void Main(void) { volatile int i; Macro_Set_Bit(RCC->APB2ENR, 3); Macro_Write_Block(GPIOB->CRH, 0xff, 0x66, 0); Macro_Set_Area(GPIOB->ODR, 0x3, 8); for(;;) { Macro_Invert_Area(GPIOB->ODR, 0x3, 8); for(i=0; i<0x80000; i++); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex04] BIT_OP_LAB #2 ๋นํธ ์ฐ์ฐ Macro ํ์ฉ์ ์ํ LED ON
๋ฌธ์ ์ค๋ช // main.c #2 void Main(void) { /* ์ด ๋ถ๋ถ์ ์์ ํ์ง ๋ง ๊ฒ */ Macro_Set_Bit(RCC->APB2ENR, 3); /* Macro๋ฅผ ์ด์ฉํ์ฌ LED0์ ON, LED1์ OFF๋ก ํ๋ ์ฝ๋๋ฅผ ์ค๊ณํ์์ค */ } ์ ๋ต ์ฝ๋ void Main(void) { Macro_Set_Bit(RCC->APB2ENR, 3); Macro_Write_Block(GPIOB->CRH, 0xff, 0x66, 0); Macro_Write_Block(GPIOB->ODR, 0x3, 0x2, 8); } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex03] BIT_OP_LAB #1 ๋นํธ ์ฐ์ฐ์ ์ํ LED ON
๋ฌธ์ ์ค๋ช // main.c #1 void Main(void) { /* ์ด ๋ถ๋ถ์ ์์๋ก ๋ฐ๊พธ์ง ๋ง์์ค */ RCC->APB2ENR |= (1<<3); /* ๋นํธ ์ฐ์ฐ์ ์ด์ฉํ์ฌ LED0์ ON, LED1์ OFF๋ก ํ๋ ์ฝ๋๋ฅผ ์ค๊ณํ์์ค */ } ์ ๋ต ์ฝ๋ #if 1 void Main(void) { RCC->APB2ENR |= (1<<3); GPIOB->CRH &=~((1<<7)|(3<<3)|(1<<0)); GPIOB->CRH |= (3<<5)|(3<<1); GPIOB->ODR &=~(1<<8); GPIOB->ODR |= (1<<9); } #endif #if 0 void Main(void) { RCC->APB2ENR |= (1<<3); GPIOB->CRH = (GPIOB->CRH & ~((1<<7)|(3<<3)|(1<<0)))|((3<<5)|(3<<1)); GPIOB->ODR = (GPIOB->ODR & ~(0x1<<8)) | (1<<9); } #endif #if 0 void Main(void) { RCC->APB2ENR |= (1<<3); GPIOB->CRH = (GPIOB->CRH & ~(0xff<<0)) | (0x66<<0); GPIOB->ODR = (GPIOB->ODR & ~(0x3<<8)) | (0x1<<8); } #endif ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex02] CMSIS_LAB
๋ฌธ์ ์ค๋ช // main.c #include "device_driver.h" void Main(void) { volatile int i; Uart_Init(115200); Uart_Printf("CMSIS based Register Define\n"); // ์ด ๋ถ๋ถ์ ์์ ํ์ง ๋ง ๊ฒ RCC->APB2ENR |= (1<<3); // LED Pin์ ์ถ๋ ฅ์ผ๋ก ์ค์ for(;;) { // LED ๋ชจ๋ ON for(i=0; i<0x40000; i++); // LED ๋ชจ๋ OFF for(i=0; i<0x40000; i++); } } ์ ๋ต ์ฝ๋ #include "device_driver.h" void Main(void) { volatile int i; Uart_Init(115200); Uart_Printf("CMSIS Based Register Define\n"); RCC->APB2ENR |= (1<<3); GPIOB->CRH = 0x66 << 0; for(;;) { GPIOB->ODR = 0x0 << 8; for(i=0; i<0x40000; i++); GPIOB->ODR = 0x3 << 8; for(i=0; i<0x40000; i++); } } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
[ex01] LED_ON_LAB
๋ฌธ์ ์ค๋ช // main.c // ์ด ๋ถ๋ถ์ ์์๋ก ๋ฐ๊พธ์ง ๋ง์์ค #define RCC_APB2ENR (*(unsigned long*)0x40021018) // ์ฌ๊ธฐ์ ์ฌ์ฉ์ ์์์ define์ ์์ฑํ์์ค #define GPIOB_CRH #define GPIOB_ODR void Main(void) { // ์ด ๋ถ๋ถ์ ์์๋ก ๋ฐ๊พธ์ง ๋ง์์ค RCC_APB2ENR |= (1<<3); // GPB[9:8]์ ์ถ๋ ฅ ๋ชจ๋๋ก ์ค์ ํ์์ค GPIOB_CRH = // GPB[9:8]์ LED0์ OFF, LED1์ ON ์ํค๋๋ก ์ค์ ํ์์ค GPIOB_ODR = } ์ ๋ต ์ฝ๋ // ์ด ๋ถ๋ถ์ ์์๋ก ๋ฐ๊พธ์ง ๋ง์์ค #define RCC_APB2ENR (*(unsigned long*)0x40021018) #define GPIOB_CRH (*(unsigned long*)0x40010C04) #define GPIOB_ODR (*(unsigned long*)0x40010C0C) void Main(void) { // ์ด ๋ถ๋ถ์ ์์๋ก ๋ฐ๊พธ์ง ๋ง์์ค RCC_APB2ENR |= (1<<3); GPIOB_CRH = 0x66 << 0; GPIOB_ODR = 0x01 << 8; } ๋ฉ๋ชจ printf ๋ด๋ถ์ \n ์ต๊ดํ ํ์
Study
ยท 2025-03-18
Classic Literature #2: Don Quixote
About the book Author: Miguel de Cervantes Original title: El ingenioso hidalgo don Quixote de la Mancha Country: Spain Genre: Novel Publication date: 1605 (Part One) 1615 (Part Two) Chapter I. In a village of La Mancha, the name of which I have no desire to call to mind, there lived not long since one of those gentlemen that keep a lance in the lance-rack, an old buckler, a lean hack, and a greyhound for coursing. An olla of rather more beef than mutton, a salad on most nights, scraps on Saturdays, lentils on Fridays, and a pigeon or so extra on Sundays, made away with three-quarters of his income. The rest of it went in a doublet of fine cloth and velvet breeches and shoes to match for holidays, while on week-days he made a brave figure in his best homespun. He had in his house a housekeeper past forty, a niece under twenty, and a lad for the field and market-place, who used to saddle the hack as well as handle the bill-hook. The age of this gentleman of ours was bordering on fifty; he was of a hardy habit, spare, gaunt-featured, a very early riser and a great sportsman. They will have it his surname was Quixada or Quesada (for here there is some difference of opinion among the authors who write on the subject), although from reasonable conjectures it seems plain that he was called Quexana. This, however, is of but little importance to our tale; it will be enough not to stray a hairโs breadth from the truth in the telling of it. You must know, then, that the above-named gentleman whenever he was at leisure (which was mostly all the year round) gave himself up to reading books of chivalry with such ardour and avidity that he almost entirely neglected the pursuit of his field-sports, and even the management of his property; and to such a pitch did his eagerness and infatuation go that he sold many an acre of tillageland to buy books of chivalry to read, and brought home as many of them as he could get. But of all there were none he liked so well as those of the famous Feliciano de Silvaโs composition, for their lucidity of style and complicated conceits were as pearls in his sight, particularly when in his reading he came upon courtships and cartels, where he often found passages like โthe reason of the unreason with which my reason is afflicted so weakens my reason that with reason I murmur at your beauty;โ or again, โthe high heavens, that of your divinity divinely fortify you with the stars, render you deserving of the desert your greatness deserves.โ Over conceits of this sort the poor gentleman lost his wits, and used to lie awake striving to understand them and worm the meaning out of them; what Aristotle himself could not have made out or extracted had he come to life again for that special purpose. He was not at all easy about the wounds which Don Belianis gave and took, because it seemed to him that, great as were the surgeons who had cured him, he must have had his face and body covered all over with seams and scars. He commended, however, the authorโs way of ending his book with the promise of that interminable adventure, and many a time was he tempted to take up his pen and finish it properly as is there proposed, which no doubt he would have done, and made a successful piece of work of it too, had not greater and more absorbing thoughts prevented him. Many an argument did he have with the curate of his village (a learned man, and a graduate of Siguenza) as to which had been the better knight, Palmerin of England or Amadis of Gaul. Master Nicholas, the village barber, however, used to say that neither of them came up to the Knight of Phoebus, and that if there was any that could compare with him it was Don Galaor, the brother of Amadis of Gaul, because he had a spirit that was equal to every occasion, and was no finikin knight, nor lachrymose like his brother, while in the matter of valour he was not a whit behind him. In short, he became so absorbed in his books that he spent his nights from sunset to sunrise, and his days from dawn to dark, poring over them; and what with little sleep and much reading his brains got so dry that he lost his wits. His fancy grew full of what he used to read about in his books, enchantments, quarrels, battles, challenges, wounds, wooings, loves, agonies, and all sorts of impossible nonsense; and it so possessed his mind that the whole fabric of invention and fancy he read of was true, that to him no history in the world had more reality in it. He used to say the Cid Ruy Diaz was a very good knight, but that he was not to be compared with the Knight of the Burning Sword who with one back-stroke cut in half two fierce and monstrous giants. He thought more of Bernardo del Carpio because at Roncesvalles he slew Roland in spite of enchantments, availing himself of the artifice of Hercules when he strangled Antaeus the son of Terra in his arms. He approved highly of the giant Morgante, because, although of the giant breed which is always arrogant and ill-conditioned, he alone was affable and well-bred. But above all he admired Reinaldos of Montalban, especially when he saw him sallying forth from his castle and robbing everyone he met, and when beyond the seas he stole that image of Mahomet which, as his history says, was entirely of gold. To have a bout of kicking at that traitor of a Ganelon he would have given his housekeeper, and his niece into the bargain. In short, his wits being quite gone, he hit upon the strangest notion that ever madman in this world hit upon, and that was that he fancied it was right and requisite, as well for the support of his own honour as for the service of his country, that he should make a knight-errant of himself, roaming the world over in full armour and on horseback in quest of adventures, and putting in practice himself all that he had read of as being the usual practices of knights-errant; righting every kind of wrong, and exposing himself to peril and danger from which, in the issue, he was to reap eternal renown and fame. Already the poor man saw himself crowned by the might of his arm Emperor of Trebizond at least; and so, led away by the intense enjoyment he found in these pleasant fancies, he set himself forthwith to put his scheme into execution. The first thing he did was to clean up some armour that had belonged to his great-grandfather, and had been for ages lying forgotten in a corner eaten with rust and covered with mildew. He scoured and polished it as best he could, but he perceived one great defect in it, that it had no closed helmet, nothing but a simple morion. This deficiency, however, his ingenuity supplied, for he contrived a kind of half-helmet of pasteboard which, fitted on to the morion, looked like a whole one. It is true that, in order to see if it was strong and fit to stand a cut, he drew his sword and gave it a couple of slashes, the first of which undid in an instant what had taken him a week to do. The ease with which he had knocked it to pieces disconcerted him somewhat, and to guard against that danger he set to work again, fixing bars of iron on the inside until he was satisfied with its strength; and then, not caring to try any more experiments with it, he passed it and adopted it as a helmet of the most perfect construction. He next proceeded to inspect his hack, which, with more quartos than a real and more blemishes than the steed of Gonela, that โtantum pellis et ossa fuit,โ surpassed in his eyes the Bucephalus of Alexander or the Babieca of the Cid. Four days were spent in thinking what name to give him, because (as he said to himself) it was not right that a horse belonging to a knight so famous, and one with such merits of his own, should be without some distinctive name, and he strove to adapt it so as to indicate what he had been before belonging to a knight-errant, and what he then was; for it was only reasonable that, his master taking a new character, he should take a new name, and that it should be a distinguished and full-sounding one, befitting the new order and calling he was about to follow. And so, after having composed, struck out, rejected, added to, unmade, and remade a multitude of names out of his memory and fancy, he decided upon calling him Rocinante, a name, to his thinking, lofty, sonorous, and significant of his condition as a hack before he became what he now was, the first and foremost of all the hacks in the world. Having got a name for his horse so much to his taste, he was anxious to get one for himself, and he was eight days more pondering over this point, till at last he made up his mind to call himself โDon Quixote,โ whence, as has been already said, the authors of this veracious history have inferred that his name must have been beyond a doubt Quixada, and not Quesada as others would have it. Recollecting, however, that the valiant Amadis was not content to call himself curtly Amadis and nothing more, but added the name of his kingdom and country to make it famous, and called himself Amadis of Gaul, he, like a good knight, resolved to add on the name of his, and to style himself Don Quixote of La Mancha, whereby, he considered, he described accurately his origin and country, and did honour to it in taking his surname from it. So then, his armour being furbished, his morion turned into a helmet, his hack christened, and he himself confirmed, he came to the conclusion that nothing more was needed now but to look out for a lady to be in love with; for a knight-errant without love was like a tree without leaves or fruit, or a body without a soul. As he said to himself, โIf, for my sins, or by my good fortune, I come across some giant hereabouts, a common occurrence with knights-errant, and overthrow him in one onslaught, or cleave him asunder to the waist, or, in short, vanquish and subdue him, will it not be well to have some one I may send him to as a present, that he may come in and fall on his knees before my sweet lady, and in a humble, submissive voice say, โI am the giant Caraculiambro, lord of the island of Malindrania, vanquished in single combat by the never sufficiently extolled knight Don Quixote of La Mancha, who has commanded me to present myself before your Grace, that your Highness dispose of me at your pleasureโ?โ Oh, how our good gentleman enjoyed the delivery of this speech, especially when he had thought of some one to call his Lady! There was, so the story goes, in a village near his own a very good-looking farm-girl with whom he had been at one time in love, though, so far as is known, she never knew it nor gave a thought to the matter. Her name was Aldonza Lorenzo, and upon her he thought fit to confer the title of Lady of his Thoughts; and after some search for a name which should not be out of harmony with her own, and should suggest and indicate that of a princess and great lady, he decided upon calling her Dulcinea del Tobosoรขโฌโshe being of El Tobosoรขโฌโa name, to his mind, musical, uncommon, and significant, like all those he had already bestowed upon himself and the things belonging to him.
Study
ยท 2023-12-05
Classic Literature #1: Romeo and Juliet
About the book Author: William Shakespeare Country: England Genre: Shakespearean tragedy Publication date: 1597 Synopsis The prologue of Romeo and Juliet calls the title characters โstar-crossed loversโโand the stars do seem to conspire against these young lovers. Romeo is a Montague, and Juliet a Capulet. Their families are enmeshed in a feud, but the moment they meetโwhen Romeo and his friends attend a party at Julietโs house in disguiseโthe two fall in love and quickly decide that they want to be married. A friar secretly marries them, hoping to end the feud. Romeo and his companions almost immediately encounter Julietโs cousin Tybalt, who challenges Romeo. When Romeo refuses to fight, Romeoโs friend Mercutio accepts the challenge and is killed. Romeo then kills Tybalt and is banished. He spends that night with Juliet and then leaves for Mantua. Julietโs father forces her into a marriage with Count Paris. To avoid this marriage, Juliet takes a potion, given her by the friar, that makes her appear dead. The friar will send Romeo word to be at her family tomb when she awakes. The plan goes awry, and Romeo learns instead that she is dead. In the tomb, Romeo kills himself. Juliet wakes, sees his body, and commits suicide. Their deaths appear finally to end the feud.
Study
ยท 2023-12-04
<
>
Touch background to close