utils.c
来自「比较完整的遗传算法的源代码」· C语言 代码 · 共 63 行
C
63 行
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "utils.h"void strcreate(char **dest, char *src) { *dest = (char *)malloc(sizeof(char) * strlen(src) + 1); if (!*dest) { syserror("strcreate"); } strcpy(*dest, src);}int flip(double prob) { if (prob == 1.0) { return 1; } else { return ( randFraction() <= prob); }}double randFraction(void) { return (random() / (RAND_MAX + 1.0));}int randInt(int l, int h) { if (l < h) { return l + (int)(randFraction() * (double)(h - l + 1)); } else { return h + (int)(randFraction() * (double)(l - h + 1)); }}extern char *appName;void syserror(char *loc) { char errorbuffer[128]; sprintf(errorbuffer, "%s, %s", appName, loc); perror(errorbuffer); exit(EXIT_FAILURE);}void error(char *loc, char *message) { fprintf(stderr, "%s, %s: %s\n", appName, loc, message); exit(EXIT_FAILURE);}#ifdef USE_PVM#include <pvm3.h>void pvmerror(char *loc) { char errorbuffer[128]; sprintf(errorbuffer, "%s, %s", appName, loc); pvm_perror(errorbuffer); exit(EXIT_FAILURE);}#endif /* USE_PVM */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?