private string changeBitnessHelper(string bin, string dec, int MAX, CalcViews.Programmer.ProgrammerCalcBits currentBits, CalcViews.Programmer.ProgrammerCalcBits prevBits) { string retVal = string.Empty; // First we'll resize the bin string to have the right number of bits // per the prevBits value int prevMax = (prevBits == CalcViews.Programmer.ProgrammerCalcBits.Qword) ? LONG : (prevBits == CalcViews.Programmer.ProgrammerCalcBits.Dword) ? INT : (prevBits == CalcViews.Programmer.ProgrammerCalcBits.Word) ? SHORT : BYTE; int count = prevMax - bin.Length; for (int i = 0; i < count; i++) { bin = "0" + bin; } if (bin.StartsWith("1")) { count = MAX - bin.Length; string tempExp = string.Empty; for (int i = 0; i < count; i++) { tempExp += "1"; } return(CalcHelpers.bin2Dec(tempExp + bin, (currentView as ProgrammerView).currentBits)); } return(dec); }
public string GetDecimal(List <int> bin) { if (!_mainPage.isProgrammer()) { return(string.Empty); } string exp = string.Empty; foreach (int b in bin) { exp += b.ToString(); } return(CalcHelpers.bin2Dec(exp, (currentView as ProgrammerView).currentBits)); }
// always returns "64-bit" representation public List <int> GetBinary() { if (!_mainPage.isProgrammer()) { return(new List <int>()); } string bin = CalcHelpers.dec2Bin(Get(), (currentView as ProgrammerView).currentBits).PadLeft(MAX_BITNESS, '0'); List <int> binArray = new List <int>(); foreach (char c in bin.ToCharArray()) { binArray.Add(int.Parse(c.ToString())); } return(binArray); }
private string GetDecimal(string exp, bool asLong) { string dec = exp; if (!_mainPage.isProgrammer()) { return(dec); } if (asLong) { if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Hex) { dec = CalcViews.Programmer.hex2Long(exp); } else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Oct) { dec = CalcViews.Programmer.oct2Long(exp); } else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Bin) { dec = CalcViews.Programmer.bin2Long(exp); } } else { if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Hex) { dec = CalcHelpers.hex2Dec(exp, (currentView as ProgrammerView).currentBits); } else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Oct) { dec = CalcHelpers.oct2Dec(exp, (currentView as ProgrammerView).currentBits); } else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Bin) { dec = CalcHelpers.bin2Dec(exp, (currentView as ProgrammerView).currentBits); } } return(dec); }
private string GetExpression(string dec, bool asLong) { string exp = dec; if (!_mainPage.isProgrammer()) { return(exp); } if (asLong) { if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Hex) { exp = CalcViews.Programmer.dec2Long(dec, 16); } else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Oct) { exp = CalcViews.Programmer.dec2Long(dec, 8); } else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Bin) { exp = CalcViews.Programmer.dec2Long(dec, 2); } } else { if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Hex) { exp = CalcHelpers.dec2Hex(dec, (currentView as ProgrammerView).currentBits); } else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Oct) { exp = CalcHelpers.dec2Oct(dec, (currentView as ProgrammerView).currentBits); } else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Bin) { exp = CalcHelpers.dec2Bin(dec, (currentView as ProgrammerView).currentBits); } } return(exp); }
/// <summary> /// Converts a standard infix expression to list of tokens in /// postfix order. /// </summary> /// <param name="expression">Expression to evaluate</param> /// <returns></returns> protected List <string> TokenizeExpression(string expression) { List <string> tokens = new List <string>(); Stack <string> stack = new Stack <string>(); State state = State.None; int parenCount = 0; string temp; TextParser parser = new TextParser(expression); while (!parser.EndOfText) { if (Char.IsWhiteSpace(parser.Peek())) { // Ignore spaces, tabs, etc. } else if (parser.Peek() == '(') { // Cannot follow operand if (state == State.Operand) { throw new EvalException(ErrOperatorExpected, parser.Position); } // Allow additional unary operators after "(" if (state == State.UnaryOperator) { state = State.Operator; } // Push opening parenthesis onto stack stack.Push(parser.Peek().ToString()); // Track number of parentheses parenCount++; } else if (parser.Peek() == ')') { // Must follow operand if (state != State.Operand) { throw new EvalException(ErrOperandExpected, parser.Position); } // Must have matching open parenthesis if (parenCount == 0) { throw new EvalException(ErrUnmatchedClosingParen, parser.Position); } // Pop all operators until matching "(" found temp = stack.Pop(); while (temp != "(") { tokens.Add(temp); temp = stack.Pop(); } // Track number of parentheses parenCount--; } else if ("+-*/^&|⊕<>%÷×CP".Contains(parser.Peek().ToString())) { // Need a bit of extra code to support unary operators if (state == State.Operand) { // Pop operators with precedence >= current operator int currPrecedence = CalcHelpers.determineOPValue(parser.Peek().ToString()); while (stack.Count > 0 && CalcHelpers.determineOPValue(stack.Peek()) >= currPrecedence) { tokens.Add(stack.Pop()); } stack.Push(parser.Peek().ToString()); state = State.Operator; } else if (state == State.UnaryOperator) { // Don't allow two unary operators together throw new EvalException(ErrOperandExpected, parser.Position); } else { // Test for unary operator if (parser.Peek() == '-') { // Push unary minus stack.Push(UnaryMinus); state = State.UnaryOperator; } else if (parser.Peek() == '+') { // Just ignore unary plus state = State.UnaryOperator; } else { throw new EvalException(ErrOperandExpected, parser.Position); } } } else if (Char.IsDigit(parser.Peek()) || parser.Peek() == '.') { if (state == State.Operand) { // Cannot follow other operand throw new EvalException(ErrOperatorExpected, parser.Position); } // Parse number temp = ParseNumberToken(parser); tokens.Add(temp); state = State.Operand; continue; } else { double result; // Parse symbols and functions if (state == State.Operand) { // Symbol or function cannot follow other operand throw new EvalException(ErrOperatorExpected, parser.Position); } // TBD ADD ADDITIONAL OPERATORS HERE if (!Char.IsLetter(parser.Peek()) && parser.Peek() != '√' && parser.Peek() != '!' || parser.Peek() == '_') { // Invalid character temp = String.Format(ErrUnexpectedCharacter, parser.Peek()); throw new EvalException(temp, parser.Position); } // Save start of symbol for error reporting int symbolPos = parser.Position; // Parse this symbol temp = ParseSymbolToken(parser); // Skip whitespace parser.MovePastWhitespace(); // Check for parameter list bool wasString = false; string stringResult = string.Empty; if (parser.Peek() == '(') { wasString = true; // Found parameter list, evaluate function stringResult = EvaluateFunction(parser, temp, symbolPos); result = Convert.ToDouble(stringResult); } else { // No parameter list, evaluate symbol (variable) result = EvaluateSymbol(temp, symbolPos); } //// Handle negative result if (result < 0) { stack.Push(UnaryMinus); result = Math.Abs(result); stringResult = stringResult.Substring(1); } if (wasString) { tokens.Add(stringResult); } else { tokens.Add(result.ToString()); } state = State.Operand; continue; } parser.MoveAhead(); } // Expression cannot end with operator if (state == State.Operator || state == State.UnaryOperator) { throw new EvalException(ErrOperandExpected, parser.Position); } // Check for balanced parentheses if (parenCount > 0) { throw new EvalException(ErrClosingParenExpected, parser.Position); } // Retrieve remaining operators from stack while (stack.Count > 0) { tokens.Add(stack.Pop()); } return(tokens); }
public void ChangeBitness(CalcViews.Programmer.ProgrammerCalcBits prevBits) { if (!_mainPage.isProgrammer()) { _mainPage.initCalcError(); return; } string dec = Get(); string bin = CalcViews.Programmer.dec2Long(dec, 2); // next if the user has chosen a specific bitness if ((currentView as ProgrammerView).currentBits == CalcViews.Programmer.ProgrammerCalcBits.Qword) { // ASSUME we're comming from a lower order bit dec = changeBitnessHelper(bin, dec, LONG, (currentView as ProgrammerView).currentBits, prevBits); } else if ((currentView as ProgrammerView).currentBits == CalcViews.Programmer.ProgrammerCalcBits.Dword) { // if we're going down in bitness if (bin.Length > INT) { bin = bin.Substring(bin.Length - INT); dec = CalcHelpers.bin2Dec(bin, (currentView as ProgrammerView).currentBits); } // if we're going up in bitness else { dec = changeBitnessHelper(bin, dec, INT, (currentView as ProgrammerView).currentBits, prevBits); } } else if ((currentView as ProgrammerView).currentBits == CalcViews.Programmer.ProgrammerCalcBits.Word) { // if we're going down in bitness if (bin.Length > SHORT) { bin = bin.Substring(bin.Length - SHORT); dec = CalcHelpers.bin2Dec(bin, (currentView as ProgrammerView).currentBits); } // if we're going up in bitness else { dec = changeBitnessHelper(bin, dec, SHORT, (currentView as ProgrammerView).currentBits, prevBits); } } else if ((currentView as ProgrammerView).currentBits == CalcViews.Programmer.ProgrammerCalcBits.Byte) { // if we're going down in bitness if (bin.Length > BYTE) { bin = bin.Substring(bin.Length - BYTE); //dec = mainPage.bin2Dec(bin); } // can't go smaller than byte dec = CalcHelpers.bin2Dec(bin, (currentView as ProgrammerView).currentBits); } Set(dec, GetLastInputType(), true); }