AA折れ線グラフ
入力は'R','F','C'の3種類の文字からなる長さ1以上の文字列
- 'R'は上昇を表し,折れ線グラフの要素としては '/' (スラッシュ)1文字に対応
- 'F'は下降を表し,折れ線グラフの要素としては '\' (バックスラッシュ)1文字に対応
- 'C'は変化なしを表し,折れ線グラフの要素としては'_'(アンダスコア)1文字に対応
http://practical-scheme.net/wiliki/wiliki.cgi?Shiro%3Alog%3A2006%E5%89%8D%E5%8D%8A
using System; using System.Collections.Generic; using System.Linq; class Program { static void Graph(string input) { int top = 0; int btm = 0; int p = 0; foreach (var c in input) { switch (c) { case 'R': p++; break; case 'F': p--; break; case 'C': break; default: throw new Exception(); } top = Math.Max(top, p); btm = Math.Min(btm, p); } int rows = top + (btm < 0 ? -btm+1 : 0); int cols = input.Length; char[,] g = new char[rows, cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) g[i, j] = ' '; int y = rows - 1 - Math.Abs(btm); for (int i = 0; i < input.Length; i++) { int r = y; char c = ' '; switch (input[i]) { case 'R': c = '/'; y--; break; case 'F': c = '\\'; r++; y++; break; case 'C': c = '_'; break; } g[r, i] = c; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Console.Write(g[i, j]); } Console.WriteLine(); } } static void Main() { Graph("RCRFCRFFCCRFFRRCRRCCFRFRFF"); } }
実行結果です。
__ / \/\/\ _/\_/\ _/ \ / \__/\ / \/