Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Analytical Engine calculations with bell when error!
// Code for Ada's table: https://www.fourmilab.ch/babbage/figures/menat3_l.png
// Source: https://www.fourmilab.ch/babbage/sketch.html
// table with code references for no-developers: https://cdn-images-1.medium.com/max/2000/1*OMxBw7S_0c60qzx5pMs6Pw.png
// working program: https://cdn-images-1.medium.com/max/800/1*xomOzLz9HRJBiqRrK4MlEw.gif
using System;
using System.Threading;
namespace AnalyticalEngine
{
public enum VariableChangeCard //just for reference
{
Zero = 0,
Retain = 1
}
public enum OperationCard //just for reference
{
Add = 0,
Subtract = 1,
Multiply = 2,
Divide = 3
}
class Engine
{
const int numberOfOperations = 11;
const int numberOfVariables = 17;
const int numberOfInputVariables = 6;
int[] V = new int[numberOfVariables];
int[] operationCards = new[] { 2, 2, 2, 2, 2, 2, 1, 1, 1, 3, 3 }; //see OperationCard enum for the numerical values meanings
int[] firstVariableChangeCards = new[] { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }; //see VariableChangeCard enum for the numerical values meanings
int[] secondVariableChangeCards = new[] { 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 }; //see VariableChangeCard enum for the numerical values meanings
int[] firstVariableSupplyingCards = new[] { 0, 3, 2, 5, 0, 2, 6, 8, 10, 13, 14 }; //index of the first variable that will be used in operation
int[] secondVariableSupplyingCards = new[] { 4, 1, 4, 1, 5, 3, 7, 9, 11, 12, 12 };//index of the second variable that will be used in operation
int[] receivingVariableCards = new[] { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; //index of the variable that will store the operation result
public void Run()
{
Console.WriteLine("Please enter the input variables");
/*
V[0] = m
V[1] = n
V[2] = d
V[3] = m'
V[4] = n'
V[5] = d'
*/
for (int i = 0; i < numberOfInputVariables; i++)
{
readInputVariable(i);
}
PrintVariables();
Console.WriteLine();
try
{
for (int i = 0; i < numberOfOperations; i++)
{
var resultVariableIndex = receivingVariableCards[i];
var operation = operationCards[i];
var firstVariableIndex = firstVariableSupplyingCards[i];
var secondVariableIndex = secondVariableSupplyingCards[i];
var firstVariableValueChange = firstVariableChangeCards[i];
var secondVariableValueChange = secondVariableChangeCards[i];
V[resultVariableIndex] = CalculateOperation(operation, firstVariableIndex, secondVariableIndex);
V[firstVariableIndex] = ChangeVariable(firstVariableIndex, firstVariableValueChange);
V[secondVariableIndex] = ChangeVariable(secondVariableIndex, secondVariableValueChange);
PrintVariables();
}
}
catch (Exception)
{
Console.WriteLine("Oops, something went wrong. Ringing the bell to notify the user");
Console.Beep();
}
Console.WriteLine("Done");
}
private void readInputVariable(int i)
{
Console.Write("V[{0}] = ", i);
var input = Console.ReadLine();
var inputInt = 0;
int.TryParse(input, out inputInt);
V[i] = inputInt;
}
private void PrintVariables()
{
for (int i = 0; i < numberOfVariables; i++)
{
Console.Write("V{0}={1} ", i, V[i]);
}
Console.WriteLine();
}
private int CalculateOperation(int operation, int firstVariableIndex, int secondVariableIndex)
{
simulateMill();
switch (operation)
{
case 0: // OperationCard.Add:
return V[firstVariableIndex] + V[secondVariableIndex];
case 1: // OperationCard.Subtract:
return V[firstVariableIndex] - V[secondVariableIndex];
case 2: // OperationCard.Multiply
return V[firstVariableIndex] * V[secondVariableIndex];
case 3: // OperationCard.Divide:
return V[firstVariableIndex] / V[secondVariableIndex];
default:
return 0;
}
}
private int ChangeVariable(int variableIndex, int variableChangeCard)
{
switch (variableChangeCard)
{
case 0:
return 0;
case 1:
return V[variableIndex];
default:
return 0;
}
}
private static void simulateMill()
{
Console.Write("Calculating");
for (int i = 0; i < 10; i++)
{
Console.Write(".");
Thread.Sleep(200);
}
Console.WriteLine();
}
}
class Program
{
static void Main(string[] args)
{
var engine = new Engine();
engine.Run();
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment