"Source" https://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=2266
This problem is relatively simple, the main work is the segmentation of the string, how to write a beautiful code point is the focus, readability is very important. The use of function atof (stdlib.h) is more convenient than a bit to spell numbers.
#include <iostream>#include<stdio.h>#include<cstring>#include<stdlib.h>#defineMaxLen 100005using namespacestd;intSubexplen (Char* S,intstart) { intlen=0; if(s[start]!='(') { while(s[start+len]!=' ') {len++; } returnLen; } //The first character is a ' (' intnum=1; Len=1; while(num) {if(s[start+len]=='(') Num++; if(s[start+len]==')') Num--; Len++; } returnLen;}DoubleToDouble (Char* S,intStartintlength) { CharSub[maxlen]; strncpy (Sub,s+start,length); Sub[length]=' /'; returnAtof (sub);}DoubleExpression (Char* S,intStartintlength) { intI=start; intLen; DoubleP,e1,e2; if(s[i]!='(') returnToDouble (S, start, length); I=i+1; Len=Subexplen (S, i); P=Expression (S, I, Len); I=i+len+1; Len=Subexplen (S, i); E1=Expression (S, I, Len); I=i+len+1; Len=Subexplen (S, i); E2=Expression (S, I, Len); returnp* (e1+e2) + (1-P) * (e1-E2);}intMain () {CharS[maxlen]; while(Gets (s) && strcmp (s),"()")!=0) {printf ("%.2lf\n", Expression (S,0, strlen (s))); } return 0;}
UVA 11291 Smeech