России Бориса Николаевича Ельцина Кафедра "Информационные технологии и автоматизация проектирования" курсовая

Вид материалаКурсовая
Подобный материал:
1   2   3


#region ILexeme Members


public MyType Number

{

get { throw new Exception("The method or operation is not implemented."); }

}


#endregion

}

class OPENPARENTHESIS : ILexeme

{

public OPENPARENTHESIS() { }

public char Symbol

{ get { return '('; } }

public override string ToString()

{

return "(";

}


#region ILexeme Members


public MyType Number

{

get { throw new Exception("The method or operation is not implemented."); }

}


#endregion

}

class PLUS : ILexeme

{

public PLUS()

{

}

public char Symbol

{ get { return '+'; } }

public override string ToString()

{

return "+";

}


#region ILexeme Members


public MyType Number

{

get { throw new Exception("The method or operation is not implemented."); }

}


#endregion

}

class POINT : ILexeme

{

public POINT() { }

public char Symbol

{ get { return '.'; } }

public override string ToString()

{

return ".";

}


#region ILexeme Members


public MyType Number

{

get { throw new Exception("The method or operation is not implemented."); }

}


#endregion

}

class POW : ILexeme

{

public POW() { }

char ILexeme.Symbol

{

get { return ''; }

}

public string ToString()

{

return "";

}

#region ILexeme Members


MyType ILexeme.Number

{

get { throw new Exception("The method or operation is not implemented."); }

}


#endregion

}

class TFLOAT : ILexeme

{

public TFLOAT() { }

public char Symbol

{ get { return 'k'; } }

public override string ToString()

{

return "float";

}


#region ILexeme Members


public MyType Number

{

get { throw new Exception("The method or operation is not implemented."); }

}


#endregion

}

class TINTEGER : ILexeme

{

public TINTEGER() { }

public char Symbol

{ get { return 'k'; } }

public override string ToString()

{

return "integer";

}


#region ILexeme Members


public MyType Number

{

get { throw new Exception("The method or operation is not implemented."); }

}


#endregion

}

Интерфейс для всех лексем , и класс число:

public interface ILexeme

{

char Symbol

{ get; }


string ToString();

MyType Number

{ get; }

}


public class MyType

{

public int itsInteger;

public float itsFloat;

private string flag;


public MyType(int tmp)

{

itsInteger = tmp;

flag = "integer";

}

public MyType(float tmp)

{

itsFloat = tmp;

flag = "float";

}

public MyType(MyType tmp)

{

if (tmp.Type == "integer")

{

flag = "integer";

this.itsInteger = tmp.itsInteger;

}

else

{

flag = "float";

this.itsFloat = tmp.itsFloat;

}

}

//*******************

public static MyType operator +(MyType a, MyType b)

{

if ((a.Type == b.Type) & (a.Type == "float"))

{

return new MyType(a.itsFloat + b.itsFloat);

}

if ((a.Type == b.Type) & (a.Type == "integer"))

{

return new MyType(a.itsInteger + b.itsInteger);

}

if ((a.Type == "float") & (b.Type == "integer"))

{

return new MyType(a.itsFloat + b.itsInteger);

}

else return new MyType(a.itsInteger + b.itsFloat);

}


public static MyType operator -(MyType a, MyType b)

{

if ((a.Type == b.Type) & (a.Type == "float"))

{

return new MyType(a.itsFloat - b.itsFloat);

}

if ((a.Type == b.Type) & (a.Type == "integer"))

{

return new MyType(a.itsInteger - b.itsInteger);

}

if ((a.Type == "float") & (b.Type == "integer"))

{

return new MyType(a.itsFloat - b.itsInteger);

}

else return new MyType(a.itsInteger - b.itsFloat);

}


public static MyType operator *(MyType a, MyType b)

{

if ((a.Type == b.Type) & (a.Type == "float"))

{

return new MyType(a.itsFloat * b.itsFloat);

}

if ((a.Type == b.Type) & (a.Type == "integer"))

{

return new MyType(a.itsInteger * b.itsInteger);

}

if ((a.Type == "float") & (b.Type == "integer"))

{

return new MyType(a.itsFloat * b.itsInteger);

}

else return new MyType(a.itsInteger * b.itsFloat);

}


public static MyType operator /(MyType a, MyType b)

{

if ((a.Type == b.Type) & (a.Type == "float"))

{

return new MyType(a.itsFloat / b.itsFloat);

}

if ((a.Type == b.Type) & (a.Type == "integer"))

{

return new MyType(a.itsInteger / b.itsInteger);

}

if ((a.Type == "float") & (b.Type == "integer"))

{

return new MyType(a.itsFloat / b.itsInteger);

}

else return new MyType(a.itsInteger / b.itsFloat);

}


public static MyType operator (MyType a, MyType b)

{

double x,y;

if (a.Type == "integer") x = a.itsInteger;

else x = a.itsFloat;

if (b.Type == "integer") y = b.itsInteger;

else y = b.itsFloat;

return new MyType((float)Math.Pow(x, y));

}


public void Inverse()

{

if (flag == "float") itsFloat = 0 - itsFloat;

else itsInteger = 0 - itsInteger;

}


public void Init(MyType tmp)

{

if (this.flag == "integer")

{

if (tmp.flag == "integer") this.itsInteger = tmp.itsInteger;

else this.itsInteger = (int)tmp.itsFloat;

}

else if (tmp.flag == "float") this.itsFloat = tmp.itsFloat;

else this.itsFloat = tmp.itsInteger;

}


public void ToFloat()

{

if (flag == "integer")

{

flag = "float";

itsFloat = itsInteger;

}

}

public void ToInteger()

{

if (flag == "float")

{

flag = "integer";

itsInteger = (int)itsFloat;

}

}

//*******************

public string Type

{

get { return flag; }

}

public override string ToString()

{

if (flag == "float") return itsFloat.ToString();

else return itsInteger.ToString();

}

}

Арифметический блок для 2 этапа:

class Calculation

{

private float itsResult;


public Calculation(string expression)

{

itsResult = Partition(expression);

}


private float Partition(string expression)

{

float result;

if (float.TryParse(expression, out result)) return result;


string tmp = expression;

for (int i = tmp.Length - 1; i > 0; --i)

{

if ((!char.IsNumber(tmp[i])) && (tmp[i] != '.') && (tmp[i] != ','))

{

return Operation( Partition(tmp.Substring(0, i)) , tmp[i], float.Parse(tmp.Substring(i+1)));

}

}

return 0f;

}


private float Operation(float A, char sign, float B)

{

switch (sign)

{

case '+':

return A + B;

case '-':

return A - B;

case '*':

return A * B;

case '/':

return A / B;

case '':

return (float)Math.Pow(A, B);

default:

return 0f;

}

}


public float Result

{

get { return itsResult; }

}

}

Список литературы

Копорушкин П.А. Лекции

Вирт.Н. Алгоритмы и структуры данных. Мир 1985. стр 319 - 340