C++编程题,求大神指教,实在是小白不会。已知速度和时间和加速度。框架也有了,但是真的不太会

2025-05-22 09:36:32
推荐回答(1个)
回答(1):

Firstly, you need bulid up two mathematical equations to calculate the velocity and the acceleration.

double calculate_velocity( double& t ) {
return 1e-5*t*t*t - 0.00488*t*t + 0.75795*t + 181.3566;
}

double calculate_acceleration( double& v ) {
return 3.0 - 0.000062*v*v;
}

Secondly, you need to pay attention to the decmial point of your calculated result, which need to fix the length of double variable by

printf( "Velocity = %.3f m/s\n", velocity );
printf( "Acceleration = %.3f m/s^2\n", acceleration );

If you fill in these two pieces of codes inside the original programme, the sample should looks like:

#include 
#include 

double calculate_velocity( double& t ) {
return 1e-5*t*t*t - 0.00488*t*t + 0.75795*t + 181.3566;
}

double calculate_acceleration( double& v ) {
return 3.0 - 0.000062*v*v;
}

int main( void ) {
// Declare variables
double time;
double velocity;
double acceleration;

// Get time value from the keyboard
printf( "Enter new time value in seconds:\n");
scanf( "%lf", &time );

// Compute velocity and acceleration
velocity = calculate_velocity( time );
acceleration = calculate_acceleration( velocity );

// Print velocity and acceleration
printf( "Velocity = %.3f m/s\n", velocity );
printf( "Acceleration = %.3f m/s^2\n", acceleration );

// Exit the program
return 0;
}

The generated result will look exactly as you expected as