Programming Fundamentals
For this exercise, make a program called cs_calculator.c
which will scan in
instructions until End-Of-Input (which we enter to our terminal with the
keyboard shortcut CTRL-D
) and prints the output as
specified below. An instruction is a character, followed by one or two positive
integers.
The first character identifies what type the instruction is.
- If the first character in the instruction is
's'
, then your program should print out the square of the next number in the instruction. - If the first character in the instruction is
'p'
, then your program should print out the value of the next number raised to the power of the number after next.
Examples
dcc cs_calculator.c -o cs_calculator ./cs_calculator Enter instruction: s 2 4 Enter instruction: p 5 3 125 Enter instruction: s 4 16 Enter instruction: p 3 4 81 Enter instruction: ./cs_calculator Enter instruction: p 3 3 27 Enter instruction: s 10 100 Enter instruction:
One major challenge of this exercise is figuring out how to use scanf
effectively. The lessons you learn in this exercise regarding scanf
will be
useful in assignment 1!
Assumptions/Restrictions/Clarifications
- You can assume that the first character in the instruction is only either
's'
or'p'
- You can assume that for each instruction, the correct number of successive positive integers will be given.
- The autotest for this exercise expects your program to end WITHOUT a new line
character when the user presses
Ctrl-D
. This means that the command prompt for the next command should be on the same line as the end of your program.