示例#1
0
        private int BasicArithmeticalExpression(System.Collections.Generic.List<string> _tokens)
        {
            // PERFORMING A BASIC ARITHMETICAL EXPRESSION CALCULATION
            // THIS METHOD CAN ONLY OPERATE WITH NUMBERS AND OPERATORS
            // AND WILL NOT UNDERSTAND ANYTHING BEYOND THAT.

            if (_tokens.Count == 1)
            {
                return int.Parse(_tokens[0], this.CULTURE_INFO);
            }
            if (_tokens.Count == 2)
            {
                string op = _tokens[0];
                if (op == "-" || op == "+")
                {
                    return int.Parse((op == "+" ? "" : "-") + _tokens[1], this.CULTURE_INFO);
                }
                return this.OperatorAction[op](0, int.Parse(_tokens[1], this.CULTURE_INFO));
            }
            if (_tokens.Count == 0)
            {
                return 0;
            }
            for (int i = 0; i < OperatorList.Count; i++)
            {
                string op = OperatorList[i];
                while (_tokens.IndexOf(op) != -1)
                {
                    int opPlace = _tokens.IndexOf(op);

                    int numberA = Convert.ToInt32(_tokens[opPlace - 1], this.CULTURE_INFO);
                    int numberB = Convert.ToInt32(_tokens[opPlace + 1], this.CULTURE_INFO);

                    int result = this.OperatorAction[op](numberA, numberB);

                    _tokens[opPlace - 1] = result.ToString(this.CULTURE_INFO);
                    _tokens.RemoveRange(opPlace, 2);
                }
            }
            return Convert.ToInt32(_tokens[0], this.CULTURE_INFO);
        }
示例#2
0
        private int MathParserLogic(System.Collections.Generic.List<string> _tokens)
        {
            // CALCULATING THE EXPRESSIONS INSIDE THE BRACKETS
            // IF NEEDED, EXECUTE A FUNCTION

            while (_tokens.IndexOf("(") != -1)
            {
                // getting data between "(", ")"
                int open = _tokens.LastIndexOf("(");
                int close = _tokens.IndexOf(")", open); // in case open is -1, i.e. no "(" // , open == 0 ? 0 : open - 1
                if (open >= close)
                {
                    // if there is no closing bracket, throw a new exception
                    throw new ArithmeticException("No closing bracket/parenthesis! tkn: " + open);
                }
                var roughExpr = new System.Collections.Generic.List<string>();
                for (int i = open + 1; i < close; i++)
                {
                    roughExpr.Add(_tokens[i]);
                }

                int result; // the temporary result is stored here

                string functioName = _tokens[open == 0 ? 0 : open - 1];
                var _args = new int[0];
                if (this.LocalFunctions.Keys.Contains(functioName))
                {
                    if (roughExpr.Contains(","))
                    {
                        // converting all arguments into a int array
                        for (int i = 0; i < roughExpr.Count; i++)
                        {
                            int firstCommaOrEndOfExpression = roughExpr.IndexOf(",", i) != -1
                                ? roughExpr.IndexOf(",", i)
                                : roughExpr.Count;

                            var defaultExpr = new System.Collections.Generic.List<string>();
                            while (i < firstCommaOrEndOfExpression)
                            {
                                defaultExpr.Add(roughExpr[i]);
                                i++;
                            }

                            // changing the size of the array of arguments
                            Array.Resize(ref _args, _args.Length + 1);
                            if (defaultExpr.Count == 0)
                            {
                                _args[_args.Length - 1] = 0;
                            }
                            else
                            {
                                _args[_args.Length - 1] = this.BasicArithmeticalExpression(defaultExpr);
                            }
                        }

                        // finnaly, passing the arguments to the given function
                        result = int.Parse(
                            this.LocalFunctions[functioName](_args).ToString(this.CULTURE_INFO),
                            this.CULTURE_INFO);
                    }

                    else
                    {
                        // but if we only have one argument, then we pass it directly to the function
                        result =
                            int.Parse(
                                this.LocalFunctions[functioName](new[] { this.BasicArithmeticalExpression(roughExpr) })
                                    .ToString(this.CULTURE_INFO),
                                this.CULTURE_INFO);
                    }
                }
                else
                {
                    // if no function is need to execute following expression, pass it
                    // to the "BasicArithmeticalExpression" method.
                    result = this.BasicArithmeticalExpression(roughExpr);
                }

                // when all the calculations have been done
                // we replace the "opening bracket with the result"
                // and removing the rest.
                _tokens[open] = result.ToString(this.CULTURE_INFO);
                _tokens.RemoveRange(open + 1, close - open);
                if (this.LocalFunctions.Keys.Contains(functioName))
                {
                    // if we also executed a function, removing
                    // the function name as well.
                    _tokens.RemoveAt(open - 1);
                }
            }

            // at this point, we should have replaced all brackets
            // with the appropriate values, so we can simply
            // calculate the expression. it's not so complex
            // any more!
            return this.BasicArithmeticalExpression(_tokens);
        }
 protected int readExtendedFrames(StreamReader rdr, System.Collections.ArrayList frames)
 {
     string str;
     int realFrames = 0;
     for (int frms = 0; frms < FRAMES2_MAX; ++frms)
     {
         ExtendedFrame frm = new ExtendedFrame();
         bool[] set = new bool[PARTS_MAX];
         for (int parts = 0; parts < PARTS_MAX; ++parts)
         {
             str = rdr.ReadLine();
             if (str.Length > 0)
             {
                 set[parts] = true;
                 frm.names.Add(str);
             }
         }
         //read offsets
         str = rdr.ReadLine();
         string[] split = str.Split(';');
         for (int i = 0; i < PARTS_MAX; ++i)
         {
             if (!set[i])
                 continue;
             Vec2i offset = new Vec2i(Convert.ToInt32(split[2*i]), Convert.ToInt32(split[2*i+1]));
             frm.offsets.Add(offset);
         }
         if (set[0] || set[1])
             realFrames = frms+1;
         if (split.Length > PARTS_MAX * 2)
             frm.script = split[PARTS_MAX * 2].Replace('\xaf', ';');
         frames.Add(frm);
     }
     frames.RemoveRange(realFrames, FRAMES2_MAX - realFrames);
     str = rdr.ReadLine();
     return Convert.ToInt32(str);
 }
示例#4
0
		/*******************************/
		/// <summary>
		/// Sets the size of the ArrayList. If the new size is greater than the current capacity, then new null items are added to the end of the ArrayList. If the new size is lower than the current size, then all elements after the new size are discarded
		/// </summary>
		/// <param name="arrayList">The ArrayList to be changed</param>
		/// <param name="newSize">The new ArrayList size</param>
		public static void SetSize(System.Collections.ArrayList arrayList, int newSize)
		{
			if (newSize < 0) throw new System.ArgumentException();
			else
			{
				if (newSize < arrayList.Count)
					arrayList.RemoveRange(newSize,(arrayList.Count-newSize));
				else
					while(newSize > arrayList.Count)
						arrayList.Add(null);
			}
		}
示例#5
0
 // added by .net follower (http://dotnetfollower.com)
 /*******************************/
 /// <summary>
 /// Sets the capacity for the specified ArrayList
 /// </summary>
 /// <param name="vector">The ArrayList which capacity will be set</param>
 /// <param name="newCapacity">The new capacity value</param>
 // public static void SetCapacity(System.Collections.ArrayList vector, int newCapacity) // commented by .net follower (http://dotnetfollower.com)
 public static void SetCapacity(System.Collections.Generic.List<Object> vector, int newCapacity)
 {
     if (newCapacity > vector.Count)
         vector.AddRange(new Array[newCapacity-vector.Count]);
     else if (newCapacity < vector.Count)
         vector.RemoveRange(newCapacity, vector.Count - newCapacity);
     vector.Capacity = newCapacity;
 }
示例#6
0
		/// <summary>
		/// Matches the header collection against this subtree and uses the matchList
		/// and any new matches to augment the result.  This method calls ProcessSubtree()
		/// but then removes the matches that it adds to the matchList.
		/// </summary>
		/// <param name="header">the header collection to evaluate (invariant)</param>
		/// <param name="result">the result of the match (might be changed if a match is found)</param>
		/// <param name="matchList">the matches to use to do substitutions (invariant)</param>
		/// <returns>true iff this node or one of it's descendants matches</returns>
		internal bool Process(System.Collections.Specialized.NameValueCollection header, nBrowser.Result result,
		                      System.Collections.Generic.List<Match> matchList)
		{
			// The real work is done in ProcessSubtree.  This method just ensures that matchList is restored
			// to its original state before returning.
			int origMatchListCount = matchList.Count;
			bool matched = ProcessSubtree(header, result, matchList);
			if (matchList.Count > origMatchListCount)
				matchList.RemoveRange(origMatchListCount, matchList.Count-origMatchListCount);
			return matched;
		}
示例#7
0
	/*******************************/
	/// <summary>
	/// Sets the capacity for the specified ArrayList
	/// </summary>
	/// <param name="vector">The ArrayList which capacity will be set</param>
	/// <param name="newCapacity">The new capacity value</param>
	internal static void SetCapacity(System.Collections.ArrayList vector, int newCapacity)
	{
		if (newCapacity > vector.Count)
			vector.AddRange(new Array[newCapacity-vector.Count]);
		else if (newCapacity < vector.Count)
			vector.RemoveRange(newCapacity, vector.Count - newCapacity);
		vector.Capacity = newCapacity;
	}