代写C++代码 代做C++程序 C++辅导 C++教学

C++网络家教 远程写代码 在线Debug 讲解答疑 不是中介,本人直接写

微信: ittutor QQ: 14061936 Email: ittutor@qq.com

导航


CS240

Lab4:  Basic I/O, Implementing an RPN Calculator, Number Conversion

Goal:


In this lab you will write programs that use basic I/O, implementing a command line RPN calculator, and number conversion.

1. Downloading the Initial Files


Copy the initial files in your cs240 directory:

cs cs240

cp /homes/cs240/2014Fall/lab4/lab4-src.tar.gz .

gunzip lab4-src.tar.gz

tar -xvf lab4-src.tar

 lab4-src will include the initial files that you will need as well as the test files.


2. The Makefile


The UNIX command make is used to help build programs. make will compile a program only if it has been recently modified. The command make uses as input a file called Makefile. The Makefile has rules like the folowing:


all: toascii countlines towords rpncalc printargs


toascii: toascii.c

gcc -g -o toascii toascii.c


The "all": rule lists all the executables that need to be build.


The second rule means that the executable toascii depends on the file toascii.c. To convert toascii.c into toascii, make needs to run the command "gcc -g -o toascii toascii.c". This command will run when either toascii does not exist, or when the modification time of toascii.c is more recent than toascii.


Try typing "make" to make all the files in lab1-src.

3. Implementing countlines


The first program you will write is called countlines.This program will read the characters from the console, and after typing ctrl-d it will print the number of lines. 

bash-4.1$ ./countlines

Program to count lines. Type a string and ctrl-d to exit

Hello 

world

how

are you

fine

Total lines: 5

bash-4.1$ 

Also, you can redirect a file to the input of this program:

bash-4.1$ ./countlines < hamlet

Program to count lines. Type a string and ctrl-d to exit

Total lines: 32

bash-4.1$ 

See the program toascii.c to get some ideas on how to implement this program. Hint: A line ends with the new line character "\n".

We are providing you with a file ./countlines.org that runs exactly like how it is expected that your countlines program should run.


Run the script "testall" to test your program. This script uses different inputs into your program.

4. Implementing a Program that Counts and Displays Words


Also implement a program named towords that displays and counts the words typed. A word is a sequence of characters separated by one or more spaces.


For example:


bash-4.1$ ./towords

Program to separate text to words. Type a string and ctrl-d to exit

Hello world. Welcome to CS240.

Word 0: Hello

Word 1: world.

Word 2: Welcome

Word 3: to

Word 4: CS240.

We will have lots of fun.

Word 5: We

Word 6: will

Word 7: have

Word 8: lots

Word 9: of

Word 10: fun.

words total = 11

bash-4.1$ 


Or you can also redirect the input:


bash-4.1$ ./towords < in1 

Program to separate text to words. Type a string and ctrl-d to exit

Word 0: Hello

Word 1: world.

Word 2: How

Word 3: are

Word 4: you.

words total = 5

bash-4.1$ 


We also provide you with a program towords.org that runs exactly the way your tolines.c is expected to run.


Run the testall script to test your implementation.

5. Implementing an RPN Calculator


RPN (Reverse Polish Notation) is a simple way to represent arithmetic equations that do not need parenthesis. The evaluation of RPN equations uses a stack. 


An RPN equation is a sequence of numbers and operations such as:

3 4 +

The equation is read from left to right.  When a number appears it means that the number has to be pushed to the stack. In this case "3" is pushed to the stack and then "4". When an operation such as "+" appears, the top two numbers are popped from the stack, added, and the result is pushed back to the stack. Therefore, "3 4 +" in RPN is equivalent to 3 + 4 in standard arithmetic. 

A more complex example such as:

3 7 4 - 5 * +

is evaluated in the following way. 


First the stack is empty:

_stack = {} _top = -1

3 is read and pushed to the stack

_stack = {3} _top = 0

7 is read and pushed to the stack

_stack = {3, 7} _top = 1

4 is read and pushed to the stack

_stack = {3, 7, 4} _top = 2

"-" is read. The top two values are popped and sustracted. The result is pushed back to the stack.

_stack = {3, 3} _top = 1

5 is read and pushed to the stack.

_stack = {3, 3, 5} _top = 2

"*" is read. The top two values are popped and multiplied. The result is pushed back to the stack.

_stack = {3, 15} _top = 1

"+" is read. The top two values are popped and added. The result is pushed back to the stack.

_stack = {18} _top = 0

The result of the expression will be in the top of the stack.


The RPN expression "3 7 4 - 5 * +" is equivalent to "3 + (7 - 4)*5" in standard notation. 


You will implement aprogram rpncalc that takes an RPN equations as argument and evaluates it. Each operator or operand will be an argument in the command line. See the example printargs.c that shows how arguments are passed to main. The functions you will implement in rpncalc are: +, -, x, /,  sin, cos, pow, log, exp. Notice that the operator "*" has been substituted by "x" top prevent conflicts with wildcard expansion in the shell. The log operator is the natural logarithm.


For example:


bash-4.1$ rpncalc 3.0 7 -4 - 5 x +

58.000000

bash-4.1$ 


You have to also consider the cases when there are not enough operands (stack underflow) or too many operands. A program rpncalc.org example has been provided so you can see how your program should behave for all conditions. 


Also, the script testall will test your implementation.


6. Number conversions


Write a program to convert a given number from one base to another. The format is:


convert <basefrom> <baseto> <number>


You now know how to input arguments on the command line. Assume that basefrom and baseto are integers between 2 and 25 (both inclusive). Since base 16 uses the characters A, B, C, D, E, F to represent decimal numbers 10 through 15, bases 17 through 20 will involve additional characters. For example, base 20 will involve the additional characters G, H, I and J. Use defensive programming to make sure your command line arguments are what you would expect. You may assume that no number (in any base) has more than 32 digits, and all numbers are nonnegative.


To convert a number, say (123)9, from base 9 to another base such as base 8, simply convert from base 9 to decimal (i.e., base 10), to get the number, i.e.,


(1 x 92) + (2 x 91) + (3 x 90) = (102) 10


and then convert to base 8 by doing repeated division, just the way you convert from decimal to binary (base 2)  - simple, because you understand decimal (base 10) arithmetic. In this case you should get (146)8. The following link may refresh your memory.




7. The script testall


A script testall has been provided to test your project. The output is an estimated grade that will count for about 80% of your grade in the lab. The reminder of the grade will depend on code organization, comments, and other test cases not included in the lab4-src.

8. Turning In your Project


Follow these instructions to turnin lab1:

Login to data.cs.purdue.edu

Change above the directory to lab4-src.

Type "turnin -c cs240 -v -p lab4 lab4-src"

Type  "turnin -c cs240 -p lab4 -v" to make sure you have submitted the right files

The deadline for this lab is Monday October 6th, 11:59pm.  









相关推荐