Вычисление формулы - C# (183517)
Формулировка задачи:
Кто может помочь написать программу, которая вычислит это выражение
Решение задачи: «Вычисление формулы»
textual
Листинг программы
using System;
using System.Collections.Generic;
class ForthMachine
{
private Stack<double> s;
public ForthMachine()
{
s = new Stack<double>();
}
public ForthMachine Push(double a)
{
s.Push(a);
return this;
}
public ForthMachine Tan()
{
s.Push(Math.Tan(s.Pop()));
return this;
}
public ForthMachine Swap()
{
double b = s.Pop();
double a = s.Pop();
s.Push(b);
s.Push(a);
return this;
}
public ForthMachine Plus()
{
s.Push(s.Pop() + s.Pop());
return this;
}
public ForthMachine Abs()
{
s.Push(Math.Abs(s.Pop()));
return this;
}
public ForthMachine Dup()
{
s.Push(s.Peek());
return this;
}
public ForthMachine Sqrt()
{
s.Push(Math.Sqrt(s.Pop()));
return this;
}
public ForthMachine Sin()
{
s.Push(Math.Sin(s.Pop()));
return this;
}
public ForthMachine Pow()
{
double b = s.Pop();
s.Push(Math.Pow(s.Pop(), b));
return this;
}
public ForthMachine Div()
{
double b = s.Pop();
s.Push(s.Pop() / b);
return this;
}
public ForthMachine Mul()
{
s.Push(s.Pop() * s.Pop());
return this;
}
public ForthMachine Dot()
{
Console.WriteLine(s.Pop());
return this;
}
}
class Program
{
public static void Main()
{
double a = 0.5;
double b = 1.5;
new ForthMachine().Push(a).Push(b)
.Tan().Push(1).Swap().Div().Swap().Tan().Plus().Abs()
.Dup().Sqrt().Swap().Dup().Sin().Swap()
.Dup().Push(Math.E).Swap().Pow().Plus().Div().Mul().Dot();
}
}