⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mel.c

📁 C语言多种求最优的程序源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
*/
int meli_num_params()
{
    return meli_datum.num_param;
}

/*
---------------------------------------------------------------------------
module 4.0

extract information associated with the param_num'th parameter that has
been input.
---------------------------------------------------------------------------
*/
int meli_param(
int param_num,		      /* which parameter are we interested in? */
char *param,		      /* data associated with this param: */
union meli_param_data *data,
char *units,
int *array_len,
int *unknown_flag)
{

	/* is param_num within range of number of params input? */
	if (param_num >= 0 && param_num < meli_datum.num_param) {
	    /* yes it is! */
	    strcpy(param, meli_descrip[meli_datum.descrip_type].
		param[meli_datum.param[param_num].name_index].name);
	    *data = meli_datum.param[param_num].data;
	    strcpy(units, meli_datum.param[param_num].units);
	    *array_len = meli_datum.param[param_num].array_len;
	    *unknown_flag = meli_datum.param[param_num].unknown_flag;
	    return 0;	/* no errors (match found) */
	} else
	    /* index out of bounds, return an error condition: */
	    return 1;
}

/*
---------------------------------------------------------------------------
module 5.0

extract a particular parameter's value that has been input.
see mel.doc for more information on using this procedure.

algorithm synopsis: see if param string was one of the parameters input with
current descriptor.  if it is, then return with all the data about this
parameter that was input.
---------------------------------------------------------------------------
*/
int meli_data(
char *param,		      /* which parameter are we interested in? */
union meli_param_data *data,  /* return with data about this parameter: */
char *units,
int *array_len,
int *unknown_flag)
{
    int i;    /* loop counter */

    /* compare param with each parameter that was input: */
    for (i = 0; i < meli_datum.num_param; i++)

	/* does it match? (if not, continue looking.) */
	if (name_match(param, meli_descrip[meli_datum.descrip_type].
	    param[meli_datum.param[i].name_index].name, meli_descrip[
	    meli_datum.descrip_type].param[meli_datum.param[i].name_index].
	    min_name_len)) {

	    /* yes it does! */
	    *data = meli_datum.param[i].data;
	    strcpy(units, meli_datum.param[i].units);
	    *array_len = meli_datum.param[i].array_len;
	    *unknown_flag = meli_datum.param[i].unknown_flag;
	    return 0;	/* no errors (match found) */
	};

    /* no match was ever found, return an error condition: */
    return 1;
}

/*
---------------------------------------------------------------------------
module 6.0

initialize melo_datum for a descriptor of type *descrip_type.
---------------------------------------------------------------------------
*/
int melo_init(
char *descrip_type)
{
    int i;    /* loop counter */

    /* look for descriptor of given type: */
    for (i = 0; i < MELO_NUM_DESCRIP_NAMES; i++)

	/* does it match with any item in list of descriptor names? */
	if (!strcmp(descrip_type, melo_descrip[i].name)) {

	    /* yes, it does! */
	    melo_datum.descrip_type = i;
	    melo_datum.num_param = 0;
	    return 0;	 /* no errors */
	};

    /* no match was ever found, return an error condition: */
    return 1;
}

/*
---------------------------------------------------------------------------
module 7.0

for parameter *param, put data in rest of arguement list into melo_datum.
---------------------------------------------------------------------------
*/
int melo_data(
char *param,		      /* which parameter are we interested in? */
union melo_param_data *data,  /* data about this parameter: */
char units[],
int array_len,
int unknown_flag)
{
    int i;    /* loop counter */

    /* compare param with each parameter type: */
    for (i = 0; i < MELO_MAX_PARAMS; i++)

	/* does it match? (if not, continue looking.) */
	if (name_match(param, melo_descrip[melo_datum.descrip_type].
	    param[i].name, melo_descrip[melo_datum.descrip_type].
	    param[i].min_name_len)) {

	    /* yes it does! put it in the proper place: */
	    melo_datum.param[melo_datum.num_param].name_index = i;
	    melo_datum.param[melo_datum.num_param].data = *data;
	    strcpy(melo_datum.param[melo_datum.num_param].units, units);
	    melo_datum.param[melo_datum.num_param].array_len = array_len;
	    melo_datum.param[melo_datum.num_param].unknown_flag = unknown_flag;
	    melo_datum.num_param++;
	    return 0;	/* no errors (match found) */
	};

    /* no match was ever found, return an error condition: */
    return 1;
}

/*
---------------------------------------------------------------------------
module 8.0

write MEL data to the output file.
---------------------------------------------------------------------------
*/
int melo_file(
FILE *output_file_handle,
int verbose_flag)
{
    mel_err.type = mel_no_err;	/* assume no error will occur */

    melo(verbose_flag);  /* translate data structure to string */

    /* now output the string to file: */

    fprintf(output_file_handle, "%s", descrip_str);
    if (ferror(output_file_handle)) {
	mel_err.type = mel_write_err;
	strcpy(mel_err.msg, "MEL output file");
	return mel_write_err;
    };

    return 0;

}

/*
---------------------------------------------------------------------------
module 8.1

write MEL data to the descriptor string. that is, using the output dictionary,
translate data from the melo_datum data structure into a string of data. see
the file mel.doc for more information.
---------------------------------------------------------------------------
*/
void melo(
int verbose_flag)
{
    int i, j;   /* loop counters */
    enum mel_data_types type;   /* temp storage */
    char temp_str[MELO_MAX_DESCRIP_STR_LEN+1];	/* temp storage */

    mel_err.type = mel_no_err;	/* assume no error will occur */

    /* will the "output" string be verbose (human readable) or terse 
       (compressed for machine efficiency/readability)? */

    if (verbose_flag) {
	/* first write descriptor type: */
	strcpy(descrip_str, melo_descrip[melo_datum.descrip_type].name);

	/* next, write its parameters and parameter values: */
	for (i = 0; i < melo_datum.num_param; i++) {

	    /* which parameter is it? */
	    sprintf(temp_str, ",\n    %s =",
		melo_descrip[melo_datum.descrip_type].
		param[melo_datum.param[i].name_index].name);
	    strcat(descrip_str, temp_str);

	    /* is its value known? (if not, write "unknown" and
	       proceed to next parameter.) */
	    if (melo_datum.param[i].unknown_flag) {
		strcat(descrip_str, " unknown");
		continue;
	    };

	    /* what type of value is it (int, array, etc.)? */

	    type = melo_descrip[melo_datum.descrip_type].
		param[melo_datum.param[i].name_index].type;

	    switch (type) {

		case (mel_bool):

		    if (melo_datum.param[i].data.integer)
			strcat(descrip_str, " true");
		    else
			strcat(descrip_str, " false");
		    break;

		case (mel_int):

		    sprintf(temp_str, " %d", melo_datum.param[i].data.integer);
		    strcat(descrip_str, temp_str);
		    write_units_if_any(i);
		    break;

		case (mel_real):

		    sprintf(temp_str, " %lg", melo_datum.param[i].data.real);
		    strcat(descrip_str, temp_str);
		    write_units_if_any(i);
		    break;

		case (mel_str):

		    sprintf(temp_str, " '%s'", melo_datum.param[i].data.string);
		    strcat(descrip_str, temp_str);
		    write_units_if_any(i);
		    break;

		case(mel_int_array):

		    strcat(descrip_str, "{");

		    for (j = 0; j < melo_datum.param[i].array_len; j++) {
			if (j != 0) strcat(descrip_str, " ");
			if (!(j % 5)) strcat(descrip_str, "\n        ");
			sprintf(temp_str, "%d",
			    melo_datum.param[i].data.integer_array[j]);
			strcat(descrip_str, temp_str);
		    }

		    strcat(descrip_str, "\n    }");
		    write_units_if_any(i);
		    break;

		case(mel_real_array):

		    strcat(descrip_str, "{");

		    for (j = 0; j < melo_datum.param[i].array_len; j++) {
			if (j != 0) strcat(descrip_str, " ");
			if (!(j % 3)) strcat(descrip_str, "\n        ");
			sprintf(temp_str, "%lg",
			    melo_datum.param[i].data.real_array[j]);
			strcat(descrip_str, temp_str);
		    }

		    strcat(descrip_str, "\n    }");
		    write_units_if_any(i);
		    break;

		case(mel_str_array):

		    strcat(descrip_str, "{");

		    for (j = 0; j < melo_datum.param[i].array_len; j++) {
			strcat(descrip_str, "\n        ");
			sprintf(temp_str, "'%s'",
			    melo_datum.param[i].data.string_array[j]);
			strcat(descrip_str, temp_str);
		    }

		    strcat(descrip_str, "\n    }");
		    write_units_if_any(i);
		    break;
	    }
	}

	strcat(descrip_str, ";\n");

    } else {   /* terse output */

	/* first write descriptor type: */
	strncpy(descrip_str, melo_descrip[melo_datum.descrip_type].name,
	    melo_descrip[melo_datum.descrip_type].min_name_len);
	descrip_str[melo_descrip[melo_datum.descrip_type].min_name_len] = '\0';

	/* next, write its parameters and parameter values: */
	for (i = 0; i < melo_datum.num_param; i++) {

	    /* which parameter is it? */
	    strcat(descrip_str,",");
	    strncpy(temp_str, melo_descrip[melo_datum.descrip_type].
		param[melo_datum.param[i].name_index].name, melo_descrip[
		melo_datum.descrip_type].param[melo_datum.param[i].
		name_index].min_name_len);
	    temp_str[melo_descrip[melo_datum.descrip_type].param[melo_datum.
	    param[i].name_index].min_name_len] = '\0';
	    strcat(descrip_str, temp_str);
	    strcat(descrip_str, "=");

	    /* is its value known? (if not, write "unknown" and
	       proceed to next parameter.) */
	    if (melo_datum.param[i].unknown_flag) {
		strcat(descrip_str, "unknown");
		continue;
	    };

	    /* what type of value is it (int, array, etc.)? */

	    type = melo_descrip[melo_datum.descrip_type].
		param[melo_datum.param[i].name_index].type;

	    switch (type) {

		case (mel_bool):

		    if (melo_datum.param[i].data.integer)
			strcat(descrip_str, "t");
		    else
			strcat(descrip_str, "f");
		    break;

		case (mel_int):

		    sprintf(temp_str, "%d", melo_datum.param[i].data.integer);
		    strcat(descrip_str, temp_str);
		    write_units_if_any(i);
		    break;

		case (mel_real):

		    sprintf(temp_str, "%lg", melo_datum.param[i].data.real);
		    strcat(descrip_str, temp_str);
		    write_units_if_any(i);
		    break;

		case (mel_str):

		    sprintf(temp_str, "'%s'", melo_datum.param[i].data.string);
		    strcat(descrip_str, temp_str);
		    write_units_if_any(i);
		    break;

		case(mel_int_array):

		    strcat(descrip_str, "{");

		    for (j = 0; j < melo_datum.param[i].array_len; j++) {
			if (j != 0) strcat(descrip_str, " ");
			sprintf(temp_str, "%d",
			    melo_datum.param[i].data.integer_array[j]);
			strcat(descrip_str, temp_str);
		    }

		    strcat(descrip_str, "}");
		    write_units_if_any(i);
		    break;

		case(mel_real_array):

		    strcat(descrip_str, "{");

		    for (j = 0; j < melo_datum.param[i].array_len; j++) {
			if (j != 0) strcat(descrip_str, " ");
			sprintf(temp_str, "%lg",
			    melo_datum.param[i].data.real_array[j]);
			strcat(descrip_str, temp_str);
		    }

		    strcat(descrip_str, "}");
		    write_units_if_any(i);
		    break;

		case(mel_str_array):

		    strcat(descrip_str, "{");

		    for (j = 0; j < melo_datum.param[i].array_len; j++) {
			if (j != 0) strcat(descrip_str, " ");
			sprintf(temp_str, "'%s'",
			    melo_datum.param[i].data.string_array[j]);
			strcat(descrip_str, temp_str);
		    }

		    strcat(descrip_str, "}");
		    write_units_if_any(i);
		    break;
	    }
	}

	strcat(descrip_str, ";");
    }
}

/*
---------------------------------------------------------------------------
module 8.1.1

write the units associated with a parameter to the end of the descriptor
string descrip_str. don't bother if no units associated with this parameter.
---------------------------------------------------------------------------
*/
static void write_units_if_any(
int param_num)
{
    char temp_str[MELO_MAX_DESCRIP_STR_LEN+1];

    if (!strlen(melo_datum.param[param_num].units)) return;

    sprintf(temp_str, " (%s)", melo_datum.param[param_num].units);
    strcat(descrip_str, temp_str);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -