Программирование линейных алгоритмов - C# (183524)

Узнай цену своей работы

Формулировка задачи:

Составить консольный и оконный приложение для вычисления и печати значений математических выражений. Значения переменных для подсчета ввести в диалоге с пользователем.

Решение задачи: «Программирование линейных алгоритмов»

textual
Листинг программы
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. class ForthMachine
  6. {
  7.     private Stack<double> s;
  8.     private Stack<double> r;
  9.  
  10.     public ForthMachine(IEnumerable<double> init)
  11.     {
  12.         s = new Stack<double>(init);
  13.         r = new Stack<double>();
  14.     }
  15.  
  16.     public ForthMachine Push(double a)
  17.     {
  18.         s.Push(a);
  19.         return this;
  20.     }
  21.  
  22.     public ForthMachine Drop()
  23.     {
  24.         s.Pop();
  25.         return this;
  26.     }
  27.  
  28.     public ForthMachine Dup()
  29.     {
  30.         s.Push(s.Peek());
  31.         return this;
  32.     }
  33.  
  34.     public ForthMachine Swap()
  35.     {
  36.         double b = s.Pop();
  37.         double a = s.Pop();
  38.         s.Push(b);
  39.         s.Push(a);
  40.         return this;
  41.     }
  42.  
  43.     public ForthMachine Plus()
  44.     {
  45.         s.Push(s.Pop() + s.Pop());
  46.         return this;
  47.     }
  48.  
  49.     public ForthMachine Minus()
  50.     {
  51.         double a = s.Pop();
  52.         s.Push(s.Pop() - a);
  53.         return this;
  54.     }
  55.  
  56.     public ForthMachine Mul()
  57.     {
  58.         s.Push(s.Pop() * s.Pop());
  59.         return this;
  60.     }
  61.  
  62.     public ForthMachine Div()
  63.     {
  64.         double a = s.Pop();
  65.         s.Push(s.Pop() / a);
  66.         return this;
  67.     }
  68.  
  69.     public ForthMachine Abs()
  70.     {
  71.         s.Push(Math.Abs(s.Pop()));
  72.         return this;
  73.     }
  74.  
  75.     public ForthMachine Sqr()
  76.     {
  77.         s.Push(s.Peek() * s.Pop());
  78.         return this;
  79.     }
  80.  
  81.     public ForthMachine Pow()
  82.     {
  83.         double a = s.Pop();
  84.         s.Push(Math.Pow(s.Pop(), a));
  85.         return this;
  86.     }
  87.  
  88.     public ForthMachine Sin()
  89.     {
  90.         s.Push(Math.Sin(s.Pop()));
  91.         return this;
  92.     }
  93.  
  94.     public ForthMachine Cos()
  95.     {
  96.         s.Push(Math.Cos(s.Pop()));
  97.         return this;
  98.     }
  99.  
  100.     public ForthMachine Atan()
  101.     {
  102.         s.Push(Math.Atan(s.Pop()));
  103.         return this;
  104.     }
  105.  
  106.     public ForthMachine ToR()
  107.     {
  108.         r.Push(s.Pop());
  109.         return this;
  110.     }
  111.  
  112.     public ForthMachine FromR()
  113.     {
  114.         s.Push(r.Pop());
  115.         return this;
  116.     }
  117.  
  118.     public ForthMachine AtR()
  119.     {
  120.         s.Push(r.Peek());
  121.         return this;
  122.     }
  123.  
  124.     public ForthMachine Dot()
  125.     {
  126.         Console.WriteLine(s.Pop());
  127.         return this;
  128.     }
  129. }
  130.  
  131. class Program
  132. {
  133.     public static void Main()
  134.     {
  135.         new ForthMachine(Console.ReadLine().Split().Select(Double.Parse))
  136.         .Swap().ToR().Swap().ToR().Dup().ToR().Push(1).Swap().Div().Atan().Cos().Sqr().FromR().FromR().Swap().FromR().Swap()
  137.         .ToR().ToR().Dup().AtR().Abs().Pow().Swap().FromR().FromR()
  138.         .ToR().ToR().Dup().AtR().Plus().Sin().Sqr().Push(1).Plus().Swap().FromR().FromR()
  139.         .ToR().ToR().Dup().Dup().Sqr().AtR().Sqr().Mul().Push(1).Plus().AtR().Push(2).Mul().Swap().Div().Minus().Abs().Swap().FromR().FromR()
  140.         .Drop().Drop().Drop().Div().Mul().Plus().Dot();
  141.     }
  142. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 3.75 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы