Lab 4

Description
In this lab we made a basic calculator that the user can select from a menu to do the basic
math operations.The menu consist of adding two numbers, subtracting two numbers,
multiplying two numbers, doing exponents, and converting the input number into
roman numerals.For the multiplication we had to use loops that did repetitive addition.
This is the same Lab that was done in Lab 2 but instead of coding in HLA we had to code in C or C++.

HLA vs Assembly
I noticed a lot the way the program is read is much different. In assembly all of the outputs
are put into strings.In assembly they separate the outputs and the inputs with the outputs
coming first and then the inputs are separated into different sections that can be jumped
to.In assembly you do not have to repeat the outputs in every section because the
assembly just calls in when needed. The assembly code also has more coding into it
because it does a lot more moving and calling before even doing the actual adding, subtracting, etc.

C++ Code

#include 
using namespace std;

int main()
{
	int a, b, r, i;
	char selection;

	do
	{
		cout << "  \n";
		cout << "  ====================================\n";
		cout << "  1.  Addition\n";
		cout << "  2.  Subtraction\n";
		cout << "  3.  Multipication\n";
		cout << "  4.  Exponents\n";
		cout << "  5.  Decimal to Roman\n";
		cout << "  0.  Exit\n";
		cout << "  ====================================\n";
		cout <> selection;
		cout << endl;

		switch (selection)
		{
		case '1':
			cout <> a;
			if (a>65536)
				cout << "Number is Greater Than 65536" << endl;
			cout <> b;
			if (b>65536)
				cout << "Number is Greater Than 65536" << endl;
			cout << "Answer is: " << a + b <65536)
				cout << "Number is Greater Than 65536" << endl;
			break;

		case '2':
			cout <> a;
			if (a>65536)
				cout << "Number is Greater Than 65536" << endl;
			cout <> b;
			if (b>65536)
				cout << "Number is Greater Than 65536" << endl;
			cout << "Answer is: " << a - b <65536)
				cout << "Number is Greater Than 65536" << endl;
			break;
		case '3':
			cout << "Enter first number:" <> a;
			if (a>65536)
				cout << "Number is Greater Than 65536" << endl;
			cout << "Enter second number:" <> b;
			if (b>65536)
				cout << "Number is Greater Than 65536" << endl;
			r = 0;
			i = 0;
			do { r += a; i++; } while (i<b);
			cout << "Answer is: " << r <65536)
				cout << "Number is Greater Than 65536" << endl;
			break;

		case '4':
			cout << "Enter first number:" <> a;
			if (a>65536)
				cout << "Number is Greater Than 65536" << endl;
			cout << "Enter second number:" <> b;
			if (b>65536)
				cout << "Number is Greater Than 65536" << endl;
			r = 1;
			while (b--)
				r *= a;
			cout << "Answer is: " << r <65536)
				cout << "Number is Greater Than 65536" << endl;
			break;

		case '5':
			do{
				cout << "Enter first number:" <> a;
				if (a>3999)
				    cout << "Number is Greater Than 3999" <3999);
			while (a > 0){

				if (a >= 1000){
					a = a - 1000;
					cout <= 900){
					a = a - 900;
					cout <= 500){
					a = a - 500;
					cout <= 400){
					a = a - 400;
					cout <= 100){
					a = a - 100;
					cout <= 90){
					a = a - 90;
					cout <= 50){
					a = a - 50;
					cout <= 40){
					a = a - 40;
					cout <= 10){
					a = a - 10;
					cout <= 9){
					a = a - 9;
					cout <= 5){
					a = a - 5;
					cout <= 4){
					a = a - 4;
					cout <= 1){
					a = a - 1;
					cout << "I";
				}
			}
			break;

		case '0':
			cout << "Goodbye.\n";
			return 0;
		}

	} while (selection != 0);
}

Screenshot
lab4

Leave a Reply

Your email address will not be published. Required fields are marked *