int get_valid_input(void)
{
  int i, ch, hour, minute;
  char input[TIME_STR_SIZE] = {'\0'};

  for (;;) {
    printf("Request departure time: (e.g. 21:23): ");
    /* swallow spaces */
    while (isspace(ch = getchar()))
        ;

    /* fill in the input buffer,
     * and consume any remaining input
     * on stdin including the newline.
     * That way, we don't get any double prompts
     * should we have to re-prompt after bad input
     */
    for (i = 0; ch != '\n'; ch = getchar())
      if (i < TIME_STR_SIZE)
        input[i++] = ch;

    input[i] = '\0';
    if (is_format_valid(input)) {
      convert(input, &hour, &minute);
      if (hour < 24 && minute < 60)
        return (hour * 60) + minute;
    }

    fprintf(stderr, "Invalid Input: '%s'\n", input);
  }
}
int to_minutes(char *s)
{
  int hour, minute;
  if (is_format_valid(s)) {
    convert(s, &hour, &minute);
    if (hour < 24 && minute < 60)
      return (hour * 60) + minute;
  }
  return -1;
}
Exemple #3
0
/*** create new mm_real object
 * MMRealFormat	format: MM_REAL_DENSE or MM_REAL_SPARSE
 * MMRealSymm		symm  : MM_REAL_GENERAL, MM_REAL_SYMMETRIC_UPPER or MM_REAL_SYMMETRIC_LOWER
 * int				m, n  : rows and columns of the matrix
 * int				nnz   : number of nonzero elements of the matrix ***/
mm_real *
mm_real_new (MMRealFormat format, MMRealSymm symm, const int m, const int n, const int nnz)
{
	mm_real	*x;
	bool	symmetric;

	if (!is_format_valid (format))
		error_and_exit ("mm_real_new", "invalid MMRealFormat format.", __FILE__, __LINE__);
	if (!is_symm_valid (symm))
		error_and_exit ("mm_real_new", "invalid MMRealSymm symm.", __FILE__, __LINE__);

	symmetric = symm & MM_SYMMETRIC;
	if (symmetric && m != n)
		error_and_exit ("mm_real_new", "symmetric matrix must be square.", __FILE__, __LINE__);

	x = mm_real_alloc ();
	if (x == NULL) error_and_exit ("mm_real_new", "failed to allocate object.", __FILE__, __LINE__);
	x->m = m;
	x->n = n;
	x->nnz = nnz;

	// typecode[1] = 'C' or 'A'
	if (format == MM_REAL_SPARSE) {
		mm_set_coordinate (&x->typecode);
		x->i = (int *) malloc (x->nnz * sizeof (int));
		x->p = (int *) malloc ((x->n + 1) * sizeof (int));
		if (x->i == NULL || x->p == NULL) error_and_exit ("mm_real_new", "cannot allocate memory.", __FILE__, __LINE__);
		// initialize x->p[0]
		x->p[0] = 0;
	} else mm_set_array (&x->typecode);

	x->symm = symm;
	// typecode[3] = 'G' -> 'S'
	if (symmetric) mm_set_symmetric (&x->typecode);

	if (!is_type_supported (x->typecode)) {
		char	msg[128];
		sprintf (msg, "matrix type does not supported :[%s].", mm_typecode_to_str (x->typecode));
		error_and_exit ("mm_real_new", msg, __FILE__, __LINE__);
	}

	// allocate arrays
	x->data = (double *) malloc (x->nnz * sizeof (double));
	if (x->data == NULL) error_and_exit ("mm_real_new", "cannot allocate memory.", __FILE__, __LINE__);

	return x;
}