testsAndMisc-archive/C/1dvelocitysimulator/main.c

189 lines
4.4 KiB
C

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#define SLEEP_MS(ms) Sleep(ms)
#define CLEAR_SCREEN() system("CLS")
#define PAUSE() system("PAUSE")
#else
#include <unistd.h>
#define SLEEP_MS(ms) usleep((ms) * 1000U)
#define CLEAR_SCREEN() system("clear")
#define PAUSE() \
do \
{ \
printf("Press Enter to continue..."); \
getchar(); \
} while (0)
#endif
#define LINE_LENGTH 100
void C()
{
printf("\nCheck\n");
return;
}
void printAcceleration(int acceleration)
{
printf("The value of acceleration is: %d\n", acceleration);
PAUSE();
return;
}
void pauseSystem() { PAUSE(); }
void clearScreen()
{
CLEAR_SCREEN();
return;
}
void pauseForASecond()
{
SLEEP_MS(1000);
return;
}
void pauseForGivenTime(float given_time)
{
SLEEP_MS((unsigned int)fabs(given_time * 1000));
return;
}
float calculateVelocity(float starting_velocity, unsigned int physics_time, int *acceleration)
{
// cppcheck-suppress nullPointer
return (*acceleration) * physics_time + starting_velocity;
}
int calculateDisplacement(float starting_velocity, int *acceleration, unsigned int physics_time)
{
// cppcheck-suppress nullPointer
// cppcheck-suppress ctunullpointer
return starting_velocity * physics_time + ((1 / 2) * (*acceleration) * (physics_time ^ 2));
}
void printXPosition(int position)
{
printf("\nx position is: %d\n", position);
return;
}
void printClock(unsigned int *time)
{
printf("%u seconds passed\n", *time);
return;
}
float calculateStopTime(float velocity) { return 1 / velocity; }
void printLine(int position)
{
clearScreen();
for (int i = -(LINE_LENGTH / 2); i < LINE_LENGTH / 2; i++)
{
if (i == position)
printf("x");
else
printf("-");
}
return;
}
void printVelocity(float velocity)
{
printf("Velocity is: %f\n", velocity);
return;
}
int calculateTimePassed(float velocity)
{
if (velocity >= 1 || velocity <= -1)
return 1;
else
{
printf("Time passed is: %f\n", fabs(1 / velocity));
return fabs(1 / velocity);
}
}
void printAllInfo(int position, unsigned int *time, float *velocity)
{
pauseForGivenTime(calculateStopTime(*velocity));
printLine(position);
printXPosition(position);
*time += calculateTimePassed(*velocity);
printClock(time);
printVelocity(*velocity);
// pauseForASecond();
return;
}
float chooseVelocity()
{
float velocity;
printf("Write velocity of the object in m / s: ");
scanf("%f", &velocity);
return velocity;
}
int chooseAcceleration()
{
int acceleration;
printf("Choose acceleration of the object in m / (s ^ 2):");
scanf("%d", &acceleration);
return acceleration;
}
int outOfLine(int position)
{
if ((position < LINE_LENGTH / 2) && (position > -1 * (LINE_LENGTH / 2)))
{
return 0;
}
else
return 1;
}
void moveUntillOutOfLine(int position, unsigned int *time)
{
while (!outOfLine(position))
{
float velocity = chooseVelocity();
float *Pvelocity = &velocity;
position += calculateDisplacement(velocity, 0, 1);
printAllInfo(position, time, Pvelocity);
}
return;
}
void moveUntillOutOfVelocity(int position, int *acceleration, unsigned int *time)
{
float velocity = 0;
float *Pvelocity = &velocity;
while (!outOfLine(position))
{
position += calculateDisplacement(velocity, acceleration, 1);
printXPosition(position);
pauseSystem();
velocity = calculateVelocity(velocity, 1, acceleration);
printAllInfo(position, time, Pvelocity);
}
return;
}
int main()
{
int position = 0, acceleration = -1;
int *Pacceleration = &acceleration;
unsigned int time = 0;
unsigned int *Ptime = &time;
moveUntillOutOfLine(position, Ptime);
// moveUntillOutOfVelocity(position, Pacceleration, Ptime);
return 0;
}