Subversion Repositories wimsdev

Rev

Rev 3519 | Rev 4672 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2. *********************************************************************************
  3. * J.M. Evers 10/2010                                                            *
  4. * This is all amateur scriblings... So no copyrights.                           *
  5. * This source code file, and compiled objects derived from it,                  *
  6. * can be used and distributed without restriction, including for commercial use *
  7. * No warrenty whatoever                                                         *
  8. *********************************************************************************
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <string.h>
  14.  
  15. int main( int argc , char *argv[]){
  16.     /* test for correct argument */
  17.     if( argc != 2){
  18.         fprintf(stdout,"error !\nusage:\n!exec moneyprint $your_wims_item_list\nexample:\nmoney=!exec moneyprint 1.2,30.1,.4,-.23123456\nThe result is a comma separated list: 1.20,30.10,0.40,-0.23\nNote: no calculations are done.\nNote: all numbers will be rounded to 2 decimals.\n");
  19.         exit(0);
  20.     }
  21.     /* test for illegal characters */
  22.     const char *invalid_characters = "\n\"\'!+=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%^*&()[]{}:;~><?/\\|";
  23.     char *inp;
  24.     inp = argv[1];
  25.     while (*inp){
  26.         if ( strchr(invalid_characters, *inp) ){
  27.             fprintf(stdout,"error !\nfound illegal character \"%c\" in argument\n",*inp);
  28.             return(0);
  29.         }
  30.         inp++;
  31.     }
  32.    
  33.     int cnt;
  34.     char *ptr;
  35.     char *sign;
  36.     float n,t2;
  37.     int t1;
  38.     int t3;
  39.     cnt = 0;
  40.     inp = argv[1];
  41.     ptr = (char *) strtok(inp,",");
  42.     while (ptr != NULL){
  43.         sign="";
  44.         n = atof(ptr);
  45.         if(n > 100000000){fprintf(stdout,"error!\nNumber \"%f\" is too large for realistic money print...\n",n);return(0);}
  46.         n = (round(100*n))/100;
  47.         if(n < 0 ){sign = "-"; n = -1*n;}
  48.         t1 = floor(n);
  49.         t2 = 1000*(n - t1);
  50.         t2=(round(t2)/1000);
  51.         t3 = 100*t2;
  52.         if(cnt == 0){
  53.             if(t3<10){fprintf(stdout,"%s%d.0%d",sign,t1,t3);}
  54.             else{fprintf(stdout,"%s%d.%d",sign,t1,t3);}
  55.         }
  56.         else
  57.         {
  58.             if(t3<10){fprintf(stdout,",%s%d.0%d",sign,t1,t3);}
  59.             else{fprintf(stdout,",%s%d.%d",sign,t1,t3);}
  60.         }
  61.         cnt = 1;
  62.         ptr = (char *)  strtok(NULL, ",");
  63.     }
  64.     fprintf(stdout,"\n");
  65.     return (0);
  66. }
  67.  
  68.