Advertisement
Guest User

Formula Finder

a guest
Sep 12th, 2019
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 KB | None | 0 0
  1.     static class Calculator
  2.     {
  3.         private struct Operation
  4.         {
  5.             public char op;
  6.             public double val;
  7.         }
  8.  
  9.         private const int maxDigits = 6;
  10.         private const int maxPercentage = 2;
  11.         private static readonly double[] concats = { 0f, 8f, 88f, 888f, 8888f, 88888f, 888888f, 8888888f, 88888888f };
  12.         private const double minAcceptable = 1998f - 0.000001f;
  13.         private const double maxAcceptable = 1998f + 0.000001f;
  14.  
  15.         public static void Start()
  16.         {
  17.             Step(new Stack<double>(10), new Stack<Operation>(30), 0, 0);
  18.         }
  19.  
  20.         private static bool checkVictory(char op, Stack<Operation> opStack, double result)
  21.         {
  22.             if ( result < minAcceptable || result > maxAcceptable)
  23.             {
  24.                 return false;
  25.             }
  26.  
  27.             if (double.IsNaN(result) || double.IsInfinity(result))
  28.             {
  29.                 return true;
  30.             }
  31.  
  32.             Console.Write(op);
  33.             foreach ( var stackOp in opStack )
  34.             {
  35.                 if ( stackOp.op == 'n')
  36.                 {
  37.                     Console.Write(" {0}", stackOp.val);
  38.                 } else
  39.                 {
  40.                     Console.Write(stackOp.op);
  41.                 }
  42.             }
  43.             Console.WriteLine();
  44.  
  45.             return true;
  46.         }
  47.  
  48.         private static void Step(Stack<double> numStack, Stack<Operation> opStack, int digitsUsed, int percentageCount)
  49.         {
  50.             if( numStack.Count >= 2 )
  51.             {
  52.                 var op1 = numStack.Pop();
  53.                 var op2 = numStack.Pop();
  54.  
  55.                 var r = op1 + op2;
  56.                 if ( !checkVictory('+', opStack, r) )
  57.                 {
  58.                     numStack.Push(r);
  59.                     opStack.Push(new Operation() { op = '+', val = 0 });
  60.                     Step(numStack, opStack, digitsUsed, 0);
  61.                     opStack.Pop();
  62.                     numStack.Pop();
  63.                 }
  64.  
  65.                 r = op1 - op2;
  66.                 if (!checkVictory('-', opStack, r))
  67.                 {
  68.                     numStack.Push(r);
  69.                     opStack.Push(new Operation() { op = '-', val = 0 });
  70.                     Step(numStack, opStack, digitsUsed, 0);
  71.                     opStack.Pop();
  72.                     numStack.Pop();
  73.                 }
  74.  
  75.                 r = op1 * op2;
  76.                 if (!checkVictory('*', opStack, r))
  77.                 {
  78.                     numStack.Push(r);
  79.                     opStack.Push(new Operation() { op = '*', val = 0 });
  80.                     Step(numStack, opStack, digitsUsed, 0);
  81.                     opStack.Pop();
  82.                     numStack.Pop();
  83.                 }
  84.  
  85.                 r = Math.Pow(op1, op2);
  86.                 if (!checkVictory('*', opStack, r))
  87.                 {
  88.                     numStack.Push(r);
  89.                     opStack.Push(new Operation() { op = '^', val = 0 });
  90.                     Step(numStack, opStack, digitsUsed, 0);
  91.                     opStack.Pop();
  92.                     numStack.Pop();
  93.                 }
  94.  
  95.                 if (op2 != 0)
  96.                 {
  97.                     r = op1 / op2;
  98.                     if (!checkVictory('/', opStack, r))
  99.                     {
  100.                         numStack.Push(r);
  101.                         opStack.Push(new Operation() { op = '/', val = 0 });
  102.                         Step(numStack, opStack, digitsUsed, 0);
  103.                         opStack.Pop();
  104.                         numStack.Pop();
  105.                     }
  106.                 }
  107.  
  108.                 numStack.Push(op2);
  109.                 numStack.Push(op1);
  110.             }
  111.  
  112.             if ( numStack.Count >= 1 && percentageCount < maxPercentage )
  113.             {
  114.                 var op1 = numStack.Peek();
  115.                 var r = op1 / 100;
  116.                 if (!checkVictory('%', opStack, r))
  117.                 {
  118.                     numStack.Pop();
  119.                     numStack.Push(r);
  120.                     opStack.Push(new Operation() { op = '%', val = 0 });
  121.                     Step(numStack, opStack, digitsUsed, percentageCount + 1);
  122.                     opStack.Pop();
  123.                     numStack.Pop();
  124.                     numStack.Push(op1);
  125.                 }
  126.             }
  127.  
  128.             for( int i = 1; i+digitsUsed <= maxDigits; i++ )
  129.             {
  130.                 numStack.Push(concats[i]);
  131.                 opStack.Push(new Operation() { op = 'n', val = concats[i] });
  132.                 Step(numStack, opStack, i + digitsUsed, 0);
  133.                 numStack.Pop();
  134.                 opStack.Pop();
  135.             }
  136.         }
  137.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement