大概800行代码,供各位参考。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "flowgen.h"
BOOLEAN Verbose = FALSE; /* flag for optional verbose */
char FlowChar[] = {' ', '|', '/', '-', '\\', '|', '/', '-', '\\'};
/*char FlowChar[] = {' ', '1', '2', '3', '4', '5', '6', '7', '8'};*/
int main(int argc, char **argv)
{
BOOLEAN HasFraction = FALSE; /* flag for whether fraction is provided */
BOOLEAN HasMask = FALSE; /* flag for optional mask */
BOOLEAN HasUserDefOutsideValue = FALSE;
/* flag whether a user defined value is given
to indicate values outside the basin */
BOOLEAN **MaskMatrix; /* matrix with mask */
char AccFilename[BUFSIZE+1]; /* filename for accumulation file */
char MaskFilename[BUFSIZE+1]; /* filename for mask file */
char OutFilename[BUFSIZE+1]; /* filename for output file */
char *EndPtr = NULL;
FILE *AccFile = NULL; /* accumulation file */
FILE *MaskFile = NULL; /* optional mask file */
FILE *OutFile = NULL; /* output */
float Fraction; /* fraction of border through which flow
exits perpendicular */
int i; /* counter */
int j; /* counter */
int m; /* counter */
int n; /* counter */
int AccCols; /* number of columns in accumulation file */
int AccRows; /* number of rows in accumulation file */
int CellCols; /* number of columns per cell */
int CellRows; /* number of rows per cell */
int Col;
int CornerCols;
int CornerRows;
int ExtraCols; /* superfluous columns in accumulation file */
int ExtraRows; /* superfluous rows in accumulation file */
int MiddleCols;
int MiddleRows;
int OutCols; /* number of columns in output file */
int OutRows; /* number of rows in output file */
int Row;
int *FlowDirection; /* array with flow directions */
int **FlowMatrix; /* matrix with flow directions */
size_t NRead; /* number of records read */
long *Border; /* cells on the border */
long **AccMatrix; /* matrix with accumulation totals */
long **CellMatrix; /* matrix with accumulation totals for a single
cell */
long **MaxAccMatrix; /* matrix with maximum accumulation total */
long OutletAccumulation; /* largest accumulation value in
AccMatrix */
long UserDefOutsideValue; /* User defined value indicating which values
are outside the basin */
CORNER_INFO Corner[4];
CORNER_NODE **CornerMatrix;
/****************************************************************************/
/* read the command-line options */
/****************************************************************************/
/* Echo the function call to the screen */
printf("\n\n");
for (i = 0; i < argc; i++)
printf("%s ", argv[i]);
printf("\n\n");
/* Get command-line options */
if (argc < 7) {
fprintf(stderr, "Usage: %s %s\n\n", argv[0], Usage);
exit(EXIT_FAILURE);
}
/* Read information about accumulation file */
strcpy(AccFilename, *++argv);
--argc;
AccRows = (int) strtol(*++argv, &EndPtr, 0);
--argc;
if ((EndPtr != NULL && EndPtr[0] != '\0') || AccRows < 0) {
fprintf(stderr, "%s: Not a valid number of rows\n\n", *argv);
exit(EXIT_FAILURE);
}
AccCols = (int) strtol(*++argv, &EndPtr, 0);
--argc;
if ((EndPtr != NULL && EndPtr[0] != '\0') || AccCols < 0) {
fprintf(stderr, "%s: Not a valid number of columns\n\n", *argv);
exit(EXIT_FAILURE);
}
/* Read information about output file */
strcpy(OutFilename, *++argv);
--argc;
CellRows = (int) strtol(*++argv, &EndPtr, 0);
--argc;
if ((EndPtr != NULL && EndPtr[0] != '\0') || CellRows < 0) {
fprintf(stderr, "%s: Not a valid number of rows\n\n", *argv);
exit(EXIT_FAILURE);
}
if (CellRows < 2) {
fprintf(stderr,
"%s: Number of rows per cell must be at least 2\n",
*argv);
exit(EXIT_FAILURE);
}
CellCols = (int) strtol(*++argv, &EndPtr, 0);
--argc;
if ((EndPtr != NULL && EndPtr[0] != '\0') || CellCols < 0) {
fprintf(stderr, "%s: Not a valid number of columns\n\n", *argv);
exit(EXIT_FAILURE);
}
if (CellCols < 2) {
fprintf(stderr,
"%s: Number of columns per cell must be at least 2\n",
*argv);
exit(EXIT_FAILURE);
}
OutRows = AccRows/CellRows;
ExtraRows = AccRows % CellRows;
if (ExtraRows != 0) {
fprintf(stderr,
"Warning: The number of output rows is not an integer\n");
fprintf(stderr,
"multiple of the number of rows in the accumulation file.\n");
fprintf(stderr,
"The last %d rows in the accumulation file will not be \n",
ExtraRows);
fprintf(stderr,
"taken into account\n\n");
AccRows -= ExtraRows;
}
OutCols = AccCols/CellCols;
ExtraCols = AccCols % CellCols;
if (ExtraCols != 0) {
fprintf(stderr,
"Warning: The number of output columns is not an integer\n");
fprintf(stderr,
"multiple of the number of columns in the accumulation file.\n");
fprintf(stderr,
"The last %d columns in the accumulation file will not be \n",
ExtraCols);
fprintf(stderr,
"taken into account\n\n");
AccCols -= ExtraCols;
}
/*