Пример #1
0
/*Giải thuật A Star để tìm đường đi ngắn nhất**********************************/
void PATH :: A_Star()	{
	/*Khởi tạo*/
	int i, j;
	long min, *heuritic = new long[Get_vertices()], *estimate = new long[Get_vertices()];
	mark = new bool[Get_vertices()];
	before = new int[Get_vertices()];
	min_len = new long[Get_vertices()];

	for (i=0; i<Get_vertices(); ++i)	{
		mark[i] = false;
		before[i] = -1;
		min_len[i] = estimate[i] = INT_MAX;
		heuritic[i] = Distance(Get_location(i), Get_location(Get_destination()));
	}
    min_len[0] = 0;
    estimate[0] = heuritic[0] = 0;

    clock_t time_begin = clock();

	do {
		i = -1;
		min = INT_MAX;

		/*Tìm trong các đỉnh lân cận j đỉnh nào gần đỉnh 0 nhất*/
		for (j=0; j<Get_vertices(); ++j)
			if (!mark[j] && min > estimate[j])	{
				min = estimate[j];
				i = j;
			}

		if (i==-1)		break;

		for (j=0; j<Get_vertices(); ++j)
			if (Get_length(i, j) && 
				min_len[j] > min_len[i] + Get_length(i, j))	{
					min_len[j] = min_len[i] + Get_length(i, j);
				estimate[j] = heuritic[j] + min_len[j];
				before[j] = i;
			}

			mark[i] = true;
	} while (i != Get_destination());
	/*Dừng lại khi không cần tối ưu đỉnh nào nữa (i==-1) hoặc đã duyệt đến 
	đỉnh đích (i==destination)*/

	clock_t time_end = clock();
	cout << "Thuat toan chay trong " 
		<< (double)(time_end - time_begin) / CLOCKS_PER_SEC << " s.\n";

	delete[] heuritic;
	delete[] estimate;
}/*Kết thúc hàm A_Star*********************************************************/
Пример #2
0
String& String :: operator= (const char* s){
    
    this->length=Get_length();
    
    reserve(this->length);

     this->_string = new char[this->length]; 
    
    for (size_t j=0;j<length;j++){
      _string[j]=s[j];
    }
    return *this;

  }
Пример #3
0
char& String::operator[] (size_t pos)
{

  char* character;

  length=Get_length();

  _string=Get_string();

   size_t i;
   //We do pos-1 to have the first case of the array as number 1 instead of 0.
   --pos;

  if(pos>this->length-1){
    printf("ERROR position bigger than length\n");
    exit(EXIT_FAILURE);
  } else if(pos==this->length){
    character='\0';
    return *character;
  }else if (pos!= this->length){
    return _string[pos];
  }
}