#include #include #define TEXT_WIDTH 20 /* define the text width for the printing */ #define VALUE_WIDTH 13 /* define the value width for the printing */ /* Define a element_t with all the needed data */ struct element_t{ int atomic; char *name; char *symbol; char *class; double weight; int electrons[7]; }; /* function prototypes */ struct element_t scan_element(); void print_element(struct element_t e); /* The main method, creates a new element, asks for user input and prints it */ int main(int argc, char **argv){ /* declare a new element sodium */ struct element_t sodium; /* initialize the sodium element with user input */ sodium = scan_element(); /* print the element sodium */ print_element(sodium); /* exit the program with return value 0 */ return 0; } /* The scan_element method, asks the user for input */ struct element_t scan_element(){ /* declare a new element */ struct element_t e; /* declare and allocate some memory for the values */ e.name = malloc(10*sizeof(char)); e.symbol = malloc(3*sizeof(char)); e.class = malloc(15*sizeof(char)); /* ask the user for the values */ printf("Please enter the element properties: \n"); scanf("%d %s %s %s %lf %d %d %d %d %d %d %d", &e.atomic, e.name, e.symbol, e.class, &e.weight, &e.electrons[0], &e.electrons[1], &e.electrons[2], &e.electrons[3], &e.electrons[4], &e.electrons[5], &e.electrons[6] ); /* return the element */ return e; } /* The print_element method, prints all of the values for the given element */ void print_element(struct element_t e){ /* print the values */ printf("%-*s: %*d\n", TEXT_WIDTH, "Atomic number", VALUE_WIDTH, e.atomic); printf("%-*s: %*s\n", TEXT_WIDTH, "Name", VALUE_WIDTH, e.name); printf("%-*s: %*s\n", TEXT_WIDTH, "Symbol", VALUE_WIDTH, e.symbol); printf("%-*s: %*s\n", TEXT_WIDTH, "Class", VALUE_WIDTH, e.class); printf("%-*s: %*f\n", TEXT_WIDTH, "Weight", VALUE_WIDTH, e.weight); printf("%-*s: %d %d %d %d %d %d %d\n", TEXT_WIDTH, "Electrons", e.electrons[0], e.electrons[1], e.electrons[2], e.electrons[3], e.electrons[4], e.electrons[5], e.electrons[6]); }