Question: In this programming project, you will develop a NumberType class which has a nonnegative decimal integer member and a C-string member corresponding to the binary
In this programming project, you will develop a NumberType class which has a nonnegative decimal integer member and a C-string member corresponding to the binary number representation of the decimal integer. At any time, the integer member and the C-string member are always corresponding to each other. In particular, you will practice using stacks to facilitate the decimal-to-binary conversion.
Problem Description
In this programming assignment, you will implement a new class called NumberType, using a header file NumberType.h (the members are specified and brief descriptions of operations are also given for you). You are to write the implementation file NumberType.cpp for the NumberType class.
You are to implement the following operations for the NumberType class. Since the operations are only briefly described in the header file NumberType.h, you may add more details to the precondition/postcondition contract of the member functions.
-A constructor.
-Two set functions.
-Two get functions.
-A decimal-to-binary conversion function.
-A binary-to-decimal conversion function.
The NumberType class objects are used to represent nonnegative integers in both decimal and binary representations.
Additional Requirements and Hints
The Protected Member Variables
Carefully read the class definition in NumberType.h. The NumberType class has a protected integer member num to store the nonnegative decimal integer number. It also has a protected C-string member binRep to store the binary representation corresponding to num. Here we use C-string to store binRep instead of using int such that binRep can accommodate possibly long binary bit sequences. At any time, num and binRep always correspond to each other in the sense that they are different representations of the same number using different bases.
The dec2Bin Function
Use the method of successive division (see the supplementary reading on positional-number-system conversions posted on Blackboard) to write this function. Use a stack (you may use array-based or linked-list-based stacks) to facilitate the process of storing and retrieving the remainders. Do not use any available C++ built-in function which directly achieves such a conversion.
The bin2Dec Function
Write this function to implement the weighted sum formula for binary-to-decimal conversion. Do not use any available C++ built-in function which directly achieves such a conversion.
THE NumberType HEADER FILE:
//**********************************************************
// SPECIFICATION FILE (NumberType.h)
// This class specifies the basic members to implement a
// number with both decimal and binary representations.
//**********************************************************
#ifndef NUMBERTYPE_H
#define NUMBERTYPE_H
#include
#include
#include "LinkedStackType.h"
using namespace std;
const int MAX_NUM_OF_BITS = 50; // Maximum number of bits allowed
// for binary representation
class NumberType
{
public:
// Constructor
NumberType(int a = 0);
// Post: num = a; binRep contains the corresponding
// binary number bit sequence.
void setDecNum(int a);
// Post: num = a; binRep contains the corresponding
// binary number bit sequence.
void setBinNum(char binNum[]);
// Post: binRep contains the same bit sequence as bitNum;
// num contains the corresponding decimal value.
int getDecNum();
// Return the value of num.
void getBinNum(char binNum[]);
// Post: bitNum contains the same bit sequence as bitRep.
protected:
int num; // nonnegative decimal integer
char binRep[MAX_NUM_OF_BITS]; // the corresponding binary
// representation for num; we use a C-string to
// represent the bit sequence
private:
void dec2Bin();
// Decimal to binary conversion (use a LinkedStackType stack
// in its implementation).
// Post: binRep contains the bit sequence corresponding to
// the decimal value num.
void bin2Dec();
// Binary to decimal conversion.
// Post: num contains the decimal value corresponding to
// the binary representation bitRep.
};
#endif
*************************************************** PLEASE make sure the program compiles and all functions are implemented. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
