Friday, September 4, 2009

Kalkulaator v0.02

#include <>
#include <>
#include <>
#include <>

//binary nr is negative if it starts with 0
int getEquation(char str[100],float *t1,float *t2,char *sng,int *inv);
//int86(int intrptnum, struct REGS *regs, struct REGS *regs);
//int86x(int intrptnum, struct REGS *inregs, struct REGS *outregs, struct SSREGS *sregs);
int main(int argc, char *argv[])
{

float a1,a2,res;
char tehe[5];
char sign='x';
char *signpt;
float *aipt,*aipt2,*invpt;
int loc;
int Finished = 0;
int ScriptRunLimit=1000;
char function[4];
function[0]='x';//sets default function to x, to check it later
char tempc;
int invalid=0;//if the equation contains some illegal letters
aipt = &a1;
aipt2 = &a2;
signpt = &sign;
invpt = &invalid;

printf("C Kalkulaator:Rainer Plumer v0.02 \n");

while ( (Finished == 0)||(ScriptRunLimit ==0) )
{
a1=0;
a2=0;
res = 0;
if(invalid == 0)
{

sign='x';signpt = &sign;
printf("enter the equation \n");
printf("operators supported: +,-,*,/,sin(),cos(),tan(),sqrt(), \n");
scanf("%s",&tehe);


//loc = strlen(tehe);
getEquation(tehe,aipt,aipt2,signpt,invpt);
printf("sign is:%f \n",*aipt);
if(sign == '+'){res = a1+a2;}
else if(sign == '-'){res = (a1-(a2));}
else if(sign == '*'){res = a1*a2;}
else if(sign == '/'){res = a1/a2;}
else {res = a1;printf("USED FN \n");}
printf("running a1 %f \n",a1);printf("running a2 %f \n",a2);
printf("%f \n",res);
printf("Press q to exit or any other key to continue \n");
getchar();//this one gets rid of the enter key pressed
tempc = getchar();
}
else
{
invalid = 0;
}
if (tempc == 'q'){Finished = 1;}
;
ScriptRunLimit--;
}


getchar();
return 0;
}

int getEquation(char str[100],float *t1,float *t2,char *sng,int *inv)//returns the position of the first occurance of the given character param.
{
int i=0;
float a;
char static ValindNumbers[17] = {'0','1','2','3','4','5','6','7','8','9','.','-','(',')','+','/','*'};
// char static InvalidChars[]= {'0','1','2','3','4','5','6','7','8','9','.','-','(',')','+','/','*'};
int signPos = 0;
int nr1,nr2,n1pt,n2pt;
int funPt,nrPt;
char temp1[100],temp2[100];
char sign='x';
int isSign = 0;
char *funLoc = 'x';
char fun[4];
int neg=0;
int stage=0;//0 looks chars until the '(' sign, 1 looks chars until ')'
n1pt = 0;
n2pt = 0;

funLoc = strpbrk(str,"+-*/");
printf("strcspn(str,ValindNumbers):%i: \n",strspn(str,ValindNumbers));
if( ( strspn(str,ValindNumbers) != (strlen(str)))&&( (strstr(str,"cos(")==NULL)&&(strstr(str,"sin(")==NULL)&&(strstr(str,"tan(")==NULL) &&(strstr(str,"sqrt(")==NULL) ) )
{printf("ERROR:INVALID CHARACTER ENTERED: \n");*inv=1;return 0;}
else if (funLoc!=NULL)
{
printf("str is :%s: \n",str);
printf("tehe:%f %c %f \n",*t1,*sng,*t2);
for (i=0;i <= strlen(str);i++)
{
if((str[i]=='-')&&(i==0))//check if number starts with - sign
{
neg=1;//negative is true
temp1[0]=str[0];n1pt++;
printf("first number is negative");
}
else
{
if(isSign==0)
{
if(str[i]=='+'){sign='+';signPos = i;}
else if(str[i]=='-'){sign='-';signPos = i;}
else if(str[i]=='*'){sign='*';signPos = i;}
else if(str[i]=='/'){sign='/';signPos = i;}
}
if(sign == 'x'){temp1[n1pt]=str[i];n1pt++;}
else
{
if(isSign==1){temp2[n2pt]=str[i];n2pt++;}
isSign=1;//gets did of the sign mark
}
}
}
printf("str is:%s: to temp2:%f:\n",temp2,atof(temp2));

*t1 = atof(temp1);
*t2 = atof(temp2);
*sng = sign;
// if(neg==1){*t1=*t1*(-1);}
}
else
{
printf("Making function");
funPt=0;
nrPt =-1;
for (i=0;i < strlen(str);i++)
{
//look for first non valid number, thats the
if(str[i]=='('){stage=1;}
if(stage==0){fun[funPt]=str[i];funPt++;}//gets the function part, e.g cos sin sqrt tan
else if (stage == 1)
{
if(nrPt!=(-1)){temp1[nrPt]=str[i];}
nrPt++;
}
if(str[i]==')')
{
stage=2;
*t1 = atof(temp1);
if((fun[0]=='c')&&(fun[1]=='o')&&(fun[2]=='s')){*sng='x';*t1=cos((*t1*3.14159)/180);}
else if((fun[0]=='s')&&(fun[1]=='i')&&(fun[2]=='n')){*sng='x';*t1=sin((*t1*3.14159)/180);}
else if((fun[0]=='t')&&(fun[1]=='a')&&(fun[2]=='n')){*sng='x';*t1=tan((*t1*3.14159)/180);}
else if((fun[0]=='s')&&(fun[1]=='q')&&(fun[2]=='r')&&(fun[3]=='t')){*sng='x';*t1=sqrt(*t1);}
}
}
}
return 1;
}

No comments:

Post a Comment