Purchase Solution

A C++ CARDIAN simulator using a switch statement

Not what you're looking for?

Ask Custom Question

Write a not so bad CARDIAC simulator (in C++) that tells the user how to execute an instruction typed as a 3 digit number by the user.

The user should be prompted to type in a 3 digit number, and then the program should print to the screen whatever the CARDIAC Instruction Decoder box would show had that been the instruction.

* Use a pleasant switch statement for decoding the instructions based on op-codes, rather than nested if, consecutive if, or if/elseif monstrosity.

* Use a loop to allow users to provide as many instructions as they would like. Inform the users what they could type to exit the program (e.g., entering "1234" as an instruction).

* Keep track of the accumulator for the user. Use an accumulator variable for this purpose.

Assume that the accumulator contains 0 when the program starts. If the instruction modifies the accumulator (i.e., instructions 1xx, 2xx, 4xx, and 7xx), prompt the user for whatever data is necessary to execute the instruction, and execute
it ensuring that the accumulator is modified appropriately. Do not forget that the accumulator is only 3 digits long.

* Instruction 6xx should replace the word "accumulator" with the current value of the accumulator in its output to the user.

An example run of program might look like this.

Enter a 3 digit CARDIAC instruction (negative to exit): 123
Replace accumulator with cell 23
Enter the data in cell 23: 431
Enter a 3 digit CARDIAC instruction (negative to exit): 412
Shift accumulator left 1 digits, then right 2 digits
The accumulator now contains 3
Enter a 3 digit CARDIAC instruction (negative to exit): 375
Move bug to cell 75
Enter a 3 digit CARDIAC instruction (negative to exit): 250
Add the contents of cell 50 to the accumulator
Enter the data in cell 50: 99
Enter a 3 digit CARDIAC instruction (negative to exit): 348
Enter a 3 digit CARDIAC instruction (negative to exit): 630
Copy accumulator to cell 30
The accumulator now contains 96
Enter a 3 digit CARDIAC instruction (negative to exit): -1

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.

Purchase this Solution

Solution Summary

This solutions provides a reasonable CARDIAC simulator. The code is structured to use a switch statement and a while loop so that multiple instructions can be processed.

Solution Preview

There are several interesting parts to this solution. We use a switch statement to handle the op-code for each instruction instead of a series of if statements. This makes the code much easier to read and ...

Purchase this Solution


Free BrainMass Quizzes
Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.