From 0bcafe685464bb4337c42e9e6bac24e323232074 Mon Sep 17 00:00:00 2001 From: kemasama Date: Wed, 27 Apr 2022 19:13:20 +0900 Subject: [PATCH] add: diamond art --- README.md | 25 ++++++++++ diamond_art/diamond | Bin 0 -> 33620 bytes diamond_art/diamond.c | 86 ++++++++++++++++++++++++++++++++ diamond_art/diamond2 | Bin 0 -> 33621 bytes diamond_art/diamond2.c | 86 ++++++++++++++++++++++++++++++++ diamond_art/diamond3 | Bin 0 -> 33621 bytes diamond_art/diamond3.c | 108 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 305 insertions(+) create mode 100755 diamond_art/diamond create mode 100644 diamond_art/diamond.c create mode 100755 diamond_art/diamond2 create mode 100644 diamond_art/diamond2.c create mode 100755 diamond_art/diamond3 create mode 100644 diamond_art/diamond3.c diff --git a/README.md b/README.md index 2cdab7d..742ee80 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,31 @@ GCCを使用してコンパイルしています。 8. ピボットより小さいデータをクイックソートする 9. ピボットより大きいデータをクイックソートする +### ダイヤモンドアート + +コマンドラインで指定されたサイズのダイヤモンドを生成する + +2022/04/27 diamond, diamond2, diamond3 + +#### 考え方 + +1. 一度にすべてを出力することはできないので、処理の分割を行う +2. まず文字列として出力することを考えると、一行ずつ出力することができる +3. 次にダイヤモンドの形にするには左と右で分割して考えたほうが簡単だとわかる +4. 以上を踏まえ、上左側、上右側、下左側、下右側で処理を分割する +5. 共通化できる部分を関数化する + +#### 処理内容 + +1. コマンドラインの入力を確認する +2. 出力関数を呼び出す +3. 上から順に一行ずつ再起地点まで出力する +4. 左側を出力する +5. 右側を出力する +6. 再起地点まで来たら降順に上から順に出力する +7. 左側を出力する +8. 右側を出力する + # Copyright Copyright (c) 2022 kema All Rights Reserved. diff --git a/diamond_art/diamond b/diamond_art/diamond new file mode 100755 index 0000000000000000000000000000000000000000..f1df82f78f2b28b080450ccd2cd0a15ae49f536c GIT binary patch literal 33620 zcmeI5e`u9e7{||h@8<5Nxh;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ja>fAIkU5C8!X009sH0T2KI5C8!X009sH z0T2KI5C8!X009sH0T2KI5C8!X009sH0T2KI5C8!X009sH0T2KI5C8!X009sH0T2KI z5C8!X009sH0T2KI5C8!X009vA-wCL+Vl8PsDZd|Jz6C(F)B^!}o&P1C6us`GzqmV- zEyg^B94P$rbLrB8ULjjT-hj51Y(1p#cL^O|V^0?fdkXR6mXe|*_@yI-5L@7=@GMpu1#bdhuUnoCRWnWxM? zyx;Zs505?n&dVR2-2KK|^}(|jw+?1}H9FdCTU(!*=;-fws_UB%o~e3z;ph6` z%=dnJv;AmRS>uuOkNU2@>K^`aPyamk$+_FQnonMLt?S?Q>e6R3!obK`M zZy(P0ZN6NycyIfx-l_cm+x|Du>jNBhyG Fe*ky{mpK3c literal 0 HcmV?d00001 diff --git a/diamond_art/diamond.c b/diamond_art/diamond.c new file mode 100644 index 0000000..43645d3 --- /dev/null +++ b/diamond_art/diamond.c @@ -0,0 +1,86 @@ +#include +#include + +int +output_whitespace( + int length +) +{ + int i; + + for ( i = 0 ; i < length ; i++ ) { + printf(" "); + } + + return 0; +} + +int +output_diamond_line( + int diamond_size, + int current_line +) +{ + int i; + int j; + + output_whitespace(diamond_size - current_line); + + for ( i = 1 ; i < current_line ; i++ ) { + printf("%d", i); + } + + for ( i = 0 ; i < current_line ; i++ ) { + printf("%d", current_line - i); + } + + printf("\n"); + + return 0; +} + +int +output_diamond( + int diamond_size +) +{ + int i; + int j; + + for ( i = 0 ; i < diamond_size; i++ ) { + output_diamond_line(diamond_size, i); + } + + for ( j = diamond_size ; j > 0 ; j-- ) { + output_diamond_line(diamond_size, j); + } + + return 0; +} + +int +main( + int argc, + char *argv[] +) +{ + int diamond_size; + + if (argc < 2) + { + printf("Invalid command line arguments.\n"); + return -1; + } + + diamond_size = atoi(argv[1]); + + if (diamond_size < 1 || diamond_size > 9) + { + printf("Can not create diamond (s: %d).\n", diamond_size); + return -1; + } + + output_diamond(diamond_size); + + return 0; +} diff --git a/diamond_art/diamond2 b/diamond_art/diamond2 new file mode 100755 index 0000000000000000000000000000000000000000..486b1cb97b257a74f045a33a0bac54edb9168e90 GIT binary patch literal 33621 zcmeI5e{54#6vt274K}v8Ap|gL_KZ;nl93>PC}hw9(*X(uMyR-WDeHsPt?kyn3L8@k zghVG%Ygi=wHH`_If+2`;Gbke?M)^mU5F|9O#GFMIO{pLuj92$Fwywe_ax`t zd+v{O&-vW<@4ff4t3Uo(OynXKH|tbZBadh=8C)Z}jkShVskK#)RBx(Yw?_JQHn_OXUu8NOpClu~cBZQBJWA-& z{wNi*;#*?lO}YMjOVa*k*(1bu`YW*gF+(j%)yJ%8FuZNt&d#^l_9sk5#CD3)x3nu2 z4{r~K11cEa8qs#Pzup{wV(0pB`j>q;p#3bbs;yGX)~sHi-VLw)6L6^|r}S{6sO(iK zt5Vw{j%{FjVt+DLi#I*rwrCPpoN-?J&mLW|dR6uE6`6k|krNl&A{!(a7KumyKiVQ% zK$OsHCWsoi?E%|vux(jW_VNPTlezTmN{=hz`dU_}Wd1#8oX&o!*YNzzzv=9|g$;vM z{)>6>Sae=faLc?vYg2%2k=t1%j=>tE;L6bA*L&N~w|?~9qGR*9&0ux0>e-%RT~DWb z)jfw>$C+}yobwYuM;G(QA+qj+tkNg(dje&gj_X=19MAgJm}Q14mR4|F!7V(9Oo>z~yY)7G5HEvPyqH=iQkOQko$k00JNY0w4eaAOHd&00JNY0w4eaAOHd&00JNY z0w4eaAOHd&00JNY0w4eaAOHd&00JNY0w4eaAOHd&00JNY0w4eaAOHd&00JNY0w4ea zAn>0NNbRA)v7N4-v1OF(8Yt^c6;U4_Yn(n*L1MA{5NxTK4&YFKJLF+>^I`l#W`K(xKDFF6LFE8 zojDe`sO2%6sh-O@y_cDr&aXwr|27fR0ogBi&lod4$@|r{r;OL8(e6FGSB2Cv#kp4( zDwB6{ZUvOMwVTKC2FbpTQoeVT&&BS)R6>12C7NeThdpnY?IBO$TbfPX;^eT0lI1QM zEphnagS68x@woRnW{E*^mORFGF6~JTr1zfB$Q~_q^IGn*^SVAMT{q}%nnEkWPy3sK z0i!+=3i-nUqbV3R4S#f7JYpDVu=36g4#2E*1?wq1(y$)iFI%G_br$1PJenErqn zr8$wf)f~4JRfPP(Fe!b5fqQ#H&@yAq{(6(D?b{D%rH2x29HNrz5@-W$^3cULzSZDB zJAaNoqXS8_)tzu1aOpfsTzvC_EX_KDwS+73PP*7~XM)(XYd*DxIR8aV9U7ZKu=AG&(@!B9%a{epTGhk#ur36Werd zlU>+FZho2Gtx@>7U$$4IkZ4!G;nq*nr(16L^=5iqcD(axW#8{#o};$dqC{issX*tc z-pIkX-+k!5i%U*NJ)izkyz0nvhmZLp9}jQs&-;9Eu+hE7m!E1s-rlzNiw|F{d13CM zoAvM8Ja^iW6EDx7v2dET=-A%8rVknqhs^uU1uu1e{p348NB!%LKl{|Vjx%}tmcF^0 zg6$`}nmSs~Ol_(gS+!ty>x}N3`Tw{5(>j_reiMKA)xZy@zx`v!@{=P!U3sHu +#include + +int +output_whitespace( + int length +) +{ + int i; + + for ( i = 0 ; i < length ; i++ ) { + printf(" "); + } + + return 0; +} + +int +output_diamond_line( + int diamond_size, + int current_line +) +{ + int i; + int j; + + output_whitespace(diamond_size - current_line); + + for ( i = 1 ; i < current_line ; i++ ) { + printf("#"); + } + + for ( i = 0 ; i < current_line ; i++ ) { + printf("#"); + } + + printf("\n"); + + return 0; +} + +int +output_diamond( + int diamond_size +) +{ + int i; + int j; + + for ( i = 0 ; i < diamond_size; i++ ) { + output_diamond_line(diamond_size, i); + } + + for ( j = diamond_size ; j > 0 ; j-- ) { + output_diamond_line(diamond_size, j); + } + + return 0; +} + +int +main( + int argc, + char *argv[] +) +{ + int diamond_size; + + if (argc < 2) + { + printf("Invalid command line arguments.\n"); + return -1; + } + + diamond_size = atoi(argv[1]); + + if (diamond_size < 1) + { + printf("Can not create diamond (s: %d).\n", diamond_size); + return -1; + } + + output_diamond(diamond_size); + + return 0; +} diff --git a/diamond_art/diamond3 b/diamond_art/diamond3 new file mode 100755 index 0000000000000000000000000000000000000000..34a43c55724442c3de467036dfa11a2b74940e0d GIT binary patch literal 33621 zcmeI5Z)_Ar6u{r!ws0+fT9klVwfm*o5@-`3(U2CdEmaCg8)%aX#L3aSwkPdf+uMVd z9&*r-RM5mmh!`*ygW(R85QE7zYFa#FB0>NosC+P>m>h`^h(v6YdcHTidu%I8jL}c; zC2x1${CV@kUTMjI zNV<##A%c2nLoi)6(O=yX!{0+@53=q4oTfiyC`E|Ipx){8H>d6K`L>$=s?Bz??PB*$ z^+JUFn|*$d@cEkp(jMRWY*794lC*!|iyU}k7mTOV%V(mS9oc4AqFT?TH-n6$( zM?*uUeNhq$c9yjIHk5d}TRmu#)JE~Eo_tJ4xf=_{v!*+!d)taDiZL$V2JD3L z1SNfC(@G zCcp%k025#WOn?b60Vco%m;e)C0!)AjFaajO1egF5U;<2l2`~XBzyz286JP>NfC(@G zCcp%k025#WOyEBtkm`l=V?9c2tP=VM68Z7e6d1(geKV6&T!XQMh&z?6jms6NqdJ`a zH8@_@{{_GvtB*4iW+ZXU?Nn+u9!Xq1)>9rspZ&wCxR_EE^sUCm5{B=Cz4`Gd`i%7& zexsH7aq<(bBpW);nDfNYPO4c)pzeX@hiA5cA4?g9sG2fS9tedn8pyO4GC$^_~)-F!( zw;Y>aJVZP5GWI)xIZ#YiUSmBKu@vra+V6Ni_IR!X=XT^9 z8Ut-@Zofxs_4&P;yR$je=Jo5r;%umN`!#<+*BU#$Zr!VSeD1aY_A3l7(h599*Z~FS z1CF2={FBz3=gX-~XNc)EajqPVM*8S-ZiM`yMC6x0On?b60Vco%m;e)C z0!)AjFaajO1egF5U;<2l2`~XBzyz286JP>NfC(@GCcp%k025#WOn?b60Vco%m;e)C z0!)AjFaajO1egF5U;<2l2`~XBzyz286JP>NfC(@GCcp%k!2eD_!x?K)8&K)~0P`*Y zETu09kY##H+!9%K;VbSgtHv0opaX>-KNoI2-~-X#>GSJNXe$CTK^1K+!rSF-4C!9c z;&pqxoiHa5(%VD2fZ{f{&kuqk0LSJQpY9E|yBobwWnO>)%T?&>!ZtxSCO|!`RpDwE z-fGYn#?OJRG!8qgKcwQVB;+MWp_j^yxMoS)cqiSuJ0S-PPJB0=?73#U2k%ZGyWF(t zE(NmdQ1MSiDQod-x9P2Tdeh$eYas5Ct1wbDGq&;)<8327tI`HyB2o%u^!us97|C8N zTCvTDHkSo7zjeF63Q!CqDiCZSQ^15cvFB({RQ&=g)6+)HGzK!Y9I8c3%2q z_p+Dg9=>0D<)yjLmG*ttFuE@m>Z}Nt_&&X}`{!x(EAIUG=H|0STbtagPrUTv*Sk(- zyi##!2l&E61FgHdPt9mue`CeM9o@5{_w)a6+aH;>_}EXcFNx~=Kg!rteZ5BC892FS R-t*p%kKLJf`-k~`e*k%Uod*B_ literal 0 HcmV?d00001 diff --git a/diamond_art/diamond3.c b/diamond_art/diamond3.c new file mode 100644 index 0000000..644043d --- /dev/null +++ b/diamond_art/diamond3.c @@ -0,0 +1,108 @@ +#include +#include + +int +output_whitespace( + int length +) +{ + int i; + + for ( i = 0 ; i < length ; i++ ) { + printf(" "); + } + + return 0; +} + +int +output_diamond_line( + int diamond_size, + int current_line +) +{ + int i; + int j; + int k; + int l; + + // Left Side + // Decoration + for ( i = diamond_size - current_line ; i > 0 ; i-- ) { + printf("%d", i); + } + + // Padding + output_whitespace(1); + + // Diamond + for ( j = 1 ; j < current_line ; j++ ) { + printf("%d", j); + } + + // Right Side + // Diamond + for ( k = 0 ; k < current_line ; k++ ) { + printf("%d", current_line - k); + } + + // Padding + if ( current_line != 0 ) { + output_whitespace(1); + } + + // Decoration + for ( l = 1 ; l <= diamond_size - current_line ; l++ ) { + printf("%d", l); + } + + printf("\n"); + + return 0; +} + +int +output_diamond( + int diamond_size +) +{ + int i; + int j; + + for ( i = 0 ; i < diamond_size; i++ ) { + output_diamond_line(diamond_size, i); + } + + for ( j = diamond_size ; j >= 0 ; j-- ) { + output_diamond_line(diamond_size, j); + } + + return 0; +} + +int +main( + int argc, + char *argv[] +) +{ + int diamond_size; + + if (argc < 2) + { + printf("Invalid command line arguments.\n"); + return -1; + } + + diamond_size = atoi(argv[1]); + + if (diamond_size < 1 || diamond_size > 9) + { + printf("Can not create diamond (s: %d).\n", diamond_size); + return -1; + } + + output_diamond(diamond_size); + + return 0; +}