C++ You are required to write a universal calculator that performs DOUBLE UP of different types of data. Especially the calculator must take the following types of input. A decimal number A float point number A binary number which must be prefixed with “0b”(e.g 0b111 ob1010 are valid 01b1010, 0b101, 01010 are invalid) A hexadecimal number which must be prefixed with “0x” (e.g. 0x19, 0xAAare valid. 01x1A, ox1A, 01A are invalid)   Note Your program must implement function overloading. You can consider the examples below for your implementation. You are provided with the initial code which can be found.   #include #include #include #include   using namespace std;   /* * IMPLEMENT YOUR OVERLOADED FUNCTIONS HERE */   /* this function takes a string and * returns 1 if the string represents a decimal * returns 2 if the string represents a floating point * returns 3 if the string represents a binary * returns 4 if the string represents a hexadecimal * return 0 if the input string is anything else */ int check_type(char *input) { int res = 1; int len = strlen(input); int i;   // check the first char for special sign chars [-,+] and '.' char first = input[0]; char second = input[1]; if (first=='-' && first=='+') { if (second=='.') { res = 2; } } // got a '.' from the beginning, got a float else if(first=='.') { if (second=='.') return 0; res = 2; } //check to see if we got a prefix 0b --> expect a binary else if (first=='0' && second=='b') res = 3; // check to see if we got a prefix 0x --> expect a hexa else if(first=='0' && second=='x') res = 4; else if(first>='0'||first<='9') { if (second=='.') res = 2; } else if ( (first<'0'||first>'9') && (second<'0'||second>'9') ) return 0;   for (i=2; i'9')) return 0; } // already expecting a binary, only '0' or '1' else if (res==3) { if (input[i]!='0' && input[i]!='1') return 0; } // already expecting a hexa, only [0-9] or [A-F] else if (res==4) { if ( (input[i]<'0'||input[i]>'9') && (input[i]<'A'||input[i]>'F') ) return 0; } // still seeing integer, could be a float with a '.' else if (res==1) { if (input[i]=='.') res = 2; else if (input[i]<'0' || input[i]>'9') return 0; } } return res; }   int main(int argc, char* argv[]) { int type;   if (argc!=2) { cerr << "Wrong number of inputs" << endl; return 0; } type = check_type(argv[1]); if (type==1) { doubleup(atoi(argv[1])); } else if (type==2) { doubleup(stof(argv[1])); } else if (type==3) { doubleup(argv[1]); } else if (type==4) { doubleup(string(argv[1])); } else { cerr << "Invalid input" << endl; }   return 0; }   -The init code has already checked different types of input for you. You only need to implement the overloaded version of the DOUBLEUP function. - No more built in libraries allowed apart from those than have been included in the init code   Output A decimal number 12, 24 Floating pint number 12.12, 24.24 A binary number 0b111 0b1110 Hexadecimal number 0xAA 0x154

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.1: Function And Parameter Declarations
Problem 11E
icon
Related questions
Question

C++ You are required to write a universal calculator that performs DOUBLE UP of different types of data. Especially the calculator must take the following types of input.

  1. A decimal number

  2. A float point number

  3. A binary number which must be prefixed with “0b”(e.g 0b111 ob1010 are valid 01b1010, 0b101, 01010 are invalid)

  4. A hexadecimal number which must be prefixed with “0x” (e.g. 0x19, 0xAAare valid. 01x1A, ox1A, 01A are invalid)

 

Note

Your program must implement function overloading. You can consider the examples below for your implementation. You are provided with the initial code which can be found.

 

#include <iostream>

#include <string>

#include <string.h>

#include <iomanip>

 

using namespace std;

 

/*

* IMPLEMENT YOUR OVERLOADED FUNCTIONS HERE

*/

 

/* this function takes a string and

* returns 1 if the string represents a decimal

* returns 2 if the string represents a floating point

* returns 3 if the string represents a binary

* returns 4 if the string represents a hexadecimal

* return 0 if the input string is anything else

*/

int check_type(char *input) {

int res = 1;

int len = strlen(input);

int i;

 

// check the first char for special sign chars [-,+] and '.'

char first = input[0];

char second = input[1];

if (first=='-' && first=='+') {

if (second=='.') {

res = 2;

}

}

// got a '.' from the beginning, got a float

else if(first=='.') {

if (second=='.') return 0;

res = 2;

}

//check to see if we got a prefix 0b --> expect a binary

else if (first=='0' && second=='b')

res = 3;

// check to see if we got a prefix 0x --> expect a hexa

else if(first=='0' && second=='x')

res = 4;

else if(first>='0'||first<='9') {

if (second=='.')

res = 2;

}

else if ( (first<'0'||first>'9') && (second<'0'||second>'9') )

return 0;

 

for (i=2; i<len; i++) {

// already expecting a float, no more '.'

if (res==2) {

if (input[i]=='.' || (input[i]<'0'||input[i]>'9'))

return 0;

}

// already expecting a binary, only '0' or '1'

else if (res==3) {

if (input[i]!='0' && input[i]!='1')

return 0;

}

// already expecting a hexa, only [0-9] or [A-F]

else if (res==4) {

if ( (input[i]<'0'||input[i]>'9') && (input[i]<'A'||input[i]>'F') )

return 0;

}

// still seeing integer, could be a float with a '.'

else if (res==1) {

if (input[i]=='.') res = 2;

else if (input[i]<'0' || input[i]>'9')

return 0;

}

}

return res;

}

 

int main(int argc, char* argv[]) {

int type;

 

if (argc!=2) {

cerr << "Wrong number of inputs" << endl;

return 0;

}

type = check_type(argv[1]);

if (type==1) {

doubleup(atoi(argv[1]));

}

else if (type==2) {

doubleup(stof(argv[1]));

}

else if (type==3) {

doubleup(argv[1]);

}

else if (type==4) {

doubleup(string(argv[1]));

}

else {

cerr << "Invalid input" << endl;

}

 

return 0;

}

 

-The init code has already checked different types of input for you. You only need to implement the overloaded version of the DOUBLEUP function.

- No more built in libraries allowed apart from those than have been included in the init code

 

Output

A decimal number 12, 24

Floating pint number 12.12, 24.24

A binary number 0b111 0b1110

Hexadecimal number 0xAA 0x154

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Mathematical functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,