BEST代写-线上编程学术专家

Best代写-最专业靠谱代写IT | CS | 留学生作业 | 编程代写Java | Python |C/C++ | PHP | Matlab | Assignment Project Homework代写

C语言代写 | CSE 5A: Programming Assignment 4

C语言代写 | CSE 5A: Programming Assignment 4

这个作业是用C语言完成处理文本数据的命令行程序

CSE 5A: Programming Assignment 4

4.1 Part 1
In Part1 (pa4a.c), your program will read input from the command line arguments and return the output according to the directions specified below. The main part of your program would be to output the characters of the sentence alphabetically and also list out the unique letters from the sentence entered by the user.
● Make sure your prompt and output messages exactly match the sample prompts and messages word-for-word.
● No hardcoding or magic numbers in your code.
● You must use all the functions definitions provided at the top of the file
○ You can add extra helper functions if you want/need to
● Keep your function bodies (inside the { }) as small as possible
○ Good rule of thumb: each function should perform one task
○ Avoid duplicate code, use extra methods.
Entering Command Line Arguments:
In this part of the assignment, we will be passing a name and a sentence as command line arguments. This means when we run the program, rather than typing:
pa4a (for Windows)
./pa4a (for Mac)

We will add the “name” and the “sentence” at the end of the run command:
pa4a Greg “programming assignment” (for Windows)
./pa4a Greg “programming assignment” (for Mac)

So in this case, “Greg” would be used as the name and “programming assignment” as the sentence, which would be further changed by the user by alphabetizing it, etc.

Processing Command Line Arguments:
Now that we know how to enter command line arguments as the user, we need to learn how to process these command line arguments in our code. In C, the command line arguments are automatically passed as an array of char* to main():

void main(int argc, char *argv[]) {…}
or
int main(int argc, char *argv[]) {…}

For example:
command: pa4a Greg “programming assignment”
argc: 3

argv[0] = “pa4a” argv[1] = “Greg” argv[2] = “programming assignment”

4.1.1 Getting Started
Follow the give instructions on details on how to finish the sections you need to complete

1) Declare any pre-processor directives and constants that you will need at the top of the program (under the file header). No magic numbers allowed other than 1, -1, or 0. You will also need to include the following Std Library header files for the various library functions you will use:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

You must also define any constants as such:
#define ALPHABET_LENGTH 26

2) Add the following function prototypes to your code before main()

//Function Prototypes
void printErrorMessage(char* message);
char* allLowerCase(char* originalSentence, int sentenceLength);
char* alphabetizeSentence(char* sentence, char* array);
char* uniqueLetters(char* alphabetizedSentence, char* array, int sentenceLength);

int* sizeCurrentWord(char* sentence, int index);
void copyWord(char* sentence, char* holder, int* pIndex);
void traverseAlphabet(char* array, char* holder, int* pIndex);

3) Create your main() function with a return type of int which takes two parameters. You must use this version of main() to return error codes for invalid command line parameters.

int main(int argc, char* argv[])

4) Create a function called printErrorMessage() with a return type of void and one parameter: a pointer to a char. The parameter should be an error message stating the cause of error. It should print out the error message passed in as a parameter as well as a usage statement. The usage statement should be:
“Usage: pa4a [name] [sentence]\n”

5) Before you can use any of the command line arguments, you need to verify that they were entered properly. Other than the name of the program, you should only have two additional arguments: name and a sentence that the user wants to transform.

● In the main() function, check to see if the proper number of arguments have been entered. Keep in mind that if no arguments have been entered, argc has a value of 1. If the number of arguments is invalid, print out the usage statement and error message by calling the printErrorMessage() function with the appropriate character string pointing out the cause of error. The message should be “Wrong number of arguments”.
● After printing out an error message, return -1 to indicate that an error occurred in the program.

6) After checking for the correct number of arguments, you also need to check if the first letter of the name entered by the user is a capital letter or not. If it’s not the case, then we should print out an error message by calling the printErrorMessage() function with an appropriate character string pointing out the cause of error. The message, in this case, should be “Your name must begin with a capital letter”. Similar to the last step, after printing out an error message, exit the program using return -1.

7) After checking if the arguments are valid, use a printf statement to print out “Hello” and the “Name” entered on the command line followed by an exclamation mark (!). Similarly, on the next line, print out the “sentence” that was also entered on the command line. Take a look at the sample outputs below to make sure that your output matches the expected output.

● As a reminder, the user would run the program by using: hw4a “Name” “sentence”. You need to extract the “Name” and the “sentence” from here.

8) The next task is to print out the sentence entered by the user in lowercase format by calling the allLowerCase() function.The function definition is, further, defined below. Again, make sure to check your output with the sample outputs when printing out the return value of this function.

Create a function called allLowerCase() with a char* return value. This function takes in two parameters: pointer to a char (which is the string that needs to be transformed into lower case format) and an integer value (which is the length of the word entered by the user). This converts every letter of the string into lowercase format before returning it.

● You will most likely want to use the Std C Library function: tolower() and strlen() at some point.
● For your reference, you need to terminate any new string with a ‘\0’ .

9) Back in main(), you need to call the alphabetizeWord() function to alphabetize each word separately from the sentence entered by the user. The function is further defined below. Again, make sure to check your output with the sample outputs when printing out the return value of this function.

Create a function called alphabetizeWord() with a return type of char* and two parameters. The first parameter is a char* referring to the sentence entered on the command line. The second parameter is another char*, which would be used later in the function. You need to figure out a way to return a char* that has the letters arranged alphabetically for every word in the sentence. Remember, that you can’t ignore the “space” between any two words. For example, the sentence “internet programming” should become “eeinnrtt aggimmnoprr” and the space should be preserved. You should use the sizeCurrentWord(), copyWord(), traverseAlphabet() helper functions in order to traverse through all letters in the alphabet to return the correct output.

10) Create a function called sizeCurrentWord() with a return type of int and two parameters. The first parameter parameter is a char* referring to the sentence entered on the command line. The second parameter is an int which is the index into the sentence where the next word starts.

The sizeCurrentWord() function should loop through the sentence until it either finds a space or newline character to count how many letters are in the word. Return the count.

11) Create a function called copyWord() with a return type of void and three parameters. The first parameter parameter is a char* referring to the sentence entered on the command line. The second parameter is another char* where you will store the copy of the word. The third parameter is an int* which is the index into the sentence.

The copyWord() function should loop through the sentence until it either finds a space or newline character and copies each character from sentence to holder. The index for holder should start at 0, but the index for sentence should use pIndex. pIndex is a pointer because as pIndex is incremented, it should change the value outside the function in alphabetizeWord().

12) Create a function called traverseAlphabet() with a return type of void and three parameters. The first parameter parameter is a char* referring to the output string. The second parameter is another char* has the word that was copied earlier. The third parameter is an int* which is the index into the output string.

The traverseAlphabet() function should loop through the entire alphabet. For each letter in the alphabet, look through the input string (holder) to find each occurrence of that letter and if found, copy the letter into the output string (array) at pIndex (make sure to dereference pIndex to get the index value). Make sure to increment the value pointed at by pIndex each time a letter is copied to the output string so that the index value in alphabetizeWord() is updated properly. In this way each of the words will be appended to the output string one at a time and they will all be in the correct order.

13) Finally, in main() you need to call the uniqueLetters() function, which would list out only the unique letters using the alphabetized word from the last step. The function is, further, defined below. Again, make sure to check your output with the sample outputs when printing out the return value of this function.

Create a function called uniqueLetters() with a return type of char* and three parameters: two pointers to a char and an integer value (which is the length of the word entered by the user). The first char* is referring to the sentence entered by the user and the second char* would be used further in the function. You need to figure out a way to remove all duplicate letters from the output of the alphabetizeWord() function. As an example from the last step, the alphabetized version of “internet programming” ends up becoming “eeinnrtt aggimmnoprr”. This should now become “einrt agimnopr” as an output for this function.

14) Add “return 0;” which will exit the main function.
Sample Output
Example 1:
./pa4a Ayushi “internet programming”

Hello Ayushi!
You entered the sentence: internet programming
Shifting to all lowercase gives us: internet programming
Alphabetically, this would look like: eeinnrtt aggimmnoprr
Keeping only the unique letters gives us: einrt agimnopr
Example 2:
./pa4a Juan

Wrong number of arguments
Usage: pa4a [name] [sentence]
Example 3:
./pa4a Kenneth internet programming

Wrong number of arguments
Usage: pa4a [name] [sentence]

Example 4:
./pa4a laurence “windows and mac”

Your name must begin with a capital letter
Usage: pa4a [name] [sentence]
Example 5:
./pa4a Ronak “STRINGS ARE GREAT”

Hello Ronak!
You entered the sentence: STRINGS ARE GREAT
Shifting to all lowercase gives us: strings are great
Alphabetically, this would look like: ginrsst aer aegrt
Keeping only the unique letters gives us: ginrst aer aegrt
Example 6:
./pa4a Samarth “alphabetize Every Word”

Hello Samarth!
You entered the sentence: alphabetize Every Word
Shifting to all lowercase gives us: alphabetize every word
Alphabetically, this would look like: aabeehilptz eervy dorw
Keeping only the unique letters gives us: abehilptz ervy dorw
4.2 Part 2
Part 2 (pa4b.c) is a simple program that will read in a file consisting of a list of shapes with their dimensions and print out their perimeter and areas depending on the shape.
The goal of the program is to parse a file given as a command line argument and parse it differently depending on the shape. On doing the shape-specific parsing, the program should calculate the perimeter and area depending on the type of shape used and then store it in a structure instance of that specific shape. Once the shape has been fully initialized, the pointer should be passed into a function that prints its contents to the screen.
● Make sure your prompt and output messages exactly match the sample prompts and messages word-for-word.
● No hardcoding or magic numbers in your code.
● You cannot directly print the contents of the file without using any structs. You must use all the functions definitions provided at the top of the file
○ You can add extra helper functions if you want/need to
● Keep your function bodies (inside the { }) as small as possible
○ Good rule of thumb: each function should perform one task
○ Avoid duplicate code, use extra methods.
● Remember to check the validity of your shapes when necessary
● Do not modify any of the structs defined in the starter code
● Do not declare any extra local (or global) variables, only use the variables given in the starter code.
● Structures that are passed to function as pointers should use -> to dereference the struct members.
4.2.1 Getting Started
The sections where you will need to add your own code are the constant definitions at the top of the file, main(), and the function bodies at the bottom of the file. Important: you are not allowed to declare any local variables (or global) in your code. You may only use the variables that have already been provided for you, but you may (and will) use #define constants.

The following list gives more details about sections you need to complete.

1) Define any constant definitions before the main() method. Use #define, a descriptive name in all caps with words separated by underscores, and a number (or letter) to create the constant.

Example:
#define SHAPE_LEN 10
Use constants for any number (except 0, 1, or -1) and any character constant.

2) In main(), after the given declarations, check if the user has entered the valid number of arguments by using argc. Remember, argc counts the name of the program also, so if no arguments have been entered, argc would be 1. If the number of arguments are invalid then call the usage() function.

usage() is a void function that does not take any arguments and just prints the usage statement. The usage statement is as follows

usage: pa4b [ shapes filename ]

3) If the user does enter the right number of arguments, then open the file in reading mode through fopen() and store it in FILE* shapesFile. If shapesFile doesn’t point to a valid file, then call printError() with the string “ERROR: Couldn’t open file” and return -1. Otherwise, call parseFile().

printError() is a void function that takes in a string(char *) and prints with the error warning.

4) parseFile() is a void function that takes in two arguments, one is the FILE * to the file to be parsed while the second is a pointer to the ShapeHandler instance declared in main. The goal of the function is to parse the file and use the ShapeHandler pointer to call specific parseShape methods (parseCircle, parseSquare etc) and then print the shapes.

First implement a while loop that keeps reading the file line by line. The while loop will end when the end of the file has been reached. To check for the end of the file, use the feof() function (Read the documentation for feof to figure out how it works –
http://www.cplusplus.com/reference/cstdio/feof/).

For each line, use fscanf() to get the shape of the handler (a character string) and store it in the handler’s shapeType variable. (Read the documentation for fscanf to figure out how it works – http://www.cplusplus.com/reference/cstdio/fscanf/).

Once the shape type has been figured out, it is time to call the appropriate parseShape method(). We’ll do this by first calling the parseShapeType() function in our switch statement.

This function will just take in a string (in this case handler’s shapeType variable) and return a corresponding number to it. We can then use this number for a switch-case that can call the appropriate parseShape method corresponding to the particular method.

For example – If the shape we’re dealing with is a circle, then handler’s shapeType variable will scan the string “circle” from the file pointed to by shapesFile. This string is then passed to the parseShapeType() function that will return a specific number corresponding to this string. So for “circle”, the function can return 0. Now with this number chosen in our switch, we can go to case 0. Here we can call the parse function associated with a circle which would be parseCircle. This will take in two arguments, the pointer to the circle struct which is part of the StructHandler struct and the file pointer. The other parseShape() functions will be called in similar fashion for the other shapes.

5) The parseShape methods are a group of methods that will take in a pointer to the shape struct which is part of the handler (circle, square, triangle, etc) and a file pointer. It will then scan the different dimensions of the shapes depending on the shape, calculate each shape’s area and perimeter and save the information in the shape’s struct. You can use M_PI and pow(), defined in math.h to help you do this.

a) parseCircle() – This will take in a pointer to the circle struct, will scan the radius from the file, and then will calculate the area and perimeter of the circle. It will then assign these corresponding values to the variables present in the struct.
b) parseTriangle() – This will take in a pointer to the triangle struct, will scan all three sides from the file, will call isValidTriangle() and if valid will calculate the area (using Heron’s Formula) and perimeter of the triangle. It will then assign these corresponding values to the variables present in the struct.

i) isValidTriangle() is a void function that takes in the pointer to the triangle structure and will assign the struct pointer’s isValid to be either true or false (using Triangle Inequality Theroem). It will decide this by checking that the sum of the length of two sides is greater than the length of the third side.

c) parseSquare() – This will take in a pointer to the square struct, scan the side from the file, and then will calculate the area and perimeter of the square. It will then assign these corresponding values to the variables present in the struct.
d) parseRectangle() – This will take in a pointer to the rectangle struct, scan the length and breadth from the file, and then will calculate the area and perimeter of the rectangle. It will then assign these corresponding values to the variables present in the struct.
e) parseInvalidShape() – This will not take any pointers except for the file pointer. It will basically skip over the rest of the line(through a loop using fgetc()) until it reaches the ‘\n’ character.

6) Coming back to the while loop in parseFile(). Call the printShape() function when the shape in the line has finally been parsed. The function is a void function that will only take in one argument, our ShapeHandler instance. Now using parseShapeType() in our switch statement, we can pass in the shapeType string and get our corresponding number that can be used in our switch-case. Once this is done, print the details of the shape corresponding to the number returned in the switch statement. For example, if parseShapeType returns 0 corresponding to a circle, then print the name of the shape(“circle”) followed by the values of its area and circumference. For triangles, you must first verify that the dimensions represent a valid triangle. If the triangle dimensions are invalid, you must use printError() to display an error message and continue on to the next shape (line).

Make sure that there is a tab character before each shape value (‘\t’). Additionally, all double values must be printed with a precision of 2.

7) Once the while loop ends reading the file ends, and we are back in main(), make sure to close the file and return 0.

4.2.2 Sample Output
Example Executions (user input in BOLD):
Only one example has been provided for this program since it requires a sample file as an input. This was the output when the following sample file was provided – https://drive.google.com/file/d/1zufxTIYyAuhf2I4G0zYqOlnthxHglJtk/view?usp=sharing
Important: Even if you are using Xcode or some other IDE, it is highly recommended that you compile and run your program using the command line (Terminal.app or cmd.exe). Please refer to the spec for PA1 for details on how to do this.
Example 1:
./pa4b test.txt
Parsing file
circle
area: 95.03
circumference: 34.56
circle
area: 113.10
circumference: 37.70
triangle
area: 7.81
perimeter: 13.00
ERROR: invalid triangle dimensions, skipping it
square
area: 16.00
perimeter: 16.00
square
area: 6.25
perimeter: 10.00
rectangle
area: 2.00
perimeter: 6.00
rectangle
area: 13.50
perimeter: 15.00
ERROR: invalid shape found in file, skipping it
ERROR: invalid shape found in file, skipping it
circle
area: 63.62
circumference: 28.27

Example 2:
./pa4b
usage: pa4b [ shapes filename ]

Example 3:
./pa4b badname.txt
ERROR: Couldn’t open file

bestdaixie

评论已关闭。