Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. In some programming languages, binary constants are indicated by letter B fol

ID: 3878714 • Letter: 3

Question

3. In some programming languages, binary constants are indicated by letter B following the binary string, and hexadecimal constants (i.e., base-16 numbers) by letter X following the hexadecimal string (the additional hexadecimal digits are denoted by letters A to F, A repre- senting 10, and F representing 15, so "2FX" denotes the hexadecimal number 2*16+15-47) All such constants must begin with a digit (for example, "O") to distinguish them from iden- tifiers. Observe that strings "20B or "1A5" are incorrect. Design a deterministic finite automaton which accepts (by different final states) correct binary decimal and hexadecimal constants. Note: Observe that "10" is a decimal number, "10B" is a binary number and "10BX" is a hexidecimal one

Explanation / Answer

You can take the input and take the last character to check whether it is either B , X or a number. Then accordingly you can check if the other characters are valid.

Characters are stored by their Ascii values , 0 has an ascii value of 48 and 9 is 57 , so you can use the numbers to check if a character is an integer of not.

For binary the character has to be 0 or 1

For decimal all characters have to be numbers

For hexadecimal it can be numbers or letters between A and F with ascii values 65 and 70 respectively

I've made the code so that it is case insensitive, so use the lowercase ascii characters

CODE (C++):


#include<iostream>
#include<string>
using namespace std;

bool checkBinary(string inp)
{
int sz = inp.size();
for(int i=0;i<sz-1;i++)
if(inp[i] != 48 && inp[i] != 49) // Binary can be only 0 or 1
return false;
return true;
}
bool checkHexa(string inp)
{
int sz = inp.size();
for(int i=0;i<sz-1;i++)
if(!((inp[i] >=48 && inp[i] <= 57) || (inp[i] >=65 && inp[i] <= 70) || (inp[i] >=97 && inp[i] <= 102)))
return false;
return true;
}

bool checkDecimal(string inp)
{
int sz = inp.size();
for(int i=0;i<sz-1;i++)
if(!(inp[i] >=48 && inp[i] <= 57))
return false;
return true;
}

int main()
{
string inp;
cout << "Enter the number in any format" << endl;
cin >> inp;
int sz = inp.size();
char lastchar = inp[sz-1];
bool valid;
if(lastchar == 'B' || lastchar == 'b')
valid = checkBinary(inp);
else if(lastchar == 'X' || lastchar == 'x')
valid = checkHexa(inp);
else if(lastchar >=48 && lastchar <= 57)
valid = checkDecimal(inp);
else
valid = false;

if(valid)
cout << "The input is a valid number format" << endl;
else
cout << "The input is not a valid number format" << endl;
}

NOTE:

You can add another check for negative numbers, if the first character is - or + it can be ignored, this code does not have that check.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote