added header printf out and 919class

This commit is contained in:
Haldrup-tech 2024-09-20 11:42:26 -04:00
parent db90a9f122
commit 372c51fcb2
6 changed files with 232 additions and 112 deletions

View File

@ -1,11 +1,15 @@
/* Name: Garrett Haldrup
BMI.c
Problem: Take in the weight, heightm, and gender of a user and calculate and display their BMI, Life Expectancy, and BSA score using multiple methods.
Certification of Authenticity:
I certify that this assignment is entirely my own work.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
void header(int width, int margin, char lineChar, char *headerTitle, char *inputMessage, int meslen);
#include <stdbool.h>
int main(){
@ -18,12 +22,31 @@ int main(){
// Char to store the gender with max length of 7 and a line string for formatting
char gender[7];
char message[] = "This program will calculate your BMI, Life Expectancy, and BSA(multiple methods) with just your weight(kg), height(cm), and gender.";
// declare and set bools to false as default
bool isMale = false;
bool isFemale = false;
bool overWeight = false;
// Display Program Purpose
header(80, 4, '*', "Purpose", message, (int)strlen(message));
printf("***Purpose**********************************************************************\n");
printf("* This program will calculate your BMI, Life Expectancy, and *\n");
printf("* BSA(multiple methods) with just your weight(kg), height(cm), and *\n");
printf("* gender. *\n");
printf("********************************************************************************\n");
// Nice little space
printf("\n");
// Litte overview of the data input section
printf("***Data Input*******************************************************************\n");
printf("* Please enter your weight in kg fmt: XX.X and your height in cm fmt: *\n");
printf("* XXX.X, Male and Female are the only valid options for gender. *\n");
printf("********************************************************************************\n");
// Read in weight, height, and gender from user
// Make sure correct values for weight is entered
printf("Please enter your weight(kg): ");
weightResult = scanf("%f", &weight);
while (weight <= 0 || weightResult != 1) {
@ -37,6 +60,7 @@ int main(){
weightResult = scanf("%f", &weight);
}
// Make sure correct values for weight is entered
printf("Please enter your height(cm): ");
heightResult = scanf("%f", &height);
while (height <= 0 || heightResult != 1) {
@ -60,19 +84,26 @@ int main(){
for (int i = 0; i < (int)strlen(gender); i++) {
gender[i] = tolower(gender[i]);
}
isMale = !strcmp("male", gender);
isFemale = !strcmp("female", gender);
// Check if gender is valid
if (!(strcmp("male", gender)) || !(strcmp("female", gender))) {
if (isMale || isFemale) {
break;
} else {
printf("Bad value: Please enter either male or female\n");
}
}
// nice space
printf("\n");
// Calculate the BMI
bmi = 10000 * (weight / pow(height, 2));
// Assign bool
overWeight = bmi > 25;
// Calculate the BSA(Mosteller's)
mostellar = sqrt((height * weight) / 3600);
@ -84,20 +115,23 @@ int main(){
// 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) {
if (isFemale) {
if (overWeight) {
lifeExpect = 81.4;
} else {
lifeExpect = 83.5;
}
} else {
if (bmi > 25) {
if (overWeight) {
lifeExpect = 77.3;
} else {
lifeExpect = 79.4;
}
}
// Demark output
printf("***Output***********************************************************************\n");
// 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);
@ -109,106 +143,4 @@ int main(){
return 0;
}
void header(int width, int margin, char lineChar, char *headerTitle, char *inputMessage, int meslen) {
int TITLE_MARGIN = 3;
int CONTENT_MARGIN = margin;
int rows = 1;
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] = 0;
strcpy(topLine, line);
// Add header title
for (int i = TITLE_MARGIN; i < ((int)strlen(headerTitle) + TITLE_MARGIN); i++) {
topLine[i] = headerTitle[i - TITLE_MARGIN];
}
topLine[width] = 0;
char *message = (char *)malloc((meslen + 100) * sizeof(char));
if (message == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
strncpy(message, inputMessage, meslen + 100);
while(1) {
if ((contentWidth * rows) > (int)strlen(message)) { break; }
if (message[(contentWidth * rows)] != ' ') {
int i = 1;
int tempCounter = 0;
while (1) {
if (message[(contentWidth * rows) - i] == ' ') {
int start = ((contentWidth * rows) - i) + 1;
char tempMessage[(int)strlen(message)];
int k = 0;
for (int j = 0; j < (int)strlen(message); j++) {
tempMessage[j] = ' ';
}
for (int j = start; j < strlen(message); j++) {
tempMessage[k] = message[j];
tempCounter = k;
k++;
}
for (int j = 0; j < i; j++) {
message[start + j] = ' ';
}
for (int j = 0; j < tempCounter + 2; j++) {
message[(start + i) + j] = tempMessage[j];
if (tempCounter + 1 == j) {
message[(start + i) + j] = 0;
}
}
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 (!(message[j + (i * contentWidth)])) {
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);
free(message);
}

Binary file not shown.

18
csci218/Lect/919class.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
int main() {
int x;
printf("Enter number: ");
scanf("%d", &x);
if (!(11 % x)) {
printf("is multiple\n");
} else {
printf("not multiple\n");
}
return 0;
}

Binary file not shown.

Binary file not shown.

170
csci218/header2.c Normal file
View File

@ -0,0 +1,170 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void header(int width, int margin, char lineChar, char *headerTitle, char *inputMessage, int meslen);
int main() {
char message1[] = "This program will calculate your BMI, Life Expectancy, and BSA(multiple methods) with just your weight(kg), height(cm), and gender.";
char message2[] = "Please enter your weight in kg fmt: XX.X and your height in cm fmt: XXX.X, Male and Female are the only valid options for gender.";
header(80, 4, '*', "Purpose", message1, (int)strlen(message1));
printf("\n");
header(80, 4, '*', "Data Input", message2, (int)strlen(message2));
header(80, 4, '*', "Output", "", 1);
int width, margin, meslen;
char *message, *title, lineChar;
size_t messageSize = 500;
title = (char *)malloc(100 * sizeof(char));
message = (char *)malloc(messageSize * sizeof(char));
while (1) {
printf("Welcome to header maker 2!\n");
printf("Please enter the width as an int: ");
scanf("%d", &width);
printf("Please enter the margin: ");
scanf("%d", &margin);
printf("Please enter the char: ");
while (getchar() != '\n');
scanf("%c", &lineChar);
printf("Please enter the title: ");
while (getchar() != '\n');
scanf("%s", title);
printf("Please enter the message: ");
while (getchar() != '\n');
if (getline(&message, &messageSize, stdin) == -1) {
printf("Error reading message!");
free(message);
return 1;
}
printf("%s\n", message);
message[(int)strlen(message) - 1] = 0;
header(width, margin, lineChar, title, message, (int)strlen(message));
break;
}
return 0;
}
void header(int width, int margin, char lineChar, char *headerTitle, char *inputMessage, int meslen) {
int TITLE_MARGIN = 3;
int CONTENT_MARGIN = margin;
int rows = 1;
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] = 0;
strcpy(topLine, line);
// Add header title
for (int i = TITLE_MARGIN; i < ((int)strlen(headerTitle) + TITLE_MARGIN); i++) {
topLine[i] = headerTitle[i - TITLE_MARGIN];
}
topLine[width] = 0;
char *message = (char *)malloc((meslen + 100) * sizeof(char));
if (message == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
strncpy(message, inputMessage, meslen + 100);
while(1) {
if ((contentWidth * rows) > (int)strlen(message)) { break; }
if (message[(contentWidth * rows)] != ' ') {
int i = 1;
int tempCounter = 0;
while (1) {
if (message[(contentWidth * rows) - i] == ' ') {
int start = ((contentWidth * rows) - i) + 1;
char tempMessage[(int)strlen(message)];
int k = 0;
for (int j = 0; j < (int)strlen(message); j++) {
tempMessage[j] = ' ';
}
for (int j = start; j < strlen(message); j++) {
tempMessage[k] = message[j];
tempCounter = k;
k++;
}
for (int j = 0; j < i; j++) {
message[start + j] = ' ';
}
for (int j = 0; j < tempCounter + 2; j++) {
message[(start + i) + j] = tempMessage[j];
if (tempCounter + 1 == j) {
message[(start + i) + j] = 0;
}
}
break;
}
i++;
}
}
rows++;
}
//printf("%s\n", topLine);
printf("printf(\"%s\\n\");\n", topLine);
for (int i = 0; i < rows; i++) {
int charCount = 0;
printf("printf(\"");
printf("%c", lineChar);
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 (!(message[j + (i * contentWidth)])) {
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("%c", lineChar);
printf("\\n\");\n");
}
// printf("%s\n", line);
printf("printf(\"%s\\n\");\n", line);
free(message);
}