public Form1() { InitializeComponent(); resultBox.Text = ""; buttons.Add(button0); buttons.Add(button1); buttons.Add(button2); buttons.Add(button3); buttons.Add(button4); buttons.Add(button5); buttons.Add(button6); buttons.Add(button7); buttons.Add(button8); buttons.Add(button9); // ------------------------------------------------------------------------ var rem1 = from i in numbers where i > 80 select i; // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ List<int> iHolder = new List<int>(); foreach(int i in numbers) { if(i > 80) { iHolder.Add(i); } } var rem2 = iHolder; // ------------------------------------------------------------------------- }
private float calculateResult() { try { List<string> splitList = new List<string>(); splitList = inputTextString.Split(' ').ToList<string>(); if (splitList.Count > 1) { number1 = float.Parse(splitList[0]); operation = splitList[1].Trim(); number2 = float.Parse(splitList[2]); number1 = performOperation(operation); if (splitList.Count > 3) { for (int i = 3; i < splitList.Count; i = i + 2) { operation = splitList[i].Trim(); number2 = float.Parse(splitList[i + 1]); number1 = performOperation(operation); } } return number1; } return 0; } catch (Exception e) { Console.WriteLine(e.Message); return 0; } }
public List<OperationElement> GetOperationElements(string expression) { if (string.IsNullOrEmpty(expression)) throw new EmptyStringException(); var operationElements = new List<OperationElement>(); var builder = new StringBuilder(); foreach (char character in expression) { if (char.IsDigit(character)) { builder.Append(character); } else if (IsOperator(character)) { if (builder.Length != 0) { operationElements.Add(new OperationElement(OperationElementType.Number, builder.ToString())); builder.Clear(); } operationElements.Add(new OperationElement(OperationElementType.Operator, character.ToString())); } else { throw new IncorrectOperationStringException(expression); } } if (builder.Length != 0) operationElements.Add(new OperationElement(OperationElementType.Number, builder.ToString())); return operationElements; }
public void Initialize() { if (mInitialized) { return; } mValues = new List<AValue>(); mVariables = new List<Variable> (); //Retrieve all child values foreach (AValue v in GetComponentsInChildren<AValue>()) { mValues.Add (v); v.SetParentContainer (this); } //Retrieve all child variables (which are also values but may be accessed specifically foreach (Variable v in GetComponentsInChildren<Variable>()) { mVariables.Add (v); } //Set self being to all formulas foreach (IHasFormula f in GetComponentsInChildren<IHasFormula>()) { f.Initialize(); } mInitialized = true; }
public MegaCalculator(int accuracy, IStorage storage, List<IOperator> ops) { this.accuracy = accuracy; _storage = storage; _historylist = new List<Expression>(); _operators = ops; }
public static void Main() { IStack stack; Console.WriteLine("Type:\n -2 to use Stack on references;\n -1 to use List as an Stack;\n -0 to use Stack on an Array."); ushort command = Convert.ToUInt16(Console.ReadLine()); while (command > 2) { Console.WriteLine("Wrong input! Try again."); command = Convert.ToUInt16(Console.ReadLine()); } if (command == 2) { stack = new ReferenceStack(); } else if (command == 1) { stack = new List(); } else { stack = new ArrayStack(); } Calculator calc = new Calculator(stack); calc.Push(9); calc.Push(4); calc.Subtract(); calc.Push(2); calc.Push(6); calc.Add(); calc.Multiply(); calc.Push(4); calc.Divide(); Console.WriteLine("The result of classic expression is {0}.", calc.Result()); }
public IList<string> Parse(string expression) { if (!Validate(expression)) throw new ArgumentException("Invalid expresion", "expression"); const string left = "left"; const string @operator = "operator"; const string right = "right"; const string pattern = @"^\s*(?<left>\d+)\s*((?<operator>(\+|\-|\*|\/))\s*(?<right>.*)\s*){0,1}$"; var parsedExpression = new List<string>(); string operatorText; do { var match = Regex.Match(expression, pattern, RegexOptions.IgnorePatternWhitespace); var leftText = match.Groups[left].Value; operatorText = match.Groups[@operator].Value; expression = match.Groups[right].Value; parsedExpression.Add(leftText); if (!string.IsNullOrEmpty(operatorText)) parsedExpression.Add(operatorText); } while (!string.IsNullOrEmpty(operatorText)); return parsedExpression; }
public double CalculatePower(string input) { input = input.Trim(); if (!string.IsNullOrEmpty(input) && InputIsPower(input)) { string[] digitsStrings = input.Split('^'); string firstDigit = digitsStrings[0]; string secondDigit = digitsStrings[1]; double total = Math.Pow(double.Parse(firstDigit), double.Parse(secondDigit)); if (digitsStrings.Length > 2) { var digitList = new List<string>(digitsStrings); digitList.RemoveAt(0); digitList.RemoveAt(0); foreach (string digit in digitList) { double i = double.Parse(digit); total = Math.Pow(total, i); } } return total; } return 0; }
public List<string> getAvialableOperators() { var operatorList = new List<string>(); foreach (var op in operators) operatorList.Add(op.Key); return operatorList; }
public Node(string value) { this.value = value; this.parent = null; this.children = new List<Node>() { }; this.isRoot = true; }
private static List<IToken> ConvertToPostfixForm( List<IToken> infixExpression ) { var evaluationState = new EvaluationState(); foreach( var token in infixExpression ) token.ConvertToPostfixForm( evaluationState ); return evaluationState.PostfixExpression; }
//Задача №2 Даны три действительных числа. Если число неотрицательно - вывести его квадрат. Иначе - само число. public List<double> task2(double x, double y, double z) { if (x >= 0) { x = Math.Pow(x, 2); } if (y >= 0) { y = Math.Pow(y, 2); } if (z >= 0) { z = Math.Pow(z, 2); } List<double> result = new List<double> { x, y, z }; return result; }
public MathsCalculator() { operations = new List<Operation>(); operations.Add(new Addition()); operations.Add(new Subtraction()); operations.Add(new Multiplication()); operations.Add(new Division()); }
// on load update listbox with past equations from text file private void frmHistory_Load(object sender, EventArgs e) { equations = new List<string>(HistoryDB.Read()); foreach (string s in equations) { lstLoad.Items.Add(s); } }
static UserInterface() { List<string> mainChoices = new List<string>(new[] { "Enter Calculation", "Quit" }); MainMenu = new Menu("What is your choice", mainChoices); }
public List<int> Calculate2() { List<HomeWork> lhwc = new List<HomeWork>(); for (int a = 1; a <= 11; a++) { lhwc.Add(new HomeWork() { Id = a, Cost = a, Revenue = 10 + a, SellPrice = 20 + a }); } return Calculation_results(4, "1", lhwc); }
public static IEnumerable<Symbol> CreateWhiteSpace(int amount) { List<Symbol> ws = new List<Symbol>(); for (int i=0; i<amount; i++) { ws.Add(new WhiteSpace()); } return (IEnumerable<Symbol>)ws; }
public List<string> ValidateInputExpression(string inputexpression) { _logErros = new List<string>(); _logErros = CheckStartEndExpression(inputexpression); _logErros = CheckQuotes(inputexpression); _logErros = CheckRightSymbols(inputexpression); return _logErros; }
// update text file after button is cleared in frmHistory public static void Update(List<string> list) { using (StreamWriter textOut = new StreamWriter(new FileStream(path, FileMode.Truncate, FileAccess.Write))) { foreach (string s in list) { textOut.WriteLine(s); } } }
//Задача №7 public List<double> task7(double V1, double T1, double V2, double T2) { if (V1 > 0 && V2 > 0) { List<double> result = new List<double> { V1 + V2, (T1 * V1 + T2 * V2) / (V1 + V2) }; return result; } else { throw new ArgumentException("Объем должен быть > 0"); } }
public List<NullableFact> AggregateFactsBySalesComponent(List<NullableFact> facts, List<Relation> salesComponentRelations) { var hierarchy = new Hierarchy(salesComponentRelations); var aggregator = new Aggregator(hierarchy); var results = new List<NullableFact>(); foreach (var parent in salesComponentRelations.Select(s => s.Parent).Distinct()) { results.AddRange(aggregator.Aggregate(facts, parent)); } facts.AddRange(results); return facts; }
//Задача №5. //Даны действительные числа x, y. //Если одно из этих чисел отрицательно, то вывести модули обоих чисел. public List<double> task5(double X, double Y) { if (X < 0 || Y < 0) { List<double> result = new List<double> { Math.Abs(X), Math.Abs(Y) }; return result; } else { List<double> result = new List<double> {X, Y}; return result; } }
//Задача №4. //Даны четыре действительных числа a, b, c, d. //Если a>b>c>d, то вывести квадраты этих чисел. //Если условие не выполняется, вывести значения без изменений. public List<double> task4(double a, double b, double c, double d) { if (a > b && b > c && c > d) { List<double> result = new List<double> { Math.Pow(a, 2), Math.Pow(b, 2), Math.Pow(c, 2), Math.Pow(d, 2) }; return result; } else { List<double> result = new List<double> { a, b, c, d }; return result; } }
// clear last equation in history and update text file private void btnClear_Click(object sender, EventArgs e) { if (lstLoad.Items.Count > 0) { lstLoad.Items.RemoveAt(lstLoad.Items.Count - 1); } List<string> update = new List<string>(); foreach (string s in lstLoad.Items) { update.Add(s); } HistoryDB.Update(update); }
private bool ValidateInput(string input) { Regex key = new Regex(@"[a-z|A-Z]{1}"); Regex equal = new Regex(@"[=]{1}"); Regex value = new Regex(@"[0-9]"); List<Regex> requirements = new List<Regex> { key, equal, value }; bool valid = true; foreach (Regex item in requirements) { valid = item.IsMatch(input); if (!valid) { return false; } } return valid; }
// read text file to show history in frmHistory public static List<string> Read() { List<string> equations = new List<string>(); using (StreamReader textIn = new StreamReader(new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))) { while (textIn.Peek() != -1) { string eq = textIn.ReadLine(); equations.Add(eq); } textIn.Close(); } return equations; }
public static void AddPolyLine(Canvas canvas, List<Point> points) { // Create a red Brush SolidColorBrush redBrush = new SolidColorBrush(); redBrush.Color = Colors.Red; PointCollection collection = new PointCollection(points); Polyline polyLine = new Polyline(); polyLine.StrokeThickness = 1; polyLine.Stroke = redBrush; polyLine.Points = collection; canvas.Children.Add(polyLine); }
public override string HandleInput(string input) { _variablesToUnset = new List<string>(); _initialInput = input; _index = 0; if ( StringParse.StringStartsWith(_initialInput, Command) ) { _index = Command.Length; } E(); return _variablesToUnset.Count > 1 ? "Variables " + String.Join(", ", _variablesToUnset) + " unset." : "Variable " + String.Join(", ", _variablesToUnset) + " unset."; }
private double bracket(List<object> List) { bool isInBracket = false; bool isAnotherBracket = false; double result; int countBracket = 0; List<object> HoList = new List<object>(); List<object> ResultList = new List<object>(); for (int i = 0; i < List.Count; i++) { if (List[i] is Operator && ((Operator)List[i] == Operator.BracketOpen || (Operator)List[i] == Operator.BracketClose)) { if (Operator.BracketOpen == ((Operator)List[i])) { if (isInBracket) { isAnotherBracket = true; countBracket++; HoList.Add(List[i]); } else isInBracket = true; } else if (Operator.BracketClose == ((Operator)List[i]) && countBracket > 0) { countBracket--; HoList.Add(List[i]); } else { if (isAnotherBracket) result = bracket(HoList); else result = calculate(HoList); isInBracket = false; ResultList.Add(result); } } else if (isInBracket) HoList.Add(List[i]); else ResultList.Add(List[i]); } return calculate(ResultList); }
public CalculatorStack(string input) { char[] delimiters = {' '}; string[] tokensArray = input.Split(delimiters); valuesList = new List<double>(); operatorsList = new List<string>(); foreach (string token in tokensArray) { if (isNumber(token)) valuesList.Add(Convert.ToDouble(token)); else if (isOperator(token)) operatorsList.Add(token); else errorCode = (int)ErrorCodes.InvalidInput; } }