1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
void solveset(char* input, int startset, int* endset){
DoParenth(input,startset,endset);
DoExp(input,startset,endset);
DoMultiDiv(input,startset,endset);
DoPlusMinus(input,startset,endset);
return;
}
void DoExp(char* input, int startset, int* endset){
int counter=startset;
for(;counter<=*endset;counter++){
if(input[counter]=='^'){
int tempcount=counter-1;
for(;tempisnum;tempcount--){
if(tempcount<startset) break;
if(tempcount==0){
tempcount--;
break;
}
}
tempcount++;
int startreplace=tempcount;
double dleft=0;
double* pleft=&dleft;
getTerm(input,tempcount,pleft);
double dright=0;
double* pright=&dright;
for(;tempisnum;tempcount++){
if(tempcount>*endset){
cout << "error. right term is outside of set" << endl;
return;
}
}
tempcount++;
getTerm(input,tempcount,pright);
double danswer=pow(dleft,dright);
string stanswer= to_string(danswer);
for(;tempisnum;tempcount++){}
int appendhere=tempcount;
AnsReplace(input,startreplace,appendhere,stanswer);
}
}
return;
}
void getTerm(char* input,int counter,double* term){
if(!isnum){
cout << "error in getTerm. current char is "<< input[counter] << endl;
return;
}
int ndigit=10;
int ndecimal=0;
for(;isnum;counter++){
if(input[counter]=='.'){
if(ndecimal){
cout << "error in getTerm" << endl;
return;
}
else{
*term/= power(10,ndigit);
ndecimal++;
continue;
}
}
else{
if(ndecimal){
if(input[counter]=='0'){
ndecimal++;
continue;
}
else{
*term+=(double(input[counter])-48)*power(10,-ndecimal);
ndecimal++;
continue;
}
}
else{
if(input[counter]=='0'){
ndigit--;
continue;
}
else{
ndigit--;
*term+=(double(input[counter])-48)*power(10,ndigit);
continue;
}
}
}
}
if(!ndecimal) *term/=power(10,ndigit);
return;
}
void AnsReplace(char* input,int beginreplace,int appendhere,string streplace){
int counter=appendhere;
int tempcount=0;
string appendthis="";
string stinput="";
for(;input[counter]!='\0';counter++,tempcount++){}
appendthis.append(&(input[appendhere]),tempcount);
char* deletethis=&(input[0]);
input = new char[strlen(input)];
strncpy(input,deletethis,beginreplace);
strncpy(&(input[beginreplace]),streplace.c_str(),streplace.size());
strncpy(&(input[beginreplace+streplace.size()]),appendthis.c_str(),appendthis.size());
input[beginreplace+streplace.size()+appendthis.size()]='\0';
int initial=appendhere-beginreplace;
cout << initial << endl;
delete deletethis;
return;
}
|