C++代写:CMPT130 Binary Arithmetic

使用C++对Binary Number进行算术运算。

Binary Number

Requirement

Read this document in its entirety and carefully before you start anything and understand it. If you have any questions, don’t hesitate to email me.

In this assignment, we will work on arithmetic of integers in two’s complement binary arithmetic. To store the two’s complement representations of integers, we will use the C++ string data type. You are not allowed to use any ARRAY variable in this assignment. Read the restriction section below.
Consider the main program given below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{

// Read in the bit pattern size
int L;
do
{
cout << "Enter positive integer for the bit pattern size ";
cin >> L;
} while (L <= 0);

// Read in two integers a and b
int a, b;
cout << "Enter an integer a ";
cin >> a;
cout << "Enter an integer b ";
cin >> b;

// Calculate the decimal arithmetic sum of a and b and print the result
int c1 = a + b;
cout << "In decimal " << a << " + " << b << " is " << c1 << endl;
// Compute the two's complement representations of a and b
// Each integer must be represented in L-bits pattern
// Also these two's complement representations must be returned as string data types
string A = decimalToTwocomplementString(a, L);
string B = decimalToTwocomplementString(b, L);

// Print the two's complement representations of a and b
cout << "The two's complement of " << a << " is\t " << A << endl;
cout << "The two's complement of " << b << " is\t " << B << endl;

// Compute the binary sum of the two's complement representations of a and b
// The result must be returned as L-bit pattern string data type
string C = TwoComplementStringAddition(A, B);

// Print the two's complement representation binary sum
cout << "The binary sum of " << A << " and " << B << " is " << C << endl;

// Convert the two's complement representation binary sum to decimal and print
int c2 = TwoComplementStringToDecimal(C);
cout << "In two's complement arithmetic, " << a << " + " << b << " is " << c2 << endl;

// Print some concluding results
if (c1 == c2)
cout << c1 << " is equal to " << c2 << ". Good Job!" << endl;
else
{
cout << c1 << " is not equal to " << c2 << endl;
cout << "Either " << c1 << " cannot be represented by the given bit pattern OR we have made some mistake!" << endl;
}
system("Pause");
return 0;
}

Your Task

The main program shown above is uploaded together with this problem statement onto Moodle under Topic 8. Download the main program code and copy and paste it to a new project. You are required to copy the given main program exactly as it is with no change whatsoever. Do not change any function signature. Then implement the missing functions so that the program works correctly. Some sample runs are given below for your reference. Of course, you can write additional helper functions that you can call from within your functions; but you are not allowed to make any change to the given main program.

Restrictions

You are required to use C++ string Data Type to represent the two’s complement binary representations. You are not allowed to use any ARRAY variable for any purpose. If you use ARRAY variable for any purpose in your program, you will automatically get zero mark.

Submission Format

You are required to submit your program online through Moodle. You will find a submission button for Assignment 2 on Moodle under Topic 7 and you are required to upload the source code of your C++ program. No assignment is submitted through email or hard copy; you must upload your work onto Moodle before the due date. Make sure to upload your .cpp file.

Marking

A nonworking program will automatically get zero. A program that works but does not give right output or gives partial right output will lose marks depending how severe its shortcoming is.

Some sample runs of the program are shown below

Remark: The user input values and the pressing of the enter key symbol are shown in red color for clarity.

Enter positive integer for the bit pattern size 5
Enter an integer a 9
Enter an integer b -14
In decimal 9 + -14 is -5
The two's complement of 9 is 01001
The two's complement of -14 is 10010
The binary sum of 01001 and 10010 is 11011
In two's complement arithmetic, 9 + -14 is -5
-5 is equal to -5. Good Job!
--------------------------------------------------------------------------------------------------------------
Enter positive integer for the bit pattern size 2
Enter an integer a -100
Enter an integer b 98
In decimal -100 + 98 is -2
The two's complement of -100 is 00
The two's complement of 98 is 10
The binary sum of 00 and 10 is 10
In two's complement arithmetic, -100 + 98 is -2
-2 is equal to -2. Good Job!
--------------------------------------------------------------------------------------------------------------
Enter positive integer for the bit pattern size 2
Enter an integer a 100
Enter an integer b -98
In decimal 100 + -98 is 2
The two's complement of 100 is 00
The two's complement of -98 is 10
The binary sum of 00 and 10 is 10
In two's complement arithmetic, 100 + -98 is -2
2 is not equal to -2
Either 2 cannot be represented by the given bit pattern OR we have made some mistake!
--------------------------------------------------------------------------------------------------------------
Enter positive integer for the bit pattern size 8
Enter an integer a 62
Enter an integer b -99
In decimal 62 + -99 is -37
The two's complement of 62 is 00111110
The two's complement of -99 is 10011101
The binary sum of 00111110 and 10011101 is 11011011
In two's complement arithmetic, 62 + -99 is -37
-37 is equal to -37. Good Job!
--------------------------------------------------------------------------------------------------------------
Enter positive integer for the bit pattern size 5
Enter an integer a 11
Enter an integer b 9
In decimal 11 + 9 is 20
The two's complement of 11 is 01011
The two's complement of 9 is 01001
The binary sum of 01011 and 01001 is 10100
In two's complement arithmetic, 11 + 9 is -12
20 is not equal to -12
Either 20 cannot be represented by the given bit pattern OR we have made some mistake!
--------------------------------------------------------------------------------------------------------------
Enter positive integer for the bit pattern size 5
Enter an integer a -321
Enter an integer b 305
In decimal -321 + 305 is -16
The two's complement of -321 is 11111
The two's complement of 305 is 10001
The binary sum of 11111 and 10001 is 10000
In two's complement arithmetic, -321 + 305 is -16
-16 is equal to -16. Good Job!
--------------------------------------------------------------------------------------------------------------
Enter positive integer for the bit pattern size 3
Enter an integer a 2
Enter an integer b 2
In decimal 2 + 2 is 4
The two's complement of 2 is 010
The two's complement of 2 is 010
The binary sum of 010 and 010 is 100
In two's complement arithmetic, 2 + 2 is -4
4 is not equal to -4
Either 4 cannot be represented by the given bit pattern OR we have made some mistake!
--------------------------------------------------------------------------------------------------------------
Enter positive integer for the bit pattern size 6
Enter an integer a 45
Enter an integer b -17
In decimal 45 + -17 is 28
The two's complement of 45 is 101101
The two's complement of -17 is 101111
The binary sum of 101101 and 101111 is 011100
In two's complement arithmetic, 45 + -17 is 28
28 is equal to 28. Good Job!
--------------------------------------------------------------------------------------------------------------
Enter positive integer for the bit pattern size 1
Enter an integer a -99
Enter an integer b 100
In decimal -99 + 100 is 1
The two's complement of -99 is 1
The two's complement of 100 is 0
The binary sum of 1 and 0 is 1
In two's complement arithmetic, -99 + 100 is -1
1 is not equal to -1
Either 1 cannot be represented by the given bit pattern OR we have made some mistake!
--------------------------------------------------------------------------------------------------------------
Enter positive integer for the bit pattern size 1
Enter an integer a -100
Enter an integer b 99
In decimal -100 + 99 is -1
The two's complement of -100 is 0
The two's complement of 99 is 1
The binary sum of 0 and 1 is 1
In two's complement arithmetic, -100 + 99 is -1
-1 is equal to -1. Good Job!