TIP: Click on subject to list as thread! ANSI
echo: aust_biz
to: All
from: Paul Edwards
date: 1995-05-20 13:34:28
subject: money.c

/*********************************************************************/
/*                                                                   */
/*  This Program Written by Paul Edwards.                            */
/*  Released to the public domain                                    */
/*                                                                   */
/*********************************************************************/
/*********************************************************************/
/*                                                                   */
/*  Money - work out how to get rich quick                           */
/*                                                                   */
/*********************************************************************/

#include 
#include 

#define OPTINT 1
#define OPTSTR 2
#define OPTBOOL 3
#define OPTLONG 4
#define OPTFLOAT 5
typedef struct {char *sw;
               int opttyp;
               void *var;} opt_t;
int getopts(int argc, char **argv, opt_t opttable[]);

main(int argc, char **argv)
{
  static float netweek,intborr,intsave,houseinc,rent,borrow;
  static float totsave,totowe,houseval,interest,incnet,incrent;
  static float totalworth, difftot;
  static int   i,j,years,defs;

  opt_t opttable[]=
  {
    { "n", OPTFLOAT, &netweek },
    { "in", OPTFLOAT, &incnet },
    { "ib", OPTFLOAT, &intborr },
    { "is", OPTFLOAT, &intsave },
    { "hv", OPTFLOAT, &houseinc },
    { "r", OPTFLOAT, &rent },
    { "ir", OPTFLOAT, &incrent },
    { "b", OPTFLOAT, &borrow },
    { "y", OPTINT, &years },
    { "d", OPTBOOL, &defs },
    { NULL, 0, NULL }
  };

  netweek=500.0;   /* how much you can save a week after paying rent */
  incnet=5.0;       /* how much net pay increases each year (%) */
  intborr=15.0;    /* how much you pay back on your borrowings */
  intsave=11.0;    /* how much interest you get after tax on savings */
  houseinc=10.0;   /*how much the value of houses goes up */
  rent=155.0;       /* how much rent you pay a week */
  incrent=5.0;     /* how much rent increases each year */
  borrow=100000.0;   /* how much you want to borrow */
  years=10;         /* how many years you want to display */
  defs=0;         /* take all defaults? no. */

  if (getopts(argc,argv,opttable) == 1)
  {
    printf("money - get rich quick!!!\n\n");
    printf("n = netweek - n500\n");
    printf("in = increase netweek each year -in5\n");
    printf("ib = interest on borrrowings -ib15\n");
    printf("is = interest on savings -is11\n");
    printf("hv = house value increment -hv10\n");
    printf("r = rent you pay in 1 week -r155\n");
    printf("ir = yearly increase in rent -ir5\n");
    printf("b = how much you want to borrow -b100000\n");
    printf("y = how many years you want to calculate -y10\n");
    printf("d = take all defaults?\n");
    return (EXIT_SUCCESS);
  }

  totsave=0;
  totowe=borrow;
  totalworth=0;
  difftot=0;
  houseval=borrow;
  printf("year     save     borrow      house"
      "   totalworth   difftot\n");
  printf("%2d %10.0f %10.0f %10.0f %10.0f %10.0f\n",
      0,totsave,totowe,houseval,totalworth,difftot);
  for (i=1;i<=years;i=i+1)
  {
    for (j=1;j<=12;j=j+1)
    {
      totsave=totsave+totsave*(intsave/100)/12+4*netweek;
      if (totowe > 0) interest=totowe*(intborr/100)/12;
      else interest=totowe*(intsave/100)/12;
      totowe=totowe+interest-(netweek+rent)*4;
    }
    houseval=houseval*(1+houseinc/100);
    totalworth = houseval - totowe;
    difftot = totalworth - totsave;
    rent=rent*(1+incrent/100);
    netweek=netweek*(1+incnet/100);
    printf("%2d %10.0f %10.0f %10.0f %10.0f %10.0f\n",
        i,totsave, totowe, houseval, totalworth, difftot);
  }
  return (EXIT_SUCCESS);
}

/*********************************************************************/
/*                                                                   */
/*  This Program Written by Paul Edwards.                            */
/*                                                                   */
/*********************************************************************/
/*********************************************************************/
/*                                                                   */
/*  getopts - scan the command line for switches.                    */
/*                                                                   */
/*  This program takes the following parameters:                     */
/*                                                                   */
/*  1) argc (which was given to main)                                */
/*  2) argv (which was given to main)                                */
/*  3) Array of options                                              */
/*                                                                   */
/*  Returns the number of the argument that is next to be processed  */
/*  that wasn't recognised as an option.                             */
/*  Example of use:                                                  */
/*                                                                   */
/*  #include                                              */
/*  int baud = 2400;                                                 */
/*  char fon[13] = "telix.fon";                                      */
/*  opt_t opttable[] =                                               */
/*  {                                                                */
/*    { "b", OPTINT, &baud },                                        */
/*    { "f", OPTSTR, fon },                                          */
/*    { NULL, 0, NULL }                                              */
/*  }                                                                */
/*  optup = getopts(argc,argv,opttable);                             */
/*                                                                   */
/*  The OPTINT means that an integer is being supplied.  OPTSTR      */
/*  means a string (with no check for overflow).  Also there is      */
/*  OPTBOOL which means it is a switch that is being passed, and an  */
/*  OPTLONG to specify a long.  Also OPTFLOAT for float.             */
/*                                                                   */
/*  This program was inspired by a description of a getargs function */
/*  written by Dr Dobbs Small-C Handbook.  Naturally I didn't get    */
/*  to see the code, otherwise I wouldn't be writing this!           */
/*                                                                   */
/*  This program is dedicated to the public domain.  It would be     */
/*  nice but not necessary if you gave me credit for it.  I would    */
/*  like to thank the members of the International C Conference      */
/*  (in Fidonet) for the help they gave me in writing this.          */
/*                                                                   */
/*  Written 16-Feb-1990.                                             */
/*                                                                   */
/*********************************************************************/

#define DOFLOAT

#include 
#include 
#include 
#include 

int getopts(int argc, char **argv, opt_t opttable[])
{
  int i,j;
  argv++;
  argc--;
  for (i=1;i<=argc;i++)
  {
    if ((*(*argv) != '-') && (*(*argv) != '/')) return (i);
    for (j=0;opttable[j].sw != NULL;j++)
    {
      if (strncmp(*argv+1,opttable[j].sw,
          strlen(opttable[j].sw)) == 0)
      {
        switch ((int)opttable[j].opttyp)
        {
          case OPTINT :
            *((int *)opttable[j].var) =
                (int)strtol(*argv+1+strlen(opttable[j].sw),NULL,10);
            if (errno == ERANGE) return (i);
            break;
          case OPTSTR :
            strcpy((char *)opttable[j].var,
                *argv+1+strlen((char *)opttable[j].sw));
            break;
          case OPTBOOL :
            *((int *)opttable[j].var) = 1;
            break;
          case OPTLONG :
            *((long *)opttable[j].var) =
                strtol(*argv+1+strlen(opttable[j].sw),NULL,10);
            if (errno == ERANGE) return (i);
            break;
#ifdef DOFLOAT
          case OPTFLOAT :
            *((float *)opttable[j].var) =
                (float)strtod(*argv+1+strlen(opttable[j].sw),NULL);
            break;
#endif
        }
      }
    }
    argv++;
  }
  return (i);
}
@EOT:

---
* Origin: Kludging up the works (3:711/934.9)

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.