Skip to content

Instantly share code, notes, and snippets.

@dhhdev
Last active December 18, 2017 08:19
Show Gist options
  • Save dhhdev/ccbee612fff8a2a97bd34e3223f66fa2 to your computer and use it in GitHub Desktop.
Save dhhdev/ccbee612fff8a2a97bd34e3223f66fa2 to your computer and use it in GitHub Desktop.
Calculating moment, using (Q) kg * (D) m = (M) kgm.
#include <stdio.h>
#include <stdlib.h>
typedef enum {FALSE, TRUE} BOOLEAN;
void usage(char *program)
{
printf("Usage:\n");
printf("\tExample: %s 0 50 0.6\n\n", program);
printf("First argument is moment. (kgm)\n");
printf("Second argument is weight. (kg)\n");
printf("Third argument is arm. (m)\n\n");
}
/*
* moment = weight * arm
* weight = moment / arm
* arm = moment / weight
*/
BOOLEAN calculate(double *moment, double *weight, double *arm)
{
if (*moment <= 0 && *weight > 0 && *arm > 0) {
printf("Calculating moment...\n");
*moment = *weight * *arm;
} else if (*moment > 0 && *weight <= 0 && *arm > 0) {
printf("Calculating weight...\n");
*weight = *moment / *arm;
} else if (*moment > 0 && *arm <= 0 && *weight > 0) {
printf("Calculating arm...\n");
*arm = *moment / *weight;
} else {
return FALSE;
}
return TRUE;
}
/*
* EUR-1 pallets weighs between 22-25 kg depending on if the
* pallet is dry or wet.
*
* An EUR-1 pallet is 800mm x 1200mm.
* When taken from the short side, the balance point becomes
* 1200mm / 2, if load is evenly placed or centered.
*
* So let us say the load is 75kg, including the pallet - of course.
* It would be 75.0 (kg) * 0.6 (m) = moment in kgm.
*/
int main(int argc, char *argv[])
{
double moment, weight, arm;
BOOLEAN success;
if (argc != 4) {
usage(argv[0]);
printf("Printing arguments:\n\n");
printf("argv[0] = %s\n", argv[0]);
printf("argv[1] (Moment) = %s\n", argv[1]);
printf("argv[2] (Weight) = %s\n", argv[2]);
printf("argv[3] (Arm) = %s\n", argv[3]);
return EXIT_FAILURE;
}
moment = atof(argv[1]);
weight = atof(argv[2]);
arm = atof(argv[3]);
success = calculate(&moment, &weight, &arm);
if (success) {
printf("Calculation is done...\n\n");
printf("Moment:\t %.2f kgm.\n", moment);
printf("Weight:\t %.2f kg.\n", weight);
printf(" Arm:\t %.2f m.\n", arm);
return EXIT_SUCCESS;
} else {
printf("Something went wrong...\n");
printf("You need to at least know 2 of the 3 values.\n\n");
printf("Moment:\t %.2f kgm.\n", moment);
printf("Weight:\t %.2f kg.\n", weight);
printf(" Arm:\t %.2f m.\n", arm);
usage(argv[0]);
return EXIT_FAILURE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment