fork download
  1. =begin
  2.  
  3. https :// ideone.com/OJiVYh の続き
  4. 入力オプションは "a1 b1 a2 b2 a3 b3 a4 b4 z"。 ex. "X Y X c1 Y c1 c2 c3 c4"
  5.  
  6. 配線は手抜き
  7.  
  8. # NAND の SVG のオリジナルは
  9. https :// ja.wikipedia.org/wiki/NAND%E3%82%B2%E3%83%BC%E3%83%88
  10. https :// ja.wikipedia.org/wiki/NAND%E3%82%B2%E3%83%BC%E3%83%88#/media/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:NAND_ANSI_Labelled.svg
  11.  
  12. =end
  13.  
  14.  
  15. ab = gets.split
  16. exit if ab.size != 9
  17.  
  18. Logic = Struct.new( :a, :b, :q, :stg, :x, :y, :no )
  19. logics = []
  20.  
  21.  
  22. lHash = { 'x'=>0,'y'=>1, 'c1'=>2,'c2'=>3,'c3'=>4,'c4'=>5 }
  23. lName = %W{ X Y c1 c2 c3 c4 }
  24.  
  25. 4.times{|n|
  26. logics << Logic.new( lHash[ ab[2*n].downcase ],lHash[ ab[2*n+1].downcase ],false, false, -1,-1, n )
  27. }
  28. logics[ lHash[ ab[8].downcase ]-2 ].q = true
  29.  
  30. # ステージ調査
  31. begin
  32. lg = []
  33. logics.each{|s|
  34. lg[2*s.no] = true if s.a <= 1
  35. lg[2*s.no+1] = true if s.b <= 1
  36. }
  37. 3.times{|st|
  38. sq = []
  39. 4.times{|i|
  40. next if logics[i].stg
  41. next unless lg[2*i] && lg[2*i+1]
  42. logics[i].stg = st
  43. sq << "c#{i+1}"
  44. }
  45. sq.each{|q|
  46. ab.each_with_index{|s,i|
  47. lg[i] = true if s == q
  48. }
  49. }
  50. }
  51. end
  52.  
  53. # 座標決定
  54. begin
  55. max_stg = []
  56. logics.each{|s| max_stg[s.stg] = (max_stg[s.stg] || 0) + 1 }
  57. max_stg.size.times{|j|
  58. si = 0
  59. logics.each_with_index{|s,i|
  60. next unless j == s.stg
  61. s.x = s.stg * 120 * 2 + 20
  62. s.y = si * 100 + 20
  63. si += 1
  64. }
  65. }
  66. (max_stg.max-1).times{|n|
  67. max_stg.each_with_index{|s,i|
  68. next unless s == n + 1
  69. logics.each{|x|
  70. next unless x.stg == i
  71. x.y += 50 * (s-n)
  72. }
  73. }
  74. }
  75. end
  76.  
  77. # SVG出力
  78. # File.open( save_path, 'w:UTF-8' ){|fh|
  79. print <<_EOT_
  80. <svg xmlns:xlink="http://w...content-available-to-author-only...3.org/1999/xlink" xmlns="http://w...content-available-to-author-only...3.org/2000/svg" id="nand_logic"
  81.   width="#{120*8}"
  82.   height="#{50*8}"
  83. >
  84. <defs>
  85. <g id="nand">
  86. <path style="font-size:medium;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-indent:0;text-align:start;text-decoration:none;line-height:normal;letter-spacing:normal;word-spacing:normal;text-transform:none;direction:ltr;block-progression:tb;writing-mode:lr-tb;text-anchor:start;fill:black;fill-opacity:1;stroke:none;stroke-width:3;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate;font-family:Bitstream Vera Sans;-inkscape-font-specification:Bitstream Vera Sans"
  87. d="M 40,5 L 40,6.4285714 L 40,43.571429 L 40,45 L 41.428571,45 L 60.47619,45 C 71.744098,45 80.47619,35.999955 80.47619,25 C 80.47619,14.000045 71.744099,5.0000002 60.47619,5 C 60.47619,5 60.47619,5 41.428571,5 L 40,5 z M 42.857143,7.8571429 C 50.834264,7.8571429 55.918368,7.8571429 58.095238,7.8571429 C 59.285714,7.8571429 59.880952,7.8571429 60.178571,7.8571429 C 60.327381,7.8571429 60.409227,7.8571429 60.446429,7.8571429 C 60.465029,7.8571429 60.471543,7.8571429 60.47619,7.8571429 C 70.236853,7.857143 77.142857,15.497098 77.142857,25 C 77.142857,34.502902 69.760662,42.142857 60,42.142857 L 42.857143,42.142857 L 42.857143,7.8571429 z" />
  88. <g style="fill:none;stroke:black;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1">
  89. <path d="M 41,15 L 15,15" />
  90. <path d="M 41,35 L 15,35" />
  91. <path d="M 88.11111,25 C 101.62222,25 105,25 105,25" />
  92. <path d="M 88,25 A 4,4 0 1 1 80,25 A 4,4 0 1 1 88,25 z" />
  93. </g>
  94. </g>
  95. </defs>
  96. <style type="text/css">
  97. .wire1{ stroke:black; stroke-width:2; stroke-opacity:1 }
  98. .wire2{ stroke:blue; stroke-width:2; stroke-opacity:0.4; fill:none }
  99. </style>
  100.  
  101. _EOT_
  102.  
  103. logics.each{|s|
  104. puts %Q{\t<use xlink:href="#nand" x="#{s.x}" y="#{s.y}" />}
  105. }
  106. puts %Q{ <g font-weight="400" font-size="17px" >}
  107. logics.each{|s|
  108. puts %Q{\t<text text-anchor="end" x="#{s.x+38}" y="#{s.y+10}">a#{s.no+1}</text>}
  109. puts %Q{\t<text text-anchor="end" x="#{s.x+38}" y="#{s.y+32}">b#{s.no+1}</text>}
  110. puts %Q{\t<text x="#{s.x+90}" y="#{s.y+21}">c#{s.no+1}</text>}
  111. }
  112. puts " </g>"
  113.  
  114. puts %Q{ <g font-weight="bolder" font-size="20px" >}
  115. logics.each{|s|
  116. puts %Q{\t<text text-anchor="end" x="#{s.x+12}" y="#{s.y+22}">#{lName[s.a]}</text>} if s.a < 2
  117. puts %Q{\t<text text-anchor="end" x="#{s.x+12}" y="#{s.y+42}">#{lName[s.b]}</text>} if s.b < 2
  118. if s.q
  119. puts %Q{\t<path class="wire1" d="M #{s.x+105},#{s.y+25} l 20,0" />}
  120. puts %Q{\t<text x="#{s.x+112+20}" y="#{s.y+32}">Z</text>}
  121. else
  122. sno = s.no + 2
  123. corner = s.no * 10
  124. logics.each{|s2|
  125. if s2.a == sno
  126. puts %Q{\t<path class="wire2" d="M #{s.x+105},#{s.y+25} l #{50+corner},0 l 0,#{s2.y+15-s.y-25} L #{s2.x+15},#{s2.y+15}" />}
  127. end
  128. if s2.b == sno
  129. puts %Q{\t<path class="wire2" d="M #{s.x+105},#{s.y+25} l #{50+corner},0 l 0,#{s2.y+35-s.y-25} L #{s2.x+15},#{s2.y+35}" />}
  130. end
  131. }
  132. end
  133. }
  134. puts " </g>"
  135.  
  136. print <<_EOT_
  137. </svg>
  138. _EOT_
  139.  
  140.  
Success #stdin #stdout 0s 6584KB
stdin
X Y X c1 Y c1 c2 c3 c4
stdout
<svg xmlns:xlink="http://w...content-available-to-author-only...3.org/1999/xlink" xmlns="http://w...content-available-to-author-only...3.org/2000/svg" id="nand_logic"
   width="960"
   height="400"
>
<defs>
	<g id="nand">
		<path style="font-size:medium;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-indent:0;text-align:start;text-decoration:none;line-height:normal;letter-spacing:normal;word-spacing:normal;text-transform:none;direction:ltr;block-progression:tb;writing-mode:lr-tb;text-anchor:start;fill:black;fill-opacity:1;stroke:none;stroke-width:3;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate;font-family:Bitstream Vera Sans;-inkscape-font-specification:Bitstream Vera Sans"
			d="M 40,5 L 40,6.4285714 L 40,43.571429 L 40,45 L 41.428571,45 L 60.47619,45 C 71.744098,45 80.47619,35.999955 80.47619,25 C 80.47619,14.000045 71.744099,5.0000002 60.47619,5 C 60.47619,5 60.47619,5 41.428571,5 L 40,5 z M 42.857143,7.8571429 C 50.834264,7.8571429 55.918368,7.8571429 58.095238,7.8571429 C 59.285714,7.8571429 59.880952,7.8571429 60.178571,7.8571429 C 60.327381,7.8571429 60.409227,7.8571429 60.446429,7.8571429 C 60.465029,7.8571429 60.471543,7.8571429 60.47619,7.8571429 C 70.236853,7.857143 77.142857,15.497098 77.142857,25 C 77.142857,34.502902 69.760662,42.142857 60,42.142857 L 42.857143,42.142857 L 42.857143,7.8571429 z" />
		<g style="fill:none;stroke:black;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1">
			<path d="M 41,15 L 15,15" />
			<path d="M 41,35 L 15,35" />
			<path d="M 88.11111,25 C 101.62222,25 105,25 105,25" />
			<path d="M 88,25 A 4,4 0 1 1 80,25 A 4,4 0 1 1 88,25 z" />
		</g>
	</g>
</defs>
<style type="text/css">
	.wire1{ stroke:black; stroke-width:2; stroke-opacity:1 }
	.wire2{ stroke:blue; stroke-width:2; stroke-opacity:0.4; fill:none }
</style>

	<use xlink:href="#nand" x="20" y="70" />
	<use xlink:href="#nand" x="260" y="20" />
	<use xlink:href="#nand" x="260" y="120" />
	<use xlink:href="#nand" x="500" y="70" />
  <g font-weight="400" font-size="17px" >
	<text text-anchor="end" x="58" y="80">a1</text>
	<text text-anchor="end" x="58" y="102">b1</text>
	<text x="110" y="91">c1</text>
	<text text-anchor="end" x="298" y="30">a2</text>
	<text text-anchor="end" x="298" y="52">b2</text>
	<text x="350" y="41">c2</text>
	<text text-anchor="end" x="298" y="130">a3</text>
	<text text-anchor="end" x="298" y="152">b3</text>
	<text x="350" y="141">c3</text>
	<text text-anchor="end" x="538" y="80">a4</text>
	<text text-anchor="end" x="538" y="102">b4</text>
	<text x="590" y="91">c4</text>
  </g>
  <g font-weight="bolder" font-size="20px" >
	<text text-anchor="end" x="32" y="92">X</text>
	<text text-anchor="end" x="32" y="112">Y</text>
	<path class="wire2" d="M 125,95 l 50,0 l 0,-40 L 275,55" />
	<path class="wire2" d="M 125,95 l 50,0 l 0,60 L 275,155" />
	<text text-anchor="end" x="272" y="42">X</text>
	<path class="wire2" d="M 365,45 l 60,0 l 0,40 L 515,85" />
	<text text-anchor="end" x="272" y="142">Y</text>
	<path class="wire2" d="M 365,145 l 70,0 l 0,-40 L 515,105" />
	<path class="wire1" d="M 605,95 l 20,0" />
	<text x="632" y="102">Z</text>
  </g>
</svg>