Вычисление формулы - C# (183517)

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

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

Кто может помочь написать программу, которая вычислит это выражение

Решение задачи: «Вычисление формулы»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class ForthMachine
  5. {
  6.     private Stack<double> s;
  7.  
  8.     public ForthMachine()
  9.     {
  10.         s = new Stack<double>();
  11.     }
  12.  
  13.     public ForthMachine Push(double a)
  14.     {
  15.         s.Push(a);
  16.         return this;
  17.     }
  18.  
  19.     public ForthMachine Tan()
  20.     {
  21.         s.Push(Math.Tan(s.Pop()));
  22.         return this;
  23.     }
  24.  
  25.     public ForthMachine Swap()
  26.     {
  27.         double b = s.Pop();
  28.         double a = s.Pop();
  29.         s.Push(b);
  30.         s.Push(a);
  31.         return this;
  32.     }
  33.  
  34.     public ForthMachine Plus()
  35.     {
  36.         s.Push(s.Pop() + s.Pop());
  37.         return this;
  38.     }
  39.  
  40.     public ForthMachine Abs()
  41.     {
  42.         s.Push(Math.Abs(s.Pop()));
  43.         return this;
  44.     }
  45.  
  46.     public ForthMachine Dup()
  47.     {
  48.         s.Push(s.Peek());
  49.         return this;
  50.     }
  51.  
  52.     public ForthMachine Sqrt()
  53.     {
  54.         s.Push(Math.Sqrt(s.Pop()));
  55.         return this;
  56.     }
  57.  
  58.     public ForthMachine Sin()
  59.     {
  60.         s.Push(Math.Sin(s.Pop()));
  61.         return this;
  62.     }
  63.  
  64.     public ForthMachine Pow()
  65.     {
  66.         double b = s.Pop();
  67.         s.Push(Math.Pow(s.Pop(), b));
  68.         return this;
  69.     }
  70.  
  71.     public ForthMachine Div()
  72.     {
  73.         double b = s.Pop();
  74.         s.Push(s.Pop() / b);
  75.         return this;
  76.     }
  77.  
  78.     public ForthMachine Mul()
  79.     {
  80.         s.Push(s.Pop() * s.Pop());
  81.         return this;
  82.     }
  83.  
  84.     public ForthMachine Dot()
  85.     {
  86.         Console.WriteLine(s.Pop());
  87.         return this;
  88.     }
  89. }
  90.  
  91. class Program
  92. {
  93.     public static void Main()
  94.     {
  95.         double a = 0.5;
  96.         double b = 1.5;
  97.         new ForthMachine().Push(a).Push(b)
  98.         .Tan().Push(1).Swap().Div().Swap().Tan().Plus().Abs()
  99.         .Dup().Sqrt().Swap().Dup().Sin().Swap()
  100.         .Dup().Push(Math.E).Swap().Pow().Plus().Div().Mul().Dot();
  101.     }
  102. }

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


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

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

6   голосов , оценка 4.333 из 5

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

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

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