Classes/csci218/Assignments/BMI.c
2024-09-19 09:07:30 -04:00

194 lines
5.3 KiB
C

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
void header(int width, int margin, char lineChar, char headerTitle[50], char inputMessage[500]);
int main(){
// Declare Variables
// Float values for calculations
float bmi, weight, height, mostellar, duBois, boyd, lifeExpect;
// Int values to store the result of the scanf fucntion
int weightResult, heightResult;
// Char to store the gender with max length of 7 and a line string for formatting
char gender[7];
// Display Program Purpose
header(80, 4, '*', "Purpose", "This program will calculate your BMI, Life Expectancy, and BSA(multiple methods) with just your weight(kg), height(cm), and gender.");
// Read in weight, height, and gender from user
printf("Please enter your weight(kg): ");
weightResult = scanf("%f", &weight);
while (weight <= 0 || weightResult != 1) {
while (getchar() != '\n');
if (weight <= 0) {
printf("Bad Value: Weight must greater than 0! fmt: XX.X\n");
} else {
printf("Bad Value: Please enter a decimal input! fmt: XX.X\n");
}
printf("Please re-enter your weight(kg): ");
weightResult = scanf("%f", &weight);
}
printf("Please enter your height(cm): ");
heightResult = scanf("%f", &height);
while (height <= 0 || heightResult != 1) {
while (getchar() != '\n');
if (height <= 0) {
printf("Bad Value: Height must greater than 0! fmt: XX.X\n");
} else {
printf("Bad Value: Please enter a decimal input! fmt: XX.X\n");
}
printf("Please re-enter your height(cm): ");
heightResult = scanf("%f", &height);
}
// Make sure correct values for gender are entered
while (1) {
// Read in gender from user
printf("Please enter your gender(male or female): ");
scanf("%s", gender);
// Loop through all chars in gender and convert to lower case
for (int i = 0; i < strlen(gender); i++) {
gender[i] = tolower(gender[i]);
}
// Check if gender is valid
if (!(strcmp("male", gender)) || !(strcmp("female", gender))) {
break;
} else {
printf("Bad value: Please enter either male or female\n");
}
}
// Calculate the BMI
bmi = 10000 * (weight / pow(height, 2));
// Calculate the BSA(Mosteller's)
mostellar = sqrt((height * weight) / 3600);
// Calculate the BSA(Dubois)
duBois = (0.20247 * pow((height / 100), 0.725)) * pow(weight, 0.425);
// Calculate the BSA(boyd)
boyd = (0.0003207 * pow(height, 0.3)) * pow((1000 * weight), (0.6721 - (0.0188 * log10(weight))));
// Check if overweight and gender to select correct life expectancy message
// Only need to check one since the input screens to just two options
if (strcmp("male", gender)) {
if (bmi > 25) {
lifeExpect = 81.4;
} else {
lifeExpect = 83.5;
}
} else {
if (bmi > 25) {
lifeExpect = 77.3;
} else {
lifeExpect = 79.4;
}
}
// Display values and output
printf("For a %s with weight(kg) of %.1fkg and height(cm) of %.1fcm\n", gender, weight, height);
printf("You have a BMI of %.1f and a life expectancy of %.1f years\n", bmi, lifeExpect);
printf("You also have a BSA calculated using\n");
printf(" - Mosteller's:\t%.3f\n", mostellar);
printf(" - DuBois:\t\t%.3f\n", duBois);
printf(" - Boyd:\t\t%.3f\n", boyd);
return 0;
}
void header(int width, int margin, char lineChar, char headerTitle[50], char inputMessage[500]) {
int TITLE_MARGIN = 3;
int CONTENT_MARGIN = margin;
int rows = 1;
char message[500];
strcpy(message, inputMessage);
char line[width + 1];
char topLine[width + 1];
int contentWidth = (width - 2 * CONTENT_MARGIN) - 2;
// Create Line
for (int i = 0; i < width; i++) {
line[i] = lineChar;
}
line[width + 1] = '\0';
strcpy(topLine, line);
// Add header title
for (int i = TITLE_MARGIN; i < (strlen(headerTitle) + TITLE_MARGIN); i++) {
topLine[i] = headerTitle[i - TITLE_MARGIN];
}
topLine[width + 1] = '\0';
int rowCheck = 1;
char tempMessage[500];
while(1) {
if (message[(contentWidth * rows)] == '\0') { break; }
if (message[(contentWidth * rows)] != ' ') {
int i = 1;
while (1) {
if (message[(contentWidth * rows) - i] == ' ') {
int start = ((contentWidth * rows) - i) + 1;
int k = 0;
for (int j = start; j < strlen(message); j++) {
tempMessage[k] = message[j];
k++;
}
for (int j = 0; j < i; j++) {
message[start + j] = ' ';
}
for (int j = 0; j < strlen(tempMessage); j++) {
message[(contentWidth * rows) + 1 + j] = tempMessage[j];
}
break;
}
i++;
}
}
rows++;
}
printf("%s\n", topLine);
for (int i = 0; i < rows; i++) {
int charCount = 0;
printf("*");
for (int j = 0; j < CONTENT_MARGIN; j++) {
printf(" ");
}
for (int j = 0; j < contentWidth; j++) {
if (charCount == 0 && (message[j + (i * contentWidth)] == ' ')) {j++;}
if (j + (i * contentWidth) == (int)strlen(message)) {
break;
}
printf("%c", message[j + (i * contentWidth)]);
charCount++;
}
for (int j = 0; j < contentWidth - charCount; j++) {
printf(" ");
}
for (int j = 0; j < CONTENT_MARGIN; j++) {
printf(" ");
}
printf("*\n");
}
printf("%s\n", line);
}