#include "stdio.h"
int main(int argc,char *argv[]){
int i,k,t,m,p;
char s[50];
printf("Please enter an arithmetic expression(only + & -)...\n");
scanf("%49s",s);
for(p=m=t=i=0;s[i];i++){
sscanf(s+i,"%d%n",&k,&t);
p ? m-=k : m+=k;
if(s[i+=t]=='+')
p=0;
else if(s[i]=='-')
p=1;
else if(s[i]=='\0')
i--;
}
printf("\n%s = %d\n",s,m);
return 0;
}
运行样例: