home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e065 / 2.ddi / CONV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  3.1 KB  |  98 lines

  1. /*  10:15 04-Jul-88  (conv.c)  Convert RAW Stock Prices to Scaled Prices */
  2.  
  3. /************************************************************************
  4.  * Copyright(C) 1988 Neural-Ware Inc          *
  5.  * 103 Buckskin Court, Sewickley, PA 15143, United States of America  *
  6.  *                  *
  7.  * All rights reserved.  No part of this program may be reproduced, *
  8.  * stored in a retrieval system, or tramsmitted, in any form or by any  *
  9.  * means, electronic, mechanical, photocopying, recording or otherwise  *
  10.  * without the prior written permission of the copyright owner,   *
  11.  * Neural-Ware, Inc.              *
  12.  ************************************************************************
  13.  */
  14.  
  15. /************************************************************************
  16.  *                  *
  17.  *  Convert RAW stock prices to SCALED stock prices     *
  18.  *                  *
  19.  ************************************************************************
  20.     To use this program:
  21.  
  22.   CONV input.file output.file
  23.  
  24.     It will take the input file, one data item per input line, and
  25.     re-scale them to the range 0.1 to 0.9.  This was used for both
  26.     the stock prediction and noise filtering examples.  In the case of
  27.     noise filtering, only the output files are provided.
  28.  
  29.     If no arguments are given, it assumes "sp500.nnz" as input and
  30.     "sp500.in" for output.
  31.  */
  32.  
  33. #include <stdio.h>
  34. #undef max
  35. #undef min
  36.  
  37. main( ac, av )
  38. int     ac;   /* # of input arguments */
  39. char    **av;   /* array of pointers to the arguments */
  40. {
  41.     FILE   *ifp;    /* input file pointer */
  42.     FILE   *ofp;    /* output file pointer */
  43.     float  flt;   /* work number for sscanf */
  44.     double   max, min;  /* max and min of input */
  45.     double   scale, offset; /* computed scale and offset */
  46.     char  *ifn, *ofn; /* input file name and output file name */
  47.  
  48.     if ( ac > 2 ) {
  49.   ifn = av[1];
  50.   ofn = av[2];
  51.     } else {
  52.   ifn = "sp500.nnz";
  53.   ofn = "sp500.in";
  54.     }
  55.     ifp = fopen( ifn, "r" );    /* raw input prices */
  56.     ofp = fopen( ofn, "w" );    /* scaled output prices */
  57.  
  58.     if ( ifp == (FILE *)0 ) {
  59.   fprintf( stderr, "Could not open input file <%s>\n", ifn );
  60.   exit( 1 );
  61.     }
  62.  
  63.     if ( ofp == (FILE *)0 ) {
  64.   fprintf( stderr, "Could not open output file <%s>\n", ifn );
  65.   exit( 1 );
  66.     }
  67.  
  68.     /* compute the minimum and maximum price in the input file */
  69.  
  70.     max = 0.0;
  71.     min = 9999999.0;
  72.     while( fscanf( ifp, "%f", &flt ) == 1 ) {
  73.   if ( flt < min )  min = flt;
  74.   if ( flt > max )  max = flt;
  75.     }
  76.  
  77.     /* compute scaling so that new prices vary from 0.1 to 0.9 */
  78.  
  79.     scale  = 0.8 / (max - min);
  80.     offset = 0.1 - scale * min;
  81.  
  82.     /* tell the user what scale and offset we are using */
  83.  
  84.     fprintf( stderr, "scale = %.4f, offset = %.4f\n", scale, offset );
  85.  
  86.     /* re-parse the input file and convert prices */
  87.  
  88.     fseek( ifp, 0l, 0 );
  89.     while( fscanf( ifp, "%f", &flt ) == 1 ) {
  90.   fprintf( ofp, "%.3f\n", flt*scale + offset );
  91.     }
  92.  
  93.     /* close the input and output files */
  94.  
  95.     fclose( ifp );
  96.     fclose( ofp );
  97. }
  98.