#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int InsertIntoStr(char *target,char value[100],int start,int n);
int StrCpyFromToPtrs(char *str,char *targetstr,int start,int end);//returns the index right after the substring found in the
string e.g "hello world","ello" would return 4, position right after o,
int GetSubstrPos(int startingPos,char str[100],char substr[50]);//returns the index right after the end of the substring
int StrCpyFromTo(char str[100],char *targetstr[100],int start,int end);
int FormatData(char *formatedData,char name[100],char val[100],char capsule,char delimiter);
int AddIntoMemory(char *mem,char name[100],char value[100]);
float GetFromMemory(char *mem,char name[100]);
int SolveString(char *str,char sign);
int CountCharsInStr(char str[400],char symbol);//counts how many of the given characters tehre are in a str
int CutFromStr(char *str,int start,int end);
int Validate(char str[400],char validChars[20]);//checks if a string contains ONLY the given characters
int GetBetweenSymbols(char *str,char *result,char symbol1,char symbol2);//copys the inner most characters between symbol1 and
symbol2
int main(int argc, char *argv[])
{
system("TITLE Calculator by Rainer Plumer");
int i;
char teststr[100],targetstr[100];
char Memory[500];
Memory[0]='\0';
char chartestStr[100];
char BetweenBraces[100];
char *memPtr,*testStrPtr;
float arv;
int BracPos = 0;
int solveReturn=0;
int isValid = 0;//boolean for validation
memPtr = &Memory;
char *testptr,*betweenptr;
int cycles = 1;
int BracLen = 0;
int BracStart = 0;
char choise = 'x';
float resultAsNr = 0.0;
char function[10];
char *functionPtr;
int nrOfSigns=0;
functionPtr = &function;
testptr = &teststr;
betweenptr = &BetweenBraces;
//InsertIntoStr(testptr," world",5,5);||
while (1+1==2)
{
//------------------------------------
choise='x';
printf("Do you wish to use functions like [sin(),cos(),tan(),average()]\nOptions [y,n,e],e=exit\n");
scanf("%c",&choise);
if( (choise=='e')||(choise=='E') ){break;}
if( (choise=='y')||(choise=='Y') )
{
printf("Enter function name and a value between the braces \n Valid functions are[sin()sin(),cos(),tan(),average()] \n");
scanf("%s",testptr);getchar();
BracStart=0;
BracStart = GetBetweenSymbols(testptr,betweenptr,'(',')');//leiab sulgude vahelise osa
BracStart--;
StrCpyFromToPtrs(testptr,functionPtr,0,BracStart);//gets the function name part
printf("sub is:%i \n",GetSubstrPos(0,function,"sin"));
if(GetSubstrPos(0,function,"average") == 7)
{
nrOfSigns = CountCharsInStr(BetweenBraces,'+')+1;
solveReturn=1;
while(solveReturn !=0)
{
solveReturn=SolveString(betweenptr,'+');
}
printf("Average is:%f \n",atof(BetweenBraces)/nrOfSigns);
}
else if(GetSubstrPos(0,function,"sin") == 3)
{
printf("Answer Is:%f",sin( (atof(BetweenBraces)*(3.14159/180)) ));
}
else if(GetSubstrPos(0,function,"cos") == 3)
{
printf("Answer Is:%f",cos( (atof(BetweenBraces)*(3.14159/180)) ));
}
else if(GetSubstrPos(0,function,"tan") == 3)
{
printf("Answer Is:%f",tan( (atof(BetweenBraces)*(3.14159/180)) ));
}
else if(GetSubstrPos(0,function,"sqrt") == 4)
{
printf("Answer Is:%f",sqrt( atof(BetweenBraces) ));
}
// printf("StrCpyFromTo:%s \n",function);
printf("functionPtr is:%s \n",function);
continue;
}
printf("Enter your Equation \n");
printf("Valid Characters Are[1,2,3,4,5,6,7,8,9,0,(,),+,-,*,/,.] \n");
scanf("%s",testptr);
printf("is %i",CountCharsInStr(teststr,'('));
isValid=Validate(teststr,"1234567890()+-*/.");
if(isValid == 0){printf("ERROR:YOU ENTERED AN INVALID CHARACTER \n");continue;}
if( (CountCharsInStr(teststr,'(')-CountCharsInStr(teststr,')') )!=0 ){printf("ERROR:MISSING ONE BRACE \n");continue;}
BracPos = GetSubstrPos(0,teststr,"(");
while(BracPos != (-1))
{
BracStart = GetBetweenSymbols(testptr,betweenptr,'(',')');//leiab sulgude vahelise osa
//printf("between:%s: \n",BetweenBraces);
BracLen = strlen(BetweenBraces);
CutFromStr(testptr,BracStart,BracStart+BracLen+1);//lõikab sulgude osa võrrandist välja
//----------lahendamine
cycles = 1;
while (cycles < 5)
{
if(cycles == 1){solveReturn=SolveString(betweenptr,'*');}
else if(cycles == 2){solveReturn=SolveString(betweenptr,'/');}
else if(cycles == 3){solveReturn=SolveString(betweenptr,'+');}
else if(cycles == 4){solveReturn=SolveString(betweenptr,'-');}
else if(GetSubstrPos(0,teststr,"-")==1){break;}
if(solveReturn==0){cycles++;}
//solveReturn returns 0 if sign was not found in the equation.
}
//printf("Befor3e insertion:%s: \n",testptr);
InsertIntoStr(testptr,betweenptr,BracStart,0);
//printf("after insertion:%s: \n",testptr);
//----------eo lahendamine
BracStart = 0;
BracPos = GetSubstrPos(0,teststr,"(");
}
cycles = 1;
//arv = GetFromMemory(memPtr,"vastus");
while (cycles < 5)
{
if(cycles == 1){solveReturn=SolveString(testptr,'*');}
else if(cycles == 2){solveReturn=SolveString(testptr,'/');}
else if(cycles == 3){solveReturn=SolveString(testptr,'+');}
else if(cycles == 4){solveReturn=SolveString(testptr,'-');}
else if(GetSubstrPos(0,teststr,"-")==1){break;}
if(solveReturn==0){cycles++;}
//solveReturn returns 0 if sign was not found in the equation.
}
printf("Vastus On:%s: \n",testptr);
printf("Do you wish to continue [y,n] \n");
getchar();
if(getchar()=='n'){break;}
//------------------------------------
}
//AddIntoMemory(memPtr,"tehe","1001");
//AddIntoMemory(memPtr,"vas*tus","3993");
//AddIntoMemory(memPtr,"arv1","0.32");
//AddIntoMemory(memPtr,"arv3","89.98");
//printf("Memory(%s) \n",Memory);
//printf("Memory query(%f) \n",arv);
//i = GetSubstrPos(0,"Katorlaator v0.1","tor");
//system("PAUSE");
return 0;
}
int GetSubstrPos(int startingPos,char str[100],char substr[50])//returns the index right after the substring found in the string e.g
"hello world","ello" would return 4, position right after o,
{
int i = 0;
int matches=0;//once mathes gets bigger that 0, all other symbols must match too
if (startingPos> strlen(str)){return (-1);}
for (i=startingPos;i < strlen(str);i++)
{
if(str[i]==substr[matches]){matches++;}
else if(matches != 0){matches == 0;}
if (matches == strlen(substr)){return (i+1);}//printf("match at pos: %i :",i+1);
}
return (-1);
}
int StrCpyFromTo(char str[100],char *targetstr[100],int start,int end)//returns the index right after the substring found in the
string e.g "hello world","ello" would return 4, position right after o,
{
int i = 0;
int resptr = start;
char temp[100];
//*targetstr = temp;
if (start> strlen(str)){return (-1);}
for (i=start;( (i < strlen(str))&&(i <= end) );i++)
{
//printf("pre added %i:",i);
*targetstr[resptr] = str[i];
//printf("aft added %i:",i);
resptr++;
}
return (-1);
}
int StrCpyFromToPtrs(char *str,char *targetstr,int start,int end)//returns the index right after the substring found in the string
e.g "hello world","ello" would return 4, position right after o,
{
int i = 0;
int resptr =start;
char temp[100];
//*targetstr = temp;
if (start> strlen(str)){return (-1);}
for (i=start;( (i < strlen(str))&&(i <= end) );i++)
{
//printf("pre added %i:",i);
targetstr[resptr] = str[i];
//printf("aft added %i:",i);
resptr++;
}
return (-1);
}
int InsertIntoStr(char *target,char value[100],int start,int n)
{
//printf("Started FN InsertIntoStr:\n");
char eos[100];//end part of string
int eosPtr=0;
int i;
int cptr=0;
int targetLen=strlen(target);
int valueLen =strlen(value);
//printf("target len is:%i: \n",targetLen);
int valueptr = 0;
if (n==0){n=valueLen-1;}//if 0 is entered, all the string is copied into 1 param
//must keep copyng from target str until found end of strhing char \0
//n=1;
//for (i=start;(i <= start+n);i++)
for (i=start;i <= (targetLen-start)+(start+n);i++)
{
if(i < targetLen)
{
//printf("eos[%i] = :%c: at pos %i\n",cptr,target[i],i);
eos[cptr] = target[i];//copis the char that is gona be shifted to the right
}
if(i <= start+n)
{
// printf("target[%i] = :%c: \n",i,value[cptr]);
target[i] = value[valueptr];valueptr++;
}
cptr++;
}
for (i=0;(i <= cptr-1);i++)
{
//printf("conpying end target[%i] = :%c: \n",(1+start+n)+i,eos[i]);
target[(1+start+n)+i] = eos[i];
}
target[targetLen+n+1] = '\0';
//printf("str copied :%s: \n",target);
//printf("Finished FN InsertIntoStr:\n");
return 1;
}
int FormatData(char *formatedData,char name[100],char val[100],char capsule,char delimiter)//capsule, a character which is added
around each value,delimiter separates name-value pairs
{
int i=0;
//char formatedData[203];//:name;value;:
int memLen = strlen(formatedData);
int nameLen = strlen(name);
int valLen = strlen(val);
int formatedLen = 0;
//char *fpt;
//fpt = &formatedData;
formatedData[0]=delimiter;
int valptr=0;
for (i=1;i <= nameLen;i++)//first add the name part
{
formatedData[i]=name[i-1];
if(i+1==nameLen){formatedData[i+2]=capsule;formatedData[i+3]='\0';}//adds the capsule´s left part
}
formatedLen = strlen(formatedData);
for (i=formatedLen;i < formatedLen+valLen;i++)//first add the name part
{
formatedData[i]=val[valptr];valptr++;
if(i+1==formatedLen+valLen){formatedData[i+1]=capsule;formatedData[i+2]='\0';}//adds the capsule´s left part
}
formatedLen = strlen(formatedData);
formatedData[formatedLen]= delimiter;
formatedData[formatedLen+1]= '\0';
//printf("formated data %s \n",formatedData);
}
int AddIntoMemory(char *mem,char name[100],char value[100])
{
int i=0;
int memLen = strlen(mem);
int nameLen = strlen(name);
int valueLen = strlen(value);
int fdataInd = 0;
char fdata[203];//formated tata, e.g ;name:value:;
char *fdataPtr;
//fdataPtr = &fdata;
fdata[0]='\0';
//FormatData(fdataPtr,name,value,';',':');
for (i=0;i < nameLen;i++)//first add the name part
{
fdata[i]=name[i];
}
for (i=nameLen;i < (nameLen+valueLen);i++)//first add the name part
{
fdata[i]=value[i-nameLen];
}
fdata[nameLen+valueLen]=';';
fdata[(nameLen+valueLen)+1]='\0';
int fdataLen = strlen(fdata);
for (i=memLen;i < (memLen+fdataLen);i++)//first add the name part
{
//printf(" mem[%i]=fdata[%i] \n",i,fdataInd);
mem[i]=fdata[fdataInd];fdataInd++;
if(i+1==(memLen+fdataLen)){mem[i+1]='\0';}//terminates the string
}
}
float GetFromMemory(char *mem,char name[100])
{
int i=0;
int memLen = strlen(mem);
int nameLen = strlen(name);
char res[100];
char *resptr;
resptr = &res;
int fdataInd = 0;
int startInd,endInd;
//printf("len %s \n",name);
//name[nameLen+1]='\0';
nameLen = strlen(name);
//adds the ; at the end of the lookup word
startInd=GetSubstrPos(0,mem,name);//get substr pos returns pos right after the lookup value
endInd=GetSubstrPos(startInd,mem,";");
endInd-=2;
for (i=startInd;i <=endInd;i++)//first add the name part
{
res[i-startInd]=mem[i];
if (i==endInd){res[i-startInd+1]='\0'; }//terminate string
}
if(strlen(res)==0){return (0);}//if not set yet
return atof(res);
}
//4----1--2--3
//10+(5-3)*3/3
//9-3+4/2
int SolveString(char *str,char sign)
{
int i= 0;
int strLen = strlen(str);
char nr1[100],nr2[100],resultAsString[100];
int singFound = 0;
int startPt,finishPt;
int sliceStart,sliceEnd,signPos;
sliceStart=0;
sliceEnd=0;
startPt = 0;
finishPt = 0;
float result = 0.0;
for (i=0;i <strLen;i++)
{
if((str[i]==sign)&&(singFound == 0)&&(sign=='-')&&(startPt == 0) )//special case:if a minus sign is solved and a number
starts with minus
{
nr1[0]='-';startPt=1;
printf("solving - sign and first number starts with -");
}
else if((str[i]==sign)&&(singFound == 0))
{
singFound=1;
signPos=i;
finishPt=0;
}//if a sign Is found
else if((str[i]=='+')||(str[i]=='-')||(str[i]=='*')||(str[i]=='/'))//if some OTHER a sign is found
{
//printf("Found some other sign:%c: \n",str[i]);
if((singFound == 0)&&(str[i]!=sign))
{
if(str[i]=='-')//if a number starts with a -
{
nr1[0]='-';startPt=1;
}
else{startPt=0;}
}
else //we reached finish
{
if( (finishPt == 0)&&(str[i]=='-') ){nr2[0]='-';finishPt=1;}
else{break;}
//break;
}
}
//(5+-10)*10
//else if((str[i]=='\0')){printf("FINISH \n");break;}//if reached finish
else//if a character must be read into either starting part or finishing part
{
if(singFound == 0){nr1[startPt]=str[i];startPt++;}
else //reading input for second part
{
//printf("Added:%c:to nr 2 \n",str[i]);
nr2[finishPt]=str[i];finishPt++;
}
}
}
sliceStart = (signPos-startPt);
sliceEnd = (signPos+finishPt);
//printf("sliceStart=%i \n",sliceStart);
//printf("sliceEnd =%i \n",sliceEnd);
nr1[startPt]='\0';nr2[finishPt]='\0';//terminates both strings
printf("str solved is=%s:and sign solved is %c\n",str,sign);
printf("nr1=%s \n",nr1);
printf("nr2=%s \n",nr2);
if(sign == '+'){result = atof(nr1)+atof(nr2);}
else if(sign == '-'){result = atof(nr1)-atof(nr2);}
else if(sign == '*'){result = atof(nr1)*atof(nr2);}
else if(sign == '/'){result = atof(nr1)/atof(nr2);}
if(singFound !=0)
{
CutFromStr(str,sliceStart,sliceEnd);//removes the part that was calculated from the big string
snprintf(resultAsString,100,"%f",result);//converts the float result into string
printf("bef insertion:%s: \n",str);
if(result==0){InsertIntoStr(str,"-0",sliceStart,0);}
else{InsertIntoStr(str,resultAsString,sliceStart,0);}//adds the new value back into the big string
printf("after insertion:%s: \n",str);
}
//printf("StringSolvedTO=%s \n",str);
if(startPt==0){return 0;}
return singFound;//returns 0 if the given sign was not found in the string
}
int CutFromStr(char *str,int start,int end)
{
int i= 0;
char endPart[100];
int strLen = strlen(str);
int endPartCounter=0;
int endPtLen = 0;
//int replaceLen = str(replace);
//123456789
//copy the end part of the string into temp memory
if((strLen-1)>=(end+1))//if str is gut to the end, there is no need to copy anything
{
for ( (i=end+1);i <strLen;i++)
{
endPart[endPartCounter]=str[i];endPartCounter++;
}
endPart[endPartCounter]='\0';
}
else{str[start]='\0';return 1;}//printf("cut to end str IS=%s \n",str);
//replaces the deltable part with the end part, that collapses in. terminates with \0
endPtLen = endPartCounter;
//printf("endPtLen IS=%i \n",endPtLen);
endPartCounter=0;
for (i=start;i <start+endPtLen;i++)
{
//printf("str[%i]=endPart[%c]\n",i,endPart[endPartCounter]);
str[i]=endPart[endPartCounter];endPartCounter++;
}
str[(start+endPtLen)]='\0';
//printf("cut str IS=%s \n",str);
}
int GetBetweenSymbols(char *str,char *result,char symbol1,char symbol2)
{
int i = 0;
int strLen = strlen(str);
int foundS1=0;
int foundS2=0;
int resCounter = 0;
int startingPt = 0;
int finishingPt = 0;
for (i=0;i <strLen;i++)
{
//printf("str[%i]=endPart[%c]\n",i,endPart[endPartCounter]);
if( (str[i]==symbol2)&&(foundS1 == 1) )
{
foundS2 = 1;
finishingPt = i;
return startingPt;
}
if( (foundS1 == 1)&&(foundS2 == 0) )
{
result[resCounter]=str[i];result[resCounter+1]='\0';
resCounter++;
//printf("result[%i]=str[%c] \n",resCounter,str[i]);
}
if(str[i]==symbol1){foundS1 = 1;resCounter=0;startingPt=i;}
}
return 0;
}
int Validate(char str[400],char validChars[20])
{
int j,i = 0;
int strLen = strlen(str);
int validCharsLen = strlen(validChars);
int error = 0;
for (i=0;i <strLen;i++)
{
error = 1;
for (j=0;j <validCharsLen;j++)
{
if(str[i]==validChars[j]){error = 0;}
}
if(error == 1){return 0;}
}
return 1;
}
int CountCharsInStr(char str[400],char symbol)//counts how many of the given characters tehre are in a str
{
int i=0;
int symbols=0;
int strLen = strlen(str);
for (i=0;i <strLen;i++)
{
if(str[i]==symbol){symbols++;}
}
return symbols;
}