Example #1
0
int main(){
	int i;
	int* ptr;
	FILE *fp;
	fp = fopen(FILE_NAME, "r");
	if (fp == NULL) {	//파일 존재여부 확인
		printf("ERROR : 파일이 존재하지 않습니다. ");
		return -1;
	}
	//원소의 개수 n
	fscanf(fp, "%d", &n);
	//n개의 원소값이 들어갈 배열 동적할당
	n_arr = (int*)malloc(sizeof(int)*n);
	memset(n_arr, 0, sizeof(int)*n); //n_arr 배열 초기화
	//n개의 원소값 배열에 입력받기
	for (i = 0; i < n; i++) {
		fscanf(fp, "%d", &n_arr[i]);
	}
	fclose(fp);
	ptr = &n_arr[0];
	printf("Input data %s:\n", FILE_NAME);

	//Method1
	//중첩반복문 이용
	DWORD dwStartTime = timeGetTime();	//실행시간측정 시작
	Method1(ptr, 0, n - 1);				//함수실행
	DWORD dwEndTime = timeGetTime();	//실행시간측정 종료
	printf("(Method 1)\n");
	printf("  The Maximum sum = %d, indices from %d to %d\n", result1.largest_sum, result1.index_i, result1.index_j);
	printf("  The execution time = %lu msec\n", (dwEndTime - dwStartTime));

	//Method2
	//분할정복방법 이용
	dwStartTime = timeGetTime();	//실행시간측정 시작
	Method2(ptr, 0, n - 1);			//함수실행
	dwEndTime = timeGetTime();		//실행시간측정 종료
	printf("(Method 2)\n");
	printf("  The Maximum sum = %d, indices from %d to %d\n", result2.largest_sum, result2.index_i, result2.index_j);
	printf("  The execution time = %lu msec\n", (dwEndTime - dwStartTime));

	//Method3
	//동적계획법 이용
	dwStartTime = timeGetTime();	//실행시간측정 시작
	Method3(ptr, 0, n - 1);			//함수실행
	dwEndTime = timeGetTime();		//실행시간측정 종료
	printf("(Method 3)\n");
	printf("  The Maximum sum = %d, indices from %d to %d\n", result3.largest_sum, result3.index_i, result3.index_j);
	printf("  The execution time = %lu msec\n", (dwEndTime - dwStartTime));

	return 0;
}
Example #2
0
void 
main (int argc, char **argv)
{
  ProgTime StartTime;
  int ch;
  char type = 'C';
  char mode = '0';
  int lookback = 2;
  double k = 0;
  u_long mem_reqd;
  opterr = 0;
  msg_prefix = argv[0];
  while ((ch = getopt (argc, argv, "0123CPSf:d:l:hk:HBDYM")) != -1)
    switch (ch)
      {
      case 'H':
	novel_method = MG_NOVEL_HUFFMAN_CHARS;
	break;
      case 'B':
	novel_method = MG_NOVEL_BINARY;
	break;
      case 'D':
	novel_method = MG_NOVEL_DELTA;
	break;
      case 'Y':
	novel_method = MG_NOVEL_HYBRID;
	break;
      case 'M':
	novel_method = MG_NOVEL_HYBRID_MTF;
	break;
      case 'f':		/* input file */
	file_name = optarg;
	break;
      case 'd':
	set_basepath (optarg);
	break;
      case 'C':
      case 'P':
      case 'S':
	type = ch;
	break;
      case '0':
      case '1':
      case '2':
      case '3':
	mode = ch;
	break;
      case 'l':
	lookback = atoi (optarg);
	if (!is_power_of_two (lookback))
	  FatalError (1, "The lookback value must be a power of 2");
	lookback = floorlog_2 (lookback);
	break;
      case 'k':
	k = atof (optarg) * 1024;
	break;
      case 'h':
      case '?':
	fprintf (stderr, "usage: %s [-l lookback] [-f input_file]"
	"[-d data directory] [-h] [-0|-1|-2|-3] [-C|-P|-S] [-k mem (Kb)]\n",
		 argv[0]);
	exit (1);
      }
  GetTime (&StartTime);

  ReadInWords (file_name);

  if (type == 'C')
    {
      Select_all ();
      mem_reqd = WriteOutWords (file_name, MG_COMPLETE_DICTIONARY, lookback);
    }
  else
    {
      switch (mode)
	{
	case '0':
	  Select_all ();
	  break;
	case '1':
	  Message ("Dictionary limit of %.2f Kb", k / 1024);
	  Select_on ((int) k, OccuranceOrder);
	  break;
	case '2':
	  Message ("Dictionary limit of %.2f Kb", k / 1024);
	  Select_on ((int) k, DecFreqIncWL);
	  break;
	case '3':
	  Message ("Dictionary limit of %.2f Kb", k / 1024);
	  Method3 ((int) k);
	  break;
	}
      if (type == 'P')
	{
	  mem_reqd = WriteOutWords (file_name, MG_PARTIAL_DICTIONARY, lookback);
	}
      else
	{
	  mem_reqd = WriteOutWords (file_name, MG_SEED_DICTIONARY, lookback);
	}
    }

  Message ("Num words           : %8u -> %8u\n", Num[1], keep[1].num_wds);
  Message ("Num non-words       : %8u -> %8u\n", Num[0], keep[0].num_wds);
  Message ("Chars of words      : %8u -> %8u\n", chars[1], keep[1].chars);
  Message ("Chars of non-words  : %8u -> %8u\n", chars[0], keep[0].chars);
  Message ("Mem usage           : %8u -> %8u\n",
	   (Num[0] + Num[1]) * sizeof (char *) + chars[0] + chars[1],
	     (keep[0].num_wds + keep[1].num_wds) * sizeof (char *) +
	   keep[0].chars + keep[1].chars);
  Message ("Actual mem required : %8u\n", mem_reqd);

  Message ("%s", ElapsedTime (&StartTime, NULL));
  exit (0);
}