The following sections describe the required output formats for the programs in problems 3, 4, and the extra credit of homework 5.
Here are the formats for the outputs and error messages.
Problem 3 Outputs
Here is the format of the output to be written to standard output (stdout):
printf("Chemical composition? ");If everything works correctly, the exit status code is 0 and the output is:
printf("The atomic weight of %s is %.02f\n", molecule, weight);where molecule is the formula of the molecule (that is, the input) and weight is the atomic weight of the molecule. Note you need 2 decimal places, even if the weight is a whole number.
Problem 3 Error Outputs
Here are the formats of the error messages to be written to standard error (stderr). All have an exit status code of 0 unless otherwise noted.
fprintf(stderr, "%s: malformed line %d\n", filename, lineno);where filename is the name of the file and lineno is the number of the line that is mangled. Then exit with an exit status code of 1.
sprintf(buf, "%s(%d):", filename, lineno); perror(buf);where filename is the name of the file and lineno is the number of the line that is mangled. Note that you are to use the formatted string as the argument to the function perror, which will print it followed by the system error message. Then exit with an exit status code of 1.
fprintf(stderr, "%s: no such element\n", atom);where atom is the symbol that is not in the list. Then print:
fprintf(stderr, "%s: not a valid compound\n", molecule);where molecule is the formula of the molecule (that is, the input). Do not exit; prompt the user for the next input.
fprintf(stderr, "Usage: %s weightsfile\n", programname);where programname is the name of the program. Then return an exit status code of 1.
perror(filename)where filename is the name of the file. Then return an exit status code of 1.
fprintf(stderr, "%s: no atomic weights there!\n", filename);where filename is the name of the file. Then return an exit status code of 1.
All output and error messages are as in the file with the bug, getbit.c. None of them are involved with any bugs. Please do not change them.
The outputs and error messages for the extra credit are the same as for problem 3, with the following additional output to include the full names of the elements.
If there is one element, the output is to be:
The element is fullname\nwhere fullname is the full name of the element.
If there are two elements, the output is to be:
The elements are fullnameA and fullnameB\nwhere fullnameA and fullnameB are the full names of the elements.
If there are more than two elements, the output is to be:
The elements are fullnameA, fullnameB and fullnameC\nwhere fullnameA is a comma-separated list of all the full names of the elements except the last two, and fullnameB and fullnameC are the full names of the last two elements. Note there is no comma before the “and”.
|
ECS 36A, Programming and Problem Solving Version of November 28, 2019 at 11:51PM
|
You can also obtain a PDF version of this. |