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

📄 parse.c

📁 是从一个国外的蚁群算法网站上下载的源程序,是用蚁群算法解TSP问题的源程序!感谢开放源程序
💻 C
📖 第 1 页 / 共 3 页
字号:
          options->opt_ants = 1;          break;         case 'o':          if (option [1] != '\0')            options->arg_optimum = option + 1;          else if (++i < argc)            options->arg_optimum = argv [i];          else            goto error_missing_arg_short;          option = "\0";          options->opt_optimum = 1;          break;         case 'q':          if (option [1] != '\0')            options->arg_q0 = option + 1;          else if (++i < argc)            options->arg_q0 = argv [i];          else            goto error_missing_arg_short;          option = "\0";          options->opt_q0 = 1;          break;         case 'r':          if (option [1] != '\0')            options->arg_tries = option + 1;          else if (++i < argc)            options->arg_tries = argv [i];          else            goto error_missing_arg_short;          option = "\0";          options->opt_tries = 1;          break;         case 's':          if (option [1] != '\0')            options->arg_tours = option + 1;          else if (++i < argc)            options->arg_tours = argv [i];          else            goto error_missing_arg_short;          option = "\0";          options->opt_tours = 1;          break;         case 't':          if (option [1] != '\0')            options->arg_time = option + 1;          else if (++i < argc)            options->arg_time = argv [i];          else            goto error_missing_arg_short;          option = "\0";          options->opt_time = 1;          break;         case 'u':          options->opt_as = 1;          break;         case 'v':          options->opt_eas = 1;          break;         case 'w':          options->opt_ras = 1;          break;         case 'x':          options->opt_mmas = 1;          break;         case 'y':          options->opt_bwas = 1;          break;         case 'z':          options->opt_acs = 1;          break;         default:          fprintf (stderr, STR_ERR_UNKNOWN_SHORT_OPT, program_name, *option);          return -1;         error_missing_arg_short:          fprintf (stderr, STR_ERR_MISSING_ARG_SHORT, program_name, *option);          return -1;        }      } while (*++option != '\0');  }  return i;}void check_out_of_range ( double value, double MIN, double MAX, char *optionName ) /*          FUNCTION: check whether parameter values are within allowed range      INPUT:    none      OUTPUT:   none      COMMENTS: none*/{  if ((value<MIN)||(value>MAX)){    fprintf(stderr,"Error: Option `%s' out of range\n",optionName);    exit(1);  }}int parse_commandline (int argc, char *argv []){    int i;    const char *progname;    struct options options;     progname = argv [0] != NULL && *(argv [0]) != '\0'	? argv [0]	: "acotsp";    i = parse_options (&options, progname, argc, argv);    if (i < 2)    {	fprintf (stderr, "No options are specified\n");	fprintf (stderr, "Try `%s --help' for more information.\n",		 progname);	exit(1);    }    if (options.opt_help)    {	printf ("Usage: %s [OPTION]... [ARGUMENT]...\n\n"		"Options:\n" STR_HELP, progname);	exit(0);    }    puts ("\t OPTIONS:");    if ( options.opt_time ) {	max_time = atof(options.arg_time);	fputs ("  -t  --time ", stdout);	if (options.arg_time != NULL)	    printf ("with argument \"%.3f\"\n", max_time);	check_out_of_range( max_time, 0.0, 86400., "max_time (seconds)");    } else {	fprintf(stderr,"\tNote: time limit is set to default 10 seconds\n");    }    if ( options.opt_tries ) {	max_tries = atol(options.arg_tries);	fputs ("  -r  --tries ", stdout);	if (options.arg_tries != NULL)	    printf ("with argument \"%ld\"\n", max_tries);	check_out_of_range( max_tries, 1, MAXIMUM_NO_TRIES, "max_tries (tries)");    } else {	max_tries = 10;	fprintf(stderr,"\tNote: number or trials is set to default 10\n");    }    if ( options.opt_tours ) {	max_tours = atol(options.arg_tours);	fputs ("  -s  --tours ", stdout);	if (options.arg_tries != NULL)	    printf ("with argument \"%ld\"\n", max_tours);	check_out_of_range( max_tours, 1, LONG_MAX, "max_tries (tries)");    } else {	max_tours = 100;	fprintf(stderr,"\tNote: maximum number tours is set to default 100\n");    }    if ( options.opt_optimum )    {	optimal = atol(options.arg_optimum); 	fputs ("  -o  --optimum ", stdout);	if (options.arg_optimum != NULL)	    printf ("with argument \"%ld\"\n", optimal);    } else {	optimal = 1;	fprintf(stderr,"\tNote: optimal solution value is set to default 1\n");    }    if ( options.opt_tsplibfile )    {	strncpy( name_buf, options.arg_tsplibfile, strlen( options.arg_tsplibfile ) ); 	fputs ("  -i  --tsplibfile ", stdout);	if (options.arg_tsplibfile != NULL)	    printf ("with argument \"%s\"\n", name_buf );    }    if ( options.opt_ants ) {	n_ants = atol(options.arg_ants);	fputs ("  -m  --ants ", stdout);	if (options.arg_ants != NULL)	    printf ("with argument \"%ld\"\n", n_ants);	check_out_of_range( n_ants, 1, MAX_ANTS-1, "n_ants");    } else {	n_ants = 25;	fprintf(stderr,"\tNote: number or ants is set to default 25\n");    }    if ( options.opt_nnants ) {	nn_ants = atol(options.arg_nnants);	fputs ("  -m  --ants ", stdout);	if (options.arg_ants != NULL)	    printf ("with argument \"%ld\"\n", nn_ants);	check_out_of_range( n_ants, 1, 100, "nn_ants");    } else {	nn_ants = 20;	fprintf(stderr,"\tNote: number of nearest neighbours in tour construction is set to default 25\n");    }    if ( options.opt_alpha ) {	alpha = atof(options.arg_alpha);	fputs ("  -a  --alpha ", stdout);	if (options.arg_alpha != NULL)	    printf ("with argument \"%f\"\n", alpha);	check_out_of_range( alpha, 0., 100., "alpha");    } else {	alpha = 1.0;	fprintf(stderr,"\tNote: alpha is set to default 1.0\n");    }    if ( options.opt_beta ) {	beta = atof(options.arg_beta);	fputs ("  -b  --beta ", stdout);	if (options.arg_beta != NULL)	    printf ("with argument \"%f\"\n", beta);	check_out_of_range( beta, 0., 100., "beta");    } else {	beta = 2.0;	fprintf(stderr,"\tNote: beta is set to default 2.0\n");    }    if ( options.opt_rho ) {	rho = atof(options.arg_rho);	fputs ("  -e  --rho ", stdout);	if (options.arg_rho != NULL)	    printf ("with argument \"%f\"\n", rho);	check_out_of_range( rho, 0., 1., "rho");    } else {	rho = 0.5;	fprintf(stderr,"\tNote: rho is set to default 0.5\n");    }    if ( options.opt_q0 ) {	q_0 = atof(options.arg_q0);	fputs ("  -q  --q0 ", stdout);	if (options.arg_q0 != NULL)	    printf ("with argument \"%f\"\n", q_0);	check_out_of_range( q_0, 0., 1., "q0");    } else {	q_0 = 0.0;	fprintf(stderr,"\tNote: q_0 is set to default 0\n");    }    if ( options.opt_elitistants ) {	elitist_ants = atol(options.arg_elitistants);	fputs ("  -m  --ants ", stdout);	if (options.arg_elitistants != NULL)	    printf ("with argument \"%ld\"\n", elitist_ants);	check_out_of_range( n_ants, 0, LONG_MAX, "elitistants");    } else {	elitist_ants = 100;	fprintf(stderr,"\tNote: number of elitist ants is set to default 100\n");    }    if ( options.opt_rasranks ) {	ras_ranks = atol(options.arg_rasranks);	fputs ("  -m  --ants ", stdout);	if (options.arg_rasranks != NULL)	    printf ("with argument \"%ld\"\n", ras_ranks);	check_out_of_range( n_ants, 0, LONG_MAX, "rasranks");    } else {	ras_ranks = 6;	fprintf(stderr,"\tNote: number of ranks is set to default 6\n");    }    if ( options.opt_nnls ) {	nn_ls = atol(options.arg_nnls);	fputs ("  -k  --nnls ", stdout);	if (options.arg_nnls != NULL)	    printf ("with argument \"%ld\"\n", nn_ls);	check_out_of_range( n_ants, 0, LONG_MAX, "nnls");    } else {	nn_ls = 20; 	fprintf(stderr,"\tNote: number nearest neighbours in local search is set to default 20\n");    }    if ( options.opt_localsearch ) {	ls_flag = atol(options.arg_localsearch);	fputs ("  -l  --localsearch ", stdout);	if (options.arg_localsearch != NULL)	    printf ("with argument \"%ld\"\n", ls_flag);	check_out_of_range( n_ants, 0, LONG_MAX, "ls_flag");    } else {	ls_flag = 3;	fprintf(stderr,"\tNote: local search flag is set to default 3 (3-opt)\n");    }    if ( options.opt_dlb ) {	dlb_flag = atol(options.arg_dlb);	fputs ("  -d  --dlb ", stdout);	if (options.arg_dlb != NULL)	    printf ("with argument \"%ld\"\n", dlb_flag);	check_out_of_range( dlb_flag, 0, 1, "dlb_flag");    } else {	dlb_flag = TRUE;	fprintf(stderr,"\tNote: dlb flag is set to default 1 (use don't look bits)\n");    }    if ( options.opt_as ) {	as_flag = TRUE;	eas_flag = FALSE; ras_flag = FALSE; mmas_flag = FALSE; 	bwas_flag = FALSE; acs_flag = FALSE;	fprintf(stderr,"as_flag is set to 1, run Ant System\n");/*      } else { *//*  	fprintf(stderr,"\tNote: as_flag is set to default 0 (don't run Ant System)\n"); */    }    if ( options.opt_eas ) {	eas_flag = TRUE;	as_flag = FALSE; ras_flag = FALSE; mmas_flag = FALSE; 	bwas_flag = FALSE; acs_flag = FALSE;	fprintf(stderr,"eas_flag is set to 1, run Elitist Ant System\n");/*      } else { *//*  	fprintf(stderr,"\tNote: eas_flag is set to default 0 (don't run Elitist Ant System)\n"); */    }    if ( options.opt_ras ) {	ras_flag = TRUE;	as_flag = FALSE; eas_flag = FALSE; mmas_flag = FALSE; 	bwas_flag = FALSE; acs_flag = FALSE;	fprintf(stderr,"ras_flag is set to 1, run rank-based Ant System\n");/*      } else { *//*  	fprintf(stderr,"\tNote: ras_flag is set to default 0 (don't run rank-based Ant System)\n"); */    }    if ( options.opt_mmas ) {	mmas_flag = TRUE;	as_flag = FALSE; eas_flag = FALSE; ras_flag = FALSE; 	bwas_flag = FALSE; acs_flag = FALSE;	fprintf(stderr,"mmas_flag is set to 1, run MAX-MIN Ant System\n");/*      } else { *//*  	fprintf(stderr,"\tNote: mmas_flag is set to default 1 (run MAX-MIN Ant System!)\n"); */    }    if ( options.opt_bwas ) {	bwas_flag = TRUE;	as_flag = FALSE; eas_flag = FALSE; mmas_flag = FALSE; 	ras_flag = FALSE; acs_flag = FALSE;	fprintf(stderr,"bwas_flag is set to 1, run Best-Worst Ant System\n");/*      } else { *//*  	fprintf(stderr,"\tNote: bwas_flag is set to default 0 (don't run Best-Worst Ant System)\n"); */    }    if ( options.opt_acs ) {	acs_flag = TRUE;	as_flag = FALSE; eas_flag = FALSE; mmas_flag = FALSE; 	ras_flag = FALSE; bwas_flag = FALSE;	fprintf(stderr,"acs_flag is set to 1, run Ant Colony System\n");/*      } else { *//*  	fprintf(stderr,"\tNote: acs_flag is set to default 0 (don't run Ant Colony System)\n"); */    }    puts ("Non-option arguments:");    while (i < argc) {	fprintf (stderr,"  \"%s\"\n", argv [i++]);	fprintf (stderr,"\nThere were non-option arguments\n");	fprintf (stderr,"I suspect there is something wrong, maybe wrong option name; exit\n");	exit(1);    }    return 0;}

⌨️ 快捷键说明

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