169227 ANSI C (Need By 1st October)

Devam Ediyor İlan edilme: Sep 30, 2007 Teslim sırasında ödenir
Devam Ediyor Teslim sırasında ödenir

Create an ANSI-C program that reads a description of the weight change of an adult person from standard input. The program should be designed to work in an WWW based system that processes data automatically. Since these weight measurements come from different sources, the input can be either in metric units or in imperial units in one of several formats. Due to this, you need to make your program very robust towards various weight specification formats, as long as they comply with the following specification.

The program processes the standard input line by line where each line is a sequences of characters separated by line feed characters '\n'. Each line contains one record, and weight change records cannot span multiple lines. Each record consists of a user identifier followed by a non-empty sequence of whitespace characters, the start weight, a non-empty sequence of whitespace characters, the end weight, a non-empty sequence of whitespace characters, and finally the number of days. Whitespace characters are defined in the context of this specification to be spaces, tabs or '\0' characters. No additional whitespace may occur before or after the weight record on the line. The user identifier is a single letter in the range 'A-Z'. The number of days during which the weight change occured is a non-negative integer number. The weight at the start and the end of the specified time period can be in any of the following formats, all of which must be accepted by your program - but only these! Weight format Examples Unit

#{#}kg 80kg, 120kg kilogram

#{#}lb 120lb, 200lb pound (avoirdupois)

#{#}st[#{#}] 15st, 11st4 stone (imperial) [and pounds]

The symbol # above signifies a decimal digit, which is one of the characters in the set ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']. A symbol inside curly braces { X } signifies zero or more repetitions of X. Square brackets signify optional parts, i.e. something that may be present or not. All other literals represent themselves.

You may assume that each line consists of exactly one weight change record. You can also assume that every input line ends with a newline character (\n). [ADDED Sept 18] You may also assume that the weight change record is not empty, i.e. there will not be any empty lines.

The program needs to read and process each input line until it encounters the end of the input (more precisely the end of file), at which point the program must terminate. If the line contains a valid and logically meaningful weight change record, the program computes the average weight gain or weight loss per day. It must then output first the user identifier followed by a single space and then the average weight gain or loss expressed in kilograms per day. The average weight change must be rounded to the nearest integer, using the definition of common rounding. More precisely, the weight change output must be in canonical integer format, with an optional single minus character before it to signify weight loss. No whitespace can appear before or after and the number must be followed by a single newline character:

[-]#{#}

The number must not contain any leading zeros. No unit, i.e. "kg/day" or "kilogram", should be appended.

For the purposes of this assignment, the definitions of human weight in Wikipedia are the authorative source. The conversion between pounds and kilograms is specified on the Wikipedia page on pounds. A "stone" is defined to be 14 pounds, as specified on the respective Wikipedia page.

If the input is either invalid or ambiguous, the program must handle these cases in the manner specified as follows:

If the format of an input line does not fit the above specification, i.e. there are syntactic errors, the program must output the following string followed by a newline character for this input line:

invalid syntax

Examples include lines that have only text on them, a user identifier that is longer than a single character or is not a capital letter, illegal weights ("-3kg", "a ton"), illegal number of days ("weekend"), missing elements on the line (e.g. missing weight, missing number of days, etc.), etc.

If any of the integer numbers on the weight change record is too large to fit into an 32-bit unsigned int in C, you must output the following string followed by a newline character for this input line:

overflow

If one of the weight specifications is syntactically valid, but falls outside of the possible range for humans (e.g. "100000kg"), you must output the following string followed by a newline character for this input line:

impossible weight

For the purposes of this assignment, the possible range of human weight for an adult person is defined to be between 30 and 300kg [CLARIFICATION Sept 18] (inclusive).

If the number of days is zero or too large for an adult's lifespan (here defined to be larger than 50000 days), you must output the following string followed by a newline character for this input line:

invalid time span

If the weight change is too large to be believeable, you must output the following string followed by a newline character for this input line:

questionable weight change

For the purposes of this assignment, any change equal to or above 10 kilograms per day (before rounding) is questionable.

No other output must be produced.

You must check the cases in the sequence specified above. That means e.g. a number that is too large to fit into an unsigned integer must produce "overflow" not "impossible weight".

Your C program must adhere to the following specifications:

The program must be readable and commented at an appropriate level i.e. at least one comment per functional block but not line by line. Unreadable code will be penalized by mark reductions.

You must introduce appropriate functions to deal with the parts of the functionality. If your code consists of overly complex functions, mark deductions will be applied (especially if you have only a single function that does everything). Marks will be deducted if your functions are too complex.

The program must list your CS login, your York student ID, and the first and the last name of the author (in this sequence) in a comment starting in the first line of the file.

You may only use the getchar(), putchar(), and printf() functions, plus any code you write yourself. You may not use any other library functions in your program, except the string functions mentioned on the man page of string(3). Note that this means that you cannot use scanf(), atoi()/atol(), or strtol() (all of which don't deal with overflow adequately anyways).

The program must compile and run without errors on the CS PRISM Unix system with the standard C compiler (cc). Note: Evidence that a program runs on a student's machine will not be taken into consideration.

Hint: MS-DOS and Windows encode a newline differently (\r\n) than UNIX (only \n). Make sure that your program can deal correctly with the UNIX convention! Also make sure that your local copy of the example input/output files given below doesn't contain \r characters and that you do not output \r characters!

Submission

There are 2 submissions deadlines.

Before the first deadline, you must submit the C file via the submit command from your PRISM account. The file MUST be called a1.c and be submitted with the command:

submit 2031 a1 a1.c

Please note your submission will be automatically tested by a testing program against many test cases. You don't know anything about these test cases before the first deadline, except that they will be based on the specification mentioned above. Thus, you need to create a substantial set of your own test cases, again based on the specification above, to test your program before submitting it. Shortly after the first deadline, you will receive an email to inform you how many test cases your program passed. Then, all of these test cases will be posted in the course website for you. If your program passed all of these test cases, you have finished this assignment and will receive full marks for this assignment. However, if your program failed any of these test cases, you will have to continue to debug your program to make another submission before the 2nd deadline. This time, you are allowed to use all of these test cases to help yourself to fix problems in your program.

Before the second deadline, you must submit your fixed C program file via the submit command with different arguments! Again the source file MUST be called a1.c and be submitted with the command:

submit 2031 a1R a1.c

Make sure your program passes all of the posted test cases before submitting it. Besides, you must submit a short report (in plain text), named [url removed, login to view], via the command:

submit 2031 a1R [url removed, login to view]

The first lines of [url removed, login to view] must use the following template (please complete):

# Family Name:

# Given Name:

# Section:

# Student Number:

# CS Login:

In your report, please give a list of bugs you have found, briefly explain how you corrected them and the reason for the change. Here are a couple of good ways to report corrections: "changed line #### in original a1.c from XXXX to YYYY", "added XXXX before line #### in original a1.c", "deleted line #### in original a1.c". Here are a couple of good, succinct ways to report reasons: "fixed syntax error, which caused compilation to fail" "removed extra character in printf which caused all tests to fail", "wrong variable being updated, which caused tests ## - ## to fail".

Please note that we evaluate your debugging skill in this stage, you are not allowed to write another program, or to re-write your program completely. If you submit a very different program, your submission will be treated as an attempt at cheating! A TA will read and compare your two submissions according to your report.

You must meet all the above guidelines for handing in your assignment. E.g. if you name your source file wrong, you may loose all marks. Please note that TA's and professors won't answer questions related to this assignment on the day the assignment is due. To login remotely to PRISM, e.g. to test your program on the PRISM platform before you submit it, please use ssh.

Sample Input

A 75kg 80kg 5

X 180kg 130kg 9

D 700lb 600lb 10

XY 80kg 99999lb 100

G 120kg 264lb 900

Sample Output

A 1

X -6

impossible weight

invalid syntax

G 0

For convenience, here are these two files: [url removed, login to view], a1.out. To verify that your program works correctly with these two files use the following command sequence:

cc -o a1 a1.c

a1 <[url removed, login to view] >output

diff output [url removed, login to view]

If the compile fails, does not terminate, or if there is any output from the diff command, your program has failed this most basic test. If you download the above sample input/output files to Windows systems, please make sure that that your local copy has only \n at the end of each line.

C Programlama Odd Jobs

Proje NO: #1915425

Proje hakkında

Uzak proje Aktif Jul 11, 2012