home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / EDUCATIO / NTUMIN10.ZIP / SORTER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-09  |  5.6 KB  |  196 lines

  1. /***************************************************************************
  2.  *
  3.  *    Program Name : SORTER.C
  4.  *
  5.  *    This program sorts cubes in the input file before minimization in
  6.  *    the order of 01X. ie. ascending order.
  7.  *
  8.  * --------------------------------------------------------------------------
  9.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  10.  *    University.
  11.  *
  12.  *    You are free to use, copy and distribute this software and its
  13.  *    documentation providing that:
  14.  *
  15.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  16.  *
  17.  *        IT IS NOT MODIFIED IN ANY WAY.
  18.  *
  19.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  20.  *
  21.  *    This program is provided "AS IS" without any warranty, expressed or
  22.  *    implied, including but not limited to fitness for any particular
  23.  *    purpose.
  24.  *
  25.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  26.  *    appreciated. Please send to:
  27.  *
  28.  *        Boon-Tiong Tan or Othman Bin Ahmad
  29.  *        School of EEE
  30.  *        Nanyang Technological University
  31.  *        Nanyang Avenue
  32.  *        Singapore 2263
  33.  *        Republic of Singapore
  34.  *
  35.  ***************************************************************************
  36.  *
  37.  *    n    == no. of variables
  38.  *    cu, cu1 == no. of cubes
  39.  *    i, k    == counters
  40.  *
  41.  ***************************************************************************/
  42.  
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46.  
  47.  
  48. main()
  49.  
  50. {
  51.    unsigned char     *infile, *outfile, *np, *cp, *cube, *temp;
  52.    unsigned char     n, k, c;
  53.          int     test;
  54.    unsigned long     cu, cu1, i;
  55.    FILE              *in, *out;             /* file pointer */
  56.  
  57.    printf("This program sorts cubes in order of '01X' (ie. ascending order) before\n");
  58.    printf("subjecting them to minimization. \n\n");
  59.    printf("Press any key to Continue or <ESC> to QUIT\n\n");
  60.    c = getch();
  61.    if (c==27)
  62.       exit(0);
  63.  
  64.    infile = (unsigned char *)malloc (21);      /* space for input filename */
  65.    if (infile==0)
  66.       {
  67.      printf("Out of memory -- SORTER, *infile\n");
  68.      printf("Program Terminated - 1\n");
  69.      exit(0);
  70.       }
  71.  
  72.    outfile = (unsigned char *)malloc (21);    /* space for output filename */
  73.    if (outfile==0)
  74.       {
  75.      printf("Out of memory -- SORTER, *outfile\n");
  76.      printf("Program Terminated - 2\n");
  77.      exit(0);
  78.       }
  79.  
  80.    printf("Enter input filename -> ");
  81.    gets(infile);                                 /* get input filename */
  82.  
  83.    printf("Enter output filename -> ");
  84.    gets(outfile);                               /* get output filename */
  85.  
  86.    printf("Please hang on, sorting in process ... \n\n");
  87.  
  88.    if ((in = fopen(infile, "r+")) == NULL)          /* open input file */
  89.       {
  90.      printf("Error opening file, %s\n", infile);
  91.      printf("Program terminated.\n");
  92.      exit(0);
  93.       }
  94.  
  95.    if ((out = fopen(outfile, "w")) == NULL)        /* open output file */
  96.       {
  97.      printf("Error opening file, %s\n", outfile);
  98.      printf("Program terminated.\n");
  99.      exit(0);
  100.       }
  101.  
  102.    np = (unsigned char *)malloc(4);            /* space for no. of var */
  103.    if (np==0)
  104.       {
  105.      printf("Out of memory -- SORTER, *np\n");
  106.      printf("Program Terminated - 3\n");
  107.      exit(0);
  108.       }
  109.  
  110.    fgets(np, 4, in);                           /* get no. of variables */
  111.    n = atoi(np);                                   /* no. of variables */
  112.    free(np);                                           /* free pointer */
  113.    fprintf(out, "%d\n", n);                        /* print to outfile */
  114.  
  115.    cp = (unsigned char *)malloc(5);          /* space for no. of cubes */
  116.    if (cp==0)
  117.       {
  118.      printf("Out of memory -- SORTER, *cp\n");
  119.      printf("Program Terminated - 4\n");
  120.      exit(0);
  121.       }
  122.  
  123.    temp = (unsigned char *)malloc(n+2);           /* temporary storage */
  124.    if (temp==0)
  125.       {
  126.      printf("Out of memory -- SORTER, *temp\n");
  127.      printf("Program Terminated - 5\n");
  128.      exit(0);
  129.       }
  130.  
  131.    for (k=0; k<2; k++)
  132.       {
  133.      fgets(cp, 5, in);                         /* get no. of cubes */
  134.      cu = atoi(cp);                            /* ascii to integer */
  135.      fprintf(out, "%d\n", cu);                 /* print to outfile */
  136.  
  137.      if (cu==0)                                /* zero cube */
  138.         break;
  139.  
  140.      cube = (unsigned char *)malloc(n*cu);     /* space for all cubes */
  141.      if (cube==0)
  142.         {
  143.            printf("Out of memory -- SORTER, *cube\n");
  144.            printf("Program Terminated - 6\n");
  145.            exit(0);
  146.         }
  147.  
  148.      for (i=0; i<cu; i++)                /* read and store all cubes */
  149.         {
  150.            fgets(temp, n+2, in);
  151.            memcpy((cube+n*i), temp, n);
  152.         }
  153.  
  154.      cu1 = cu;
  155.  
  156.      while (cu-- > 1)                    /* bubble sort algorithm */
  157.         {
  158.            for (i=0; i<cu; i++)
  159.           {
  160.              test = memcmp((cube+n*i), (cube+n*(i+1)), n); /* compare adjacent minterms */
  161.  
  162.              if (test > 0)            /* 1st term > 2nd term */
  163.             {
  164.                memcpy(temp, (cube+n*i), n);          /* swap */
  165.                memcpy((cube+n*i), (cube+n*(i+1)), n);
  166.                memcpy((cube+n*(i+1)), temp, n);
  167.             }
  168.  
  169.              else if (test==0)          /* duplicate cubes */
  170.             {
  171.                memcpy((cube+n*i), (cube+n*(i+1)), n*(cu1-i)); /* remove duplicate */
  172.                i--;
  173.             }
  174.           }
  175.         }
  176.  
  177.      for (i=0; i<cu1; i++)        /* print sorted cubes to outfile */
  178.         {
  179.            memcpy(temp, (cube+n*i),  n);
  180.            *(temp+n) = '\0';
  181.            fprintf(out, "%s\n", temp);
  182.         }
  183.      free(cube);                  /* free pointer */
  184.       }
  185.  
  186.    fclose(in);                        /* close files */
  187.    fclose(out);
  188.    free(cp);                          /* free pointers */
  189.    free(temp);
  190.    free(in);
  191.    free(out);
  192.  
  193.    printf("Sorting completed.\n");
  194. }
  195.  
  196.