Lab 4

Lab Description:

This lab consist of programing a simple calculator in C++. As before, the calculator will display a menu asking the user what operation to perform. The operations supported are: convert a Decimal to it equivalent in Roman numerals, Addition, Subtraction, Multiplication, and Exponentiation. The program should run until the user chooses to exit.When everything is running perfectly in c++ we will use the fallowing code: ‘gcc –S programName.c’ to covert the c++ program to an assembly. There after we obverse, compare, similarities and  differences of generated code with the program in HLA of lab2.

Observations:

My first observation is that the code generated is hard to read and understand. Look like the text in the code is missing certain part and in some occasion not in order. The program in HLA has a very limited amount of instruction while the new code is lengthy and almost double in size. Certain instruction like move(  , );  are the same as in in both code while decision structure in lab 2 are replaced by jumps and labels. the execution time is also slightly different. We should note that High Level Language require a Compiler to convert into assembly the machine level code.(Now-a-days compilers are smart enough to generate the machine code directly). To write assembly code it is necessary to know the architecture of the processor or controller. To write an High Level Program it is not necessary to know the architecture completely. In the new generated code certain parts seem to be not necessary  for the  program to run while in the HLA in lab 2 every instruction or line of code is part of the program and if missing the program will not run. In conclusion an assembly language is where each statement corresponds to one machine instruction while  high level language is where each statement corresponds to more than one, sometimes many, machine instructions.

Code:

#include 
#include
using namespace std;
	string romanNum() {
		int num;
		int num1;
		int quotient;
		int M = 0;
		int D = 0;
		int C = 0;
		int XC= 0;
		int L = 0;
		int X = 0;
		int XI = 0;
		int V = 0;
		int IV =0;
		int I = 0;
			cout <> num1;
				if (num1>65536)
			cout<<"This number is too big, be awared of over flow\n";
				num=num1;

				quotient = num/1000;
				M = quotient;
				num = num % 1000;

				quotient = num/500;
				D = quotient;
				num = num % 500;

				quotient = num/100;
				C = quotient;
				num = num % 100;

				quotient = num/90;
				XC = quotient;
				num = num % 90;

				quotient = num/50;
				L = quotient;
				num = num % 50;

				quotient = num/10;
				X = quotient;
				num = num % 10;

				quotient = num/9;
				XI = quotient;
				num = num % 9;

				quotient = num/5;
				V = quotient;
				num = num % 5;

				quotient = num/4;
				IV = quotient;
				num = num % 4;

				I = num;
					cout<<"\n";
					cout<<"The equivalent Roman Numeral of "<< num1 <<" is: ";

					for (int m = 0; m < M; m++)
						cout << "M";
					for (int d = 0; d < D; d++)
						cout << "D";
					for (int c = 0; c < C; c++)
						cout << "C";
					for (int xc = 0; xc < XC; xc++)
						cout << "XC";
					for (int l = 0; l < L; l++)
						cout << "L";
					for (int x = 0; x < X; x++)
						cout << "X";
					for (int xi = 0; xi < XI; xi++)
						cout << "XI";
					for (int v = 0; v < V; v++)
						cout << "V";
					for (int iv=0; iv<IV; iv++)
						cout<< "IV";
					for (int i = 0; i < I; i++)
						cout << "I";

	return " ";
}

		int add() {
			int x, y;
			cout <> x;
			if (x>65536)
				cout<<"This number is too big, be awared of over flow\n";
			cout <> y;
			if (y>65536)
				cout<<"This number is too big, be awared of over flow\n";
			cout << endl;
			cout<<"The sum of "<<x<<" and "<<y<<" is: ";
			return x + y;
		}

			int sub() {
				int x, y;
				cout <> x;
				if (x>65536)
					cout<<"This number is too big, be awared of over flow\n";
				cout <> y;
				if (y>65536)
					cout<<"This number is too big, be awared of over flow\n";
				cout << endl;
				cout<<"Then difference of "<<x<<" and "<<y<<" is: ";
				return x - y;
			}

			int mul() {
				int x, y, result=0;
				cout <> x;
				if (x>65536)
					cout<<"This number is too big, be awared of over flow\n";
				cout <> y;
				if (y>65536)
					cout<<"This number is too big, be awared of over flow\n";
				cout << endl;
				cout<<"The product of "<<x<<" and "<<y<<" is: ";

				for (int i = 0; i < y; i++) {
					result += x;
				}
				return result;
			}

			int exp() {
				int x, y, result=1;
				cout <> x;
				if (x>65536)
					cout<<"This number is too big, be awared of over flow\n";
				cout <> y;
				if (y>65536)
					cout<<" This number is too big, be awared of over flow\n";
				cout << endl;
				cout<< x <<" raised to the power "<<y<<" is: ";

				for (int i = 0; i < y; i++) {
					result *= x;
				}
				return result;
			}

			void calculator() {
				while (true) {
					cout<<"This program was written by Alassane Ngaide\n";
					cout<<"\n";
					cout <<"This cool calculator will ask the user which of the operation below he/shewould like to perform\n";
					cout<<"\n";
					cout << "1. Convert a Decimal to a Roman Numerals\n";
					cout << "2. Addition\n";
					cout << "3. Subtraction\n";
					cout << "4. Multiplication\n";
					cout << "5. Exponentiation\n";
					cout << "0. Exit\n\n";

					cout <> choice;
					cout << endl;

					while (true) {
						if (choice == '1') {
							cout << romanNum() << endl;
							break;
						}
						else if (choice == '2') {
							cout << add() << endl;
							break;
						}
						else if (choice == '3') {
							cout << sub() << endl;
							break;
						}
						else if (choice == '4') {
							cout << mul() << endl;
							break;
						}
						else if (choice == '5') {
							cout << exp() << endl;
							break;
						}
						else if (choice == '0') {
							cout<<"\n";
							cout << "Thank you and good bye!\n";
							exit(0);
						}
						else {
							cout <> choice;
							cout << endl;
						}
					}
					cout<<"\n";
					cout << "Thank You for using this real cool calculator\n\n";
				}
			}

int main() {
	calculator();
	return 0;
}

Screen-Shots:

1.Roman:

Screenshot from 2013-11-25 11:09:31

2.Addition:

Screenshot from 2013-10-29 01:46:56

3:Subtraction:

Screenshot from 2013-11-25 11:10:56

4.Multiplication:

Screenshot from 2013-11-25 11:11:33

5.Exponentiation:

Screenshot from 2013-11-25 11:12:14

6.Exit:

Screenshot from 2013-11-25 11:13:14

Leave a Reply

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