Мои Конспекты
Главная | Обратная связь


Автомобили
Астрономия
Биология
География
Дом и сад
Другие языки
Другое
Информатика
История
Культура
Литература
Логика
Математика
Медицина
Металлургия
Механика
Образование
Охрана труда
Педагогика
Политика
Право
Психология
Религия
Риторика
Социология
Спорт
Строительство
Технология
Туризм
Физика
Философия
Финансы
Химия
Черчение
Экология
Экономика
Электроника

ПРИЛОЖЕНИЕ Г Текст программы




 

УТВЕРЖДАЮ

Зав. кафедрой ПО, проф., к.т.н.

 

________________ В.И. Дубровин

 

“____” _________________ 2015

 

Разработка развлекательно-познавательного приложения для платформы Android

 

Текст программы

13.02070849.00013-01 12 01-1-ЛУ

1 лист

 

 

Руководитель

______________ А.В. Пархоменко

“____” ________________ 2015

 

Разработал

ст. гр. КНТ-410

____________ А. М. Перепелица

“____” ________________ 2015

 

Нормоконтроллер

______________ _____________

“____” ________________ 2015

 

2015 Литера


УТВЕРЖДЕН

13.02070849.00019-01 12 01-1-ЛУ

 

Разработка развлекательно-познавательного приложения для платформы Android

 

ТИТУЛЬНЫЙ ЛИСТ

 

Текст программы

13.02070849.00013 - 01 12 01-1

15 листов

 

 

2015 Литера


 

Г.1 Текст файла «TC_Analizator.cs»

using System;

using System.Collections.Generic;

using System.Collections;

using System.Linq;

using System.Text;

//using System.Threading.Tasks;

 

namespace TreeCalc

{

public enum TLex

{

LEX_ARG, // Arguments of func. f(Arg1 Arg2);

LEX_ERROR, // Ошибочная лексема

LEX_NUMBER, // Число

LEX_VARIABLE, // Переменная

LEX_FUNCTION, // Имя функции

LEX_OPERATION, // Операция

LEX_LEFT, // Левая скобка

LEX_RIGHT, // Правая скобка

LEX_R_UNARY, // Унарная правая операция

LEX_L_UNARY, // Унарная левая операция

LEX_START, // start выражения

LEX_SEPARATOR, // space

LEX_RAVNO,

LEX_TYPE,

LEX_CTRL_OPER // for, if, ...

};

 

public class Lexems : List<Lexem>

{

MathLine _owner;

internal LS_Members VarList { get { return _owner.VarList; } }

 

public void PrintList()

{

foreach (Lexem lex in this)

{

Console.WriteLine("{0} : {1}", lex.expression, lex.Type);

}

}

internal Lexems(MathLine owner)

: base()

{ _owner = owner; }

 

 

public bool Parse(string Expression)

{

this.Clear();

Lexem previous=null;

while (Expression != "")

{

string tmp = Expression;

if (!Add(ref Expression, previous) || tmp.Length < Expression.Length)

{

return false;

}

else previous = this.Last<Lexem>();

}

return true;

}//ALL DOne !

 

public bool Add(ref string Expression, Lexem previous)

{

Lexem Lex1 = null;

StringBuilder tmpS = new StringBuilder();

int i = 0;

bool separators = true; // delete all first separators

for (i=0; i < Expression.Length; i++)

{

if (separators)

{

if (Expression[i] == ' ') continue;

else separators = false;

}

 

if (Expression[0] != ',' && Expression[i] == ' ') { break; }

tmpS.Append(Expression[i]);

Console.WriteLine("_______["+tmpS.ToString()+"]");

Lexem NewLex = Lexem.GetLexem(tmpS.ToString(), previous, VarList);

if (NewLex == null)

{

if (Lex1 != null) { break; }

}

else Lex1 = NewLex;

}

if (Lex1 != null)

{

this.Add(Lex1);

if (Lex1.expression[Lex1.expression.Length-1] == ',') i--;

Expression = Expression.Remove(0, i).ToString();

return true;

}

else return false;

}

 

public string GetMaxLex()

{

return this[GetMaxLex(0, this.Count)].expression;

}

// Рефакторинг Плачет!!! но все в один цыкл нельзя.

public int GetMaxLex(int start, int end)

{

int bb = 0;

 

bb = 0;

for (int i = start; i < end; i++) // get ravno

{

if (this[i].Type == TLex.LEX_LEFT) { bb++; }

if (this[i].Type == TLex.LEX_RIGHT) { bb--; }

if (bb == 0)

{

if (this[i].Type == TLex.LEX_RAVNO)

return i;

}

}

bb = 0;

for (int i = start; i < end; i++) // get BlocOper

{

if (this[i].Type == TLex.LEX_LEFT) { bb++; }

if (this[i].Type == TLex.LEX_RIGHT) { bb--; }

if (bb == 0)

{

if (this[i].Type == TLex.LEX_CTRL_OPER)

return i;

}

}

bb = 0;

for (int i = start; i < end; i++) // get inistallising

{

if (this[i].Type == TLex.LEX_LEFT) { bb++; }

if (this[i].Type == TLex.LEX_RIGHT) { bb--; }

if (bb == 0)

{

if (this[i].Type==TLex.LEX_TYPE)

foreach (Lexem l in Lexem.Types)

{

if (this[i].expression == l.expression)

return i;

}

}

 

}

bb = 0;

int nowPriority = 0;

int tidx = 0;

for (int i = start; i < end; i++) // get oper

{

if (this[i].Type == TLex.LEX_LEFT) { bb++; }

if (this[i].Type == TLex.LEX_RIGHT) { bb--; }

if (bb == 0)

{

if (this[i].isNode() && this[i].Priority<1000)

foreach (Lexem l in Lexem.Operators)

{

if (this[i].Priority > nowPriority)

{

nowPriority = this[i].Priority;

tidx = i;

}

}

}

}

if (nowPriority > 0) return tidx;

 

bb = 0;

for (int i = start; i < end; i++) // get Var or int

{

if (this[i].Type == TLex.LEX_LEFT) { bb++; }

if (this[i].Type == TLex.LEX_RIGHT) { bb--; }

if (bb == 0)

{

if (this[i].Type == TLex.LEX_NUMBER || this[i].Type == TLex.LEX_VARIABLE)

return i;

}

}

return -1;

//throw new Exception("can't find max lex. ");

}

public void TrimKavicki(ref int start, ref int end)

{

int x1 = start; int x2 = end - 1;

while (this[start].Type == TLex.LEX_LEFT && this[end - 1].Type == TLex.LEX_RIGHT)

{

Console.WriteLine("_trimKavicki");

if (this[x1].Type == TLex.LEX_LEFT) start++;

if (this[x2].Type == TLex.LEX_RIGHT) end--;

}

}

}

 

public class Lexem

{

int _priority;

public int Priority { get { return _priority; } }

 

TLex _type;

public TLex Type { get { return _type; } }

 

public string expression { get; set; }

 

public Lexem(TLex T, string value, int prioriy)

{ _type = T; this.expression = value; _priority = prioriy; }

 

static internal Lexem[] Operators = {

new Lexem(TLex.LEX_OPERATION, "-",50),

new Lexem(TLex.LEX_OPERATION, "+",50),

new Lexem(TLex.LEX_OPERATION, "/",40),

new Lexem(TLex.LEX_OPERATION, "*",40),

new Lexem(TLex.LEX_OPERATION, "^",20),

new Lexem(TLex.LEX_FUNCTION, "pow",10),

new Lexem(TLex.LEX_FUNCTION, "sin",10),

new Lexem(TLex.LEX_FUNCTION, "cos",10),

new Lexem(TLex.LEX_FUNCTION, "cosM",10),

new Lexem(TLex.LEX_FUNCTION, "factorial",10),

new Lexem(TLex.LEX_L_UNARY, "-",30),

new Lexem(TLex.LEX_L_UNARY, "+",30),

new Lexem(TLex.LEX_L_UNARY, "*",30),

new Lexem(TLex.LEX_R_UNARY, "++",20),

new Lexem(TLex.LEX_R_UNARY, "--",20),

new Lexem(TLex.LEX_RAVNO, "=",0),

new Lexem(TLex.LEX_CTRL_OPER, "FOR",0),

new Lexem(TLex.LEX_CTRL_OPER, "END",0),

new Lexem(TLex.LEX_CTRL_OPER, "IF",0)

};

static internal Lexem[] Types = {

new Lexem(TLex.LEX_TYPE, "int",1),

new Lexem(TLex.LEX_TYPE, "float",1),

new Lexem(TLex.LEX_TYPE, "mass",1),

new Lexem(TLex.LEX_TYPE, "func",1),

new Lexem(TLex.LEX_TYPE, "string",1)

};

internal static Lexem GetLexem(string value, Lexem previous, LS_Members vl)

{

Lexem tmp=null;

//variable

if (previous != null && previous.Type == TLex.LEX_TYPE)

{

int l = value.Length-1;

for (int i = l; i > 0; i--)

{

if (GetType(value.Remove(0, i), vl) != null) return null;

}

tmp = new Lexem(TLex.LEX_VARIABLE, value, 1000);

}

else tmp = GetType(value, vl);

 

if (tmp!=null && checkRights(previous, ref tmp)) return tmp;

else return null;

}

public static bool checkRights(Lexem LexemPrevious, ref Lexem LexemNow)

{

// куча правил, главная философия здесь - старатся указывать ошибочные ситуации а не наоборот.

if (LexemNow == null) throw new Exception("checkRights: Lexem=null. it is strange...");

bool allRight = false;

switch (LexemNow.Type)

{

case TLex.LEX_CTRL_OPER:

allRight = true;

break;

case TLex.LEX_TYPE:

if (LexemPrevious == null || LexemPrevious.Type == TLex.LEX_LEFT) allRight = true;

else allRight = false;

break;

case TLex.LEX_RAVNO:

if (LexemPrevious.Type == TLex.LEX_VARIABLE) allRight = true;

else allRight = false;

break;

case TLex.LEX_ARG:

if (LexemPrevious.Type == TLex.LEX_ARG || LexemPrevious.Type == TLex.LEX_LEFT) allRight = true;

else allRight = false;

break;

case TLex.LEX_FUNCTION:

if (LexemPrevious.Type == TLex.LEX_RIGHT || LexemPrevious.Type == TLex.LEX_NUMBER || LexemPrevious.Type == TLex.LEX_VARIABLE || LexemPrevious.Type == TLex.LEX_ARG || LexemPrevious.Type == TLex.LEX_R_UNARY) allRight = false;

else allRight = true;

break;

case TLex.LEX_LEFT:

if (LexemPrevious.Type == TLex.LEX_RIGHT || LexemPrevious.Type == TLex.LEX_NUMBER || LexemPrevious.Type == TLex.LEX_NUMBER || LexemPrevious.Type == TLex.LEX_VARIABLE || LexemPrevious.Type == TLex.LEX_ARG) allRight = false;

else allRight = true;

break;

case TLex.LEX_RIGHT:

if (LexemPrevious.Type == TLex.LEX_FUNCTION || LexemPrevious.Type == TLex.LEX_L_UNARY || LexemPrevious.Type == TLex.LEX_OPERATION) allRight = false;

else allRight = true;

break;

case TLex.LEX_L_UNARY:

if (LexemPrevious.Type == TLex.LEX_FUNCTION || LexemPrevious.isTerm() || LexemPrevious.Type == TLex.LEX_RIGHT) allRight = false;

else allRight = true;

break;

case TLex.LEX_NUMBER:

allRight = true;

break;

case TLex.LEX_OPERATION:

//тут раньше использвалась проверка относительно и следующей лексемы

// полностью не проверяю правельность унарных операций

if (LexemPrevious.Type != TLex.LEX_LEFT && LexemPrevious.Type != TLex.LEX_SEPARATOR) // сзади нет скобок или пробелов()

{

allRight = true; // то этот оператор являеться бинарным

}

else // с зади есть скобка или пробел, а с переди нет.

{

LexemNow = Lexem.getOperLU(LexemNow.expression); // это левосторонний унарный оператор

if (LexemNow != null) allRight = true;

else allRight = false;

}

break;

case TLex.LEX_SEPARATOR:

allRight = true;

break;

case TLex.LEX_START:

allRight = true;

break;

case TLex.LEX_ERROR:

allRight = true;

break;

case TLex.LEX_R_UNARY:

if (LexemPrevious.isTerm() || LexemPrevious.Type == TLex.LEX_RIGHT) allRight = true;

else allRight = false;

break;

case TLex.LEX_VARIABLE:

if (LexemPrevious != null && (LexemPrevious.isTerm() || LexemPrevious.Type == TLex.LEX_RIGHT)) allRight = false;

else allRight = true;

break;

}

return allRight;

}

internal static Lexem GetType(string tmp, LS_Members vl)

{

double number = 0;

if (tmp == "=")

{ return new Lexem(TLex.LEX_RAVNO, tmp, 1000); }

if (tmp == ", ")

{ return new Lexem(TLex.LEX_SEPARATOR, tmp, 1000); }

if (tmp == "(") { return new Lexem(TLex.LEX_LEFT, tmp, 1000); }

if (tmp == ")") { return new Lexem(TLex.LEX_RIGHT, tmp, 1000); }

if (Char.IsDigit(tmp[0]) && Double.TryParse(tmp, out number) && !tmp.Contains(' '))

{ return new Lexem(TLex.LEX_NUMBER, tmp, 1000); }

if (vl.containsKey(tmp))

{ return new Lexem(TLex.LEX_VARIABLE, tmp, 1000); }

foreach (Lexem lex in Lexem.Types)

{

if ((lex.expression) == tmp) { return new Lexem(lex.Type, lex.expression, lex.Priority); }

}

foreach (Lexem lex in Lexem.Operators)

{

if (lex.expression == tmp) { return new Lexem(lex.Type, lex.expression, lex.Priority); }

}

return null;

}

 

#region getTypes

public bool isTerm()

{

 

TLex lex = this.Type;

if (lex == TLex.LEX_ARG || lex == TLex.LEX_NUMBER || lex == TLex.LEX_VARIABLE) return true;

else return false;

}

public bool isNode()

{

 

TLex lex = this.Type;

if (lex == TLex.LEX_R_UNARY || lex == TLex.LEX_FUNCTION || lex == TLex.LEX_OPERATION || lex == TLex.LEX_L_UNARY) return true;

else return false;

}

public static Lexem getOperLU(string tmp)

{

foreach (Lexem l in Operators)

{

if (l.Type == TLex.LEX_L_UNARY && l.expression == tmp) return new Lexem(l.Type, l.expression, l.Priority);

}

 

return null; }

public static Lexem getOperRU(string tmp) {

foreach (Lexem l in Operators)

{

if (l.Type == TLex.LEX_R_UNARY && l.expression == tmp) return new Lexem(l.Type, l.expression,l.Priority);

}

return null;

}#endregion

 

}

}

Г.2 Текст файла «TC_Function.cs»

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace TreeCalc

{

public class TC_Function

{

string _name;

public string Name { get { return _name; } }

LS_Members _vl;

public LS_Members VarList { get { return _vl; } }

Variable[] _args;

public Variable[] Args { get { return _args; } }

public List<MathLine> MathLines { get; set; }

 

public TC_Function(string name)

{

_name = name;

_vl = new LS_Members();

_args = null;

MathLines=new List<MathLine>();

}

public TC_Function(string name, LS_Members args)

{

_name = name;

if (args != null) _args = args.ToArray();

else { throw new Exception("When TC_Function Create with arguments, args is empty. try use: [ TC_Function(string name, LS_Members args) ] ."); }

MathLines = new List<MathLine>();

}

 

public void Compile(string Text)

{

_vl.Clear();

MathLines.Clear();

 

string[] ss = Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

for (int i = 0; i < ss.Length; i++)

{

MathLines.Add(new MathLine(ss[i], this));

}

}

public void Compile(string Text, LS_Members args)

{

_vl.Clear();

MathLines.Clear();

 

if (args != null)

{

//

_vl.AddRange(args.ToArray());

//

_args = args.ToArray();

}

string[] ss = Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

for (int i = 0; i < ss.Length; i++)

{

MathLines.Add(new MathLine(ss[i], this));

}

}

public LS_Members Calculate()

{

if (this.Args != null && this.Args.Length!=0) { throw new Exception("This function have arguments."); }

OperCtrl_BLOCK block = new OperCtrl_BLOCK(MathLines, 0, MathLines.Count);

block.DoAll();

return _vl;

}

public LS_Members Calculate(LS_Members args)

{

if (this.Args != null && this.Args.Length != 0)

{

OperCtrl_BLOCK block = new OperCtrl_BLOCK(MathLines, 0, MathLines.Count);

this.VarList.setValues(args);

block.DoAll();

}

return _vl;

}

}

 

}

Г.3 Код файла «MathTree.cs»

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace TreeCalc

{

public class TC_Function

{

string _name;

public string Name { get { return _name; } }

LS_Members _vl;

public LS_Members VarList { get { return _vl; } }

Variable[] _args;

public Variable[] Args { get { return _args; } }

public List<MathLine> MathLines { get; set; }

 

public TC_Function(string name)

{

_name = name;

_vl = new LS_Members();

_args = null;

MathLines=new List<MathLine>();

}

public TC_Function(string name, LS_Members args)

{

_name = name;

if (args != null) _args = args.ToArray();

else { throw new Exception("When TC_Function Create with arguments, args is empty. try use: [ TC_Function(string name, LS_Members args) ] ."); }

MathLines = new List<MathLine>();

}

 

public void Compile(string Text)

{

_vl.Clear();

MathLines.Clear();

 

string[] ss = Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

for (int i = 0; i < ss.Length; i++)

{

MathLines.Add(new MathLine(ss[i], this));

}

}

public void Compile(string Text, LS_Members args)

{

_vl.Clear();

MathLines.Clear();

if (args != null)

{

//

_vl.AddRange(args.ToArray());

//

_args = args.ToArray();

}

string[] ss = Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

for (int i = 0; i < ss.Length; i++)

{

MathLines.Add(new MathLine(ss[i], this));

}

}

public LS_Members Calculate()

{

if (this.Args != null && this.Args.Length!=0) { throw new Exception("This function have arguments."); }

OperCtrl_BLOCK block = new OperCtrl_BLOCK(MathLines, 0, MathLines.Count);

block.DoAll();

return _vl;

}

public LS_Members Calculate(LS_Members args)

{

if (this.Args != null && this.Args.Length != 0)

{

OperCtrl_BLOCK block = new OperCtrl_BLOCK(MathLines, 0, MathLines.Count);

this.VarList.setValues(args);

block.DoAll();

}

return _vl;

}

}

}

Г.4 Код файла «INode.cs»

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace TreeCalc

{

public interface INode

{

int Generation { get; }

MathLine OwnerLine { get; }

Lexems LexemList { get; }

LS_Members VarList { get; }

string Key { get; }

INode[] Refs { get; }

double Value { get; }

double GetValue();

}

}

Г.5 Код файла «Combinators.cs»

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

//using System.Threading.Tasks;

 

namespace TreeCalc

{

// узлы операторов

public class Number : INode

{

#region filds

double _value;

int _gen;

MathLine _ownerLine;

string _key;

INode[] _refs;

public int Generation

{

get { return _gen; }

}

 

public MathLine OwnerLine

{

get { return _ownerLine; }

}

 

public Lexems LexemList

{

get { return _ownerLine.LexemList; }

}

 

public LS_Members VarList

{

get { return _ownerLine.VarList; }

}

 

public string Key

{

get { return _key; }

}

 

public INode[] Refs

{

get { return _refs; }

}

 

public double Value

{

get { return _value; }

}

#endregion

public Number(string name, MathLine owner, int gen)

{

_gen = gen + 1;

_ownerLine = owner;

_key = name;

_refs = null;

_value = Double.Parse(name);

}

public double GetValue()

{

return this.Value;

}

 

}

public class OperatorBinary : INode

{

#region filds

double _value;

int _gen;

MathLine _ownerLine;

string _key;

INode[] _refs;

public int Generation

{

get { return _gen; }

}

 

public MathLine OwnerLine

{

get { return _ownerLine; }

}

 

public Lexems LexemList

{

get { return _ownerLine.LexemList; }

}

 

public LS_Members VarList

{

get { return _ownerLine.VarList; }

}

 

public string Key

{

get { return _key; }

}

 

public INode[] Refs

{

get { return _refs; }

}

 

public double Value

{

get { return _value; }

}

#endregion

public OperatorBinary(string name, INode[] args, MathLine owner, int gen)

{

_gen = gen + 1;

_ownerLine = owner;

_key = name;

_refs = args;

_value = GetValue();

}

public double GetValue()

{

double rezult = 0;

switch (Key)

{

case "=":

rezult = Refs[1].Value;

break;

case "+":

rezult = Refs[0].Value + Refs[1].Value;

break;

case "-":

rezult = Refs[0].Value - Refs[1].Value;

break;

case "*":

rezult = Refs[0].Value * Refs[1].Value;

break;

case "/":

rezult = Refs[0].Value / Refs[1].Value;

break;

case "^":

rezult = Math.Pow(Refs[0].Value, Refs[1].Value);

break;

default:

break;

}

return rezult;

}

}

public class OperatorUL : INode

{

#region filds

double _value;

int _gen;

MathLine _ownerLine;

string _key;

INode[] _refs;

public int Generation

{

get { return _gen; }

}

 

public MathLine OwnerLine

{

get { return _ownerLine; }

}

 

public Lexems LexemList

{

get { return _ownerLine.LexemList; }

}

 

public LS_Members VarList

{

get { return _ownerLine.VarList; }

}

 

public string Key

{

get { return _key; }

}

 

public INode[] Refs

{

get { return _refs; }

}

 

public double Value

{

get { return _value; }

}

#endregion

public OperatorUL(string name, INode[] args, MathLine owner, int gen)

{

_gen = gen + 1;

_ownerLine = owner;

_key = name;

_refs = args;

_value = GetValue();

}

public double GetValue()

{

double rezult = 0;

switch (Key)

{

case "+":

rezult = Math.Abs(Refs[0].Value);

break;

case "-":

rezult = (0 - 1) * Refs[0].Value;

break;

case "*":

rezult = Math.Round(Refs[0].Value);

break;

default:

break;

}

return rezult;

}

}

public class OperatorUR : INode

{

#region filds

double _value;

int _gen;

MathLine _ownerLine;

string _key;

INode[] _refs;

public int Generation

{

get { return _gen; }

}

 

public MathLine OwnerLine

{

get { return _ownerLine; }

}

 

public Lexems LexemList

{

get { return _ownerLine.LexemList; }

}

 

public LS_Members VarList

{

get { return _ownerLine.VarList; }

}

 

public string Key

{

get { return _key; }

}

 

public INode[] Refs

{

get { return _refs; }

}

 

public double Value

{

get { return _value; }

}

#endregion

public OperatorUR(string name, INode[] args, MathLine owner, int gen)

{

_gen = gen + 1;

_ownerLine = owner;

_key = name;

_refs = args;

_value = GetValue();

}

public double GetValue()

{

double rezult = 0;

switch (Key)

{

case "++":

(Refs[0] as Variable).SetValue(Refs[0].Value+1);

rezult = Refs[0].Value;

break;

case "--":

(Refs[0] as Variable).SetValue(Refs[0].Value-1);

rezult = Refs[0].Value;

break;

default:

break;

}

return rezult;

}

}

public class Func : INode

{

#region filds

double _value;

int _gen;

MathLine _ownerLine;

string _key;

INode[] _refs;

public int Generation

{

get { return _gen; }

}

 

public MathLine OwnerLine

{

get { return _ownerLine; }

}

 

public Lexems LexemList

{

get { return _ownerLine.LexemList; }

}

 

public LS_Members VarList

{

get { return _ownerLine.VarList; }

}

 

public string Key

{

get { return _key; }

}

 

public INode[] Refs

{

get { return _refs; }

}

 

public double Value

{

get { return _value; }

}

#endregion

 

public Func(string name, INode[] args, MathLine owner, int gen)

{

_gen = gen + 1;

_ownerLine = owner;

_key = name;

_refs = args;

_value = GetValue();

}

public double GetValue()

{

double rezult = 0;

switch (Key)

{

case "sin":

rezult = Math.Sin(Refs[0].Value * Math.PI / 180);

break;

case "cos":

rezult = Math.Cos(Refs[0].Value * Math.PI / 180);

break;

case "cosM":

rezult = Math.Cos((Refs[0].Value - Refs[1].Value)/Refs[2].Value);

break;

case "plus":

rezult = Refs[0].Value + Refs[1].Value;

break;

case "minus":

rezult = Refs[0].Value - Refs[1].Value;

break;

case "umnoj":

rezult = Refs[0].Value * Refs[1].Value;

break;

case "factorial":

int N = (int)Refs[0].Value;

rezult = StandartMath.fact(N);

break;

case "pow":

rezult = Math.Pow(Refs[0].Value, Refs[1].Value);

break;

default:

throw new Exception("Func:Node - function is not determing!");

}

return rezult;

}

}

//______________конец

 

// узлы хранения данных

public class Variable : INode

{

#region filds

double _value;

int _gen;

MathLine _ownerLine;

string _key;

INode[] _refs;

public int Generation

{

get { return _gen; }

}

 

public MathLine OwnerLine

{

get { return _ownerLine; }

}

 

public Lexems LexemList

{

get { return _ownerLine.LexemList; }

}

 

public LS_Members VarList

{

get { return _ownerLine.VarList; }

}

 

public string Key

{

get { return _key; }

}

 

public INode[] Refs

{

get { return _refs; }

}

 

public double Value

{

get { return _value; }

}

#endregion

public Variable(string name, double value, MathLine owner, int gen)

{

_gen = gen + 1;

_ownerLine = owner;

_key = name;

_refs = null;

_value = value;

}

public void SetValue(double dd)

{

_value = dd;

}

public double GetValue()

{

return this.Value;

}

}

public class Mass : INode

{

#region filds

double _value;

int _gen;

MathLine _ownerLine;

string _key;

INode[] _refs;

public int Generation

{

get { return _gen; }

}

 

public MathLine OwnerLine

{

get { return _ownerLine; }

}

 

public Lexems LexemList

{

get { return _ownerLine.LexemList; }

}

 

public LS_Members VarList

{

get { return _ownerLine.VarList; }

}

 

public string Key

{

get { return _key; }

}

 

public INode[] Refs

{

get { return _refs; }

}

 

public double Value

{

get { return _value; }

}

#endregion

public Mass(string name, INode[] args, MathLine owner, int gen)

{

_gen = gen + 1;

_ownerLine = owner;

_key = name;

_refs = args;

_value = GetValue();

}

public double GetValue()

{

return (double)Refs.Length;

}

}

//______________конец

 

// узлы управления кодом

public class Node_End : INode

{

#region filds

double _value;

int _gen;

MathLine _ownerLine;

string _key;

INode[] _refs;

public int Generation

{

get { return _gen; }

}

 

public MathLine OwnerLine

{

get { return _ownerLine; }

}

 

public Lexems LexemList

{

get { return _ownerLine.LexemList; }

}

 

public LS_Members VarList

{

get { return _ownerLine.VarList; }

}

 

public string Key

{

get { return _key; }

}

 

public INode[] Refs

{

get { return _refs; }

}

 

public double Value

{

get { return _value; }

}

#endregion

 

public Node_End(MathLine owner)

{

_gen = 0;

_ownerLine = owner;

_key = "END";

_refs = null;

_value = 0;

}

public double GetValue()

{

return _value;

}

}

public class Node_For : INode

{

 

#region filds

double _value;

int _gen;

MathLine _ownerLine;

string _key;

INode[] _refs;

public int Generation

{

get { return _gen; }

}

 

public MathLine OwnerLine

{

get { return _ownerLine; }

}

 

public Lexems LexemList

{

get { return _ownerLine.LexemList; }

}

 

public LS_Members VarList

{

get { return _ownerLine.VarList; }

}

 

public string Key

{

get { return _key; }

}

 

public INode[] Refs

{

get { return _refs; }

}

 

public double Value

{

get { return _value; }

}

#endregion

 

Variable _iterator;

static double _start;

static double _step;

static double _max;

 

public Variable Iterator { get { return _iterator; } }

public double Start { get { return _start; } }

public double Step { get { return _step; } }

public double Max { get { return _max; } }

public Node_For(Variable iter, double step, double max,

MathLine owner)

{

_gen = 0;

_ownerLine = owner;

_key = "FOR";

_refs = null;

_value = 0;

 

_iterator = iter;

_start = iter.Value;

_step = step;

_max = max;

}

public double GetValue()

{

return this.Start;

}

 

}

//______________конец

}

Г.6 Код файла «Members.cs»

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace TreeCalc

{

public enum VARType

{

VAR_DOUBLE,

VAR_MASS,

VAR_FUNCTION,

VAR_TYPE,

VAR_ARG

};

 

public class LS_Members : List<Variable>

{

// Module _owner;

// public Module Owner { get { return _owner; } }

public LS_Members() : base() { }

public string getListing()

{

StringBuilder ss = new StringBuilder("________________________________\r\n");

foreach (Variable v in this)

{

ss.Append(string.Format("{0} (_) = {1}\r\n", v.Key, v.Value.ToString()));

}

ss.Append("_________________________________\r\n");

return ss.ToString();

}

public new void Add(Variable variable)

{

//Console.WriteLine("addvvvar {0} {1}", variable.key, variable.Rezult);

bool used = false;

foreach (Variable v in this)

{

if (v.Key == variable.Key) { used = true; throw new Exception("this parametr is used"); }

}

if (!used) { base.Add(variable); }

}

public bool containsKey(string str)

{

foreach (Variable v in this)

{

if (v.Key == str) { return true; }

}

return false;

}

public Variable getVar(string str)

{

foreach (Variable v in this)

{

if (v.Key == str) { return v; }

}

throw new Exception(String.Format("varable base have not this variable in expression:[{0}]", str));

}

public void setValue(string ss, double dd)

{

for (int i = 0; i < this.Count; i++)

{

if (this[i].Key == ss)

{

this[i] = new Variable(ss, dd, this[i].OwnerLine, 0);

}

}

}

public bool setValues(LS_Members NewMembers)

{

Queue<Variable> FindVars = new Queue<Variable>();

for (int i = 0; i < NewMembers.Count; i++)

{

bool find = false;

for (int j = 0; j < this.Count; j++)

{

if (this[j].Key == NewMembers[i].Key)

{

FindVars.Enqueue(this[j]);

find = true;

break;

}

}

if (!find) return false;

}

if (FindVars.Count == NewMembers.Count)

{

for (int i = 0; i < FindVars.Count; i++)

{

Variable v = FindVars.Dequeue();

v.SetValue(NewMembers[i].Value);

}

if (FindVars.Count == 0) return true;

else return false;

}

return false;

}

 

}

}


 

 




Поиск по сайту:







©2015-2020 mykonspekts.ru Все права принадлежат авторам размещенных материалов.