Exemplo n.º 1
0
 public VarBase Calculate()
 {
     if (m_c == 0)
         throw new CalcException("Ошибка: пустое выражение.");
     Element top1 = null;
     Element cur = m_top;
     try {
         for (; cur != null; cur = cur.m_next) {
             if (cur.m_o is Call) {
                 int rc = (cur.m_o as Call).ReqCount;
                 ArgList al = new ArgList();
                 for (int i = 0; i < rc; i++) {
                     if (top1 == null)
                         throw new CalcException("Ошибка при вычислении выражения");
                     al.Add(top1.m_o.Compute());
                     top1 = top1.m_next;
                 }
                 (cur.m_o as Call).SetArgList(al);
                 top1 = new Element((cur.m_o as Call).Compute(), top1);
             }
             else {
                 top1 = new Element(cur.m_o, top1);
             }
         }
         if ((top1 == null) || (top1.m_next != null))
             throw new CalcException("Ошибка при вычислении выражения");
         return top1.m_o.Compute();
     }
     catch (CalcException ex) {
         throw ex;
     }
     catch {
         throw new CalcException("Ошибка при вычислении выражения");
     }
 }
Exemplo n.º 2
0
			public override VarBase Perform(ArgList al) {
				if (al.Count != ReqCount)
					throw new OtherException("Неверный список аргументов");
				al.Reset();
				VarBase arg1 = al.Get();
				VarBase arg2 = al.Get();
				if(!(arg1.IsSingle()&&arg2.IsSingle()))
					throw new CalcException("Неверные типы операндов");
				return (arg1 as SingleVar).and(arg2 as SingleVar);
			}
Exemplo n.º 3
0
			public override VarBase Perform(ArgList al) {
				if (al.Count != ReqCount)
					throw new OtherException("Неверный список аргументов");
				al.Reset();
				VarBase arg1 = al.Get();
				if (arg1 is IntVar)
					return ((arg1 as IntVar).Val>0) ? (arg1.Clone() as IntVar) : (new IntVar(-((IntVar) arg1).Val));
				else if (arg1 is RealVar)
					return ((arg1 as RealVar).Val>0) ? (arg1.Clone() as RealVar) : (new RealVar(-((RealVar) arg1).Val));
				else
					throw new CalcException("Неправильные аргументы функции");
			}
Exemplo n.º 4
0
			public override VarBase Perform(ArgList al) {
				if (al.Count != ReqCount)
					throw new OtherException("Неверный список аргументов");
				al.Reset();
				VarBase arg1 = al.Get();
				if (!arg1.IsNum())
					throw new CalcException("Неправильные аргументы функции");
				try {
					return new RealVar(Math.Acos((arg1 as NumVar).ToDouble()));
				} 
				catch {
					throw new CalcException("Ошибка в вычислениях");
				}
			}
Exemplo n.º 5
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     if (arg1.IsArray())
         throw new CalcException("Ошибка преобразования");
     if(arg1.IsString()) {
         try {
             double d = System.Convert.ToDouble((arg1 as StringVar).Val);
             return new RealVar(d);
         }
         catch {
             throw new CalcException("Ошибка преобразования");
         }
     }
     else if (arg1.IsNum()) {
         return new RealVar((arg1 as NumVar).ToDouble());
     }
     else
         return null;
 }
Exemplo n.º 6
0
			public override VarBase Perform(ArgList al) {
				if (al.Count != ReqCount)
					throw new OtherException("Неверный список аргументов");
				al.Reset();
				return new IntVar(al.Get().IsSingle());
			}
Exemplo n.º 7
0
 public override VarBase Perform(ArgList al)
 {
     return InterprEnvironment.Instance.GetSub(m_name).Perform(al);
 }
Exemplo n.º 8
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     VarBase arg2 = al.Get();
     if(!(arg1.IsNum()&&arg2.IsNum()))
         throw new CalcException("Неверные типы операндов");
     return (arg1 as NumVar).mul(arg2 as NumVar);
 }
Exemplo n.º 9
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     return new IntVar(al.Get().IsString());
 }
Exemplo n.º 10
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     VarBase arg2 = al.Get();
     if(!(arg1.IsInt()&&arg2.IsInt()))
         throw new CalcException("Неверные типы операндов");
     try {
         int res = (arg1 as IntVar).Val % (arg2 as IntVar).Val;
         return new IntVar(res);
     }
     catch(System.ArithmeticException ex) {
         throw new CalcException("Ошибка в вычислениях");
     }
 }
Exemplo n.º 11
0
 public override VarBase Perform(ArgList al)
 {
     if(al.Count!=ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     VarBase arg2 = al.Get();
     if(!((arg1.IsArray()&&arg2.IsInt()))) {
         throw new CalcException("Неверные типы операндов");
     }
     return (arg1 as ArrayVar).IsElementDefined((arg2 as IntVar).Val);
 }
Exemplo n.º 12
0
 public void SetArgList(ArgList al)
 {
     m_al = al;
 }
Exemplo n.º 13
0
			public override VarBase Perform(ArgList al) {
				if (al.Count != ReqCount)
					throw new OtherException("Неверный список аргументов");
				al.Reset();
				VarBase arg1 = al.Get();
				return new StringVar(arg1.ToString());
			}
Exemplo n.º 14
0
			public override VarBase Perform(ArgList al) {
				if (al.Count != ReqCount)
					throw new OtherException("Неверный список аргументов");
				al.Reset();
				VarBase arg1 = al.Get();
				if (!arg1.IsString())
					throw new CalcException("Неправильные аргументы функции");
				return new IntVar((arg1 as StringVar).Val.Length);
			}
Exemplo n.º 15
0
			public override VarBase Perform(ArgList al) {
				if(al.Count!=ReqCount)
					throw new OtherException("Неверный список аргументов");
				return new RealVar(m_random.NextDouble());
			}
Exemplo n.º 16
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     if(!(arg1 is NumVar))
         throw new CalcException("Неверные типы операндов");
     else if(arg1 is IntVar)
         return new IntVar(-(arg1 as IntVar).Val);
     else
         return new RealVar(-(arg1 as NumVar).ToDouble());
 }
Exemplo n.º 17
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     if(!(arg1 is NumVar))
         throw new CalcException("Неверные типы операндов");
     return (arg1.Clone() as VarBase);
 }
Exemplo n.º 18
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     return new RealVar(Math.PI);
 }
Exemplo n.º 19
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     VarBase arg2 = al.Get();
     VarBase arg3 = al.Get();
     if(!arg1.IsSingle())
         throw new CalcException("Значение условия не может быть массивом");
     if((arg1 as SingleVar).ToBool())
         return (arg2.Clone() as VarBase);
     else
         return (arg3.Clone() as VarBase);
 }
Exemplo n.º 20
0
 public override VarBase Perform(ArgList al)
 {
     if(al.Count!=ReqCount)
         throw new OtherException("Invalid argument list");
     return new RealVar(m_random.NextDouble());
 }
Exemplo n.º 21
0
 public abstract VarBase Perform(ArgList al);
Exemplo n.º 22
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     if (!arg1.IsArray())
         throw new CalcException("Неправильные аргументы функции");
     return new IntVar((arg1 as ArrayVar).GetSize());
 }
Exemplo n.º 23
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     VarBase arg2 = al.Get();
     if (!(arg1.IsNum()&&arg2.IsNum()))
         throw new CalcException("Неправильные аргументы функции");
     try {
         return new RealVar(
             Math.Log(
             (arg1 as NumVar).ToDouble(),
             (arg2 as NumVar).ToDouble()
             )
             );
     }
     catch {
         throw new CalcException("Ошибка в вычислениях");
     }
 }
Exemplo n.º 24
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     VarBase arg2 = al.Get();
     if (!(arg1.IsString()&&arg2.IsString()))
         throw new CalcException("Неправильные аргументы функции");
     int res = (arg1 as StringVar).Val.IndexOf((arg2 as StringVar).Val);
     return new IntVar(res);
 }
Exemplo n.º 25
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     if(!arg1.IsSingle())
         throw new CalcException("Неверные типы операндов");
     return (arg1 as SingleVar).not();
 }
Exemplo n.º 26
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     VarBase arg2 = al.Get();
     VarBase arg3 = al.Get();
     if(!(arg1.IsString()&&arg2.IsInt()&&arg3.IsInt()))
         throw new CalcException("Неверные типы операндов");
     string str = (arg1 as StringVar).Val;
     int b = (arg2 as IntVar).Val;
     int l = (arg3 as IntVar).Val;
     if ((b < 0) || (l < 0))
         throw new CalcException("Неправильные аргументы функции");
     int sl = str.Length;
     if (b >= sl)
         return new StringVar("");
     if (sl - b < l)
         l = sl - b;
     return new StringVar(str.Substring(b, l));
 }
Exemplo n.º 27
0
        //****************************************************************************************************
		public VarBase Perform(ArgList al) {
			Namespace ns = new Namespace(InterprEnvironment.Instance.CurrentNamespace);
			ns["result"] = new IntVar(0);
			int argc = m_args.Count;
			if (al.Count != argc)
				throw new CalcException("Ќеверное число параметров");
			al.Reset();
			for (int i = 0; i < argc; i++) {
				ns[m_args[i] as System.String] = al.Get();
			}
			InterprEnvironment.Instance.CurrentNamespace = ns;
			Moment moment = new Moment(this);
			if (m_count > 1) {
				try {
					moment.Run();
				}
				catch (SyntaxErrorException ex) {
					throw ex;
				}
				catch (CalcException ex) {
					throw new CalcException("ќшибка в функции " + m_name + "[] в строке " + (moment.Pos + 1) + " : " + ex.Message);
				}
			}
			VarBase res = ns["result"];
			InterprEnvironment.Instance.CurrentNamespace = ns.PreviousNamespace;
			if (res == null)
				throw new CalcException("ќшибка в функции " + m_name + "[] : переменна¤ result не определена на момент выхода");
			return res;
		}
Exemplo n.º 28
0
 public override VarBase Perform(ArgList al)
 {
     if (al.Count != ReqCount)
         throw new OtherException("Invalid argument list");
     al.Reset();
     VarBase arg1 = al.Get();
     if (arg1.IsArray())
         throw new CalcException("Ошибка преобразования");
     if(arg1.IsString()) {
         try {
             int i = System.Convert.ToInt32((arg1 as StringVar).Val);
             return new IntVar(i);
         }
         catch {
             throw new CalcException("Ошибка преобразования");
         }
     }
     else if (arg1.IsReal()) {
         return new IntVar(System.Convert.ToInt32((arg1 as RealVar).Val));
     }
     else if (arg1.IsInt()) {
         return (arg1.Clone() as VarBase);
     }
     else
         return null;
 }