[Exam_rang02] Inter

Cadet_42ยท2021๋…„ 7์›” 24์ผ
0

Exam_rang02

๋ชฉ๋ก ๋ณด๊ธฐ
2/2
post-thumbnail

๐Ÿ’ก Inter Subject ์ดํ•ด

  • Inter์€ .\a.out์„ ์ œ์™ธํ•œ ์ฒซ๋ฒˆ์งธ argument์™€ ๋‘๋ฒˆ์งธ argument ์•ˆ์—์„œ ํ™•์ธ๋˜๋Š” ๊ฐ™์€ ๊ธ€์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์ฒซ๋ฒˆ์งธ argument์™€ ๋‘๋ฒˆ์งธ argument์—์„œ์˜ ์ค‘๋ณต๋˜๋Š” ๊ธ€์ž๋Š” ๋ฌด์‹œํ•œ๋‹ค.
    (์˜ˆ์‹œ: ddd, fffffddfdfdfd)
.\a.out "first_argument", "second_argument"
Assignment name  : inter
Expected files   : inter.c
Allowed functions: write
--------------------------------------------------------------------------------

Write a program that takes two strings and displays, without doubles, the
characters that appear in both strings, in the order they appear in the first
one.

The display will be followed by a \n.

If the number of arguments is not 2, the program displays \n.

Examples:

$>./inter "padinton" "paqefwtdjetyiytjneytjoeyjnejeyj" | cat -e
padinto$
$>./inter ddf6vewg64f gtwthgdwthdwfteewhrtag6h4ffdhsd | cat -e
df6ewg4$
$>./inter "rien" "cette phrase ne cache rien" | cat -e
rien$
$>./inter | cat -e
$

๐Ÿ’กInter ํ•จ์ˆ˜ ๊ตฌํ˜„๋ฐฉ์‹

  • int i๋Š” ์ฒซ๋ฒˆ์งธ argument์•ˆ์„ ์ปค์„œ์ฒ˜๋Ÿผ ์›€์ง์ผ ์˜ˆ์ •์ด๋‹ค.
  • int j๋Š” ๋‘๋ฒˆ์งธ argument์•ˆ์„ ์ปค์„œ์ฒ˜๋Ÿผ ์›€์ง์ผ ์˜ˆ์ •์ด๋‹ค.
  • static int tab[256] : (union subject๊ณผ ๊ฐ™์ด) ascii ํ…Œ์ด๋ธ”์„ static int tab[256] static ์œผ๋กœ ์‚ฌ์šฉํ•ด ์คŒ์œผ๋กœ์จ, default ๊ฐ’์„ 0์œผ๋กœ ํ•˜์—ฌ, 0์œผ๋กœ ํ…Œ์ด๋ธ”์„ ์ฑ„์› ๋‹ค.
  • ์ฒซ๋ฒˆ์งธ argument์™€ ๋‘๋ฒˆ์งธ argument์˜ ์š”์†Œ๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋ฉฐ, ๋งŒ์•ฝ ๊ทธ๋ ‡๋‹ค๋ฉด
    if (tab[(int)av[1][i]])์•ˆ์— ๋“ค์–ด๊ฐ„๋‹ค.
  • tab[(int)av[1][i]] == 0 : tab์„ ์ด์šฉํ•˜์—ฌ, argument์˜ ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜์”ฉ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.(0 =switch off)
  • ๋งŒ์•ฝ, ์ฒซ๋ฒˆ์งธ argument๊ณผ ๋‘๋ฒˆ์งธ argument์—์„œ ๋™์ผํ•œ ๊ธ€์ž๋ฅผ ์ฐพ์•˜์„๊ฒฝ์šฐ,
    tab[(int)av[1][i]] = 1 table์— 1์„ ์จ์ฃผ๊ณ  (1 = switch on) ์„ ์จ์ฃผ๊ณ  if loop ์„ ๋‚˜์˜จ๋‹ค.
    j++ ์™€ i++๋ฅผ ํ•ด์ฃผ๋ฉด์„œ, ์ƒ๊ธฐ์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ณ„์† ํ…Œ์ŠคํŠธ ํ•ด์ค€๋’ค, while loop์„ ๋‚˜์˜จ๋‹ค.
if (av[1][i] == av[2][j])
{
	if (tab[(int)av[1][i]] == 0)
	{
    	   write(1, &av[1][i], 1);
	   tab[(int)av[1][i]] = 1;
	}
}

๐Ÿ’ก table ์›๋ฆฌ ์ดํ•ด ๋ฐฉ๋ฒ• (switch off/on)
์ฆ‰, 256๊ฐœ์˜ table ์•ˆ์— 0์œผ๋กœ ๊ฝ‰ ์ฐจ์žˆ๋Š”๋ฐ (switch off ์ƒํƒœ), ์ฒซ๋ฒˆ์งธ argument๊ณผ ๋‘๋ฒˆ์งธ argument์— ๋™์‹œ์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์ž๋ฅผ 1๋กœ ์จ์ฃผ๋ฉด์„œ table์„ switch on ์ƒํƒœ๋กœ ๋งŒ๋“ค์–ด์ค€ ๋’ค, ์ตœ์ข…์ ์œผ๋กœ switch on ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž๋“ค์„ ์ถœ๋ ฅํ•˜์—ฌ ๋ฆฌํ„ดํ•œ๋‹ค.

๐Ÿ’กInter ํ•จ์ˆ˜ ๊ตฌํ˜„

#include <unistd.h>

int	main(int ac, char *av[])
{
	int			i;
	int			j;
	static int	tab[256];
    
	if (ac == 3)
	{
		i = 0;
		while (av[1][i])
		{
			j = 0;
			while (av[2][j])
			{
				if (av[1][i] == av[2][j])
				{
					if (tab[(int)av[1][i]] == 0)
					{
						write(1, &av[1][i], 1);
						tab[(int)av[1][i]] = 1;
					}
				}
				j++;
			}
			i++;
		}
	}
	write(1, "\n", 1);
	return (0);
}
profile
์•ˆ๋…•ํ•˜์„ธ์š”! ๊ฐœ๋ฐœ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํžˆ ๋ฐฐ์šฐ๊ฒ ์Šต๋‹ˆ๋‹ค. ;)

0๊ฐœ์˜ ๋Œ“๊ธ€