#include<iostream.h>
char*left(char*source,char*destination,int length)
{ *(destination+--length+1)=0;
while(length>=0)
{*(destination+length)=*(source+length);}
return destination;
}
char*midstr(char*source,char*destination,int start,int length)
{ source+=start-1;
*(destination+--length+1)=0;
while(length>=0)
{*(destination+length)=*(source+length--);}
return destination;
}
char*right(char*source,char*destination,int length)
{ while(*source!=0){source++;}
source-=length;
*(destination+--length+1)=0;
while(length>=0)
{*(destination+length)=*(source+length--);}
return destination;
}
int charinstr(char*source,char char_to_find)
{ int pos=0;
while(*(source+pos)!=0)
{if(char_to_find==*(source+pos++)){return pos;}}
return 0;
}
int len(char*source)
{ int retval=0;
while(*(source+retval++)!=0){}
return --retval;
}
int pwr(int a,int b)
{ int result=1;
for(int c=1;c<=b;c++){result*=a;}
return result;
}
char*str(int value,char*destination)
{ char*tempdest=destination;
int a=0;
int multiplier=1000000000;
for(multiplier=1000000000;multiplier!=0;multiplier/=10)
{*tempdest='0'+(value/multiplier);
value-=(value/multiplier)*multiplier;
if((*tempdest!='0')||(a))
{a++;tempdest++;}}
*tempdest=0;
return destination;
}
char*addstrings(char*destination,char*source1,char*source2)
{ char*temodest=destination;
while(*source1!=0){*(tempdest++)=*(source1++);}
while(*source2!=0){*(tempdest++)=*(source2++);}
*tempdest=0;
return destination;
}//貌似有问题
int val(char*source)
{ int result=0;
int multiplier=pwr(10,len(source)-1);
while(*source!=0)
{result+=(*(source++)-'0')*multiplier;multiplier/=10;}
return result;
}
char*assignstr(char*source,char*destination)
{ char*tempdest=destination;
while(*source!=0)
{*(tempdest++)=*(source++);}
*tempdest=0;
return destination;
}
char*calculate(char*string)
{ cout<<"\n Calculation start,parameter is"<<string<<",";
char buf1[50],buf2[50],buf3[50],buf4[50],buf5[50];
char opstr[6]="*/+-";
int leftnr;
int rightnr;
int opps;
int z;
int result;
for(int pos_in_opstr=0;pos_in_opstr<=4;pos_in_opstr++)
{ while(charinstr(string,opstr[pos_in_opstr]))
{
opps=charinstr(string,opstr[pos_in_opstr]);
for(z=opps-2;z>=0;z--)
{ if((*(string+z)=='+')||(*(string+z)=='/')||
(*(string+z)=='-'||(*(string+z)=='*')||
(*(string+z)=='^'))
{leftnr=val(midstr(string,&buf1[0],z+2,opps-z-2));z=-1;}
else if(z==0)
{leftnr=val(left(string,&buf1[0],opps-1));}
}
for(z=opps-2;z<len(string);z++)
{ if((*(string+z)=='+')||(*(string+z)=='/')||
(*(string+z)=='-'||(*(string+z)=='*')||
(*(string+z)=='^'))
{rightnr=val(midstr(string,&buf2[0],opps+1,z-opps));z=len(string);}
else if(z==len(string)-1)
{rightnr=val(right(string,&buf2[0],len(string)-opps));}
}
if
(opstr[pos_in_opstr]=='+'){result=leftnr+rightnr;}
else if(opstr[pos_in_opstr]=='-'){result=leftnr-rightnr;}
else if(opstr[pos_in_opstr]=='/'){result=leftnr/rightnr;}
else if(opstr[pos_in_opstr]=='*'){result=leftnr*rightnr;}
else if(opstr[pos_in_opstr]=='^'){result=pwr(leftnr,rightnr);}
addstrings(&buf3[0],
left(string,&buf4[0],opps-len(&buf1[0])-1),
str(result,&buf5[0]));
addstrings(string,&buf3[0],
right(string,&buf5[0],len(string)-opps-len(&buf2[0])));
cout<<"\nWhile scanning for"<<opstr[pos_in_opstr]<<"s,Ifound"
<<leftnr<<" "<<opstr[pos_in_opstr]<<" "<<right
<<",new string is"<<string<<",";
}
}
cout<<"\n Calculation end,returning"<<string;
return string;
}
int main()
{ char strn[50],buf1[50],buf2[50],buf3[50],buf4[50],buf5[50];
char oristr[50];
int z,lastopen;
cout<<"\nLittle Mathematical Proggy by Sneechy,4-5-2000"
<<"\n\nNote:this program is NOT foolproof."
<<"\n\nPlease enter a string:";
cin>>oristr;
assignstr(&oristr[0],&strn[0]);
cout<<"\nStarting bracket removal loop";
while(charinstr(&strn[0],'('))
{
for(z=0;z<=len(&strn[0]);z++)
{if(strn[z]=='('){lastopen=z;}
if(strn[z]=='(')
{cout<<"\n1 found"
<<midstr(&strn[0],&buf1[0],lastopen+2,z-lastopen-1)
<<"in"<<strn<<",calling calculate unit.";
addstrings(&strn[0],addstrings(&buf1[0],
left(&strn[0],&buf3[0],lastopen),
calculate(midstr(&strn[0],&buf4[0],lastopen+2,z-lastopen-1))),
right(&strn[0],&buf2[0],len(&strn[0])-z-1));
cout<<"\nNew string:"<<strn;
z=len(&strn[0])+1;
}
}
}
cout<<"\nBrackets removed,final strings is"<<strn<<",calculating...";
calculate(&strn[0]);
if(strn[0]==0){strn[0]='0';strn[1]=0;}
cout<<"\nDone\n\n"<<oristr<<"="<<strn<<"\n";
return 0;
}
不完整!!需改写....我也在做这个。。烦死了