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

Write a function scan(num_string) IN LUA that reads in a single character at a t

ID: 3730925 • Letter: W

Question

Write a function scan(num_string) IN LUA that reads in a single character at a time, and when it reaches end of the string, reports whether it has read in a decimal, octal, or hexadecimal number, and what that number is (as a string), or "NONSENSE" if it isn't a well-formed number.

For the purposes of this assignment, use the following definitions of decimal, octal, and hexadecimal numbers

decimal - A single zero, or any continous sequence of decimal digits that starts with a non-zero decimal digit and includes 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

octal - An octal number always starts with a 0, and is followed by a single 0 or a non-empty sequence of octal digits that does not start with 0. The octal digits are 0, 1, 2, 3, 4, 5, 6, and 7.

hexadecimal - A hexadecimal number always starts with a 0 followed by a lowercase 'x' and either a single 0 or a sequence of hexadecimal digits. Hexadecimal digts include 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, A, B, C, D, E, and F.

All forms can have an optional positive or negative sign directly infront of the number, with no space between the symbol and the number.

Explanation / Answer

// NumberDetection.c
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;

// main function definition
int main()
{
// String variable to store the number entered by the user
string number;
// character variable to store the single character entered by the user
char ch;
// Loops till the entered character is not ' '
while(cin.get(ch))
{
// Checks if the entered character is ' ' then come out of the loop
if(ch == ' ')
break;
// Checks if the character is greater than 'f' and less than or equals to 'z'
// or character is greater than 'F' and less than or equals to 'Z'
if((ch > 'f' && ch <= 'z') || (ch > 'F' && ch <= 'Z'))
{
// Checks if the character is is 'x' for hexadecimal
if(ch == 'x')
{
// Concatenate the character to string number
number += ch;
// continue the loop
continue;
}// End of inner if condition
// Otherwise display the message for invalid number
cout<<"NONSENSE";
// Stop the program
exit(0);
}// End of outer if condition
// Concatenate the character to string number
number += ch;
}// End of while loop

// Checks if the first character of the number and second character of the number is zero
if(number[0] == '0' && number[1] == '0')
{
int flag = 0;
// Loops till length of the string number
for(int x = 0; x < number.length(); x++)
// Checks if each character of the string is not between zero and seven
if(!(number[x] >= '0' && number[x] <= '7'))
// Set the flag to one
flag = 1;
// Checks if the flag value is not changed i.e., it contains zero
if(flag == 0)
// Display as octal number
cout<<" Octal Number: "<<number;
}// End of if condition

// Checks if the first character of the string is not zero
if(number[0] != '0')
{
int flag = 0;
// Loops till length of the string number
for(int x = 0; x < number.length(); x++)
// Checks if each character of the string is not between zero and nine
if(!(number[x] >= '0' && number[x] <= '9'))
// Set the flag to one
flag = 1;
// Checks if the flag value is not changed i.e., it contains zero
if(flag == 0)
// Display as decimal number
cout<<" Decimal number: "<<number;
}// End of if condition

// Checks if the first character of the number is zero and second character of the number is 'x'
if(number[0] == '0' && number[1] == 'x')
{
// Display as octal number
cout<<" Hexadecimal number: "<<number;
}// End of if condition
}// End of main function

Sample Output 1:

127

Decimal number: 127

Sample Output 2:

0x12c

Hexadecimal number: 0x12c

Sample Output 3:

00274

Octal Number: 00274

Sample Output 4:

0x12Ra
NONSENSE

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