# 使用堆栈机使用Brouncker公式对PI进行逼近

The formula I'm working with is 4/(1 + 1^2/(2 + 3^2/(2 + 5^2/(2 + 7^2/(2 + 9^2/2))))) which is roughly 2.97. How can I translate this to stack machine code?

DIV
PUSH 4
DIV
PUSH 1
POW
PUSH 1
PUSH 2
DIV
POW
PUSH 3
PUSH 2
PUSH 2
DIV
POW
PUSH 5
PUSH 2
PUSH 2
DIV
POW
PUSH 7
PUSH 2
PUSH 2
DIV
POW
PUSH 9
PUSH 2
PUSH 2
PUSH 2

（从下至上阅读说明）

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Máquinas
{
/**
* @author toyomitsu
*
* @date - 2020.05.17
*/
public class StackMachine
{
private Stack<string> _Stack;
private string[] _Instructions;

public StackMachine(string[] Instructions)
{
Array.Reverse(Instructions);

_Stack = new Stack<string>();
_Instructions = Instructions;
}

public void PerformInstructions()
{
Console.WriteLine();
Console.WriteLine("*** Stack machine ***");

foreach (string Instruction in _Instructions)
{
string[] InstData = Instruction.Split(' ');

switch (InstData[0])
{
case "PUSH":
Push(InstData[1]);
break;

case "POP":
Pop();
break;

break;

case "SUB":
Sub();
break;

case "MOD":
Mod();
break;

case "MUL":
Mul();
break;

case "DIV":
Div();
break;

case "POW":
Pow();
break;

case "SQR":
Sqr();
break;

case "EXP":
Exp();
break;

default:
continue;
}

List<string> Registry = _Stack.ToList<string>();

Console.WriteLine();
Console.WriteLine(string.Format("** Instruction: {0} **", Instruction));
for (int i = 0; i < Registry.Count; i++)
{
Console.WriteLine(string.Format("Registry {0}: {1}", i + 1, Registry[i]));
}
Console.WriteLine("**********************");
}
}

#region Functions
private void Push(string Data)
{
_Stack.Push(Data);
}

private string Pop()
{
return _Stack.Pop();
}

{
string Data1 = Pop();
string Data2 = Pop();

double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

double Result = Num1 + Num2;
Push(Result.ToString(CultureInfo.InvariantCulture));
}

private void Sub()
{
string Data1 = Pop();
string Data2 = Pop();

double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

double Result = Num1 - Num2;
Push(Result.ToString(CultureInfo.InvariantCulture));
}

private void Mul()
{
string Data1 = Pop();
string Data2 = Pop();

double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

double Result = Num1 * Num2;
Push(Result.ToString(CultureInfo.InvariantCulture));
}

private void Mod()
{
string Data1 = Pop();
string Data2 = Pop();

double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

double Result = Num1 % Num2;
Push(Result.ToString(CultureInfo.InvariantCulture));
}

private void Div()
{
string Data1 = Pop();
string Data2 = Pop();

double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

double Result = Num1 / Num2;
Push(Result.ToString(CultureInfo.InvariantCulture));
}

private void Pow()
{
string Data1 = Pop();
string Data2 = Pop();

double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);
double Num2 = double.Parse(Data2, CultureInfo.InvariantCulture);

double Result = Math.Pow(Num1, Num2);
Push(Result.ToString(CultureInfo.InvariantCulture));
}

private void Sqr()
{
string Data1 = Pop();

double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);

double Result = Math.Sqrt(Num1);
Push(Result.ToString(CultureInfo.InvariantCulture));
}

private void Exp()
{
string Data1 = Pop();

double Num1 = double.Parse(Data1, CultureInfo.InvariantCulture);

double Result = Math.Exp(Num1);
Push(Result.ToString(CultureInfo.InvariantCulture));

}
#endregion
}
}