home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / W / ATIMPY.ZIP / DATER.C next >
C/C++ Source or Header  |  1992-02-19  |  14KB  |  409 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <io.h>
  4. #include <fcntl.h>
  5.  
  6. #define DATAF "matcher\\matcher.dat"
  7. #define QUESTIONS "matcher\\quest.txt"
  8. #define NUMBER_OF_MATCHES 5
  9. #define MINIMUM_COMPATABILITY 01
  10. #define MINIMUM_AGE 13
  11.  
  12. int    usernum,         /* user number for the user */
  13.        age,             /* age of the user */
  14.        screenchars,     /* chars/line user has specified */
  15.        screenlines,     /* lines/screen user has specified */
  16.        sl,              /* sec lev for user (0-255) */
  17.        so,              /* non-zero if user is sysop (sl=255) */
  18.        cs,              /* non-zero if user is co-sysop */
  19.        okansi,          /* non-zero if user can support ANSI */
  20.        incom,           /* non-zero if user is calling remotely */
  21.        comport;         /* com port user is on */
  22. char   name[81],        /* name/alias of user */
  23.        realname[81],    /* real name of user */
  24.        callsign[10],    /* amateur radio callsign of user */
  25.        sex,             /* sex of user, M or F */
  26.        laston[81],      /* date user was last on */
  27.        gfiles[81],      /* directory for text files, ends in \ */
  28.        data[81],        /* directory for non-text files, ends in \ */
  29.        sysoplog[81],    /* full path & filename for sysop log */
  30.        curspeed[81];    /* speed user is on at, "KB" if local */
  31. double gold,            /* gold user has */
  32.        timeallowed;     /* number of seconds before user logged off */
  33.   int     numdaters;
  34.   char    works[21],
  35.           cho;
  36.  
  37.  
  38.     FILE *df,*qf; /* user data file */
  39.  
  40.  
  41.   struct{
  42.         char name[81],
  43.              sex,
  44.              phone[20],
  45.              answ[44],     /* array of answers to 43 questions */
  46.              ans1[242],     /* 3 short answer strings */
  47.              ans2[242],
  48.              ans3[242];
  49.  
  50.         int  age;          /* p is the current person. */
  51.                     }  p,x;          /* x is someone being compared */
  52.                            /* to the current user. */
  53.  
  54.  
  55. int read_in_data(char *fn)
  56.  
  57. {
  58.   char buf[1024];  /* This is where the file will be put for fast access. */
  59.   char *ptr[30];   /* This will hold pointers to parts of the buf array,
  60.                       acting as an index. */
  61.   int i,f,len,i1;  /* i, i1 = loop control variables;
  62.                       len = number of bytes read from file;
  63.                       f = file descriptor number for the filename. */
  64.   float fl;        /* This holds a temporary value of the user's gold. */
  65.                    /* fn = the filename of the CHAIN.TXT file. */
  66.  
  67.   f=open(fn,O_RDONLY | O_BINARY);  /* Open the file for input as a binary file,
  68.                                       i.e. without character translations. */
  69.   if (f<0) {                       /* If the file descriptor (f) is less than
  70.                                       0 (i.e. it is -1) then abort the
  71.                                       function and return error (-1). */
  72.     return(-1);
  73.   }
  74.   i1=1;                            /* Initialize i1. */
  75.   ptr[0]=buf;                      /* Point the first index to the first character
  76.                                       of the buf array. */
  77.   len=read(f,(void *)buf,1024);    /* Read in from the file f into buf up to
  78.                                       1024 characters (until the EOF is reached)
  79.                                       and return the actual number of characters
  80.                                       read in len. */
  81.   close(f);                        /* We're finished with the file so put it
  82.                                       away. */
  83.   for (i=0; i<len; i++)            /* This for loop goes through the buf array
  84.                                       (which now contains the contents of the
  85.                                       chain info file) and assigns indices to
  86.                                       the ptr array of pointers. It causes all
  87.                                       returns to be mapped into null characters
  88.                                       (ASCII 0) for use as strings, and skips
  89.                                       over the following LF character. In this
  90.                                       way, ptr will end up pointing to a bunch
  91.                                       of character strings. */
  92.     if (buf[i]==13) {
  93.       buf[i]=0;
  94.       ptr[i1++]=&buf[i+2];
  95.     }
  96.   while (*ptr[6]==32)              /* Remove leading spaces for the float
  97.                                       values by incrementing the pointer
  98.                                       by one character. */
  99.     ++(ptr[6]);
  100.   while (*ptr[15]==32)
  101.     ++(ptr[15]);
  102.   usernum=atoi(ptr[0]);            /* From here on it is straight translation
  103.                                       from the ptr index array into the global
  104.                                       variables. */
  105.                                    /* Get the user number from index 0. */
  106.   strcpy(name,ptr[1]);             /* Get the name, real name, and callsign. */
  107.   strcpy(realname,ptr[2]);
  108.   strcpy(callsign,ptr[3]);
  109.   age=atoi(ptr[4]);                /* Get the user's age. */
  110.   sex=*ptr[5];                     /* Get the user's sex. */
  111.   sscanf(ptr[6],"%f",&fl);         /* These two lines read in the user's gold
  112.                                       and typecast it into a double float. */
  113.   gold=(double)fl;
  114.   strcpy(laston,ptr[7]);           /* Get his date of last being on. */
  115.   screenchars=atoi(ptr[8]);        /* Get the size of his screen. */
  116.   screenlines=atoi(ptr[9]);
  117.   sl=atoi(ptr[10]);                /* Get his seclev. */
  118.   so=atoi(ptr[11]);                /* Is he a sysop? (nonzero if true) */
  119.   cs=atoi(ptr[12]);                /* How about a cosysop? */
  120.   okansi=atoi(ptr[13]);            /* Does he use ANSI graphics? */
  121.   incom=atoi(ptr[14]);             /* Is he calling remotely (1) or not? */
  122.   sscanf(ptr[15],"%f",&fl);        /* These two lines read in the number of
  123.                                       seconds left to the user and typecast
  124.                                       the value into a double float. */
  125.   timeallowed=(double)fl;
  126.   strcpy(gfiles,ptr[16]);          /* What is the gfiles dir? */
  127.   strcpy(data,ptr[17]);            /* What is the data dir? */
  128.   strcpy(sysoplog,gfiles);         /* These two lines get the full pathname
  129.                                       for the sysoplog file. */
  130.   strcat(sysoplog,ptr[18]);
  131.   strcpy(curspeed,ptr[19]);        /* A string containing the user's
  132.                                       bps/baud rate or KB if local. */
  133.   comport=atoi(ptr[20]);           /* The user's comport if remote. */
  134.   return(0);                       /* Return and give a 'we read it in fine'
  135.                                       message. */
  136.  
  137.  
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. void newperson()
  146. {
  147.  
  148. int qn; /* question number */
  149. char question[81], /* stores the question read in from a file */
  150.      choice;               /* stores user's answer */
  151.  
  152.  
  153. printf("You are a new prospect.\n");
  154. strcpy(p.name,name);
  155. p.sex=sex;
  156. printf("What is your phone number (or hit return to not give it out)?\n");
  157. gets(works);
  158. strcpy(p.phone,works);
  159. p.age=age;
  160. printf("Welcome, %s.\n",p.name);
  161. printf("Your secrets are safe with me.\n\n");
  162. printf("You will be asked some questions (about 40). This program is a sincere\n");
  163. printf("attempt to find someone compatible to you. Therefore, some of the questions\n");
  164. printf("are blunt and to the point. If you do not wish to answer a question, you\n");
  165. printf("may press Q at any prompt. This will abort your entry into the dating system.\n");
  166. printf("Do you wish to proceed? ");
  167. cho=getche();
  168. printf("\n\n");
  169. if (cho=='N' || cho=='n')
  170. printf("\nYou can't go that way!.\n");
  171. /* exit(0);}*/
  172. printf("Ok. I am glad you decided to try it.\n\n");
  173. qf=fopen(QUESTIONS,"r");
  174. if (qf==NULL)
  175.      {printf("There is a serious problem.\n");
  176.    printf("Please inform the sysop that quest.txt is missing.\n");
  177.    exit(0);
  178.      }
  179. printf("\n");
  180. puts("There are 43 yes/no questions to answer. You may quit at any time by ");
  181. puts("pressing Q.  Warning- some people may find these questions offensive");
  182. puts("in nature.  If you are easily offended, I recommend that you quit now.");
  183. printf("\n\n");
  184. for (qn=0; qn<43; qn++)
  185.   {
  186.   fgets(question,80,qf);
  187.   do
  188.      {
  189.      printf("%d) %s",qn+1,question);
  190.      puts("Answer? (Y, N, or Q to QUIT)");
  191.      choice=getche();
  192.      printf("\n");
  193.      if (choice>'Z')
  194.        choice=choice-32;
  195.      if (choice=='Q')
  196.        {puts("Are you sure? All your data will be lost!");
  197.         cho=getche();
  198.         printf("\n");
  199.         if ((cho!='y') && (cho!='Y')) choice='a';
  200.         }
  201.      }
  202.   while ((choice!='Y') && (choice!='N') && (choice!='Q'));
  203.   if (choice=='Q')
  204.     {
  205.     puts("Ok.  This user has been aborted.");
  206.     exit(0);
  207.     }
  208.   p.answ[qn]=choice;
  209.   }
  210. puts("Well, if you made it this far, you don't want to abort.");
  211. puts("Just 3 short questions now...");
  212. fgets(question,80,qf);
  213. puts(question);
  214. gets(p.ans1);
  215. fgets(question,80,qf);
  216. puts(question);
  217. gets(p.ans2);
  218. fgets(question,80,qf);
  219. puts(question);
  220. gets(p.ans3);
  221.  }
  222.  
  223.  
  224.  
  225.  void main(int argc, char *argv[])
  226. {
  227.   int zots=0;  /* zots is a flag to tell if the user is new */
  228.                /* zots==1 means his record has been found, and
  229.                   his record will not be appended at the end. */
  230.  
  231.   argc=argc;
  232.   argv=argv;
  233.  
  234.   if (read_in_data("chain.txt")==-1) { /* Try to read in the parameter file
  235.                                       and if it returns the -1 error message,
  236.                                       quit the program with an error message. */
  237.     printf("\nData file not found\n\n");
  238. /*    abort(); */
  239.   }
  240.   if (okansi) printf("\x1B[2J");
  241.     printf("Welcome to the *COMPUTER*MATCH*MAKER!\n");
  242.     printf("This disservice brought to you by Elihu Feustel\n");
  243.     printf("Sysop of a weird board @ 812-877-4416\n");
  244.     /*if (age<MINIMUM_AGE)
  245.         {
  246.         printf("Sorry, but you are too young to enter *MATCH*MAKER*\n\n");
  247.         exit(0);
  248.         }*/
  249.   df=fopen(DATAF,"r");
  250.   if (df==NULL)
  251.     {
  252.     df=fopen(DATAF,"w");
  253.         printf("Initializing data file.\n\n");
  254.         strcpy(p.name,"nul");
  255.     p.sex='N';
  256.     strcpy(p.phone,"555-1212");
  257.     strcpy(p.answ,"2222222222222222222222222222222222222222222");
  258.     strcpy(p.ans1," "); strcpy(p.ans2," "); strcpy(p.ans3," ");
  259.     fwrite(&p,sizeof(p),1,df);
  260.     }
  261.   else
  262.     {
  263.         printf("Loading data file.\n\n");
  264.     do
  265.       {
  266.       zots=fread(&p,sizeof(p),1,df);
  267.       } while ((zots==1) && (strcmp(name,p.name)!=0));
  268.     }
  269.   fclose(df);
  270.   if (zots==1)
  271.         {
  272.         newperson();
  273.         df=fopen(DATAF,"a");
  274.         fwrite(&p,sizeof(p),1,df);
  275.         fclose(df);
  276.         }
  277.     else printf("Welcome back, %s.\n",name);
  278.   puts("Would you like to see your list of compatible individuals?");
  279.   cho=getche();
  280.   printf("\n\n");
  281.   if ((cho=='y') || (cho=='Y')) showmates();
  282.  
  283.   printf("<ENTER> to continue.\n");
  284.   getch(cho);
  285. }
  286.  
  287. showmates()
  288. {
  289.     int loop,loop1, /* used for WHAT? */
  290.             recread, /* 1 if record is read, 0 if EOF */
  291.             rank,    /* rank of datee in top 10 */
  292.             found=0;
  293.     float             compat;    /* number of questions answered similarly */
  294.   struct match
  295.   {     char name[81];
  296.         float percent;
  297.     } m[NUMBER_OF_MATCHES+1];
  298.     char nmatch[81],
  299.              pick=0,
  300.              question[81];
  301.  
  302.  
  303.     for (loop=0; loop<NUMBER_OF_MATCHES+1; loop++)
  304.          {
  305.          strcpy(m[loop].name,"nul");
  306.          m[loop].percent=0;
  307.          }
  308.  
  309.         df=fopen(DATAF,"r");
  310.     do
  311.       {
  312.             recread=fread(&x,sizeof(x),1,df);
  313.             if (recread==1)
  314.                     {
  315.                     compat=0;
  316.                     rank=NUMBER_OF_MATCHES+2;
  317.                     if (p.sex!=x.sex)
  318.                         {
  319.                         for (loop=0; loop<44; loop++)
  320.                         if (p.answ[loop]==x.answ[loop]) compat+=1;
  321.                         compat*=100.0/43.0;
  322.                         if (p.age>x.age) compat+=2.0*(x.age-p.age+2);
  323.                         else compat+=2.0*(p.age-x.age+2);
  324.                         if (compat>100.0) compat=100.0;
  325.  
  326.                         for (loop=NUMBER_OF_MATCHES; loop>0; loop--)
  327.                             {
  328.                             if ((compat>m[loop].percent) &&
  329.                                 (strcmp(x.name,"nul")!=0))
  330.                                 rank=loop;
  331.                             }
  332.                             if (rank<NUMBER_OF_MATCHES+1)
  333.                                 {
  334.                                 for (loop1=NUMBER_OF_MATCHES; loop1>=rank; loop1--)
  335.                                     {
  336.                                     strcpy(m[loop1+1].name,m[loop1].name);
  337.                                     m[loop1+1].percent=m[loop1].percent;
  338.                                     }
  339.                                 strcpy(m[rank].name,x.name);
  340.                                 m[rank].percent=compat;
  341.                                 }
  342.                         }
  343.                     }
  344.             } while (recread==1);
  345.      fclose(df);
  346.  
  347.      printf("#                Name          Compatability\n");
  348.      found=0;
  349.      for (loop=0; loop<NUMBER_OF_MATCHES+1; loop++)
  350.             {
  351.             if ((strcmp(m[loop].name,"nul")!=0) &&
  352.                     (m[loop].percent>MINIMUM_COMPATABILITY))
  353.                     {
  354.                      printf("%d)    %20s \t%5.1f\n",loop,m[loop].name,m[loop].percent);
  355.                      found=1;
  356.                      }
  357.             }
  358.      printf("\n");
  359.      if (found<1)
  360.          {
  361.          printf("Sorry, but there is noone who is compatible. Try back later\n");
  362.          printf("after more people have come in.\n\n");
  363.          }
  364.      if (found>0)
  365.      {
  366.      puts("What number would you like to examine, or 0 to quit?");
  367.      pick=getche();
  368.      printf("\n\n");
  369.      compat=pick-48;
  370.      if ((compat>0) && (compat<1+NUMBER_OF_MATCHES) && (compat<10))
  371.             {
  372.             strcpy(nmatch,m[compat].name);
  373.             df=fopen(DATAF,"r");
  374.             do
  375.                     {
  376.                     recread=fread(&x,sizeof(x),1,df);
  377.                     if (strcmp(x.name,nmatch)==0)    /* person's record found */
  378.                          {
  379.                          printf("%s was asked several short questions in an interview.\n",
  380.                          x.name);
  381.                          qf=fopen(QUESTIONS,"r");
  382.                          for (compat=0; compat<44; compat++)
  383.                          fgets(question,80,qf);
  384.                          puts(question);
  385.                          puts(x.ans1);
  386.                          printf("\n");
  387.                          fgets(question,80,qf);
  388.                          puts(question);
  389.                          puts(x.ans2);
  390.                          printf("\n");
  391.                          fgets(question,80,qf);
  392.                          puts(question);
  393.                          puts(x.ans3);
  394.                          printf("\n");
  395.                          if (x.sex=='M') printf("His");
  396.                          else if (x.sex=='F') printf("Her");
  397.                          printf(" phone number is: %s.\n\n",x.phone);
  398.                          fclose(qf);
  399.                          }
  400.                     } while ((recread==1) && (strcmp(x.name,nmatch)!=0));
  401.             if ((recread==0) && (pick!='0'))
  402.                 printf("Sorry, but that person was not found.");
  403.             }
  404.         }
  405.         printf("Thank you for using *MATCH*MAKER*\n");
  406.  
  407.  
  408. }
  409.