cscの作法 その548

# 概要
cscの作法、調べてみた。
練習問題やってみた。

# 練習問題
Forth電卓を書け。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/1af3b70c-47fa-0cbc-9755-446585f154b2.png)

# 9を4個使って1から15で求める式

|9を4個使う | 答え|
|:- |:- |
|9 9 - 9 9 / .| 1
|9 9 / 9 9 / + .| 2
|9 9 + 9 + 9 / .| 3
|9 9 9 + 9 / dup + . |4
|9 9 9 + 9 / dup + - . |5
|9 dup 9 + 9 + 9 / - . |6
|9 9 9 + 9 / - . |7
|9 9 9 drop 9 / - . |8
|9 9 - 9 * 9 + . |9
|9 9 / 9 dup 9 / + . |10
|9 9 9 + 9 / + . |11
|9 dup 9 9 + + 9 / + .|12
|9 9 9 + 9 / dup + + . |13
|9 dup 9 9 + 9 / dup + - + . |14
|9 dup dup 9 + 9 + 9 / - + . |15


# サンプルコード

```
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Den
{
	public class Form1: Form {
		Button btn0;
		Button btn1;
		Button btn2;
		Button btn3;
		Button btn4;
		Button btn5;
		Button btn6;
		Button btn7;
		Button btn8;
		Button btn9;
		Button btn10;
		Button btn11;
		Button btn12;
		Button btn13;
		Button btn14;
		Button btn15;
		Button btn16;
		Button btn17;
		string v;
		Label label;
		private static Stack<int> stack = new Stack<int>();
		public Form1() {
			ClientSize = new Size(260, 260);
			Text = "forth電卓";
			InitializeComponent();
			v = "";
		}
		private void InitializeComponent() {
			label = new Label();
			label.Text = "0";
			label.Font = new Font("Geneva", 20, FontStyle.Regular);
			label.BackColor = Color.Azure;
			label.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			label.Dock = DockStyle.Fill;
			TableLayoutPanel parent = new TableLayoutPanel();
			parent.Width = 250;
			parent.Height = 250;
			parent.ColumnCount = 4;
			parent.RowCount = 6;
			for (int i = 0; i < parent.RowCount; i++)
			{
				parent.RowStyles.Add(new RowStyle() {
					Height = 20,
					SizeType = SizeType.Percent
				});
			}
			for (int j = 0; j < parent.ColumnCount; j++)
			{
				parent.ColumnStyles.Add(new ColumnStyle() {
					Width = 25,
					SizeType = SizeType.Percent
				});
			}
			this.Controls.Add(parent);
			Control childControl;

			btn0 = new Button();
			btn0.Click += new System.EventHandler(btn0_Click);
			btn0.Text = "Clear";
			btn0.Dock = DockStyle.Fill;
			parent.Controls.Add(btn0);

			btn1 = new Button();
			btn1.Click += new System.EventHandler(btn1_Click);
			btn1.Text = "/";
			btn1.Dock = DockStyle.Fill;
			parent.Controls.Add(btn1);

			btn2 = new Button();
			btn2.Click += new System.EventHandler(btn2_Click);
			btn2.Text = "*";
			btn2.Dock = DockStyle.Fill;
			parent.Controls.Add(btn2);

			btn3 = new Button();
			btn3.Click += new System.EventHandler(btn3_Click);
			btn3.Text = "-";
			btn3.Dock = DockStyle.Fill;
			parent.Controls.Add(btn3);

			btn4 = new Button();
			btn4.Click += new System.EventHandler(btn4_Click);
			btn4.Text = "7";
			btn4.Dock = DockStyle.Fill;
			parent.Controls.Add(btn4);

			btn5 = new Button();
			btn5.Click += new System.EventHandler(btn5_Click);
			btn5.Text = "8";
			btn5.Dock = DockStyle.Fill;
			parent.Controls.Add(btn5);

			btn6 = new Button();
			btn6.Click += new System.EventHandler(btn6_Click);
			btn6.Text = "9";
			btn6.Dock = DockStyle.Fill;
			parent.Controls.Add(btn6);

			btn7 = new Button();
			btn7.Click += new System.EventHandler(btn7_Click);
			btn7.Text = "+";
			btn7.Dock = DockStyle.Fill;
			parent.Controls.Add(btn7);

			childControl = parent.GetControlFromPosition(3, 1);
			parent.SetRowSpan(childControl, 2);

			btn8 = new Button();
			btn8.Click += new System.EventHandler(btn8_Click);
			btn8.Text = "4";
			btn8.Dock = DockStyle.Fill;
			parent.Controls.Add(btn8);

			btn9 = new Button();
			btn9.Click += new System.EventHandler(btn9_Click);
			btn9.Text = "5";
			btn9.Dock = DockStyle.Fill;
			parent.Controls.Add(btn9);

			btn10 = new Button();
			btn10.Click += new System.EventHandler(btn10_Click);
			btn10.Text = "6";
			btn10.Dock = DockStyle.Fill;
			parent.Controls.Add(btn10);

			btn11 = new Button();
			btn11.Click += new System.EventHandler(btn11_Click);
			btn11.Text = "1";
			btn11.Dock = DockStyle.Fill;
			parent.Controls.Add(btn11);

			btn12 = new Button();
			btn12.Click += new System.EventHandler(btn12_Click);
			btn12.Text = "2";
			btn12.Dock = DockStyle.Fill;
			parent.Controls.Add(btn12);

			btn13 = new Button();
			btn13.Click += new System.EventHandler(btn13_Click);
			btn13.Text = "3";
			btn13.Dock = DockStyle.Fill;
			parent.Controls.Add(btn13);

			btn14 = new Button();
			btn14.Click += new System.EventHandler(btn14_Click);
			btn14.Text = ".";
			btn14.Dock = DockStyle.Fill;
			parent.Controls.Add(btn14);

			childControl = parent.GetControlFromPosition(3, 3);
			parent.SetRowSpan(childControl, 2);

			btn15 = new Button();
			btn15.Click += new System.EventHandler(btn15_Click);
			btn15.Text = "0";
			btn15.Dock = DockStyle.Fill;
			parent.Controls.Add(btn15);

			btn16 = new Button();
			btn16.Click += new System.EventHandler(btn16_Click);
			btn16.Text = "dup";
			btn16.Dock = DockStyle.Fill;
			parent.Controls.Add(btn16);

			btn17 = new Button();
			btn17.Click += new System.EventHandler(btn16_Click);
			btn17.Text = "drop";
			btn17.Dock = DockStyle.Fill;
			parent.Controls.Add(btn17);

			parent.Controls.Add(label);
			childControl = parent.GetControlFromPosition(0, 5);
			parent.SetColumnSpan(childControl, 3);
		}
		private void btn0_Click(object sender, EventArgs e) {//clear
			v = "";
			label.Text = v;
			stack = new Stack<int>();
		}
		private void btn1_Click(object sender, EventArgs e) {///
			v = "";
			div();
		}
		private void btn2_Click(object sender, EventArgs e) {//*
			v = "";
			mul();
		}
		private void btn3_Click(object sender, EventArgs e) {//-
			v = "";
			sub();
		}
		private void btn4_Click(object sender, EventArgs e) {//7
			v = "7";
			push(v);
			label.Text = v;
		}
		private void btn5_Click(object sender, EventArgs e) {//8
			v = "8";
			push(v);
			label.Text = v;
		}
		private void btn6_Click(object sender, EventArgs e) {//9
			v = "9";
			push(v);
			label.Text = v;
		}
		private void btn7_Click(object sender, EventArgs e) {//+
			v = "";
			add();
		}
		private void btn8_Click(object sender, EventArgs e) {//4
			v = "4";
			push(v);
			label.Text = v;
		}
		private void btn9_Click(object sender, EventArgs e) {//5
			v = "5";
			push(v);
			label.Text = v;
		}
		private void btn10_Click(object sender, EventArgs e) {//6
			v = "6";
			push(v);
			label.Text = v;
		}
		private void btn11_Click(object sender, EventArgs e) {//1
			v = "1";
			push(v);
			label.Text = v;
		}
		private void btn12_Click(object sender, EventArgs e) {//2
			v = "2";
			push(v);
			label.Text = v;
		}
		private void btn13_Click(object sender, EventArgs e) {//3
			v = "3";
			push(v);
			label.Text = v;
		}
		private void btn14_Click(object sender, EventArgs e) {//.
			int top1 = stack.Pop();
			label.Text = top1.ToString();
		}
		private void btn15_Click(object sender, EventArgs e) {//0
			v = "0";
			push(v);
			label.Text = v;
		}
		private void btn16_Click(object sender, EventArgs e) {//dup
			dup();
			int top1 = stack.Pop();
			label.Text = top1.ToString();
		}
		private void btn17_Click(object sender, EventArgs e) {//drop
			drop();
			int top1 = stack.Pop();
			label.Text = top1.ToString();
		}
		private void add() {
			int top1 = stack.Pop();
			int top2 = stack.Pop();
			int v = top2 + top1;
			stack.Push(v);
		}
		private void sub() {
			int top1 = stack.Pop();
			int top2 = stack.Pop();
			int v = top2 - top1;
			stack.Push(v);
		}
		private void mul() {
			int top1 = stack.Pop();
			int top2 = stack.Pop();
			int v = top2 * top1;
			stack.Push(v);
		}
		private void div() {
			int top1 = stack.Pop();
			int top2 = stack.Pop();
			int v = top2 / top1;
			stack.Push(v);
		}
		private void dup() {
			int top1 = stack.Pop();
			stack.Push(top1);
			stack.Push(top1);
		}
		private void drop() {
			int top1 = stack.Pop();
		}
		private void push(string s) {
			int v = Convert.ToInt32(s);
			stack.Push(v);
		}
		[STAThread]
		static void Main() {
			Application.EnableVisualStyles();
			Application.Run(new Form1());
		}
	}
}



```

以上。