コード例 #1
0
ファイル: Q71.cpp プロジェクト: quangteo25/ProjectEuler
int main()
{
	long int a1 = 2,a2 = 5;
	for (long int r2 = 1000000; r2 > 1; r2--){
            po:
		for (long int r1 = r2*0.428572; r1 > r2*0.4 ; r1--){
                if ((unsigned long long int)r1*a2 < (unsigned long long int)a1*r2){
                    r2--;
                    goto po;
                }
                if (lastch(r1,r2) == 0 && (unsigned long long int)r1*a2 > (unsigned long long int)a1*r2){
                    a1 = r1;
                    a2 = r2;
                    r2--;
                    goto po;
                }
		}
	}
	std::cout << a1 << std::endl;
	return 0;
}
コード例 #2
0
ファイル: rc.c プロジェクト: vkhromov/freebsd
/*
 * Extract the foreground, background and highlight values from an attribute
 * represented as a string in one of two forms:
 *
 * "(foreground,background,highlight)"
 " "xxxx_color"
 */
static int
str_to_attr(char *str, int *fg, int *bg, int *hl)
{
    int i = 0, get_fg = 1;
    unsigned j;
    char tempstr[MAX_LEN + 1], *part;

    if (str[0] != '(' || lastch(str) != ')') {
        if ((i = find_color(str)) >= 0) {
            *fg = dlg_color_table[i].fg;
            *bg = dlg_color_table[i].bg;
            *hl = dlg_color_table[i].hilite;
            return 0;
        }
        return -1;		/* invalid representation */
    }

    /* remove the parenthesis */
    strcpy(tempstr, str + 1);
    lastch(tempstr) = '\0';

    /* get foreground and background */

    while (1) {
        /* skip white space before fg/bg string */
        i = skip_whitespace(tempstr, i);
        if (tempstr[i] == '\0')
            return -1;		/* invalid representation */
        part = tempstr + i;	/* set 'part' to start of fg/bg string */

        /* find end of fg/bg string */
        while (!whitespace(tempstr[i]) && tempstr[i] != ','
                && tempstr[i] != '\0')
            i++;

        if (tempstr[i] == '\0')
            return -1;		/* invalid representation */
        else if (whitespace(tempstr[i])) {	/* not yet ',' */
            tempstr[i++] = '\0';

            /* skip white space before ',' */
            i = skip_whitespace(tempstr, i);
            if (tempstr[i] != ',')
                return -1;	/* invalid representation */
        }
        tempstr[i++] = '\0';	/* skip the ',' */
        for (j = 0; j < COLOR_COUNT && dlg_strcmp(part, color_names[j].name);
                j++) ;
        if (j == COLOR_COUNT)	/* invalid color name */
            return -1;
        if (get_fg) {
            *fg = color_names[j].value;
            get_fg = 0;		/* next we have to get the background */
        } else {
            *bg = color_names[j].value;
            break;
        }
    }				/* got foreground and background */

    /* get highlight */

    /* skip white space before highlight string */
    i = skip_whitespace(tempstr, i);
    if (tempstr[i] == '\0')
        return -1;		/* invalid representation */
    part = tempstr + i;		/* set 'part' to start of highlight string */

    /* trim trailing white space from highlight string */
    i = (int) strlen(part) - 1;
    while (whitespace(part[i]) && i > 0)
        i--;
    part[i + 1] = '\0';

    if (!dlg_strcmp(part, "ON"))
        *hl = TRUE;
    else if (!dlg_strcmp(part, "OFF"))
        *hl = FALSE;
    else
        return -1;		/* invalid highlight value */

    return 0;
}
コード例 #3
0
ファイル: rc.c プロジェクト: vkhromov/freebsd
/*
 * Parse the configuration file and set up variables
 */
int
dlg_parse_rc(void)
{
    int i;
    int l = 1;
    PARSE_LINE parse;
    char str[MAX_LEN + 1];
    char *var;
    char *value;
    char *tempptr;
    int result = 0;
    FILE *rc_file = 0;
    char *params;

    /*
     *  At startup, dialog determines the settings to use as follows:
     *
     *  a) if the environment variable $DIALOGRC is set, its value determines
     *     the name of the configuration file.
     *
     *  b) if the file in (a) can't be found, use the file $HOME/.dialogrc
     *     as the configuration file.
     *
     *  c) if the file in (b) can't be found, try using the GLOBALRC file.
     *     Usually this will be /etc/dialogrc.
     *
     *  d) if the file in (c) cannot be found, use the compiled-in defaults.
     */

    /* try step (a) */
    if ((tempptr = getenv("DIALOGRC")) != NULL)
        rc_file = fopen(tempptr, "rt");

    if (rc_file == NULL) {	/* step (a) failed? */
        /* try step (b) */
        if ((tempptr = getenv("HOME")) != NULL
                && strlen(tempptr) < MAX_LEN - (sizeof(DIALOGRC) + 3)) {
            if (tempptr[0] == '\0' || lastch(tempptr) == '/')
                sprintf(str, "%s%s", tempptr, DIALOGRC);
            else
                sprintf(str, "%s/%s", tempptr, DIALOGRC);
            rc_file = fopen(tempptr = str, "rt");
        }
    }

    if (rc_file == NULL) {	/* step (b) failed? */
        /* try step (c) */
        strcpy(str, GLOBALRC);
        if ((rc_file = fopen(tempptr = str, "rt")) == NULL)
            return 0;		/* step (c) failed, use default values */
    }

    DLG_TRACE(("opened rc file \"%s\"\n", tempptr));
    /* Scan each line and set variables */
    while ((result == 0) && (fgets(str, MAX_LEN, rc_file) != NULL)) {
        DLG_TRACE(("rc:%s", str));
        if (*str == '\0' || lastch(str) != '\n') {
            /* ignore rest of file if line too long */
            fprintf(stderr, "\nParse error: line %d of configuration"
                    " file too long.\n", l);
            result = -1;	/* parse aborted */
            break;
        }

        lastch(str) = '\0';
        if (begins_with(str, "bindkey", &params)) {
            if (!dlg_parse_bindkey(params)) {
                fprintf(stderr, "\nParse error: line %d of configuration\n", l);
                result = -1;
            }
            continue;
        }
        parse = parse_line(str, &var, &value);	/* parse current line */

        switch (parse) {
        case LINE_EMPTY:	/* ignore blank lines and comments */
            break;
        case LINE_EQUALS:
            /* search table for matching config variable name */
            if ((i = find_vars(var)) >= 0) {
                switch (vars[i].type) {
                case VAL_INT:
                    *((int *) vars[i].var) = atoi(value);
                    break;
                case VAL_STR:
                    if (!isquote(value[0]) || !isquote(lastch(value))
                            || strlen(value) < 2) {
                        fprintf(stderr, "\nParse error: string value "
                                "expected at line %d of configuration "
                                "file.\n", l);
                        result = -1;	/* parse aborted */
                    } else {
                        /* remove the (") quotes */
                        value++;
                        lastch(value) = '\0';
                        strcpy((char *) vars[i].var, value);
                    }
                    break;
                case VAL_BOOL:
                    if (!dlg_strcmp(value, "ON"))
                        *((bool *) vars[i].var) = TRUE;
                    else if (!dlg_strcmp(value, "OFF"))
                        *((bool *) vars[i].var) = FALSE;
                    else {
                        fprintf(stderr, "\nParse error: boolean value "
                                "expected at line %d of configuration "
                                "file (found %s).\n", l, value);
                        result = -1;	/* parse aborted */
                    }
                    break;
                }
#ifdef HAVE_COLOR
            } else if ((i = find_color(var)) >= 0) {
                int fg = 0;
                int bg = 0;
                int hl = 0;
                if (str_to_attr(value, &fg, &bg, &hl) == -1) {
                    fprintf(stderr, "\nParse error: attribute "
                            "value expected at line %d of configuration "
                            "file.\n", l);
                    result = -1;	/* parse aborted */
                } else {
                    dlg_color_table[i].fg = fg;
                    dlg_color_table[i].bg = bg;
                    dlg_color_table[i].hilite = hl;
                }
            } else {
#endif /* HAVE_COLOR */
                fprintf(stderr, "\nParse error: unknown variable "
                        "at line %d of configuration file:\n\t%s\n", l, var);
                result = -1;	/* parse aborted */
            }
            break;
        case LINE_ERROR:
            fprintf(stderr, "\nParse error: syntax error at line %d of "
                    "configuration file.\n", l);
            result = -1;	/* parse aborted */
            break;
        }
        l++;			/* next line */
    }

    (void) fclose(rc_file);
    return result;
}
コード例 #4
0
ファイル: rc.c プロジェクト: AhmadTux/DragonFlyBSD
/*
 * Parse the configuration file and set up variables
 */
int parse_rc(void)
{
  int i, l = 1, parse, fg, bg, hl;
  unsigned char str[MAX_LEN+1], *var, *value, *tempptr;
  FILE *rc_file = NULL;

  /*
   *
   *  At start, 'dialog' determines the settings to use as follows:
   *
   *  a) if environment variable DIALOGRC is set, it's value determines the
   *     name of the configuration file.
   *
   *  b) if the file in (a) can't be found, use the file $HOME/.dialogrc
   *     as the configuration file.
   *
   *  c) if the file in (b) can't be found, use compiled in defaults.
   *
   */

  if ((tempptr = getenv("DIALOGRC")) != NULL)
    rc_file = fopen(tempptr, "rt");

  if (tempptr == NULL || rc_file == NULL) {    /* step (a) failed? */
    /* try step (b) */
    if ((tempptr = getenv("HOME")) == NULL)
      return 0;    /* step (b) failed, use default values */

    if (tempptr[0] == '\0' || lastch(tempptr) == '/')
      sprintf(str, "%s%s", tempptr, DIALOGRC);
    else
      sprintf(str, "%s/%s", tempptr, DIALOGRC);

    if ((rc_file = fopen(str, "rt")) == NULL)
      return 0;    /* step (b) failed, use default values */
  }

  /* Scan each line and set variables */
  while (fgets(str, MAX_LEN, rc_file) != NULL) {
    if (lastch(str) != '\n') {    /* ignore rest of file if line too long */
      fprintf(stderr, "\nParse error: line %d of configuration file too long.\n", l);
      fclose(rc_file);
      return -1;    /* parse aborted */
    }
    else {
      lastch(str) = '\0';
      parse = parse_line(str, &var, &value);    /* parse current line */

      switch (parse) {
	case LINE_BLANK:    /* ignore blank lines and comments */
        case LINE_COMMENT:
          break;
        case LINE_OK:
          /* search table for matching config variable name */
          for (i = 0; i < VAR_COUNT && strcmp(vars[i].name, var); i++);

          if (i == VAR_COUNT) {    /* no match */
            fprintf(stderr, "\nParse error: unknown variable at line %d of configuration file.\n", l);
            return -1;    /* parse aborted */
          }
          else {    /* variable found in table, set run time variables */
            switch (vars[i].type) {
              case VAL_INT:
                *((int *) vars[i].var) = atoi(value);
                break;
              case VAL_STR:
                if (!isquote(value[0]) || !isquote(lastch(value)) || strlen(value) < 2) {
                  fprintf(stderr, "\nParse error: string value expected at line %d of configuration file.\n", l);
                  return -1;    /* parse aborted */
                }
                else {
                  /* remove the (") quotes */
                  value++;
                  lastch(value) = '\0';
                  strcpy((unsigned char *) vars[i].var, value);
		}
                break;
              case VAL_BOOL:
                if (!strcasecmp(value, "ON"))
                  *((bool *) vars[i].var) = TRUE;
                else if (!strcasecmp(value, "OFF"))
                  *((bool *) vars[i].var) = FALSE;
                else {
                  fprintf(stderr, "\nParse error: boolean value expected at line %d of configuration file.\n", l);
                  return -1;    /* parse aborted */
                }
                break;
              case VAL_ATTR:
                if (str_to_attr(value, &fg, &bg, &hl) == -1) {
                  fprintf(stderr, "\nParse error: attribute value expected at line %d of configuration file.\n", l);
                  return -1;    /* parse aborted */
                }
                ((int *) vars[i].var)[0] = fg;
                ((int *) vars[i].var)[1] = bg;
                ((int *) vars[i].var)[2] = hl;
                break;
            }
          }
          break;
        case LINE_ERROR:
          fprintf(stderr, "\nParse error: syntax error at line %d of configuration file.\n", l);
          return -1;    /* parse aborted */
      }
    }

    l++;    /* next line */
  }

  fclose(rc_file);
  return 0;    /* parse successful */
}