Saturday, April 20, 2013

Simple calculator program in C!!

Recently one of my friend asked me to implement simple Application in C which reads simple calculator commands from the input file and writes the result in the output.  Below is the complete description of the problem. And  You can find the complete C programming language source code as well.

Simple Calculator Description:

The first line of the file contains two characters giving the initials of the user (such as GN). The rest of the file consists of a sequence of commands. Each line of the command file contains one command.

Each command starts with a character and may have zero or more operands. You may assume that the data file has no errors. The number of commands in the file is unknown. But your processing may stop as soon as the Quit command is processed. The commands are: integer add, integer subtract, integer multiply, integer divide, change to uppercase, change to lowercase, separate, print out all digits, printout the k-th digit, round real number to desired number of decimal places, divide number into two.

Calculator Commands:

+ i j [Integer Add]                        Add integers i and j and print out result

* i j [Integer Multiply]                  Multiply integers i and j and print out result

- i j [Integer Subtract ]                  Subtract integer j from i and print out result

/ i j [Integer Divide ]                     Divide integer i by j and print out result of integer division

C Ch [Character Case Change ]    Change character Ch to uppercase and print it out

c Ch [Character Case Change]      Change character Ch to lowercase and print it out

P i k [Print k-th Digit ]                  Print out the k-th digit of integer i

R x i [Round Reals ]                     Round double value x to i decimal places

S x [Separate ]                               Separate out the sign, integer part and fractional part of double value x

D i x [Partition Integer ]               Given integers i and x, print out two integers j and k, where the sum of j and k equals i, and when you take x% of i and truncate it you get j

H [Help ]                                       Print a short synopsis of all the available commands

Q [Quit ]                                       Quit

Click here for the C source code

No comments:

Popular Posts