diff --git a/csci218/Assignments/BMI.c b/csci218/Assignments/BMI.c index bf84009..a33b074 100644 --- a/csci218/Assignments/BMI.c +++ b/csci218/Assignments/BMI.c @@ -3,6 +3,8 @@ #include #include +void header(int width, int margin, char lineChar, char headerTitle[50], char inputMessage[500]); + int main(){ // Declare Variables @@ -14,13 +16,9 @@ int main(){ // Char to store the gender with max length of 7 and a line string for formatting char gender[7]; - char line[] = "********************************"; // Display Program Purpose - printf("%s%s%s*\n", line, line, line); - printf("*\tThis program will calculate your BMI, Life Expectancy, and BSA(mulitple methods)\t*\n"); - printf("*\twith just your weight(kg), height(cm), and gender.\t\t\t\t\t*\n"); - printf("%s%s%s*\n", line, line, line); + 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): "); @@ -107,3 +105,89 @@ int main(){ 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); +} diff --git a/csci218/Assignments/bmi.out b/csci218/Assignments/bmi.out index fff20ca..c24325d 100755 Binary files a/csci218/Assignments/bmi.out and b/csci218/Assignments/bmi.out differ diff --git a/csci218/a.out b/csci218/a.out new file mode 100755 index 0000000..90e229d Binary files /dev/null and b/csci218/a.out differ diff --git a/csci218/header.c b/csci218/header.c new file mode 100644 index 0000000..a0f5a12 --- /dev/null +++ b/csci218/header.c @@ -0,0 +1,110 @@ +#include +#include + +void header(int width, int margin, char lineChar, char *headerTitle, char *message); + +int main() { + char message[] = "This program will calculate your BMI, Life Expectancy, and BSA(multiple methods) with just your weight(kg), height(cm), and gender."; + + printf("%d\n", (int)strlen(message)); + header(80, 4, '*', "Purpose", message); + + return 0; +} + +void header(int width, int margin, char lineChar, char *headerTitle, char *message) { + 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 + 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'; + + printf("%d\n", (int)strlen(message)); + + int rowCheck = 1; + + while(1) { + printf("%d > %d = %d\n", contentWidth * rows, (int)strlen(message), (contentWidth * rows) > (int)strlen(message)); + if ((contentWidth * rows) > (int)strlen(message)) { break; } + if (message[(contentWidth * rows)] != ' ') { + int i = 1; + while (1) { + printf("previous char = %c\n", message[(contentWidth * rows) - i]); + if (message[(contentWidth * rows) - i] == ' ') { + int start = ((contentWidth * rows) - i) + 1; + char tempMessage[(int)strlen(message)]; + printf("check char = %c\n", message[contentWidth * rows]); + printf("start = %d\n", start); + printf("char after start = %c\n", message[start]); + int k = 0; + for (int j = 0; j < (int)strlen(tempMessage); j++) { + tempMessage[j] = ' '; + } + 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 < ((int)strlen(message)) - start - 1; j++) { + message[(start + i) + j] = tempMessage[j]; + } + break; + } + i++; + } + } + rows++; + } + + printf("%s\n", tempMessage); + printf("%s\n",message); + + 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); +}