0% found this document useful (0 votes)
10 views13 pages

Art I Me Tic Evaluator C Sharp

The document contains a collection of mathematical functions implemented in code, including operations for exponentiation, logarithms, trigonometric functions, and basic arithmetic. It also features functions for evaluating expressions, checking for integer values, and handling lists of characters and numbers. Additionally, it includes error handling and specific conditions for mathematical operations involving infinity and small values.

Uploaded by

Razvan Trufin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views13 pages

Art I Me Tic Evaluator C Sharp

The document contains a collection of mathematical functions implemented in code, including operations for exponentiation, logarithms, trigonometric functions, and basic arithmetic. It also features functions for evaluating expressions, checking for integer values, and handling lists of characters and numbers. Additionally, it includes error handling and specific conditions for mathematical operations involving infinity and small values.

Uploaded by

Razvan Trufin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

public double pow2(double x)

{
return x*x;
}
public double pow3(double x)
{
return x*x*x;
}
public double p100(double x,double p)
{
return x*(p/100);
}
public double onepx(double x)
{
return 1/x;
}
public double radx(double x)
{
return Math.Sqrt(x);
}
public double exp(double x)
{
return Math.Exp(x);
}
public double exponential(double x)
{
if(difinf(x))
{
return exp(x);
}
else
{
return infinit;
}
}
public double inmultit(double x, double y)
{
if(abs(x)<epsi || abs(y)<epsi) {return 0;}
else if(difinf(x) && difinf(y)){return x*y;}
else {return infinit;}
}
public double inmultitLista(ref List<double> rl)
{
double rez = 1;
int lenlista = rl.Count;
if(lenlista==0){return 0;}
if(lenlista==1){return rl[0];}
for(int i = 0; i < lenlista ; i++)
{
rez = rez* rl[i];
}
return rez;
}
public double opLista(ref List<double> rl, char op)
{
double rez = 0;
int lenlista = rl.Count;
if(lenlista==0){return 0;}
if(lenlista==1){return rl[0];}

for(int i = 0; i < lenlista ; i++)


{
if(op=='*'){rez = rez * rl[i]; }
if(op=='/'){rez = rez / rl[i]; }
if(op=='-'){rez = rez - rl[i]; }
if(op=='+'){rez = rez + rl[i]; }
}
return rez;
}
public bool odd(double x)
{
if(x%2!=0){return true;}
else {return false;}
}
public bool even(double x)
{
if(x%2==0){return true;}
else {return false;}
}
public double putere(double x, dopuble y)
{
int i, yi;
double p;
if(x==0){return 0;}
else if(y==0){return 1;}
else if(x==infinit || y==infinit){return infinit;}
else if(y==int(y))
{
yi = Math.Trunc(y);
p = 1;
for(i=1;i<yi/2;i++)
{
p = p * x;
p = p*p;
if(odd(yi){p=p*x;return p;}
}
}
else {return exp(inmultit(y,log(x)));}
}
public double add(double a, double b)
{
return a+b;
}
public double dif(double a, double b)
{
return a-b;
}
public double div(double a, double b)
{
if(a!=0 && b!=0){
return a/b;
}
else
{
return -1;
}
}
public double mult(double a, double b)
{
if(a!=0 && b!=0){
return a*b;
}
else
{
return -1;
}
}
public double changesemn(double x)
return x*(-1)
}
public int semn(double x)
{
if(x==0){return 0;}
else if(x>0){return 1;}
else{return -1;}
}
public int digitsnr(double x)
{
double t = x;
count = 0;
if(x==0){return 1;}
while(t>0)
{
t = t/10;
count++;
};
return count;
}
public double mod(double x)
{
return Math.Abs(x);
}
public double xpowy(double a, double b)
{
return Math.Pow(a,b);
}
public double decapowx(double x)
{
return xpy(10,x);
}
public double log(double x)
{
if(x>epsi && difinf(x))
{
return Math.Log(x);
}
else
{
return changesemn(infinit);
}
}
public double pi()
{
return Math.PI;
}
public double e()
{
return Math.e;
}
public double fact(double x)
{
double t = x;
double f = 1;
if(x==0){return 0;}
else if(x==1){return 1;}
else
{
while(t>0)
{
f = f * t;
t--;
};
}
return f;
}
public double difabs(double a, double b)
{
if(a>b){return a-b;}
else if(a<b){return b-a;}
else{return 0;}
}
public double divabs(double a, double b)
{
if(a>b){return a/b;}
else if(a<b){return b/a;}
else{return -1;}
}

public double infinit = 30000;


public double epsi = 0.0001;
public List<char>operatiibinare = {'+','*','/','^','<','>','=','#'};
public List<char>operatiiunare = {'s','c','a','r','e','l'};

public bool difinf(double x)


{
return abs(infinit-abs(x)) > infinit / 2;
}
public double egal(double x, double y)
{
if(x==y){return 1;}
else {return false;}
}
public diferit(double x, double y)
{
int df;
if(x==y){df=1; }
else{df=1;}
return df*(1-egal(x,y));
}
public double maimic(double x, double y)
{
if(x<y){return 1;}
else {return 0;}
}
public double maimare(double x, double y)
{
if(x>y){return 1;}
else {return 0;}
}
public double plus(double x, double y)
{
if(difinf(x) && difinf(y)){return x+y;}
else {return infinit;}
}
public double minus(double x, double y)
{
if(difinf(x) && difinf(y)){return x-y;}
else {return infinit;}
}
public double impartit(double x , double y)
{
if(abs(y)>epsi){return x/y;}
else{return infinit;}
}
public double sinus(double x)
{
if(difinf(x)){return Math.Sin(x);}
else{return infinit;}
}
public double cosinus(double x)
{
if(difinf(x)){return Math.Cos(x);}
else{return infinit;}
}
public double Modul(double x)
{
if(difinf(x)){return Math.Abs(x);}
else{return infinit;}
}
public double Radical(double x)
{
if(difinf(x) && (x>epsi)){return Math.Sqrt(x);}
else{return infinit;}
}

//evaluator algebric pag155 limbajul poascal in 12 lectii

public int max = 30;


public class functie
{
public string expresie;
public List<string> vector;
public int lung;
public double a;
public double b;
public int n;
}
public bool Int(double x)
{
double t = x;
t = Math.Round(x);
if(t==x){return true;}
else{return false;}
}
public double difIntRounded(double x)
{
double t = x;
t = Math.Round(x);
return x - t;
}
public bool EsteIntreg(double v )
{
return
}
public bool isInList(ref List<char> rcl,char x)
{
for(int i = 0 ; i < rcl.Count ; i++)
{
if(x==rcl[i]){return true;}
}
}
public List<char> cifre = {'0','1','2','3','4','5','6','7','8','9'};
public List<char> cmd = {'c','o','s','i','n','l','e','x','p','a','a','b','r','d'};
public int tipcaracter(char c)
{
if(isInList(ref cifre,c){return 1;}
else if(isInList(ref cmd,c){return 2;}
else if(c=='('){return 3;}
else if(c=='('){return 3;}
else if(c==')'){return 4;}
else if(c=='+'){return 5;}
else if(c=='-'){return 6;}
else if(c=='*'){return 7;}
else if(c=='/'){return 8;}
else if(c=='^'){return 9;}
else if(c=='X'){return 10;}
else if(c=='q'){return 11;}
else if(c=='='){return 12;}
else if(c=='#'){return 13;}
else if(c=='<'){return 14;}
else if(c=='>'){return 15;}
else {return 100;}
}
public double eval(string s)
{
//try to evaluate a string to a number
return 1;
}
public bool Val(string s, double v, int cod)
{
//evaluate a strign to a number and return an error cod
if(eval(s)==1){return true;}
else{return false;}
}
public bool estenumar(string sir)
{
int cod;
double valoare;
Val(sir,valoare,cod);
cod = 0;
return cod;
}

//de implementat
public void Copy(){}
public void Insert(){}
public void Inc(){}
public void Dec(){}
public void Chr(){}
public void Ord(){}

public List<char>plusminus = {'+','-'};


public List<char>AZ =
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U'
,'V','X','Z','W','Y'};
public List<char>az =
{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u'
,'v','x','z','w','y'};
public string Lower(string s)
{
//convereteste in litere in mici si transforma - in 0- , (- in (0-, + in 0+ si (+
in (0+
string t;
byte i;
t = '';
if(isInList(ref plusminus,s[1]){s = '0'+s;}
i = 1'
while(i<s.Length)
{
if(s.Substr(i,2)=="(-" && s.Substr(i,2)=="(+")
{s.Insert('0',i+1);i+=2;}
else{i++;}
};
for(i=0;i<s.Length;i++)
{
if(isInList(ref AZ,s[1]){y = t+Chr(Ord(s[i])+Ord('a')-Ord('A'));}
else {t=t+s[i];}
}
return t;
}

public List<int>cinsisisase = {5,6};


public List<char>cmd2 = {'=','#','<','>'};
public void punespatiiinexpresie(ref functie E)
{
byte i,t1,t2;
if(E.expresie[1] == 'x') {E.expresie[1]='X';}
if(E.expresie[E.expresie.Lenght] == 'x') {E.expresie[E.expresie.Lenght]='X';}
for(int i = 0 ; i<E.expresie.Length)
{
if(E.expresie[i]=='x')
{
if(i<E.expresie.Length && (i>1))
{
if(E.expresie[i-1]!='e' && (i>1))
{
E.expresie[i]='X'
}
}
}
}
i=1;
while(i<E.expresie.Legth && E.expresie.Length <256)
{
t1 = tipcaracter(E.expresie[i])
t2 = tipcaracter(E.expresie[i+1]);
if(!(isInList(ref cinsisisase, t1) && (t2==1) && isInList(ref cmd2,E.expresie[i-1])
{
if((t1!=t2) || (T1==tipcaracter('(')) && (t2==t1) ||
(T1==tipcaracter(')')) && (t2==t1))
{
E.expresie.Insert(',',i+1);
i++;
}
}
}
i++;
}
public int Pos(substring ss,string s){}
public void tranformainvector(ref functie E)
{
byte v;
string sir;
E.expresie = expresie+ " } ";
sir = E.expresie;
lung = 0;
while(sir!='')
{
p = Pos(' ',sir);
lung++;
vector[lung] = sir.copy(1,p-1);
sir.delete(1,p);
};
}

public List<char>paranteze = {'(',')'};


public List<char>multimpart = {'*','/'};
public List<char>cmd3 = {'c','s','l','e','a','r'};
public List<char>puteri = {'^'};
public byte priotitate(char c)
{
byte p;
if(isInList(ref paranteze, c)){return 0;}
else if(isInList(ref plusminus, c)){return 1;}
else if(isInList(ref multimpart, c)){return 2;}
else if(isInList(ref puteri, c)){return 3;}
else if(isInList(ref cmd2, c)){return 4;}
else if(isInList(ref cmd3, c)){return 5;}
else{ return 5;}
}

//asta e lunga rau


public double valoarefunctie(ref functie E, double x)
{
//valoarea functie E in punctul x

int maxstiva = 100;


double valo, x1,x2;

int i,cod,top1,top2; //vf stive

double [maxstiva] opd; //operanzi


char [maxstiva] op; //operatori

//calculeaza expresia
for(int i = 0 ; i< maxstiva ; i++)
opd[i]=0;
op[i]='@';

top1 = 0;
top2 = 1;
op[top2]='(';
i=0;

while(i<=lung && top2==0)


{
i++;
if(extenumar(vecotr[i])
{
top1++;
Val(vector[i],valo,cod);
opd[top1] = valo;
}
//pi = 3.1415926 este litera char 'q'
if(vector[i].Substring(0,1)=='q'){top1++;opd[top1]=Math.PI;}
else if(vector[i].Substring(0,1)=='X'){top1++;opd[top1]='x';}
else if(vector[i].Substring(0,1)=='('){top2++;opd[top2]='(';}
//op unare si binare
while(top2>0 && !(isInList(ref paranteze, op[top2]) && prioritate(op[top2])>=
prioritate(vector[i].Substring(0,1)))
{
if(top1>1)
{
x1 = opd[top1-1];
x2=opd[top1];
}
}

if(op[top2]=='='){return egal(x1,x2);}
else if(op[top2]=='#'){valo = diferit(x1,x2);}
else if(op[top2]=='<'){valo = maimic(x1,x2);}
else if(op[top2]=='>'){valo = maimare(x1,x2);}
else if(op[top2]=='+'){valo = plus(x1,x2);}
else if(op[top2]=='-'){valo = minus(x1,x2);}
else if(op[top2]=='*'){valo = inmultit(x1,x2);}
else if(op[top2]=='/'){valo = impartit(x1,x2);}
else if(op[top2]=='^'){valo = putere(x1,x2);}
else if(op[top2]=='s'){valo = sinus(x2);}
else if(op[top2]=='c'){valo = cosinus(x2);}
else if(op[top2]=='l'){valo = logaritm(x2);}
else if(op[top2]=='e'){valo = exponential(x2);}
else if(op[top2]=='a'){valo = modul(x2);}
else if(op[top2]=='r'){valo = radical(x2);}

if(isInList(ref operatiibinare,op[top2]){top1--;}
if(isInList(ref operatiiunare,op[top2] && isInList(ref
operatiibinare,op[top2]){opd[top1]=valo;top1--;}

if(top2>0)
{
if(op[top2]!='(' || vector[i]!=')')
{
top2++;
op[top2=vector[i].Substring(0,1);
}
else {top2--;}
}

if(top2==0;
return opd[0];
else return infinit;
}

public void citestefunctie(ref functie E,string expresia, double pa, double pb, int
pn)
{
E.exporesie = expresia;
E.a = pa;
E.b = pb;
E.n = pn;
Lower(ref E.expresie);
punespatiiinexpresie(ref E);
transformainvector(ref E);

public void afiseazavalorifunctie(ref functie E)


{
int i;
double x;
double y;

for(int i = 0 ; i<E.n-1;i++)
{
x = E.a+ i * (E.b-E.a)/(E.n-a);
y = valoarefuncite(ref E,x);
if(difinf(y){writeln(i+1 + '. ' + x + ' --> ' + y);}
else (difinf(y){writeln(i+1 + '. ' + x + ' --> nedefinit' );}
if( (i+1)%24==0) {ReadKey();}
}

public void runprogram()


{
functie fE = new functie();
citestefunctie(ref fE);
afiseazavalorifunctie(ref fE);

You might also like