C代写:字符数组的操作

写几个C语言的函数,操作字符数组。

No.1

Files to submit: read_lines.c, read_lines.h

Time it took Matthew to Complete: 10 mins

Requriements

  • Program must compile with both -Wall and -Werror options enabled
  • Submit only the files requested
  • Use doubles to store real numbers
  • Print all doubles to 2 decimal points unless stated otherwise

Restrictions

  • No global variables may be used
  • Your main function may only declare variables and call other functions

Description

The programming language Python has some really nice functions for dealing with files. One of them is called readlines and it reads the lines of the files into an array. For this projcet you will be implementing readlines in C.

Additional Details

  • For this assignment you will be creating a function and not an entire program.
  • The function your create should have the following signature:
  • void read_lines(FILE fp, char** lines, int* num_lines)
  • This function should read all of the lines in the file pointed to by fp and

    • Set each row of lines to contain one line of the file
    • Set num_lines to be equal to the number of lines that were in the file
  • If the file is empty lines should be set to NULL and num_lines to 0

  • You should only submit read_lines.c and read_lines.h

    • I will provide main.c and the Makefile
    • Your code must compile using this Makefile and main.c
    • You cannot edit main.c
  • While you only have to write read_lines you can write as many other functions as you want

Hints

  • I highly recommend making more functions than just read_lines for solving this problem.

    • For example a function that reads a single line from the file

Examples

User input has been underlined to help you differentiate what is user input and what is program output.

Example 1

./read_lines.out Makefile

read_lines.out: read_lines.o main.o

gcc -g -Wall -Werror -o read_lines.out read_lines.o main.o

main.o: main.c read_lines.h

gcc -g -Wall -Werror -c -o main.o main.c

read_lines.o: read_lines.c read_lines.c

gcc -g -Wall -Werror -c -o read_lines.o read_lines.c

clean:

rm -f .out .o

No.2

Files to submit: wcount.c

Time it took Matthew to Complete: 10 mins

Requriements

  • Program must compile with both -Wall and -Werror options enabled
  • Submit only the files requested
  • Use doubles to store real numbers
  • Print all doubles to 2 decimal points unless stated otherwise

Restrictions

  • No global variables may be used
  • Your main function may only declare variables and call other functions

Description

Write a program that counts the number words contained within a file

Additional Details

  • The name of the file will be passed on the command line
  • A word is considered to be 1 or more consecutive whitespace characters
  • A character is considered whitespace if isspace would return true if passed that character as an arguement

Input

  • Input will always be valid

Hints

  • The test case files are available under the starter code

Examples

User input has been underlined to help you differentiate what is user input and what is program output.

Example 1

Assume that the file fun.txt contains the following:

Computer sciences classes are great!

Even though I spend all my time doing homework :(

./wcount fun.txt There are 15 word(s).

No.3

Files to submit: perimeter.c

Time it took Matthew to Complete: 20 mins

Requriements

  • Program must compile with both -Wall and -Werror options enabled
  • Submit only the files requested
  • Use doubles to store real numbers
  • Print all doubles to 2 decimal points unless stated otherwise

Restrictions

  • No global variables may be used
  • Your main function may only declare variables and call other functions

Description

Write a program called perimeter.c that find the perimeter of an arbitrary shape. Your program will be provided a binary file that contains the vertices of the shape and your program should print out the premiter of the given shape.

Additional Details

  • You MUST use a struct in your solution

    • I recommend have a Point struct
  • The name of the binary file will passed as a command line parameter

  • The binary file contains only integers

    • The first integer is the number of points in the file
    • The remaining integers are points representing the vertices of the shape
    • There is an edge between each adjacent point and between the last
      point and the first point
    • There are at least 3 points in each file
  • The perimeter of a polygon is the sum of the lengths of all of its edges

  • If you really look at the the points in the file you will see that they don’t actually represent convex polygons but treat them like the files like they do

Examples

User input has been underlined to help you differentiate what is user input and what is program output.

Hints

  • If you are curious about what is in the file you can use the binaryinteger printing program we wrote in class.

Examples

In this example assume that the file example.txt contained the following values in binary. I’m displaying the file as text below to help you get an understanding of what the file looks like but remember the file is in binary with no spaces between any of the values.

Example 1

3

287 422

283 -981

781 647

./perimeter.out example.txt The perimeter is 3648.30

No.4

Files to submit: foo.c

Time it took Matthew to Complete: 5 mins

Requriements

  • Submit only the files requested
  • Print all real numbers to 2 decimal points unless stated otherwise

Restrictions

  • No global variables may be used

Description

Write a program called foo.c that implements the following function





























foo(0)=3
foo(1)=6
foo(2)=2
foo(3)=7
foo(N)=foo(N-1) + foo(N-2) + foo(N-3) - foo(N-4) + 1

Problem Details

  • Your program can only make a SINGLE recrusive call

    • You’ll know that you’ve made too many recurisive calls if your program timeouts on some of the test cases

Input

  • Input will always be valid

Hints

  • Use the better fibanocci code that we did in class as a starting point for your solution

Examples

User input has underlined to help you differentiate what is user input and what is program output. You do not need to underline anything.

Example 1

Enter a value for n: 0 Foo(0) = 3

Example 2

Enter a value for n: 1 Foo(1) = 6

Example 3

Enter a value for n: 2 Foo(2) = 2

Example 4

Enter a value for n: 3 Foo(3) = 7

Example 5

Enter a value for n: 4 Foo(4) = 13

Example 6

Enter a value for n: 5 Foo(5) = 17

Example 7

Enter a value for n: 6 Foo(6) = 36

No.5

Files to submit: bin_str.c

Time it took Matthew to Complete: 5 mins

Requriements

  • Submit only the files requested
  • Print all real numbers to 2 decimal points unless stated otherwise

Restrictions

  • No global variables may be used

Description

Write a program called bin_str.c that takes a binary string that also contains x’s and prints out all possible binary strings where the x’s can be replaced with either 0’s or 1’s. A binary string is a string made up of 0’s and 1’s.

Input

  • Input will be passed on the command line
  • Input will always be valid
  • An X should also be treated as an x

Examples

User input has underlined to help you differentiate what is user input and what is program output. You do not need to underline anything.

Example 1

./bin_str.out 0101

0101

Example 2

./bin_str.out xx 00

01

10

11

Example 3

./bin_str.out 1x0X 1000

1001

1100

1101

kamisama wechat
KamiSama