Output
Print the number given in the input in the financial format by the rules described in the problem statement.
Sample test(s)
input
2012
output
$2,012.00input
0.000
output
$0.00input
-0.00987654321
output
($0.00)input
-12345678.9
output
($12,345,678.90)题意:输入一个数,先判断是正数还是负数。
1、如果是正数:先输出一个$,判断如果是整数,从个位开始每三个数之前加一个',' ,最前面不加;如果是小数,整数部分处理方式同整数,小数部分保留2为小数,如果小数位数大于2,则把多余的部分舍弃;
2、如果是负数,负号‘-’不输出,用()代替,其余部分同正数处理方式相同;
代码比较长,但是好理解:
#include#include #include using namespace std; int main() { char s[104]; int i,j,k,n,m,len,t; while(gets(s)!=NULL) { len=strlen(s); if(s[0]!='-') { printf("$"); if(strchr(s,'.')==NULL) { k=len%3; if(k==0) { for(i=0;i<=2;i++) printf("%c",s[i]); for(i=3,j=0;i =2) printf("%c%c",s[m+1],s[m+2]); else if(n==1) printf("%c0",s[m+1]); } else { for(i=0;i<=k-1;i++) printf("%c",s[i]); for(i=k,j=0;i =2) printf("%c%c",s[m+1],s[m+2]); else if(n==1) printf("%c0",s[m+1]); } } } else { printf("($"); if(strchr(s,'.')==NULL) { k=(len-1)%3; if(k==0) { for(i=1;i<=3;i++) printf("%c",s[i]); for(i=4,j=0;i =2) printf("%c%c",s[m+1],s[m+2]); else if(n==1) printf("%c0",s[m+1]); } else { for(i=1;i<=k;i++) printf("%c",s[i]); for(i=k+1,j=0;i =2) printf("%c%c",s[m+1],s[m+2]); else if(n==1) printf("%c0",s[m+1]); } } printf(")"); } printf("\n"); } return 0; }