/*
	Function: file_to_matrix
	-------------------------
	Fill up matrix entries that read from a source file.
	
	Parameter:
	file - source file
	src - source structure

	Returns:
	0 for success, 1 for failure.
 */
int file_to_matrix(FILE *file, Source *src)
{
	// Set file pointer to the beginning
	if (!fseek(file, 0, SEEK_SET)) {
		char line[MAX_CHARS];
		char **seg = (char**)malloc(sizeof(char*) * 3);
		char *token = NULL;
		int iLength = src->items->length;
		int nIndex;
		int kIndex;
		double value;
		fgets(line, MAX_CHARS, file);

		while (fgets(line, MAX_CHARS, file) != NULL) {
			seg[0] = strtok_s(line, _SEP, &token);
			if (seg[0] != NULL) {
				seg[1] = strtok_s(NULL, _SEP, &token);

				if (seg[1] != NULL) {
					nIndex = (int) find_number(seg[1]);
					seg[2] = strtok_s(NULL, _SEP, &token);

					if ((nIndex > 0) && (seg[2] != NULL)) {
						value = find_number(seg[2]);

						if (value > 0) {
							kIndex = find_index(
								src->items, seg[0], 0, iLength - 1);
							--nIndex;
							if ((kIndex >= 0) && nIndex < src->N) {
								src->V[nIndex][kIndex] = value;
								if ((src->min == -1) || (src->min > value)) {
									src->min = value;
								}
								if (src->max < value) {
									src->max = value;
								}
							}
						}
					}
				}
			}
		}

		return 0;
	}
	else {
		fprintf(stderr, "Fatal error: Failed to roll back file pointer "
			"while attempting to input data set.\n");
		return 1;
	}

}
Exemple #2
0
int main(void)
{
    int choice;
    if(file2mem() == FAIL){
        printf("信息文件读取出错\n");
        return 1;
    }
    while(1){
        print_choice();
        scanf("%d",&choice);
        switch(choice){
            case ADD:
                add_student();break;
            case DEL:
                delete_student();break;
            case CGE:
                change_student();break;
            case F_NAME:
                find_name();break;
            case F_NUM:
                find_number();break;
            case SORT:
                sort_score();break;
            case PRT:
                print_student();break;
            case EXIT:
                return 0;
        }
        getchar();
        printf("请按任意继续\n");
        getchar();
    }
    
}
int find_number(int x = 20)
{
  if (is_divisible_by(x, 20))
    return x;
  else if (x < 1000000000)
    return find_number(x+1);
  else
    return 0;
}
/*Main */
int main(int argc, char *argv[])
{
	int i;
	
	for(i = 2; i <= 99; i++)	
		find_number(i); 
		
	return 0;
}
/* main */
int main(int argc, const char *argv[])
{
	char str[SIZE_MAX];

	printf("Please input str:");
	Gets(str, sizeof(str));
	while (strlen(str) > 0) {
		find_number(str);
		printf("\nPlease input str:");
		Gets(str, sizeof(str));
	}

	return 0;
}
/*
	Function: get_dimension
	------------------------
	Traverses a given file and finds the total number of users and items
	for this source. The acquired number is stored in this given source
	structure. This method is used in the case that the hint line is not
	presented.

	Parameter:
	file - source file
	src - source structure
*/
void get_dimension(FILE *file, Source *src)
{
	int user_num = 0;
	char chars[MAX_CHARS];
	char *token = NULL;
	char **seg = (char**)malloc(3 * sizeof(*seg));
	struct Item_Tree *item_tree = initialize();
	Item *items = NULL;
	
	while (fgets(chars, MAX_CHARS, file) != NULL) {
		// Gets all segments
		seg[0] = strtok_s(chars, _SEP, &token);
		if (seg[0] != NULL) {
			seg[1] = strtok_s(NULL, _SEP, &token);

			if (seg[1] != NULL) {
				seg[2] = strtok_s(NULL, _SEP, &token);

				if (seg[2] != NULL) {
					// Handles item segment
					input_item(item_tree, seg[0]);
					// Handles user IDs
					long temp = (long) find_number(seg[1]);
					if (temp > user_num) {
						user_num = temp;
					}
				}
			}
		}
	}

	free(seg);

	// Retrieves all item names and free its item tree
	get_items(item_tree, &items);
	src->items = items;
	// Sets user numbers
	src->N = user_num;
}
Exemple #7
0
//整理 输入计算并输出
int input_cir_print(void)
{
	int i, n;							//n代表门的扇数
	int m_m, d=1;						//m_m代表所有满足条件的数之和
	char a[32];							//a中暂时存储密码字符串

	printf("输入门的扇数:\n");
	scanf("%d", &n);

	//随机产生1~20的进制数
	srand((unsigned)time(NULL));
	for(i=0; i<n; i++)
	{
		d=rand()%18+2;								//保证1<d<20
		m_m=find_number(d);
		
		ten_to_oth(m_m, d, a);
		printf("%d进制门的密码为:%s\n",d,a);
		memset(a, strlen(a)+1, 0);					//清零函数
	}

	return 0;
}
Exemple #8
0
int main()
{
  struct Number numbers[8];
  int d = 0;
  char n = '-';
  char line[31];
  char answer[31];
  strcpy(numbers[0].chars, "ABC\0");
  strcpy(numbers[1].chars, "DEF\0");
  strcpy(numbers[2].chars, "GHI\0");
  strcpy(numbers[3].chars, "JKL\0");
  strcpy(numbers[4].chars, "MNO\0");
  strcpy(numbers[5].chars, "PQRS\0");
  strcpy(numbers[6].chars, "TUV\0");
  strcpy(numbers[7].chars, "WXYZ\0");

  numbers[0].val = '2';
  numbers[1].val = '3';
  numbers[2].val = '4';
  numbers[3].val = '5';
  numbers[4].val = '6';
  numbers[5].val = '7';
  numbers[6].val = '8';
  numbers[7].val = '9';

  while( scanf("%s", line) != EOF ) {
   for(d = 0; d < strlen(line); d++) {
      n = find_number(numbers,  line[d]);
      answer[d] = ( n == '-' ? line[d] : n );
    }
    answer[d] = '\0';
    printf("%s\n", answer);
    answer[0] = '\0';
    line[0] = '\0';
  }
  return 0;
}
int main(void)
{
  int x = find_number();
  std::cout << "number: " << x << std::endl;
}
Exemple #10
0
static int sort_lines(const void* a, const void* b)
{
	const char* _a, *_b;
	int a_word_beg, a_word_end, b_word_beg, b_word_end;
	int a_num, b_num, a_num_start, b_num_start, a_num_end, b_num_end;
	int num_result, result, suffix_result;

	_a = a;
	_b = b;

	// find the first non-matching word
	a_word_beg = 0;
	b_word_beg = 0;
	next_unequal_word(_a, a_word_beg, &a_word_beg, &a_word_end,
		_b, b_word_beg, &b_word_beg, &b_word_end);

	if (a_word_end-a_word_beg <= 0) {
		if (b_word_end-b_word_beg <= 0)
			return 0;
		return -1;
	}
	if (b_word_end-b_word_beg <= 0) {
		if (a_word_end-a_word_beg <= 0)
			return 0;
		return 1;
	}

	// first try to find 2 numbers
	find_number(&_a[a_word_beg], a_word_end-a_word_beg,
		&a_num_start, &a_num_end);
	find_number(&_b[b_word_beg], b_word_end-b_word_beg,
		&b_num_start, &b_num_end);

	// if we cannot find both numbers, return a regular
	// string comparison over the entire word
	if (a_num_end <= a_num_start
	    || b_num_end <= b_num_start) {
		result = str_cmp(&_a[a_word_beg], a_word_end-a_word_beg,
			&_b[b_word_beg], b_word_end-b_word_beg);
		if (!result) {
			fprintf(stderr, "Internal error in %s:%i\n",
				__FILE__, __LINE__);
			exit(0);
		}
		return result;
	}
	// A number must always be prefixed by at least one character.
	if (!a_num_start || !b_num_start) {
		fprintf(stderr, "Internal error in %s:%i\n",
			__FILE__, __LINE__);
		exit(0);
	}
	// otherwise compare the string up to the 2 numbers,
	// if it does not match return that result
	result = str_cmp(&_a[a_word_beg], a_num_start,
		&_b[b_word_beg], b_num_start);
	if (result)
		return result;

	a_num_start += a_word_beg;
	a_num_end += a_word_beg;
	b_num_start += b_word_beg;
	b_num_end += b_word_beg;
	if (a_num_end > a_word_end
	    || b_num_end > b_word_end) {
		fprintf(stderr, "Internal error in %s:%i\n",
			__FILE__, __LINE__);
		fprintf(stderr, "sort_line_a: %s", _a);
		fprintf(stderr, "sort_line_b: %s", _b);
		exit(1);
	}
	if ((a_word_end-a_num_end == 0
	     || is_known_suffix(&_a[a_num_end],
		a_word_end-a_num_end))
	    && (b_word_end-b_num_end == 0
	     || is_known_suffix(&_b[b_num_end],
		b_word_end-b_num_end))) {
		// known suffix comes before number
		suffix_result = str_cmp(&_a[a_num_end],
			a_word_end-a_num_end,
			&_b[b_num_end], b_word_end-b_num_end);
		if (suffix_result)
			return suffix_result;
	}

	a_num = to_i(&_a[a_num_start], a_num_end-a_num_start);
	b_num = to_i(&_b[b_num_start], b_num_end-b_num_start);
	num_result = a_num-b_num;

	// if the non-known suffixes don't match, return numeric result
	// if numbers are not equal, otherwise suffix result
	suffix_result = str_cmp(&_a[a_num_end], a_word_end-a_num_end,
		&_b[b_num_end], b_word_end-b_num_end);
	if (suffix_result) {
		if (num_result) return num_result;
		return suffix_result;
	}
	// Should be impossible that both the number result and
	// suffix result are equal. How can the entire word then
	// be unequal?
	if (!num_result) {
		fprintf(stderr, "Internal error in %s:%i\n",
			__FILE__, __LINE__);
		fprintf(stderr, "sort_line_a: %s", _a);
		fprintf(stderr, "sort_line_b: %s", _b);
		exit(1);
	}

	// find second non-equal word
	next_unequal_word(_a, a_word_end, &a_word_beg, &a_word_end,
		_b, b_word_end, &b_word_beg, &b_word_end);
	if (a_word_end <= a_word_beg
	    || b_word_end <= b_word_beg)
		return num_result;
	
	// if no numbers in second non-equal words, fall back
	// to numeric result of first word
	find_number(&_a[a_word_beg], a_word_end-a_word_beg,
		&a_num_start, &a_num_end);
	find_number(&_b[b_word_beg], b_word_end-b_word_beg,
		&b_num_start, &b_num_end);
	if (a_num_end <= a_num_start
	    || b_num_end <= b_num_start)
		return num_result;
	// A number must always be prefixed by at least one character.
	if (!a_num_start || !b_num_start) {
		fprintf(stderr, "Internal error in %s:%i\n",
			__FILE__, __LINE__);
		exit(0);
	}
	// If the prefix string of the second word does not
	// match, fall back to numeric result of first word.
	result = str_cmp(&_a[a_word_beg], a_num_start,
		&_b[b_word_beg], b_num_start);
	if (result)
		return num_result;
	a_num_start += a_word_beg;
	a_num_end += a_word_beg;
	b_num_start += b_word_beg;
	b_num_end += b_word_beg;
	if (a_num_end > a_word_end
	    || b_num_end > b_word_end) {
		fprintf(stderr, "Internal error in %s:%i\n",
			__FILE__, __LINE__);
		exit(0);
	}
	// if there are known suffixes in second non-equal
	// words, compare those first
	if ((a_word_end-a_num_end == 0
	     || is_known_suffix(&_a[a_num_end],
		a_word_end-a_num_end))
	    && (b_word_end-b_num_end == 0
	     || is_known_suffix(&_b[b_num_end],
		b_word_end-b_num_end))) {
		// known suffix comes before number
		suffix_result = str_cmp(&_a[a_num_end],
			a_word_end-a_num_end,
			&_b[b_num_end], b_word_end-b_num_end);
		if (suffix_result)
			return suffix_result;
	}
	// otherwise fall back to numeric result of first word
	return num_result;
}
 const IntArray& find_array() {
   for ( auto i = 0; i < in->N; ++ i ) {
     B[i] = find_number(in->A[i]);
   }
   return B;
 }