<pre name="code" class="cpp">#include<iostream> #include<cstring> using namespace std; int calcute(char *input) { int length=strlen(input)+1; if(NULL==input||length<=0) return -1000; char *temp=new char[length]; char *temp2=temp; memset(temp,0,length); int i=0; int result=0; for(;i<length-1;++i) { if(*(input+i)!='*'&&*(input)!='/') { *temp=*(input+i); temp++; } else if(*(input+i)=='*') { int a=*(input+i-1)-'0'; int b=*(input+i+1)-'0'; temp--; *(temp)=a*b+'0'; temp++; i++; } else { int a=*(input+i-1)-'0'; int b=*(input+i+1)-'0'; if(0==b) return -10000; else { temp--; *(temp)=a*b+'0'; temp++; i++; } } } for(int j=0;j<strlen(temp2);++j) { if(*(temp2+j)>='0'&&*(temp2+j)<='9') { result+=*(temp2+j)-'0'; } else if(*(temp2+j)=='+') { result+=*(temp2+j+1)-'0'; j+=1; } else { result-=*(temp2+j+1)-'0'; j+=1; } } return result; } int main() { char *a="3+2*6+9+9"; cout<<calcute(a); system("pause"); return 0; }
原文:http://blog.csdn.net/qq_22335577/article/details/44619057