|
|
back to boardI've check all the data here..but i still get WA#12.. #include <iostream> #include <cstring> #include <string> #define inf 214748364 using namespace std; struct tmono { int n[9000]; int w,nega,xs; }; char s[1000]; int xs,len,con,e; tmono num; string res; inline bool isnum(char ch) { return '0'<=ch&&ch<='9'; } inline bool getnum(int st,int en,tmono &e) { memset(e.n,0,sizeof(e.n)); e.w=0;e.xs=0;e.nega=0; if(s[st]=='-') e.nega=1,++st; else if(s[st]=='+') e.nega=0,++st; /* while(s[st]=='+'||s[st]=='-') { if(s[st]=='-') e.nega=!e.nega; ++st; } */ for(int i=en;i>=st;i--) if(s[i]=='.') { e.xs=en-i; for(int j=i;j<en;j++) s[j]=s[j+1]; --en; break; } for(int i=st;i<=en;i++) if(!isnum(s[i])) return 0; e.w=0; for(int i=en;i>=st;i--) e.n[++e.w]=s[i]-'0'; return 1; } inline bool work() { res=""; cin>>xs; int tempxs=xs; scanf("\n"); if(s[0]=='.') { for(int i=len-1;i>=0;i--) s[i+1]=s[i]; s[0]='0'; len++; } if(!isnum(s[0])&&s[0]!='-'&&s[0]!='+') return 0; con=0; for(int i=0;i<len;i++) { if(s[i]=='e'||s[i]=='E') { if(con!=0) return 0; con=i; } if(s[i]!='e'&&s[i]!='E'&&s[i]!='-'&&s[i]!='+' &&s[i]!='.'&&!isnum(s[i])) return 0; if(s[i]=='.'&&!isnum(s[i+1])) return 0; } e=0; bool enega=0; if(con!=0) { e=-inf; int j=con; con++; if(s[con]=='+'||s[con]=='-') { if(s[con]=='-') enega=!enega; con++; } /* while(s[con]=='-'||s[con]=='+') { if(s[con]=='-') enega=!enega; ++con; }*/ for(int i=con;i<len;i++) { if(!isnum(s[i])) return 0; if(e==-inf) e=0; e=e*10+s[i]-'0'; if(e>500) break; } if(e==-inf) return 0; if(enega) e*=-1; len=j; } --len; if(!getnum(0,len,num)) return 0; num.xs-=e; while(num.n[num.w]==0&&num.w>0) --num.w; bool isxs=0; if(num.xs<=0) { for(int i=num.w;i>=1;i--) res+=num.n[i]+'0'; while(num.xs<0) { res+="0"; num.xs++; } }else { int j=max(num.w,num.xs); if(num.xs>num.w) { num.w=num.xs; num.xs=0; res+="0."; isxs=1; } for(int i=j;i>=1;i--) { if(i==num.xs) isxs=1; if(isxs&&xs==0) break; if(i==num.xs) res+="."; if(isxs)--xs; res+=num.n[i]+'0'; } } while(xs>0) { if(!isxs) res+=".",isxs=1; res+="0",--xs; } while(res[0]=='0') res.erase(0,1); if(res.length()==0) res.insert(0,"0"); if(res[0]=='.') res.insert(0,"0"); for(int i=0;i<res.length();i++) if(res[i]=='.') if(res.length()-i<tempxs) { tempxs-=res.length()-i; while(tempxs--) res.insert(res.length()-1,"0"); }else res.erase(i+tempxs,res.length()-i-tempxs-1); if(res[res.length()-1]=='.') res.erase(res.length()-1,1);
if(num.nega) for(int i=0;i<res.length();i++) if('1'<=res[i]&&res[i]<='9') { res.insert(0,"-"); break; } for(int i=1;i<res.length();i++) if(res[i]=='.'&&!isnum(res[i-1])) { res.insert(i,"0"); break; } if(res[res.length()-1]=='.') res.erase(res.length()-1,1);
res+="\n"; return 1; } inline void reads() { memset(s,0,sizeof(s)); gets(s); len=strlen(s); while(len>=1&&isspace(s[len-1])) s[len]=0,--len; while(isspace(s[0])) { for(int j=0;j<len-1;j++) s[j]=s[j+1]; s[len]=0;len--; } } int main() { #ifndef ONLINE_JUDGE freopen("input.in","r",stdin); freopen("output.out","w",stdout); #endif reads(); while(!(strlen(s)==1&&s[0]=='#')) { if(!work()) printf("Not a floating point number\n"); else cout<<res; reads(); } return 0; } Check that: S12.0 2 # where S is a space (" "). My AC program says that its not a correct number because of leading space. "For each pair of lines your program should write to an output a message: "Not a floating point number", if the string S is not a correct <real number>..." <real number> does not contain spaces. =) Good luck! Many thanks!!!really a helpful suggest! now I got AC. thanks again! Edited by author 23.03.2010 05:23 |
|
|