Просмотр одиночного сообщения
Old 17-01-2006, 16:09   #18
zuber
[Mon]
 
Сообщений: 10,259
Проживание: virtual
Регистрация: 06-02-2004
Status: Offline
Репутация: 42
Talking


[pre]/* U popa byla sobaka
* created by Zuber
*/

#include <stdlib.h>
#include <stdio.h>

#define BOY_SPEED 7.0
#define GIRL_SPEED 5.0
#define DOG_SPEED 11.0
// step is one second
#define TIMER_STEP 1
// 2 hours
#define TIME_UNITS 60*60
#define MAX_TIME 2 * TIME_UNITS

#define TO_BOY 1
#define TO_GIRL 2

int main(void){

int timer;
double boy_pos = 0.0;
double girl_pos = 0.0;
double dog_pos = 0.0;
int dog_runs_to = TO_GIRL;

double tmp_distance;
double distance_to_run;
double part_distance_to_run;

// report counters
int total_oscillations;
double total_trek = 0.0;


for(timer = 0; timer < MAX_TIME; timer ++){
if( boy_pos != girl_pos ){ // only if not at the same place

// NOTE, girl_pos is always less then a boy pos due to speed diff
tmp_distance = boy_pos - girl_pos;

// NOTE: since we have a somewhat descreet values, there could be small glitches in the beginning,
// when dog is actually catching up with the girl
distance_to_run = DOG_SPEED/((double)TIME_UNITS);
total_trek = total_trek + distance_to_run;

while(distance_to_run != 0){
switch(dog_runs_to){
case TO_BOY:
part_distance_to_run = boy_pos - dog_pos;
if( part_distance_to_run < 0){
printf("BUG! Dog is infront! \n");
exit(0);
}else if( part_distance_to_run == 0 ){
// same position, dog reverses
dog_runs_to = TO_GIRL;
total_oscillations++;
}else{
if(distance_to_run <= part_distance_to_run){
dog_pos = dog_pos + distance_to_run;
distance_to_run = 0.0;
}else{
dog_pos = boy_pos;
distance_to_run = distance_to_run - part_distance_to_run;
}
} // end of if(part_distance_to_run)
break; // TO_BOY
case TO_GIRL:

part_distance_to_run = dog_pos - girl_pos;
if( part_distance_to_run < 0){
// starting feature... have to catch up
dog_pos = girl_pos;
distance_to_run = distance_to_run - part_distance_to_run;
break;
}else if( part_distance_to_run == 0 ){
// same position, dog reverses
dog_runs_to = TO_BOY;
total_oscillations++;
}else{
if(distance_to_run <= part_distance_to_run){
dog_pos = dog_pos - distance_to_run;
distance_to_run = 0.0;
}else{
dog_pos = girl_pos;
distance_to_run = distance_to_run - part_distance_to_run;
}
} // end of if(part_distance_to_run)
break; // TO_GIRL
default:
printf("BUG! Nowhere to RUN! \n");
exit(0);
}
}
} // end if boy != girl

boy_pos = boy_pos + BOY_SPEED/((double)TIME_UNITS);
girl_pos = girl_pos + GIRL_SPEED/((double)TIME_UNITS);
} // end timer loop

// PRINTING RESULTS
printf("Resulting positions (in km)\n");
printf("Boy: %2.3f\n", boy_pos);
printf("Girl: %2.3f\n", girl_pos);
printf("Dog: %2.3f\n", dog_pos);
printf("Total DOG travel: %2.3f\n",total_trek);
printf("Total oscillations: %d\n", total_oscillations);

return 0;
}
[/pre]

-----------------
-=*=-
 
0
 
0
    Ответить с цитированием