171 lines
4.2 KiB
C
171 lines
4.2 KiB
C
#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);
|
|
}
|