Explore BrainMass

A simple C++ CARDIAC simulator

Write the worst CARDIAC simulator of all time (in C++) that tells the user how to execute only a single instruction typed as a 3 digit number by the user.

The user should be prompted to type in a 3 digit number. Then print to the screen whatever the CARDIAC Instruction Decoder box would show had that been the instruction. Below are 2 example runs of such a program.

Enter a 3 digit CARDIAC instruction: 493
Shift accumulator left 9 digits, then right 3 digits

Enter a 3 digit CARDIAC instruction: 265
Add the contents of cell 65 to the accumulator

Operation codes are as follows:
Code Abbreviation Meaning
0 INP Copy input to memory.
1 CLA Copy memory to accumulator.
2 ADD Add memory to accumulator.
3 TAC Move bug if accumulator < 0.
4 SFT Shift accumulator left then right.
5 OUT Copy memory to output.
6 STO Copy accumulator to memory.
7 SUB Subtract memory from accumulator.
8 JMP Backup bug to 99 then move bug.
9 HRS move bug and stop computing.

Code must use if statements. Do not use loops and arrays.

Solution Preview

The key to this solution is splitting up the instruction that the user entered into the various parts. This is done using the / and % operators. The / operator does integer division. That is, it divides the two numbers and then throws the remainder away. So the result of 3/2 is 1. We can use this feature ...

Solution Summary

This solutions shows how to implement a very simple CARDIAC machine simulator. It demonstrates the basic principles of using integer arithmetic to parse an integer command which is common in machine simulators.