Программирование линейных алгоритмов - C# (183524)
Формулировка задачи:
Составить консольный и оконный приложение для вычисления и печати значений математических выражений. Значения переменных для подсчета ввести в диалоге с пользователем.
Решение задачи: «Программирование линейных алгоритмов»
textual
Листинг программы
using System;
using System.Linq;
using System.Collections.Generic;
class ForthMachine
{
private Stack<double> s;
private Stack<double> r;
public ForthMachine(IEnumerable<double> init)
{
s = new Stack<double>(init);
r = new Stack<double>();
}
public ForthMachine Push(double a)
{
s.Push(a);
return this;
}
public ForthMachine Drop()
{
s.Pop();
return this;
}
public ForthMachine Dup()
{
s.Push(s.Peek());
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 Minus()
{
double a = s.Pop();
s.Push(s.Pop() - a);
return this;
}
public ForthMachine Mul()
{
s.Push(s.Pop() * s.Pop());
return this;
}
public ForthMachine Div()
{
double a = s.Pop();
s.Push(s.Pop() / a);
return this;
}
public ForthMachine Abs()
{
s.Push(Math.Abs(s.Pop()));
return this;
}
public ForthMachine Sqr()
{
s.Push(s.Peek() * s.Pop());
return this;
}
public ForthMachine Pow()
{
double a = s.Pop();
s.Push(Math.Pow(s.Pop(), a));
return this;
}
public ForthMachine Sin()
{
s.Push(Math.Sin(s.Pop()));
return this;
}
public ForthMachine Cos()
{
s.Push(Math.Cos(s.Pop()));
return this;
}
public ForthMachine Atan()
{
s.Push(Math.Atan(s.Pop()));
return this;
}
public ForthMachine ToR()
{
r.Push(s.Pop());
return this;
}
public ForthMachine FromR()
{
s.Push(r.Pop());
return this;
}
public ForthMachine AtR()
{
s.Push(r.Peek());
return this;
}
public ForthMachine Dot()
{
Console.WriteLine(s.Pop());
return this;
}
}
class Program
{
public static void Main()
{
new ForthMachine(Console.ReadLine().Split().Select(Double.Parse))
.Swap().ToR().Swap().ToR().Dup().ToR().Push(1).Swap().Div().Atan().Cos().Sqr().FromR().FromR().Swap().FromR().Swap()
.ToR().ToR().Dup().AtR().Abs().Pow().Swap().FromR().FromR()
.ToR().ToR().Dup().AtR().Plus().Sin().Sqr().Push(1).Plus().Swap().FromR().FromR()
.ToR().ToR().Dup().Dup().Sqr().AtR().Sqr().Mul().Push(1).Plus().AtR().Push(2).Mul().Swap().Div().Minus().Abs().Swap().FromR().FromR()
.Drop().Drop().Drop().Div().Mul().Plus().Dot();
}
}