Subversion Repositories wimsdev

Rev

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

  1. /*
  2. *********************************************************************************
  3. * J.M. Evers 3/2012                                                             *
  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 whatsoever                                                        *
  8. *********************************************************************************
  9. general use:
  10. rounding to 2 decimals (financial math)
  11. tot=!exec moneyprint 0.1,17,123.4,123.99765
  12. tot -> 0.10,17.00,123.40,124.00
  13.  
  14. 2/2012
  15.  modified for general rounding usage; a word 'DECIMALS' can be added to the list of numbers
  16.  tot=!exec moneyprint 2.1,4.123,5  2   // 2 decimals
  17.  tot -> 2.10,4.12,5.00
  18.  tot=!exec moneyprint 2.1,4.123,5  4   // 4 decimals
  19.  tot -> 2.1000,4.1230,5.0000
  20.  tot=!exec moneyprint 2.1,4.123,5
  21.  tot -> 2.10,4.12,5.00 //default value (or old syntax) is 2 decimals  
  22.  
  23. 6/2012.
  24.  modified again to support rounding of scientific numbers, like
  25.  tot=!exec moneyprint 1.23456e+06,1.23456*10^6,0.01234e-23 3
  26.  tot -> 1.235e06,1.235e6,0.012e-23
  27.  
  28.  assumed only powers of 10 [scientific notation]
  29.  
  30. 12/2012
  31. Modified : using only 10^ in powers
  32. (e+8 -> *10^8 ; e-8 -> *10^-8)
  33. Extra syntax error signal (using more than 1 'e')
  34.  
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #define MAX_DIGITS 32
  40. #define MAX_CONV 32
  41.  
  42. int main( int argc , char *argv[]){
  43.     if( argc != 2 && argc != 3){
  44.         fprintf(stdout,"error !\nusage:\n!exec moneyprint $your_wims_item_list $precision_word\nexample:\nmoney=!exec moneyprint 1.2,30.1,.4,-.23123456 2\nThe result is a comma separated list: 1.20,30.10,0.40,-0.23\n using 2 decimals\nNote: no calculations are done.\nNo spaces allowed \n");
  45.         exit(0);
  46.     }
  47.     /* test for illegal characters */
  48.     const char *invalid_characters = "\n\"\'!=ABCDFGHIJKLMNOPQRSTUVWYZabcdfghijklmnopqrstuvwyz@#$%&()[]{};:~><?/\\|";
  49.     /* +-*^XxEe are allowed : 12.34e+05  12.34e-08 1x10^5 1.234*10^123*/
  50.     char *input;
  51.     input = argv[1];
  52.     while (*input){
  53.         if ( strchr(invalid_characters, *input) ){
  54.             fprintf(stdout,"error !\nfound illegal character \"%c\" in argument\n",*input);
  55.             exit(0);
  56.         }
  57.         input++;
  58.     }
  59.     int DECIMALS;
  60.     if(argv[2] != NULL){
  61.         DECIMALS = atoi(argv[2]);
  62.         if(DECIMALS > MAX_DIGITS){
  63.             fprintf(stdout,"error ! maximum amount of decimals is %d \n",MAX_DIGITS);exit(0);
  64.         }
  65.     }
  66.     else
  67.     {
  68.         DECIMALS = 2;
  69.     }
  70.     char *ptr;
  71.     char word[MAX_DIGITS];
  72.     char exponent[MAX_DIGITS];
  73.     char number[MAX_DIGITS];
  74.     int cnt = 1;
  75.     int powE = 0;
  76.     int idx1 = 0;
  77.     int idx2 = 0;
  78.     int length= 0;
  79.     int i = 0;
  80.     int pow10 = 0;
  81.     input = argv[1];
  82.     ptr = strtok(input,",");
  83.     while( ptr != NULL){
  84.         if(  cnt > MAX_CONV ){
  85.             fprintf(stdout,"ERROR too many (> %d)conversion \n",MAX_CONV);
  86.             exit(0);
  87.         }
  88.         // next item in input argv[1]
  89.         strncpy( word, ptr, MAX_DIGITS);
  90.         length = strlen(ptr);
  91.         if(length > MAX_DIGITS-1){
  92.             fprintf(stdout,"ERROR string too large\n");
  93.             exit(0);
  94.         }
  95.         //reset counters
  96.         powE = 0;
  97.         pow10 = 0;
  98.         idx1 = 0;
  99.         idx2 = 0;
  100.         i = 0;
  101.         for( i = 0; i < length ; i++){
  102.             if(powE > 1 ){
  103.                 fprintf(stdout,"ERROR in syntax\n");
  104.                 exit(0);
  105.             }
  106.             if( idx1 + idx2  >  MAX_DIGITS-1){
  107.                 fprintf(stdout,"ERROR string too large\n");
  108.                 exit(0);
  109.             }
  110.             switch( word[i] ){
  111.                 case '+' : break; /* do not use 10^+2 */
  112.                 case 'e' : powE++;break;
  113.                 case 'E' : powE++;break;
  114.                 case 'x' : pow10++;break;
  115.                 case '*' : pow10++;break;
  116.                 case '^' : pow10=5;break;
  117.                 default  :
  118.                 if( pow10 > 0 ){
  119.                     pow10++;
  120.                     if( pow10 > 4 ){ /*  *10^ = 4 chars */
  121.                         exponent[idx2]=word[i];
  122.                         idx2++;
  123.                     }
  124.                 }
  125.                 else
  126.                 {
  127.                     if(powE > 0){
  128.                         exponent[idx2] = word[i];
  129.                         idx2++;
  130.                     }
  131.                     else
  132.                     {
  133.                         number[idx1] = word[i];
  134.                         idx1++;
  135.                     }
  136.                 }
  137.                 break;
  138.             }
  139.         }
  140.         exponent[idx2] = '\0';
  141.         number[idx1] = '\0';
  142.         if( powE == 1 || pow10> 0 ){
  143.             if(cnt > 1){
  144.                 fprintf( stdout , ",%.*f*10^%s" , DECIMALS , atof(number) , exponent );
  145.             }
  146.             else
  147.             {
  148.                 fprintf( stdout , "%.*f*10^%s" , DECIMALS , atof(number) , exponent );
  149.             }
  150.         }
  151.         else
  152.         {
  153.             if(cnt > 1 ){
  154.                 fprintf( stdout , ",%.*f" , DECIMALS , atof(number));  
  155.             }
  156.             else
  157.             {
  158.                 fprintf( stdout , "%.*f" , DECIMALS , atof(number));   
  159.             }
  160.         }
  161.         cnt++;
  162.         ptr = strtok(NULL,",");
  163.     }
  164.     fprintf(stdout,"\n");
  165.     return 0;
  166. }
  167.